- self.validate()
-
- # Fetch a new nodegroup_id if necessary
- if 'nodegroup_id' not in self:
- rows = self.api.db.selectall("SELECT NEXTVAL('nodegroups_nodegroup_id_seq') AS nodegroup_id")
- if not rows:
- raise PLCDBError, "Unable to fetch new nodegroup_id"
- self['nodegroup_id'] = rows[0]['nodegroup_id']
- insert = True
- else:
- insert = False
-
- # Filter out fields that cannot be set or updated directly
- fields = dict(filter(lambda (key, value): key in self.fields,
- self.items()))
-
- # Parameterize for safety
- keys = fields.keys()
- values = [self.api.db.param(key, value) for (key, value) in fields.items()]
-
- if insert:
- # Insert new row in nodegroups table
- sql = "INSERT INTO nodegroups (%s) VALUES (%s)" % \
- (", ".join(keys), ", ".join(values))
- else:
- # Update existing row in sites table
- columns = ["%s = %s" % (key, value) for (key, value) in zip(keys, values)]
- sql = "UPDATE nodegroups SET " + \
- ", ".join(columns) + \
- " WHERE nodegroup_id = %(nodegroup_id)d"
-
- self.api.db.do(sql, fields)
-
- if commit:
- self.api.db.commit()
-
-
- def delete(self, commit = True):
+ assert 'node_ids' in self
+ assert 'nodegroup_id' in self
+ assert isinstance(value, list)
+
+ (node_ids, hostnames) = self.separate_types(value)[0:2]
+
+ # Translate hostnames into node_ids
+ if hostnames:
+ nodes = Nodes(self.api, hostnames, ['node_id']).dict('node_id')
+ node_ids += nodes.keys()
+
+ # Add new ids, remove stale ids
+ if self['node_ids'] != node_ids:
+ from PLC.Methods.AddNodeToNodeGroup import AddNodeToNodeGroup
+ from PLC.Methods.DeleteNodeFromNodeGroup import DeleteNodeFromNodeGroup
+ new_nodes = set(node_ids).difference(self['node_ids'])
+ stale_nodes = set(self['node_ids']).difference(node_ids)
+
+ for new_node in new_nodes:
+ AddNodeToNodeGroup.__call__(AddNodeToNodeGroup(self.api), auth, new_node, self['nodegroup_id'])
+ for stale_node in stale_nodes:
+ DeleteNodeFromNodeGroup.__call__(DeleteNodeFromNodeGroup(self.api), auth, stale_node, self['nodegroup_id'])
+
+ def associate_conf_files(self, auth, field, value):