def create_tags(context, namespace_name, tag_list, session): metadef_tags_list = [] if tag_list: namespace = namespace_api.get(context, namespace_name, session) try: with session.begin(): query = (session.query( models.MetadefTag).filter_by(namespace_id=namespace['id'])) query.delete(synchronize_session='fetch') for value in tag_list: value.update({'namespace_id': namespace['id']}) metadef_utils.drop_protected_attrs(models.MetadefTag, value) metadef_tag = models.MetadefTag() metadef_tag.update(value.copy()) metadef_tag.save(session=session) metadef_tags_list.append(metadef_tag.to_dict()) except db_exc.DBDuplicateEntry: msg = ("A metadata tag name=%(name)s" " in namespace=%(namespace_name)s already exists." % { 'name': metadef_tag.name, 'namespace_name': namespace_name }) LOG.debug(msg) raise exc.MetadefDuplicateTag(name=metadef_tag.name, namespace_name=namespace_name) return metadef_tags_list
def create(context, namespace_name, values, session): namespace = namespace_api.get(context, namespace_name, session) values.update({'namespace_id': namespace['id']}) metadef_tag = models.MetadefTag() metadef_utils.drop_protected_attrs(models.MetadefTag, values) metadef_tag.update(values.copy()) try: metadef_tag.save(session=session) except db_exc.DBDuplicateEntry: LOG.debug("A metadata tag name=%(name)s" " in namespace=%(namespace_name)s already exists.", {'name': metadef_tag.name, 'namespace_name': namespace_name}) raise exc.MetadefDuplicateTag( name=metadef_tag.name, namespace_name=namespace_name) return metadef_tag.to_dict()
def update(context, namespace_name, id, values, session): """Update an tag, raise if ns not found/visible or duplicate result""" namespace_api.get(context, namespace_name, session) metadata_tag = _get(context, id, session) metadef_utils.drop_protected_attrs(models.MetadefTag, values) # values['updated_at'] = timeutils.utcnow() - done by TS mixin try: metadata_tag.update(values.copy()) metadata_tag.save(session=session) except db_exc.DBDuplicateEntry: LOG.debug("Invalid update. It would result in a duplicate" " metadata tag with same name=%(name)s" " in namespace=%(namespace_name)s.", {'name': values['name'], 'namespace_name': namespace_name}) raise exc.MetadefDuplicateTag( name=values['name'], namespace_name=namespace_name) return metadata_tag.to_dict()