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 get_scopes(account, session=None): """ get all scopes defined for an account. :param account: the account name to list the scopes of. :param session: The database session in use. :returns: a list of all scope names for this account. """ result = session.query(models.Account).filter_by(account=account).first() if result is None: raise AccountNotFound('Account ID \'%s\' does not exist' % account) scope_list = [] for s in session.query(models.Scope).filter_by(account=account).filter(models.Scope.status != ScopeStatus.DELETED): scope_list.append(s.scope) return scope_list