Interfaces can handle tags through Add/Get/Update
[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 can_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. Acceptable values for method may be retrieved via
23     GetNetworkMethods. Acceptable values for type may be retrieved via
24     GetNetworkTypes.
25
26     If type is static, ip, gateway, network, broadcast, netmask, and
27     dns1 must all be specified in interface_fields. If type is dhcp,
28     these parameters, even if specified, are ignored.
29
30     PIs and techs may only add networks to their own nodes. Admins may
31     add networks to any node.
32
33     Returns the new interface_id (> 0) if successful, faults otherwise.
34     """
35
36     roles = ['admin', 'pi', 'tech']
37
38     accepted_fields = Row.accepted_fields(can_update, [Interface.fields,Interface.tags])
39
40     accepts = [
41         Auth(),
42         Mixed(Node.fields['node_id'],
43               Node.fields['hostname']),
44         accepted_fields
45         ]
46
47     returns = Parameter(int, 'New interface_id (> 0) if successful')
48
49     
50     def call(self, auth, node_id_or_hostname, interface_fields):
51
52         [native,tags,rejected]=Row.split_fields(interface_fields,[Interface.fields,Interface.tags])
53         if rejected:
54             raise PLCInvalidArgument, "Cannot add Interface with column(s) %r"%rejected
55
56         # Check if node exists
57         nodes = Nodes(self.api, [node_id_or_hostname])
58         if not nodes:
59             raise PLCInvalidArgument, "No such node %r"%node_id_or_hostname
60         node = nodes[0]
61
62         # Authenticated function
63         assert self.caller is not None
64
65         # If we are not an admin, make sure that the caller is a
66         # member of the site where the node exists.
67         if 'admin' not in self.caller['roles']:
68             if node['site_id'] not in self.caller['site_ids']:
69                 raise PLCPermissionDenied, "Not allowed to add an interface to the specified node"
70
71         # Add interface
72         interface = Interface(self.api, native)
73         interface['node_id'] = node['node_id']
74         # if this is the first interface, make it primary
75         if not node['interface_ids']:
76                 interface['is_primary'] = True
77         interface.sync()
78         
79         # Logging variables
80         self.object_objects = { 'Node': [node['node_id']], 
81                                 'Interface' : [interface['interface_id']] }
82         self.message = "Interface %d added" % interface['interface_id']
83
84         for (tagname,value) in tags.iteritems():
85             # the tagtype instance is assumed to exist, just check that
86             if not TagTypes(self.api,{'tagname':tagname}):
87                 raise PLCInvalidArgument,"No such TagType %s"%tagname
88             interface_tags=InterfaceTags(self.api,{'tagname':tagname,'interface_id':interface['interface_id']})
89             if not interface_tags:
90                 AddInterfaceTag(self.api).__call__(auth,interface['interface_id'],tagname,value)
91             else:
92                 UpdateInterfaceTag(self.api).__call__(auth,interface_tags[0]['interface_tag_id'],value)
93
94         return interface['interface_id']