more detailed info passed when raising an exception
[plcapi.git] / PLC / Methods / BootNotifyOwners.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Auth import Auth, BootAuth
5 from PLC.Messages import Message, Messages
6 from PLC.Persons import Person, Persons
7 from PLC.Sites import Site, Sites
8
9 class BootNotifyOwners(Method):
10     """
11     Notify the owners of the node, and/or support about an event that
12     happened on the machine.
13
14     Returns 1 if successful.
15     """
16
17     roles = ['node']
18
19     accepts = [
20         BootAuth(),
21         Message.fields['message_id'],
22         Parameter(int, "Notify PIs"),
23         Parameter(int, "Notify technical contacts"),
24         Parameter(int, "Notify support")
25         ]
26
27     returns = Parameter(int, '1 if successful')
28
29     def call(self, auth, message_id, include_pis, include_techs, include_support):
30         messages = Messages(self.api, [message_id], enabled = True)
31         if not messages:
32             # raise PLCInvalidArgument, "No such message template"
33             return 1
34         message = messages[0]
35
36         if not self.api.config.PLC_MAIL_ENABLED:
37             return 1
38
39         recipients = {}
40
41         if self.api.config.PLC_MAIL_BOOT_ADDRESS:
42             recipients[self.api.config.PLC_MAIL_BOOT_ADDRESS] = "Boot Messages"
43
44         if include_support and self.api.config.PLC_MAIL_SUPPORT_ADDRESS:
45             recipients[self.api.config.PLC_MAIL_SUPPORT_ADDRESS] = self.api.config.PLC_NAME + " Support"
46
47         if include_pis or include_techs:
48             sites = Sites(self.api, [self.caller['site_id']])
49             if not sites:
50                 raise PLCAPIError, "No site associated with node"
51             site = sites[0]
52
53             persons = Persons(self.api, site['person_ids'])
54             for person in persons:
55                 if include_pis and 'pi' in person['roles'] or \
56                    include_techs and 'tech' in person['roles']:
57                     recipients[person['email']] = person['first_name'] + " " + person['last_name']
58
59         # XXX Send mail
60
61         return 1