def _prepare_service_details( service_in_registry: Dict[str, Any], service_in_db: ServiceMetaDataAtDB, service_access_rights_in_db: List[ServiceAccessRightsAtDB], service_owner: Optional[str], ) -> Optional[ServiceOut]: # compose service from registry and DB composed_service = service_in_registry composed_service.update( service_in_db.dict(exclude_unset=True, exclude={"owner"}), access_rights={rights.gid: rights for rights in service_access_rights_in_db}, owner=service_owner if service_owner else None, ) # validate the service validated_service = None try: validated_service = ServiceOut(**composed_service) except ValidationError as exc: logger.warning( "could not validate service [%s:%s]: %s", composed_service.get("key"), composed_service.get("version"), exc, ) return validated_service
async def create_service( self, new_service: ServiceMetaDataAtDB, new_service_access_rights: List[ServiceAccessRightsAtDB], ) -> ServiceMetaDataAtDB: for access_rights in new_service_access_rights: if ( access_rights.key != new_service.key or access_rights.version != new_service.version ): raise ValueError( f"{access_rights} does not correspond to service {new_service.key}:{new_service.version}" ) async with self.db_engine.acquire() as conn: # NOTE: this ensure proper rollback in case of issue async with conn.begin() as _transaction: row: RowProxy = await ( await conn.execute( # pylint: disable=no-value-for-parameter services_meta_data.insert() .values(**new_service.dict(by_alias=True)) .returning(literal_column("*")) ) ).first() created_service = ServiceMetaDataAtDB(**row) for access_rights in new_service_access_rights: insert_stmt = pg_insert(services_access_rights).values( **access_rights.dict(by_alias=True) ) await conn.execute(insert_stmt) return created_service
async def update_service( self, patched_service: ServiceMetaDataAtDB ) -> ServiceMetaDataAtDB: # update the services_meta_data table async with self.db_engine.acquire() as conn: row: RowProxy = await ( await conn.execute( # pylint: disable=no-value-for-parameter services_meta_data.update() .where( (services_meta_data.c.key == patched_service.key) & (services_meta_data.c.version == patched_service.version) ) .values(**patched_service.dict(by_alias=True, exclude_unset=True)) .returning(literal_column("*")) ) ).first() updated_service = ServiceMetaDataAtDB(**row) return updated_service