9847c3c7f666d87086631e3b7bf33e4a5397d6e3
[plcapi.git] / PLC / Methods / AddIpAddress.py
1 #
2 # Thierry Parmentelat - INRIA
3 #
4 from PLC.Faults import *
5 from PLC.Auth import Auth
6 from PLC.Method import Method
7 from PLC.Parameter import Parameter, Mixed
8 from PLC.Table import Row
9
10 from PLC.Nodes import Node, Nodes
11 from PLC.Interfaces import Interface, Interfaces
12 from PLC.IpAddresses import IpAddresses, IpAddress
13 from PLC.TagTypes import TagTypes
14
15 cannot_update = ['ip_address_id', 'interface_id']
16
17 class AddIpAddress(Method):
18     """
19
20     Adds a new address for an interface. Any values specified in
21     ip_address_fields are used, otherwise defaults are
22     used.
23
24     PIs and techs may only add interfaces to their own nodes. Admins may
25     add interfaces to any node.
26
27     Returns the new ip_address_id (> 0) if successful, faults otherwise.
28     """
29
30     roles = ['admin', 'pi', 'tech']
31
32     accepted_fields = Row.accepted_fields(cannot_update, IpAddress.fields, exclude=True)
33     accepted_fields.update(IpAddress.tags)
34
35     accepts = [
36         Auth(),
37         Mixed(Interface.fields['interface_id']),
38         accepted_fields
39         ]
40
41     returns = Parameter(int, 'New ip_address_id (> 0) if successful')
42
43
44     def call(self, auth, interface_id, ip_address_fields):
45
46         [native,tags,rejected]=Row.split_fields(ip_address_fields,[IpAddress.fields,IpAddress.tags])
47
48         # type checking
49         native = Row.check_fields (native, self.accepted_fields)
50         if rejected:
51             raise PLCInvalidArgument, "Cannot add IpAddress with column(s) %r"%rejected
52
53         # Get interface information
54         interfaces = Interfaces(self.api, [interface_id])
55         if not interfaces:
56             raise PLCInvalidArgument, "No such interfaces %r"%interface_id
57         interface = interfaces[0]
58
59         # Check if node exists
60         nodes = Nodes(self.api, [interface['node_id']])
61         if not nodes:
62             raise PLCInvalidArgument, "No such node %r"%node_id_or_hostname
63         node = nodes[0]
64
65         # Authenticated function
66         assert self.caller is not None
67
68         # If we are not an admin, make sure that the caller is a
69         # member of the site where the node exists.
70         if 'admin' not in self.caller['roles']:
71             if node['site_id'] not in self.caller['site_ids']:
72                 raise PLCPermissionDenied, "Not allowed to add an interface to the specified node"
73
74         # Add interface
75         ip_address = IpAddress(self.api, native)
76         ip_address['interface_id'] = interface['interface_id']
77         ip_address.sync()
78
79         # Logging variables
80         self.event_objects = { 'Interace': [interface['interface_id']],
81                                'IpAddress' : [ip_address['ip_address_id']] }
82         self.message = "Address %d added" % ip_address['ip_address_id']
83
84         return ip_address['ip_address_id']