def add_scope(scope, account, session=None): """ add a scope for the given account name. :param scope: the name for the new scope. :param account: the account to add the scope to. :param session: The database session in use. """ if not vo_exists(vo=scope.vo, session=session): raise exception.RucioException('VO {} not found'.format(scope.vo)) result = session.query(models.Account).filter_by( account=account, status=AccountStatus.ACTIVE).first() if result is None: raise AccountNotFound('Account ID \'%s\' does not exist' % account) new_scope = models.Scope(scope=scope, account=account, status=ScopeStatus.OPEN) try: new_scope.save(session=session) except IntegrityError as e: if match('.*IntegrityError.*ORA-00001: unique constraint.*SCOPES_PK.*violated.*', e.args[0]) \ or match('.*IntegrityError.*1062, "Duplicate entry.*for key.*', e.args[0]) \ or match('.*IntegrityError.*UNIQUE constraint failed: scopes.scope.*', e.args[0]) \ or match('.*IntegrityError.*duplicate key value violates unique constraint.*', e.args[0])\ or match('.*sqlite3.IntegrityError.*is not unique.*', e.args[0]): raise Duplicate('Scope \'%s\' already exists!' % scope) except: raise RucioException(str(format_exc()))
def add_key(key, key_type, value_type=None, value_regexp=None, session=None): """ Adds a new allowed key. :param key: the name for the new key. :param key_type: the type of the key: all(container, dataset, file), collection(dataset or container), file, derived(compute from file for collection). :param value_type: the type of the value, if defined. :param value_regexp: the regular expression that values should match, if defined. :param session: The database session in use. """ # Check if value_type is supported if value_type and value_type not in [ str(t) for t in AUTHORIZED_VALUE_TYPES ]: raise UnsupportedValueType( 'The type \'%(value_type)s\' is not supported for values!' % locals()) new_key = models.DIDKey(key=key, value_type=value_type and str(value_type), value_regexp=value_regexp, key_type=key_type) try: new_key.save(session=session) except IntegrityError as error: if error.args[0] == "(IntegrityError) column key is not unique": raise Duplicate('key \'%(key)s\' already exists!' % locals()) raise
def add_value(key, value, session=None): """ Adds a new value to a key. :param key: the name for the key. :param value: the value. :param session: The database session in use. """ new_value = models.DIDKeyValueAssociation(key=key, value=value) try: new_value.save(session=session) except IntegrityError as error: if match('.*IntegrityError.*columns? key.*value.*not unique.*', error.args[0]): raise Duplicate('key-value \'%(key)s-%(value)s\' already exists!' % locals()) if match('.*IntegrityError.*foreign key constraints? failed.*', error.args[0]): raise KeyNotFound("key '%(key)s' does not exist!" % locals()) if match('.*IntegrityError.*ORA-02291: integrity constraint.*DID_MAP_KEYS_FK.*violated.*', error.args[0]): raise KeyNotFound("key '%(key)s' does not exist!" % locals()) if error.args[0] == "(IntegrityError) (1452, 'Cannot add or update a child row: a foreign key constraint fails (`rucio`.`did_key_map`, CONSTRAINT `DID_MAP_KEYS_FK` FOREIGN KEY (`key`) REFERENCES `did_keys` (`key`))')": raise KeyNotFound("key '%(key)s' does not exist!" % locals()) raise RucioException(error.args) k = session.query(models.DIDKey).filter_by(key=key).one() # Check value against regexp, if defined if k.value_regexp and not match(k.value_regexp, value): raise InvalidValueForKey("The value '%s' for the key '%s' does not match the regular expression '%s'" % (value, key, k.value_regexp)) # Check value type, if defined type_map = dict([(str(t), t) for t in AUTHORIZED_VALUE_TYPES]) if k.value_type and not isinstance(value, type_map.get(k.value_type)): raise InvalidValueForKey("The value '%s' for the key '%s' does not match the required type '%s'" % (value, key, k.value_type))
def add_key(key, key_type, value_type=None, value_regexp=None, session=None): """ Adds a new allowed key. :param key: the name for the new key. :param key_type: the type of the key: all(container, dataset, file), collection(dataset or container), file, derived(compute from file for collection). :param value_type: the type of the value, if defined. :param value_regexp: the regular expression that values should match, if defined. :param session: The database session in use. """ # Check if value_type is supported if value_type and value_type not in [ str(t) for t in AUTHORIZED_VALUE_TYPES ]: raise UnsupportedValueType( 'The type \'%(value_type)s\' is not supported for values!' % locals()) # Convert key_type if isinstance(key_type, string_types): key_type = str(key_type) else: key_type = str(key_type.value) if key_type == 'F': key_type = 'FILE' elif key_type == 'D': key_type = 'DATASET' elif key_type == 'C': key_type = 'CONTAINER' try: key_type = KeyType(key_type) except ValueError: raise UnsupportedKeyType('The type \'%s\' is not supported for keys!' % str(key_type)) new_key = models.DIDKey(key=key, value_type=value_type and str(value_type), value_regexp=value_regexp, key_type=key_type) try: new_key.save(session=session) except IntegrityError as error: if ('UNIQUE constraint failed' in error.args[0]) \ or ('conflicts with persistent instance' in error.args[0]) \ or match('.*IntegrityError.*ORA-00001: unique constraint.*DID_KEYS_PK.*violated.*', error.args[0]) \ or match('.*IntegrityError.*1062.*Duplicate entry.*for key.*', error.args[0]) \ or match('.*IntegrityError.*duplicate key value violates unique constraint.*', error.args[0]) \ or match('.*UniqueViolation.*duplicate key value violates unique constraint.*', error.args[0]) \ or match('.*IntegrityError.*columns? key.*not unique.*', error.args[0]): raise Duplicate('key \'%(key)s\' already exists!' % locals()) raise
def add_naming_convention(scope, regexp, convention_type, session=None): """ add a naming convention for a given scope :param scope: the name for the scope. :param regexp: the regular expression to validate the name. :param convention_type: the did_type on which the regexp should apply. :param session: The database session in use. """ # validate the regular expression try: compile(regexp) except error: raise RucioException('Invalid regular expression %s!' % regexp) new_convention = models.NamingConvention(scope=scope, regexp=regexp, convention_type=convention_type) try: new_convention.save(session=session) except IntegrityError: raise Duplicate('Naming convention already exists!') except: raise RucioException(str(format_exc()))
def add_key(key, key_type, value_type=None, value_regexp=None, session=None): """ Adds a new allowed key. :param key: the name for the new key. :param key_type: the type of the key: all(container, dataset, file), collection(dataset or container), file, derived(compute from file for collection). :param value_type: the type of the value, if defined. :param value_regexp: the regular expression that values should match, if defined. :param session: The database session in use. """ # Check if value_type is supported if value_type and value_type not in [str(t) for t in AUTHORIZED_VALUE_TYPES]: raise UnsupportedValueType('The type \'%(value_type)s\' is not supported for values!' % locals()) # Convert key_type key_type = str(key_type) if key_type == 'F': key_type = 'FILE' elif key_type == 'D': key_type = 'DATASET' elif key_type == 'C': key_type = 'CONTAINER' try: key_type = KeyType.from_string(key_type) except ValueError as error: raise UnsupportedKeyType('The type \'%s\' is not supported for keys!' % str(key_type)) new_key = models.DIDKey(key=key, value_type=value_type and str(value_type), value_regexp=value_regexp, key_type=key_type) try: new_key.save(session=session) except IntegrityError as error: if match('.*IntegrityError.*columns? key.*not unique.*', error.args[0]): raise Duplicate('key \'%(key)s\' already exists!' % locals()) raise