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 from PLC.Namespace import hostname_to_hrn
9 from PLC.Peers import Peers
10 from PLC.Sites import Site, Sites
11 from PLC.Nodes import Node, Nodes
12 from PLC.TagTypes import TagTypes
13 from PLC.NodeTags import NodeTags, NodeTag
14 from PLC.Methods.AddNodeTag import AddNodeTag
15 from PLC.Methods.UpdateNodeTag import UpdateNodeTag
17 can_update = ['hostname', 'node_type', 'boot_state', 'model', 'version']
19 legacy_node_fields = {
20 'node_id': Parameter(int, "Node identifier"),
21 'node_type': Parameter(str,"Node type",max=20),
22 'hostname': Parameter(str, "Fully qualified hostname", max = 255),
23 'site_id': Parameter(int, "Site at which this node is located"),
24 'boot_state': Parameter(str, "Boot state", max = 20),
25 'run_level': Parameter(str, "Run level", max = 20),
26 'model': Parameter(str, "Make and model of the actual machine", max = 255, nullok = True),
27 'boot_nonce': Parameter(str, "(Admin only) Random value generated by the node at last boot", max = 128),
28 'version': Parameter(str, "Apparent Boot CD version", max = 64),
29 'ssh_rsa_key': Parameter(str, "Last known SSH host key", max = 1024),
30 'date_created': Parameter(int, "Date and time when node entry was created", ro = True),
31 'last_updated': Parameter(int, "Date and time when node entry was created", ro = True),
32 'last_contact': Parameter(int, "Date and time when node last contacted plc", ro = True),
33 'last_boot': Parameter(int, "Date and time when node last booted", ro = True),
34 'last_download': Parameter(int, "Date and time when node boot image was created", ro = True),
35 'last_pcu_reboot': Parameter(int, "Date and time when PCU reboot was attempted", ro = True),
36 'last_pcu_confirmation': Parameter(int, "Date and time when PCU reboot was confirmed", ro = True),
37 'last_time_spent_online': Parameter(int, "Length of time the node was last online before shutdown/failure", ro = True),
38 'last_time_spent_offline': Parameter(int, "Length of time the node was last offline after failure and before reboot", ro = True),
39 'verified': Parameter(bool, "Whether the node configuration is verified correct", ro=False),
40 'key': Parameter(str, "(Admin only) Node key", max = 256),
41 'session': Parameter(str, "(Admin only) Node session value", max = 256, ro = True),
42 'interface_ids': Parameter([int], "List of network interfaces that this node has"),
43 'conf_file_ids': Parameter([int], "List of configuration files specific to this node"),
44 # 'root_person_ids': Parameter([int], "(Admin only) List of people who have root access to this node"),
45 'slice_ids': Parameter([int], "List of slices on this node"),
46 'slice_ids_whitelist': Parameter([int], "List of slices allowed on this node"),
47 'pcu_ids': Parameter([int], "List of PCUs that control this node"),
48 'ports': Parameter([int], "List of PCU ports that this node is connected to"),
49 'peer_id': Parameter(int, "Peer to which this node belongs", nullok = True),
50 'peer_node_id': Parameter(int, "Foreign node identifier at peer", nullok = True),
51 'node_tag_ids' : Parameter ([int], "List of tags attached to this node"),
52 'nodegroup_ids': Parameter([int], "List of node groups that this node is in"),
56 class AddNode(Method):
58 Adds a new node. Any values specified in node_fields are used,
59 otherwise defaults are used.
61 PIs and techs may only add nodes to their own sites. Admins may
62 add nodes to any site.
64 Returns the new node_id (> 0) if successful, faults otherwise.
67 roles = ['admin', 'pi', 'tech']
69 accepted_fields = Row.accepted_fields(can_update,legacy_node_fields)
70 accepted_fields.update(Node.tags)
74 Mixed(Site.fields['site_id'],
75 Site.fields['login_base']),
79 returns = Parameter(int, 'New node_id (> 0) if successful')
81 def call(self, auth, site_id_or_login_base, node_fields):
83 [native,tags,rejected]=Row.split_fields(node_fields,[legacy_node_fields,Node.tags])
86 native = Row.check_fields(native, self.accepted_fields)
88 raise PLCInvalidArgument, "Cannot add Node with column(s) %r"%rejected
90 # Get site information
91 sites = Sites(self.api, [site_id_or_login_base])
93 raise PLCInvalidArgument, "No such site"
97 # Authenticated function
98 assert self.caller is not None
100 # If we are not an admin, make sure that the caller is a
101 # member of the site.
102 if 'admin' not in self.caller['roles']:
103 if site['site_id'] not in self.caller['site_ids']:
104 assert self.caller['person_id'] not in site['person_ids']
105 raise PLCPermissionDenied, "Not allowed to add nodes to specified site"
107 assert self.caller['person_id'] in site['person_ids']
109 node = Node(self.api, native)
110 node['site_id'] = site['site_id']
113 # since hostname was specified lets add the 'hrn' node tag
114 root_auth = self.api.config.PLC_HRN_ROOT
115 login_base = site['login_base']
116 tags['hrn'] = hostname_to_hrn(root_auth, login_base, node['hostname'])
118 for (tagname,value) in tags.iteritems():
119 # the tagtype instance is assumed to exist, just check that
120 tag_types = TagTypes(self.api,{'tagname':tagname})
122 raise PLCInvalidArgument,"No such TagType %s"%tagname
123 tag_type = tag_types[0]
124 node_tags=NodeTags(self.api,{'tagname':tagname,'node_id':node['node_id']})
126 node_tag = NodeTag(self.api)
127 node_tag['node_id'] = node['node_id']
128 node_tag['tag_type_id'] = tag_type['tag_type_id']
129 node_tag['tagname'] = tagname
130 node_tag['value'] = value
133 node_tag = node_tags[0]
134 node_tag['value'] = value
137 self.event_objects = {'Site': [site['site_id']],
138 'Node': [node['node_id']]}
139 self.message = "Node %d=%s created" % (node['node_id'],node['hostname'])
141 return node['node_id']