69e92ec065eed05f91ed6286a6c2af962d3a097e
[plcapi.git] / PLC / Methods / AddInterface.py
1 # $Id$
2 from PLC.Faults import *
3 from PLC.Auth import Auth
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Table import Row
7
8 from PLC.Nodes import Node, Nodes
9 from PLC.Interfaces import Interface, Interfaces
10 from PLC.TagTypes import TagTypes
11 from PLC.InterfaceTags import InterfaceTags
12 from PLC.Methods.AddInterfaceTag import AddInterfaceTag
13 from PLC.Methods.UpdateInterfaceTag import UpdateInterfaceTag
14
15 cannot_update = ['interface_id', 'node_id']
16
17 class AddInterface(Method):
18     """
19
20     Adds a new network for a node. Any values specified in
21     interface_fields are used, otherwise defaults are
22     used. 
23
24     If type is static, then ip, gateway, network, broadcast, netmask,
25     and dns1 must all be specified in interface_fields. If type is
26     dhcp, these parameters, even if specified, are ignored.
27
28     PIs and techs may only add interfaces to their own nodes. Admins may
29     add interfaces to any node.
30
31     Returns the new interface_id (> 0) if successful, faults otherwise.
32     """
33
34     roles = ['admin', 'pi', 'tech']
35
36     accepted_fields = Row.accepted_fields(cannot_update, Interface.fields, exclude=True)
37     accepted_fields.update(Interface.tags)
38
39     accepts = [
40         Auth(),
41         Mixed(Node.fields['node_id'],
42               Node.fields['hostname']),
43         accepted_fields
44         ]
45
46     returns = Parameter(int, 'New interface_id (> 0) if successful')
47
48     
49     def call(self, auth, node_id_or_hostname, interface_fields):
50
51         [native,tags,rejected]=Row.split_fields(interface_fields,[Interface.fields,Interface.tags])
52
53         # type checking
54         native = Row.check_fields (native, self.accepted_fields)
55         if rejected:
56             raise PLCInvalidArgument, "Cannot add Interface with column(s) %r"%rejected
57
58         # Check if node exists
59         nodes = Nodes(self.api, [node_id_or_hostname])
60         if not nodes:
61             raise PLCInvalidArgument, "No such node %r"%node_id_or_hostname
62         node = nodes[0]
63
64         # Authenticated function
65         assert self.caller is not None
66
67         # If we are not an admin, make sure that the caller is a
68         # member of the site where the node exists.
69         if 'admin' not in self.caller['roles']:
70             if node['site_id'] not in self.caller['site_ids']:
71                 raise PLCPermissionDenied, "Not allowed to add an interface to the specified node"
72
73         # Add interface
74         interface = Interface(self.api, native)
75         interface['node_id'] = node['node_id']
76         # if this is the first interface, make it primary
77         if not node['interface_ids']:
78                 interface['is_primary'] = True
79         interface.sync()
80         
81         # Logging variables
82         self.object_objects = { 'Node': [node['node_id']], 
83                                 'Interface' : [interface['interface_id']] }
84         self.message = "Interface %d added" % interface['interface_id']
85
86         for (tagname,value) in tags.iteritems():
87             # the tagtype instance is assumed to exist, just check that
88             if not TagTypes(self.api,{'tagname':tagname}):
89                 raise PLCInvalidArgument,"No such TagType %s"%tagname
90             interface_tags=InterfaceTags(self.api,{'tagname':tagname,'interface_id':interface['interface_id']})
91             if not interface_tags:
92                 AddInterfaceTag(self.api).__call__(auth,interface['interface_id'],tagname,value)
93             else:
94                 UpdateInterfaceTag(self.api).__call__(auth,interface_tags[0]['interface_tag_id'],value)
95
96         return interface['interface_id']