- def __init__(self, api, node_id_or_hostname_list = None):
- self.api = api
-
- sql = ""
- sql += "SELECT %s FROM view_nodes " % ", ".join(Node.fields)
- sql += "WHERE deleted IS False"
-
- if node_id_or_hostname_list:
- # Separate the list into integers and strings
- node_ids = filter(lambda node_id: isinstance(node_id, (int, long)),
- node_id_or_hostname_list)
- hostnames = filter(lambda hostname: isinstance(hostname, StringTypes),
- node_id_or_hostname_list)
- sql += " AND (False"
- if node_ids:
- sql += " OR node_id IN (%s)" % ", ".join(map(str, node_ids))
- if hostnames:
- sql += " OR hostname IN (%s)" % ", ".join(api.db.quote(hostnames)).lower()
- sql += ")"
-
- rows = self.api.db.selectall(sql)
-
- for row in rows:
- self[row['node_id']] = node = Node(api, row)
- for aggregate in ['nodenetwork_ids', 'nodegroup_ids',
- 'conf_file_ids', 'root_person_ids', 'slice_ids',
- 'pcu_ids']:
- if not node.has_key(aggregate) or node[aggregate] is None:
- node[aggregate] = []
- else:
- node[aggregate] = map(int, node[aggregate].split(','))
+ def __init__(self, api, node_filter = None):
+ Table.__init__(self, api, Node)
+
+ sql = "SELECT %s FROM view_nodes WHERE deleted IS False" % \
+ ", ".join(Node.fields)
+
+ if node_filter is not None:
+ if isinstance(node_filter, list):
+ # Separate the list into integers and strings
+ ints = filter(lambda x: isinstance(x, (int, long)), node_filter)
+ strs = filter(lambda x: isinstance(x, StringTypes), node_filter)
+ node_filter = Filter(Node.fields, {'node_id': ints, 'hostname': strs})
+ sql += " AND (%s)" % node_filter.sql(api, "OR")
+ elif isinstance(node_filter, dict):
+ node_filter = Filter(Node.fields, node_filter)
+ sql += " AND (%s)" % node_filter.sql(api, "AND")
+
+ self.selectall(sql)