X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FMethods%2FAddInterface.py;h=cd3911a89b6763c5ae924edf1c10778c3b3de9fb;hb=db42a908647b5116ecc3d63800fee6662d036914;hp=7e77485940db7f368d7f2b0797f7996ecc28f238;hpb=a54fc801d75b1d62be7633e01312b9118ba94723;p=plcapi.git diff --git a/PLC/Methods/AddInterface.py b/PLC/Methods/AddInterface.py index 7e77485..cd3911a 100644 --- a/PLC/Methods/AddInterface.py +++ b/PLC/Methods/AddInterface.py @@ -1,12 +1,18 @@ # $Id$ from PLC.Faults import * +from PLC.Auth import Auth from PLC.Method import Method from PLC.Parameter import Parameter, Mixed +from PLC.Table import Row + from PLC.Nodes import Node, Nodes from PLC.Interfaces import Interface, Interfaces -from PLC.Auth import Auth +from PLC.TagTypes import TagTypes +from PLC.InterfaceTags import InterfaceTags +from PLC.Methods.AddInterfaceTag import AddInterfaceTag +from PLC.Methods.UpdateInterfaceTag import UpdateInterfaceTag -can_update = lambda (field, value): field not in ['interface_id', 'node_id'] +can_update = ['interface_id', 'node_id'] class AddInterface(Method): """ @@ -29,25 +35,28 @@ class AddInterface(Method): roles = ['admin', 'pi', 'tech'] - interface_fields = dict(filter(can_update, Interface.fields.items())) + accepted_fields = Row.accepted_fields(can_update, [Interface.fields,Interface.tags]) accepts = [ Auth(), Mixed(Node.fields['node_id'], Node.fields['hostname']), - interface_fields + accepted_fields ] returns = Parameter(int, 'New interface_id (> 0) if successful') def call(self, auth, node_id_or_hostname, interface_fields): - interface_fields = dict(filter(can_update, interface_fields.items())) + + [native,tags,rejected]=Row.split_fields(interface_fields,[Interface.fields,Interface.tags]) + if rejected: + raise PLCInvalidArgument, "Cannot add Interface with column(s) %r"%rejected # Check if node exists nodes = Nodes(self.api, [node_id_or_hostname]) if not nodes: - raise PLCInvalidArgument, "No such node" + raise PLCInvalidArgument, "No such node %r"%node_id_or_hostname node = nodes[0] # Authenticated function @@ -57,18 +66,29 @@ class AddInterface(Method): # member of the site where the node exists. if 'admin' not in self.caller['roles']: if node['site_id'] not in self.caller['site_ids']: - raise PLCPermissionDenied, "Not allowed to add node network for specified node" + raise PLCPermissionDenied, "Not allowed to add an interface to the specified node" - # Add node network - interface = Interface(self.api, interface_fields) + # Add interface + interface = Interface(self.api, native) interface['node_id'] = node['node_id'] - # if this is the first node network, make it primary + # if this is the first interface, make it primary if not node['interface_ids']: interface['is_primary'] = True interface.sync() # Logging variables - self.object_ids = [node['node_id'], interface['interface_id']] - self.messgage = "Node network %d added" % interface['interface_id'] + self.object_objects = { 'Node': [node['node_id']], + 'Interface' : [interface['interface_id']] } + self.message = "Interface %d added" % interface['interface_id'] + + for (tagname,value) in tags.iteritems(): + # the tagtype instance is assumed to exist, just check that + if not TagTypes(self.api,{'tagname':tagname}): + raise PLCInvalidArgument,"No such TagType %s"%tagname + interface_tags=InterfaceTags(self.api,{'tagname':tagname,'interface_id':interface['interface_id']}) + if not interface_tags: + AddInterfaceTag(self.api).__call__(auth,interface['interface_id'],tagname,value) + else: + UpdateInterfaceTag(self.api).__call__(auth,interface_tags[0]['interface_tag_id'],value) return interface['interface_id']