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