Exemple #1
0
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):
    """Create an association, raise if already exists or ns not found."""

    namespace = namespace_api.get(
        context, namespace_name, session)

    # if the resource_type does not exist, create it
    resource_type_name = values['name']
    metadef_utils.drop_protected_attrs(
        models.MetadefNamespaceResourceType, values)
    try:
        resource_type = resource_type_api.get(
            context, resource_type_name, session)
    except exc.NotFound:
        resource_type = None
        LOG.debug("Creating resource-type %s" % resource_type_name)

    if resource_type is None:
        resource_type_dict = {'name': resource_type_name, 'protected': False}
        resource_type = resource_type_api.create(
            context, resource_type_dict, session)

    # Create the association record, set the field values
    ns_resource_type_dict = _to_db_dict(
        namespace['id'], resource_type['id'], values)
    new_rec = _create_association(context, namespace_name, resource_type_name,
                                  ns_resource_type_dict, session)

    return _to_model_dict(resource_type_name, new_rec)
Exemple #3
0
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
Exemple #4
0
def update(context, namespace_name, object_id, values, session):
    """Update an object, raise if ns not found/visible or duplicate result"""
    namespace_api.get(context, namespace_name, session)

    md_object = _get(context, object_id, session)
    metadef_utils.drop_protected_attrs(models.MetadefObject, values)
    # values['updated_at'] = timeutils.utcnow() - done by TS mixin
    try:
        md_object.update(values.copy())
        md_object.save(session=session)
    except db_exc.DBDuplicateEntry:
        msg = ("Invalid update. It would result in a duplicate"
               " metadata definition object with same name=%(name)s"
               " in namespace=%(namespace_name)s." % {
                   'name': md_object.name,
                   'namespace_name': namespace_name
               })
        LOG.debug(msg)
        emsg = (_("Invalid update. It would result in a duplicate"
                  " metadata definition object with the same name=%(name)s"
                  " in namespace=%(namespace_name)s.") % {
                      'name': md_object.name,
                      'namespace_name': namespace_name
                  })
        raise exc.MetadefDuplicateObject(emsg)

    return md_object.to_dict()
Exemple #5
0
def update(context, namespace_name, property_id, values, session):
    """Update a property, raise if ns not found/visible or duplicate result"""

    namespace_api.get(context, namespace_name, session)
    property_rec = _get(context, property_id, session)
    metadef_utils.drop_protected_attrs(models.MetadefProperty, values)
    # values['updated_at'] = timeutils.utcnow() - done by TS mixin
    try:
        property_rec.update(values.copy())
        property_rec.save(session=session)
    except db_exc.DBDuplicateEntry:
        msg = ("Invalid update. It would result in a duplicate"
               " metadata definition property with the same name=%(name)s"
               " in namespace=%(namespace_name)s."
               % {'name': property_rec.name,
                  'namespace_name': namespace_name})
        LOG.debug(msg)
        emsg = (_("Invalid update. It would result in a duplicate"
                  " metadata definition property with the same name=%(name)s"
                  " in namespace=%(namespace_name)s.")
                % {'name': property_rec.name,
                   'namespace_name': namespace_name})
        raise exc.MetadefDuplicateProperty(emsg)

    return property_rec.as_dict()
Exemple #6
0
def create(context, namespace_name, values, session):
    """Create an association, raise if already exists or ns not found."""

    namespace = namespace_api.get(context, namespace_name, session)

    # if the resource_type does not exist, create it
    resource_type_name = values['name']
    metadef_utils.drop_protected_attrs(models.MetadefNamespaceResourceType,
                                       values)
    try:
        resource_type = resource_type_api.get(context, resource_type_name,
                                              session)
    except exc.NotFound:
        resource_type = None
        LOG.debug("Creating resource-type %s", resource_type_name)

    if resource_type is None:
        resource_type_dict = {'name': resource_type_name, 'protected': False}
        resource_type = resource_type_api.create(context, resource_type_dict,
                                                 session)

    # Create the association record, set the field values
    ns_resource_type_dict = _to_db_dict(namespace['id'], resource_type['id'],
                                        values)
    new_rec = _create_association(context, namespace_name, resource_type_name,
                                  ns_resource_type_dict, session)

    return _to_model_dict(resource_type_name, new_rec)
Exemple #7
0
def update(context, namespace_name, object_id, values, session):
    """Update an object, raise if ns not found/visible or duplicate result"""
    namespace_api.get(context, namespace_name, session)

    md_object = _get(context, object_id, session)
    metadef_utils.drop_protected_attrs(models.MetadefObject, values)
    # values['updated_at'] = timeutils.utcnow() - done by TS mixin
    try:
        md_object.update(values.copy())
        md_object.save(session=session)
    except db_exc.DBDuplicateEntry:
        msg = (
            "Invalid update. It would result in a duplicate"
            " metadata definition object with same name=%(name)s"
            " in namespace=%(namespace_name)s." % {"name": md_object.name, "namespace_name": namespace_name}
        )
        LOG.debug(msg)
        emsg = _(
            "Invalid update. It would result in a duplicate"
            " metadata definition object with the same name=%(name)s"
            " in namespace=%(namespace_name)s."
        ) % {"name": md_object.name, "namespace_name": namespace_name}
        raise exc.MetadefDuplicateObject(emsg)

    return md_object.as_dict()
Exemple #8
0
def update(context, namespace_name, property_id, values, session):
    """Update a property, raise if ns not found/visible or duplicate result"""

    namespace_api.get(context, namespace_name, session)
    property_rec = _get(context, property_id, session)
    metadef_utils.drop_protected_attrs(models.MetadefProperty, values)
    # values['updated_at'] = timeutils.utcnow() - done by TS mixin
    try:
        property_rec.update(values.copy())
        property_rec.save(session=session)
    except db_exc.DBDuplicateEntry:
        LOG.debug(
            "Invalid update. It would result in a duplicate"
            " metadata definition property with the same name=%(name)s"
            " in namespace=%(namespace_name)s.", {
                'name': property_rec.name,
                'namespace_name': namespace_name
            })
        emsg = (_("Invalid update. It would result in a duplicate"
                  " metadata definition property with the same name=%(name)s"
                  " in namespace=%(namespace_name)s.") % {
                      'name': property_rec.name,
                      'namespace_name': namespace_name
                  })
        raise exc.MetadefDuplicateProperty(emsg)

    return property_rec.to_dict()
Exemple #9
0
def update(context, values, session):
    """Update a resource type, raise if not found"""

    name = values['name']
    metadef_utils.drop_protected_attrs(models.MetadefResourceType, values)
    db_rec = get(context, name, session)
    db_rec.update(values.copy())
    db_rec.save(session=session)

    return db_rec.to_dict()
Exemple #10
0
def create(context, values, session):
    """Create a resource_type, raise if it already exists."""

    resource_type = models.MetadefResourceType()
    metadef_utils.drop_protected_attrs(models.MetadefResourceType, values)
    resource_type.update(values.copy())
    try:
        resource_type.save(session=session)
    except db_exc.DBDuplicateEntry:
        LOG.debug("Can not create the metadata definition resource-type. "
                  "A resource-type with name=%s already exists.",
                  resource_type.name)
        raise exc.MetadefDuplicateResourceType(
            resource_type_name=resource_type.name)

    return resource_type.to_dict()
Exemple #11
0
def create(context, values, session):
    """Create a resource_type, raise if it already exists."""

    resource_type = models.MetadefResourceType()
    metadef_utils.drop_protected_attrs(models.MetadefResourceType, values)
    resource_type.update(values.copy())
    try:
        resource_type.save(session=session)
    except db_exc.DBDuplicateEntry:
        LOG.debug(
            "Can not create the metadata definition resource-type. "
            "A resource-type with name=%s already exists.", resource_type.name)
        raise exc.MetadefDuplicateResourceType(
            resource_type_name=resource_type.name)

    return resource_type.to_dict()
Exemple #12
0
def create(context, namespace_name, values, session):
    namespace = namespace_api.get(context, namespace_name, session)
    values.update({'namespace_id': namespace['id']})

    md_object = models.MetadefObject()
    metadef_utils.drop_protected_attrs(models.MetadefObject, values)
    md_object.update(values.copy())
    try:
        md_object.save(session=session)
    except db_exc.DBDuplicateEntry:
        LOG.debug("A metadata definition object with name=%(name)s"
                  " in namespace=%(namespace_name)s already exists.",
                  {'name': md_object.name,
                   'namespace_name': namespace_name})
        raise exc.MetadefDuplicateObject(
            object_name=md_object.name, namespace_name=namespace_name)

    return md_object.to_dict()
Exemple #13
0
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()
Exemple #14
0
def create(context, namespace_name, values, session):
    namespace = namespace_api.get(context, namespace_name, session)
    values.update({'namespace_id': namespace['id']})

    md_object = models.MetadefObject()
    metadef_utils.drop_protected_attrs(models.MetadefObject, values)
    md_object.update(values.copy())
    try:
        md_object.save(session=session)
    except db_exc.DBDuplicateEntry:
        msg = ("A metadata definition object with name=%(name)s"
               " in namespace=%(namespace_name)s already exists."
               % {'name': md_object.name,
                  'namespace_name': namespace_name})
        LOG.debug(msg)
        raise exc.MetadefDuplicateObject(
            object_name=md_object.name, namespace_name=namespace_name)

    return md_object.to_dict()
Exemple #15
0
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()
Exemple #16
0
def create(context, namespace_name, values, session):
    namespace = namespace_api.get(context, namespace_name, session)
    values.update({'namespace_id': namespace['id']})

    property_rec = models.MetadefProperty()
    metadef_utils.drop_protected_attrs(models.MetadefProperty, values)
    property_rec.update(values.copy())

    try:
        property_rec.save(session=session)
    except db_exc.DBDuplicateEntry:
        LOG.debug("Can not create metadata definition property. A property"
                  " with name=%(name)s already exists in"
                  " namespace=%(namespace_name)s.",
                  {'name': property_rec.name,
                   'namespace_name': namespace_name})
        raise exc.MetadefDuplicateProperty(
            property_name=property_rec.name,
            namespace_name=namespace_name)

    return property_rec.to_dict()
def create(context, namespace_name, values, session):
    namespace = namespace_api.get(context, namespace_name, session)
    values.update({'namespace_id': namespace['id']})

    property_rec = models.MetadefProperty()
    metadef_utils.drop_protected_attrs(models.MetadefProperty, values)
    property_rec.update(values.copy())

    try:
        property_rec.save(session=session)
    except db_exc.DBDuplicateEntry:
        msg = ("Can not create metadata definition property. A property"
               " with name=%(name)s already exists in"
               " namespace=%(namespace_name)s." % {
                   'name': property_rec.name,
                   'namespace_name': namespace_name
               })
        LOG.debug(msg)
        raise exc.MetadefDuplicateProperty(property_name=property_rec.name,
                                           namespace_name=namespace_name)

    return property_rec.to_dict()
def _create_association(
        context, namespace_name, resource_type_name, values, session):
    """Create an association, raise if it already exists."""

    namespace_resource_type_rec = models.MetadefNamespaceResourceType()
    metadef_utils.drop_protected_attrs(
        models.MetadefNamespaceResourceType, values)
    # values['updated_at'] = timeutils.utcnow() # TS mixin should do this
    namespace_resource_type_rec.update(values.copy())
    try:
        namespace_resource_type_rec.save(session=session)
    except db_exc.DBDuplicateEntry:
        LOG.debug("The metadata definition resource-type association of"
                  " resource_type=%(resource_type_name)s to"
                  " namespace=%(namespace_name)s, already exists.",
                  {'resource_type_name': resource_type_name,
                   'namespace_name': namespace_name})
        raise exc.MetadefDuplicateResourceTypeAssociation(
            resource_type_name=resource_type_name,
            namespace_name=namespace_name)

    return namespace_resource_type_rec.to_dict()
def _create_association(
        context, namespace_name, resource_type_name, values, session):
    """Create an association, raise if it already exists."""

    namespace_resource_type_rec = models.MetadefNamespaceResourceType()
    metadef_utils.drop_protected_attrs(
        models.MetadefNamespaceResourceType, values)
    # values['updated_at'] = timeutils.utcnow() # TS mixin should do this
    namespace_resource_type_rec.update(values.copy())
    try:
        namespace_resource_type_rec.save(session=session)
    except db_exc.DBDuplicateEntry:
        msg = ("The metadata definition resource-type association of"
               " resource_type=%(resource_type_name)s to"
               " namespace=%(namespace_name)s, already exists."
               % {'resource_type_name': resource_type_name,
                  'namespace_name': namespace_name})
        LOG.debug(msg)
        raise exc.MetadefDuplicateResourceTypeAssociation(
            resource_type_name=resource_type_name,
            namespace_name=namespace_name)

    return namespace_resource_type_rec.as_dict()