- self.validate()
-
- try:
- if not self['name'] or \
- not self['abbreviated_name'] or \
- not self['login_base']:
- raise KeyError
- except KeyError:
- raise PLCInvalidArgument, "name, abbreviated_name, and login_base must all be specified"
-
- # Fetch a new site_id if necessary
- if 'site_id' not in self:
- rows = self.api.db.selectall("SELECT NEXTVAL('sites_site_id_seq') AS site_id")
- if not rows:
- raise PLCDBError, "Unable to fetch new site_id"
- self['site_id'] = rows[0]['site_id']
- insert = True
- else:
- insert = False
-
- # Create site node group 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']
-
- nodegroup_id = self['nodegroup_id']
- # XXX Needs a unique name because we cannot delete site node groups yet
- name = self['login_base'] + str(self['site_id'])
- description = "Nodes at " + self['login_base']
- is_custom = False
- self.api.db.do("INSERT INTO nodegroups (nodegroup_id, name, description, is_custom)" \
- " VALUES (%(nodegroup_id)d, %(name)s, %(description)s, %(is_custom)s)",
- locals())
-
- # 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 sites table
- self.api.db.do("INSERT INTO sites (%s) VALUES (%s)" % \
- (", ".join(keys), ", ".join(values)),
- fields)
-
- # Setup default slice site info
- # XXX Will go away soon
- self['max_slices'] = self.default_max_slices
- self['site_share'] = self.default_site_share
- self.api.db.do("INSERT INTO dslice03_siteinfo (site_id, max_slices, site_share)" \
- " VALUES (%(site_id)d, %(max_slices)d, %(site_share)f)",
- self)
- else:
- # Update default slice site info
- # XXX Will go away soon
- if 'max_slices' in self and 'site_share' in self:
- self.api.db.do("UPDATE dslice03_siteinfo SET " \
- " max_slices = %(max_slices)d, site_share = %(site_share)f" \
- " WHERE site_id = %(site_id)d",
- self)
-
- # Update existing row in sites table
- columns = ["%s = %s" % (key, value) for (key, value) in zip(keys, values)]
- self.api.db.do("UPDATE sites SET " + \
- ", ".join(columns) + \
- " WHERE site_id = %(site_id)d",
- fields)
-
- if commit:
- self.api.db.commit()