svn keywords
[plcapi.git] / PLC / Methods / AddInterface.py
1 # $Id$
2 # $URL$
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
8
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
15
16 cannot_update = ['interface_id', 'node_id']
17
18 class AddInterface(Method):
19     """
20
21     Adds a new network for a node. Any values specified in
22     interface_fields are used, otherwise defaults are
23     used. 
24
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.
28
29     PIs and techs may only add interfaces to their own nodes. Admins may
30     add interfaces to any node.
31
32     Returns the new interface_id (> 0) if successful, faults otherwise.
33     """
34
35     roles = ['admin', 'pi', 'tech']
36
37     accepted_fields = Row.accepted_fields(cannot_update, Interface.fields, exclude=True)
38     accepted_fields.update(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
54         # type checking
55         native = Row.check_fields (native, self.accepted_fields)
56         if rejected:
57             raise PLCInvalidArgument, "Cannot add Interface with column(s) %r"%rejected
58
59         # Check if node exists
60         nodes = Nodes(self.api, [node_id_or_hostname])
61         if not nodes:
62             raise PLCInvalidArgument, "No such node %r"%node_id_or_hostname
63         node = nodes[0]
64
65         # Authenticated function
66         assert self.caller is not None
67
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"
73
74         # Add interface
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
80         interface.sync()
81         
82         # Logging variables
83         self.object_objects = { 'Node': [node['node_id']], 
84                                 'Interface' : [interface['interface_id']] }
85         self.message = "Interface %d added" % interface['interface_id']
86
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)
94             else:
95                 UpdateInterfaceTag(self.api).__call__(auth,interface_tags[0]['interface_tag_id'],value)
96
97         return interface['interface_id']