예제 #1
0
    def create_namespace(self, name, user, permissions=None):
        uid = str(uuid.uuid4()).replace('-', '')
        try:
            self.create(name, {
                'uuid': uid,
                'permissions': {**(permissions or {}), user: Permissions.ADMIN},
                'logger': {
                    'backend': settings.NAMESPACE_BACKENDS['logger'],
                    'settings': get_backend(settings.NAMESPACE_BACKENDS['logger']).settings_for(self.uuid, uid, 'logger')
                },
                'state': {
                    'backend': settings.NAMESPACE_BACKENDS['state'],
                    'settings': get_backend(settings.NAMESPACE_BACKENDS['state']).settings_for(self.uuid, uid, 'state')
                },
                'storage': {
                    'backend': settings.NAMESPACE_BACKENDS['storage'],
                    'settings': get_backend(settings.NAMESPACE_BACKENDS['storage']).settings_for(self.uuid, uid, 'storage')
                }
            }, user)
        except exceptions.KeyExists:
            raise exceptions.KeyExists(
                code='N409',
                title='Namespace already exists',
                detail='Namespace "{}" already exists'.format(name)
            )

        return self.get_namespace(name)
예제 #2
0
    def create_namespace(self, name, user, permissions=None, **kwargs):
        if kwargs:
            raise exceptions.InvalidFields(kwargs.keys())

        uid = str(uuid.uuid4()).replace('-', '')

        if isinstance(permissions or {}, dict):
            try:
                permissions = {
                    key: Permissions.from_string(value)
                    for key, value in (permissions or {}).items()
                }
                permissions[user] = Permissions.ADMIN
            except KeyError as e:
                raise exceptions.InvalidPermission(e.args[0])
            except AttributeError:
                pass  # Schema validation will catch issues

        try:
            self.create(
                name, {
                    'uuid': uid,
                    'permissions': {
                        **(permissions or {}), user: Permissions.ADMIN
                    },
                    'logger': {
                        'backend':
                        settings.NAMESPACE_BACKENDS['logger'],
                        'settings':
                        get_backend(settings.NAMESPACE_BACKENDS['logger']).
                        settings_for(self.uuid, uid, 'logger')
                    },
                    'state': {
                        'backend':
                        settings.NAMESPACE_BACKENDS['state'],
                        'settings':
                        get_backend(
                            settings.NAMESPACE_BACKENDS['state']).settings_for(
                                self.uuid, uid, 'state')
                    },
                    'storage': {
                        'backend':
                        settings.NAMESPACE_BACKENDS['storage'],
                        'settings':
                        get_backend(settings.NAMESPACE_BACKENDS['storage']).
                        settings_for(self.uuid, uid, 'storage')
                    }
                }, user)
        except exceptions.KeyExists:
            raise exceptions.KeyExists(
                code='N409',
                title='Namespace already exists',
                detail='Namespace "{}" already exists'.format(name))

        return self.get_namespace(name)
예제 #3
0
    def create(self, key, data, user):
        if self.schema:
            self.schema.validate(data)

        try:
            self._state.get(key)
        except exceptions.NotFound:
            pass
        else:
            raise exceptions.KeyExists(
                code='D409',
                title='Document already exists',
                detail='Document "{}" already exists'.format(key))

        data_object = self._storage.create(data)

        return self._state.apply(
            self._logger.create(key, Operation.CREATE, data_object.ref, user),
            data)
예제 #4
0
    def create_collection(self, name, user, logger=None, storage=None, state=None, permissions=None, schema=None):
        uid = str(uuid.uuid4()).replace('-', '')
        state = state or settings.COLLECTION_BACKENDS['state']
        logger = logger or settings.COLLECTION_BACKENDS['logger']
        storage = storage or settings.COLLECTION_BACKENDS['storage']
        if isinstance(permissions or {}, dict):
            permissions = {**(permissions or {}), user: Permissions.ADMIN}

        collection_dict = {
            'uuid': uid,
            'schema': schema,
            'permissions': permissions,
            'logger': {
                'backend': logger,
                'settings': get_backend(logger).settings_for(self.uuid, uid, 'logger')
            },
            'state': {
                'backend': state,
                'settings': get_backend(state).settings_for(self.uuid, uid, 'state')
            },
            'storage': {
                'backend': storage,
                'settings': get_backend(storage).settings_for(self.uuid, uid, 'storage')
            }
        }

        # Validate that our inputs can actually be deserialized to a collection
        collection = Collection.from_dict(collection_dict)

        try:
            self.create(name, collection_dict, user)
        except exceptions.KeyExists:
            raise exceptions.KeyExists(
                code='C409',
                title='Collection already exists',
                detail='Collection "{}" already exists in namespace "{}"'.format(name, self.name)
            )

        return collection
예제 #5
0
    def create_collection(self,
                          name,
                          user,
                          logger=None,
                          storage=None,
                          state=None,
                          permissions=None,
                          schema=None,
                          plugins=None,
                          **kwargs):
        if kwargs:
            raise exceptions.InvalidFields(kwargs.keys())

        uid = str(uuid.uuid4()).replace('-', '')
        state = state or settings.COLLECTION_BACKENDS['state']
        logger = logger or settings.COLLECTION_BACKENDS['logger']
        storage = storage or settings.COLLECTION_BACKENDS['storage']

        if isinstance(permissions or {}, dict):
            try:
                permissions = {
                    key: Permissions.from_string(value)
                    for key, value in (permissions or {}).items()
                }
                permissions[user] = Permissions.ADMIN
            except KeyError as e:
                raise exceptions.InvalidPermission(e.args[0])
            except AttributeError:
                pass  # Schema validation will catch issues

        collection_dict = {
            'uuid': uid,
            'plugins': plugins or {},
            'schema': schema,
            'permissions': permissions,
            'logger': {
                'backend':
                logger,
                'settings':
                get_backend(logger).settings_for(self.uuid, uid, 'logger')
            },
            'state': {
                'backend':
                state,
                'settings':
                get_backend(state).settings_for(self.uuid, uid, 'state')
            },
            'storage': {
                'backend':
                storage,
                'settings':
                get_backend(storage).settings_for(self.uuid, uid, 'storage')
            }
        }

        # Validate that our inputs can actually be deserialized to a collection
        Collection(
            Document(
                ref=name,
                log_ref=None,
                data_ref=None,
                created_on=0,
                created_by='',
                modified_on=0,
                modified_by='',
                data=collection_dict,
            ))

        try:
            return Collection(self.create(name, collection_dict, user))
        except exceptions.KeyExists:
            raise exceptions.KeyExists(
                code='C409',
                title='Collection already exists',
                detail='Collection "{}" already exists in namespace "{}"'.
                format(name, self.ref))