# $Id$ # $URL$ from PLC.Faults import * from PLC.Method import Method from PLC.Parameter import Parameter, Mixed from PLC.Filter import Filter from PLC.Nodes import Node, Nodes from PLC.Persons import Person, Persons from PLC.Auth import Auth admin_only = ['key', 'session', 'boot_nonce' ] legacy_node_fields = { 'node_id': Parameter(int, "Node identifier"), 'node_type': Parameter(str,"Node type",max=20), 'hostname': Parameter(str, "Fully qualified hostname", max = 255), 'site_id': Parameter(int, "Site at which this node is located"), 'boot_state': Parameter(str, "Boot state", max = 20), 'run_level': Parameter(str, "Run level", max = 20), 'model': Parameter(str, "Make and model of the actual machine", max = 255, nullok = True), 'boot_nonce': Parameter(str, "(Admin only) Random value generated by the node at last boot", max = 128), 'version': Parameter(str, "Apparent Boot CD version", max = 64), 'ssh_rsa_key': Parameter(str, "Last known SSH host key", max = 1024), 'date_created': Parameter(int, "Date and time when node entry was created", ro = True), 'last_updated': Parameter(int, "Date and time when node entry was created", ro = True), 'last_contact': Parameter(int, "Date and time when node last contacted plc", ro = True), 'last_boot': Parameter(int, "Date and time when node last booted", ro = True), 'last_download': Parameter(int, "Date and time when node boot image was created", ro = True), 'last_pcu_reboot': Parameter(int, "Date and time when PCU reboot was attempted", ro = True), 'last_pcu_confirmation': Parameter(int, "Date and time when PCU reboot was confirmed", ro = True), 'last_time_spent_online': Parameter(int, "Length of time the node was last online before shutdown/failure", ro = True), 'last_time_spent_offline': Parameter(int, "Length of time the node was last offline after failure and before reboot", ro = True), 'verified': Parameter(bool, "Whether the node configuration is verified correct", ro=False), 'key': Parameter(str, "(Admin only) Node key", max = 256), 'session': Parameter(str, "(Admin only) Node session value", max = 256, ro = True), 'interface_ids': Parameter([int], "List of network interfaces that this node has"), 'conf_file_ids': Parameter([int], "List of configuration files specific to this node"), # 'root_person_ids': Parameter([int], "(Admin only) List of people who have root access to this node"), 'slice_ids': Parameter([int], "List of slices on this node"), 'slice_ids_whitelist': Parameter([int], "List of slices allowed on this node"), 'pcu_ids': Parameter([int], "List of PCUs that control this node"), 'ports': Parameter([int], "List of PCU ports that this node is connected to"), 'peer_id': Parameter(int, "Peer to which this node belongs", nullok = True), 'peer_node_id': Parameter(int, "Foreign node identifier at peer", nullok = True), 'node_tag_ids' : Parameter ([int], "List of tags attached to this node"), 'nodegroup_ids': Parameter([int], "List of node groups that this node is in"), } def clean_node_fields(node): remove_keys = [key for key in node.keys() if key not in legacy_node_fields.keys()] for key in remove_keys: del node[key] return node class GetNodes(Method): """ Returns an array of structs containing details about nodes. If node_filter is specified and is an array of node identifiers or hostnames, or a struct of node attributes, only nodes matching the filter will be returned. If return_fields is specified, only the specified details will be returned. NOTE that if return_fields is unspecified, the complete set of native fields are returned, which DOES NOT include tags at this time. Some fields may only be viewed by admins. """ roles = ['admin', 'pi', 'user', 'tech', 'node', 'anonymous'] accepts = [ Auth(), Mixed([Mixed(Node.fields['node_id'], Node.fields['hostname'])], Parameter(str,"hostname"), Parameter(int,"node_id"), Filter(legacy_node_fields)), Parameter([str], "List of fields to return", nullok = True), ] returns = [legacy_node_fields] # needed for generating the doc and prevent conflicts in the xml ids status = 'legacy' def call(self, auth, node_filter = None, return_fields = None): # Must query at least slice_ids_whitelist if return_fields is not None: added_fields = set(['slice_ids_whitelist', 'site_id']).difference(return_fields) return_fields += added_fields else: added_fields =[] # Get node information nodes = Nodes(self.api, node_filter, return_fields) # Remove admin only fields if not isinstance(self.caller, Person) or \ 'admin' not in self.caller['roles']: slice_ids = set() site_ids = set() if self.caller: slice_ids.update(self.caller['slice_ids']) if isinstance(self.caller, Node): site_ids.update([self.caller['site_id']]) else: site_ids.update(self.caller['site_ids']) # if node has whitelist, only return it if users is at # the same site or user has a slice on the whitelist for node in nodes[:]: if 'site_id' in node and \ site_ids.intersection([node['site_id']]): continue if 'slice_ids_whitelist' in node and \ node['slice_ids_whitelist'] and \ not slice_ids.intersection(node['slice_ids_whitelist']): nodes.remove(node) # remove remaining admin only fields for node in nodes: for field in admin_only: if field in node: del node[field] # remove added fields if not specified if added_fields: for node in nodes: for field in added_fields: del node[field] for node in nodes: node = clean_node_fields(node) return nodes