from PLC.Filter import Filter
from PLC.Debug import profile
from PLC.Table import Row, Table
-from PLC.Interfaces import Interface, Interfaces
+from PLC.NodeTypes import NodeTypes
from PLC.BootStates import BootStates
+from PLC.Interfaces import Interface, Interfaces
def valid_hostname(hostname):
# 1. Each part begins and ends with a letter or number.
table_name = 'nodes'
primary_key = 'node_id'
- join_tables = [ 'slice_node', 'peer_node', 'slice_attribute',
+ join_tables = [ 'slice_node', 'peer_node', 'slice_tag',
'node_session', 'node_slice_whitelist',
'node_tag', 'conf_file_node', 'pcu_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),
}
related_fields = {
'interfaces': [Mixed(Parameter(int, "Interface identifier"),
- Filter(Interface.fields))],
- 'nodegroups': [Mixed(Parameter(int, "NodeGroup identifier"),
- Parameter(str, "NodeGroup name"))],
+ Filter(Interface.fields))],
'conf_files': [Parameter(int, "ConfFile identifier")],
'slices': [Mixed(Parameter(int, "Slice identifier"),
Parameter(str, "Slice name"))],
Parameter(str, "Slice name"))]
}
+ view_tags_name = "view_node_tags"
+ # tags are used by the Add/Get/Update methods to expose tags
+ # this is initialized here and updated by the accessors factory
+ tags = { }
+
def validate_hostname(self, hostname):
if not valid_hostname(hostname):
raise PLCInvalidArgument, "Invalid hostname"
return hostname
+ def validate_node_type(self, node_type):
+ node_types = [row['node_type'] for row in NodeTypes(self.api)]
+ if node_type not in node_types:
+ raise PLCInvalidArgument, "Invalid node type %r"%node_type
+ return node_type
+
def validate_boot_state(self, boot_state):
boot_states = [row['boot_state'] for row in BootStates(self.api)]
if boot_state not in boot_states:
- raise PLCInvalidArgument, "Invalid boot state"
-
+ raise PLCInvalidArgument, "Invalid boot state %r"%boot_state
return boot_state
validate_date_created = Row.validate_timestamp
def associate_interfaces(self, auth, field, value):
"""
- Delete interfaces not found in value list (using DeleteNodeNetwor)k
+ Delete interfaces not found in value list (using DeleteInterface)
Add interfaces found in value list (using AddInterface)
Updates interfaces found w/ interface_id in value list (using UpdateInterface)
"""
- assert 'interfacep_ids' in self
+ assert 'interface_ids' in self
assert 'node_id' in self
assert isinstance(value, list)
assert 'node_id' in self
assert 'interface_ids' in self
- # we need to clean up InterfaceSettings, so handling interfaces as part of join_tables does not work
+ # we need to clean up InterfaceTags, so handling interfaces as part of join_tables does not work
for interface in Interfaces(self.api,self['interface_ids']):
interface.delete()
def __init__(self, api, node_filter = None, columns = None):
Table.__init__(self, api, Node, columns)
- sql = "SELECT %s FROM view_nodes WHERE deleted IS False" % \
- ", ".join(self.columns)
+ # the view that we're selecting upon: start with view_nodes
+ view = "view_nodes"
+ # as many left joins as requested tags
+ for tagname in self.tag_columns:
+ view= "%s left join %s using (%s)"%(view,Node.tagvalue_view_name(tagname),Node.primary_key)
+
+ sql = "SELECT %s FROM %s WHERE deleted IS False" % \
+ (", ".join(self.columns.keys()+self.tag_columns.keys()),view)
if node_filter is not None:
if isinstance(node_filter, (list, tuple, set)):
node_filter = Filter(Node.fields, {'node_id': ints, 'hostname': strs})
sql += " AND (%s) %s" % node_filter.sql(api, "OR")
elif isinstance(node_filter, dict):
- node_filter = Filter(Node.fields, node_filter)
+ allowed_fields=dict(Node.fields.items()+Node.tags.items())
+ node_filter = Filter(allowed_fields, node_filter)
sql += " AND (%s) %s" % node_filter.sql(api, "AND")
elif isinstance (node_filter, StringTypes):
node_filter = Filter(Node.fields, {'hostname':[node_filter]})