def patch()

in src/lambdas/r8s_api_handler/processors/storage_processor.py [0:0]


    def patch(self, event):
        _LOG.debug(f'Patch storage event" {event}')

        validate_params(event, (NAME_ATTR,))

        optional_attrs = (TYPE_ATTR, ACCESS_ATTR)

        if not any([event.get(attr) for attr in optional_attrs]):
            _LOG.error(f'At least one of the following attributes must be '
                       f'specified: {optional_attrs}')
            return build_response(
                code=RESPONSE_BAD_REQUEST_CODE,
                content=f'At least one of the following attributes must be '
                        f'specified: {optional_attrs}'
            )

        name = event.get(NAME_ATTR)
        storage = self.storage_service.get_by_name(name=name)
        if not storage:
            _LOG.error(f'Storage with name \'{name}\' does not exists.')
            return build_response(
                code=RESPONSE_RESOURCE_NOT_FOUND_CODE,
                content=f'Storage with name \'{name}\' does not exists.'
            )

        storage_type = event.get(TYPE_ATTR)
        if storage_type:
            _LOG.debug(f'Validating storage type: \'{storage_type}\'')
            storage_type = self._validate_storage_type(
                storage_type=storage_type)
            if storage_type != storage.type:
                _LOG.debug(f'Updating storage \'{name}\' storage type to '
                           f'{storage_type.value}')
                storage.type = storage_type

        access = event.get(ACCESS_ATTR)
        if access:
            access_doc = None
            if access.get(BUCKET_NAME_ATTR):
                access_doc = {
                    BUCKET_NAME_ATTR: access.get(BUCKET_NAME_ATTR),
                    PREFIX_ATTR: access.get(PREFIX_ATTR)
                }
            elif access.get(PREFIX_ATTR):
                access_doc = {
                    BUCKET_NAME_ATTR: storage.access.bucket_name,
                    PREFIX_ATTR: access.get(PREFIX_ATTR)
                }
            if access_doc:
                _LOG.debug(f'Validating storage type access')
                access_doc = self.storage_service.validate_storage_access(
                    service=storage.service,
                    access=access_doc
                )
                _LOG.debug(f'Updating storage \'{name}\' access to '
                           f'\'{access_doc}\'')
                if isinstance(storage, Storage):
                    storage.access = access_doc.to_mongo().to_dict()
                elif isinstance(storage, S3Storage):
                    storage.access = access_doc

        _LOG.debug(f'Saving updated storage')
        self.storage_service.save(storage=storage)

        _LOG.debug('Describing storage dto')
        storage_dto = storage.get_dto()

        _LOG.debug(f'Response: {storage_dto}')
        return build_response(
            code=RESPONSE_OK_CODE,
            content=storage_dto
        )