bring over newinterface branch from Verivue
[plcapi.git] / PLC / Methods / DeleteIpAddress.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
5 from PLC.Nodes import Node, Nodes
6 from PLC.Interfaces import Interface, Interfaces
7 from PLC.IpAddresses import IpAddress, IpAddresses
8
9 class DeleteIpAddress(Method):
10     """
11     Deletes an existing ip address.
12
13     Admins may delete any ip address. PIs and techs may only delete
14     ip addresses associated with interfaces on nodes at their sites.
15
16     Returns 1 if successful, faults otherwise.
17     """
18
19     roles = ['admin', 'pi', 'tech']
20
21     accepts = [
22         Auth(),
23         IpAddress.fields['ip_address_id']
24         ]
25
26     returns = Parameter(int, '1 if successful')
27
28
29     def call(self, auth, ip_address_id):
30
31         # Get interface information
32         ip_addresses = IpAddresses(self.api, [ip_address_id])
33         if not ip_addresses:
34             raise PLCInvalidArgument, "No such ip_address %r"%ip_address_id
35         ip_address = ip_addresses[0]
36
37         # Authenticated functino
38         assert self.caller is not None
39
40         # If we are not an admin, make sure that the caller is a
41         # member of the site at which the node is located.
42         if 'admin' not in self.caller['roles']:
43             # Get interface information
44             interfaces = Interfaces(self.api, [ip_address['interface_id']])
45             if not interfaces:
46                 raise PLCInvalidArgument, "No such interface %r"%ip_address['interface_id']
47             interface = interfaces[0]
48
49             # Get node information
50             nodes = Nodes(self.api, [interface['node_id']])
51             if not nodes:
52                 raise PLCInvalidArgument, "No such node %r"%node_id
53             node = nodes[0]
54
55             if node['site_id'] not in self.caller['site_ids']:
56                 raise PLCPermissionDenied, "Not allowed to delete this ip address"
57
58         ip_address.delete()
59
60         # Logging variables
61         self.event_objects = {'IpAddress': [ip_address['ip_address_id']]}
62         self.message = "IpAddress %d deleted" % ip_address['ip_address_id']
63
64         return 1