3 from PLC.Faults import *
4 from PLC.Auth import Auth
5 from PLC.Method import Method
6 from PLC.Parameter import Parameter, Mixed
7 from PLC.Table import Row
9 from PLC.Nodes import Node, Nodes
10 from PLC.Interfaces import Interface, Interfaces
11 from PLC.TagTypes import TagTypes
12 from PLC.InterfaceTags import InterfaceTags
13 from PLC.Methods.AddInterfaceTag import AddInterfaceTag
14 from PLC.Methods.UpdateInterfaceTag import UpdateInterfaceTag
16 cannot_update = ['interface_id', 'node_id']
18 class AddInterface(Method):
21 Adds a new network for a node. Any values specified in
22 interface_fields are used, otherwise defaults are
25 If type is static, then ip, gateway, network, broadcast, netmask,
26 and dns1 must all be specified in interface_fields. If type is
27 dhcp, these parameters, even if specified, are ignored.
29 PIs and techs may only add interfaces to their own nodes. Admins may
30 add interfaces to any node.
32 Returns the new interface_id (> 0) if successful, faults otherwise.
35 roles = ['admin', 'pi', 'tech']
37 accepted_fields = Row.accepted_fields(cannot_update, Interface.fields, exclude=True)
38 accepted_fields.update(Interface.tags)
42 Mixed(Node.fields['node_id'],
43 Node.fields['hostname']),
47 returns = Parameter(int, 'New interface_id (> 0) if successful')
50 def call(self, auth, node_id_or_hostname, interface_fields):
52 [native,tags,rejected]=Row.split_fields(interface_fields,[Interface.fields,Interface.tags])
55 native = Row.check_fields (native, self.accepted_fields)
57 raise PLCInvalidArgument, "Cannot add Interface with column(s) %r"%rejected
59 # Check if node exists
60 nodes = Nodes(self.api, [node_id_or_hostname])
62 raise PLCInvalidArgument, "No such node %r"%node_id_or_hostname
65 # Authenticated function
66 assert self.caller is not None
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"
75 interface = Interface(self.api, native)
76 interface['node_id'] = node['node_id']
77 # if this is the first interface, make it primary
78 if not node['interface_ids']:
79 interface['is_primary'] = True
83 self.object_objects = { 'Node': [node['node_id']],
84 'Interface' : [interface['interface_id']] }
85 self.message = "Interface %d added" % interface['interface_id']
87 for (tagname,value) in tags.iteritems():
88 # the tagtype instance is assumed to exist, just check that
89 if not TagTypes(self.api,{'tagname':tagname}):
90 raise PLCInvalidArgument,"No such TagType %s"%tagname
91 interface_tags=InterfaceTags(self.api,{'tagname':tagname,'interface_id':interface['interface_id']})
92 if not interface_tags:
93 AddInterfaceTag(self.api).__call__(auth,interface['interface_id'],tagname,value)
95 UpdateInterfaceTag(self.api).__call__(auth,interface_tags[0]['interface_tag_id'],value)
97 return interface['interface_id']