79c86b6e1864b4c88edb9b28365f22cd7e51948d
[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).values()
31         if not messages:
32             raise PLCInvalidArgument, "No such message template"
33
34         if not self.api.config.PLC_MAIL_ENABLED:
35             return 1
36
37         recipients = {}
38
39         if self.api.config.PLC_MAIL_BOOT_ADDRESS:
40             recipients[self.api.config.PLC_MAIL_BOOT_ADDRESS] = "Boot Messages"
41
42         if include_support and self.api.config.PLC_MAIL_SUPPORT_ADDRESS:
43             recipients[self.api.config.PLC_MAIL_SUPPORT_ADDRESS] = self.api.config.PLC_NAME + " Support"
44
45         if include_pis or include_techs:
46             sites = Sites(self.api, [self.caller['site_id']]).values()
47             if not sites:
48                 raise PLCAPIError, "No site associated with node"
49             site = sites[0]
50
51             persons = Persons(self.api, site['person_ids']).values()
52             for person in persons:
53                 if include_pis and 'pi' in person['roles'] or \
54                    include_techs and 'tech' in person['roles']:
55                     recipients[person['email']] = person['first_name'] + " " + person['last_name']
56
57         # XXX Send mail
58
59         return 1