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.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()