def create_from(self, entity): """Sub-class hook: create from entity.""" if not entity: msg = "Must supply non-None {0}.".format(self._do_entity_name) raise exception.Invalid(msg) if entity.id: msg = "Must supply {0} with id=None(i.e. new entity).".format( self._do_entity_name) raise exception.Invalid(msg) LOG.debug("Begin create from...") session = get_session() with session.begin(): # Validate the attributes before we go any further. From my # (unknown Glance developer) investigation, the @validates # decorator does not validate # on new records, only on existing records, which is, well, # idiotic. values = self._do_validate(entity.to_dict()) try: LOG.debug("Saving entity...") entity.save(session=session) except sqlalchemy.exc.IntegrityError: LOG.exception('Problem saving entity for create') raise exception.Duplicate("Entity ID %s already exists!" % values['id']) return entity
def _update(self, entity_id, values, purge_props=False): """ Used internally by update() :param values: A dict of attributes to set :param entity_id: If None, create the entity, otherwise, find and update it """ session = get_session() with session.begin(): if entity_id: entity_ref = self.get(entity_id, session=session) values['updated_at'] = timeutils.utcnow() else: self._do_convert_values(values) entity_ref = self._do_create_instance() # Need to canonicalize ownership if 'owner' in values and not values['owner']: values['owner'] = None entity_ref.update(values) # Validate the attributes before we go any further. From my # (unknown Glance developer) investigation, the @validates # decorator does not validate # on new records, only on existing records, which is, well, # idiotic. self._do_validate(entity_ref.to_dict()) self._update_values(entity_ref, values) try: entity_ref.save(session=session) except sqlalchemy.exc.IntegrityError: LOG.exception('Problem saving entity for _update') if entity_id: raise exception.NotFound("Entity ID %s not found" % entity_id) else: raise exception.Duplicate("Entity ID %s already exists!" % values['id']) return self.get(entity_ref.id)
def _raise_entity_already_exists(entity_name): raise exception.Duplicate( u._("Entity '{entity_name}' " "already exists").format(entity_name=entity_name))