예제 #1
0
async def put_service(service_id: str,
                      service: schemas.ServiceUpdate,
                      db: databases.Database = Depends(models.get_db)):
    updated = None
    try:
        updated = await crud.db_update(db=db,
                                       table=models.get_table("service"),
                                       values=service.dict(exclude_unset=True),
                                       obj_id=service_id)
    except sqlite3.IntegrityError as exc:
        raise errors.DataException(
            service, getattr(service, exc.args[0].split('.')[-1]),
            errors.CONFLICT_ERROR)
    if not updated:
        raise errors.DataException(service, service_id, errors.NOTFOUND_ERROR)
    return updated
예제 #2
0
async def post_service(service: schemas.ServiceCreate,
                       db: databases.Database = Depends(models.get_db)):
    logger.debug(f"Create serivce with data: {service}")
    created = None
    try:
        logger.debug("Looking for available drivers for backend service")
        drivers_avail = await drivers.match_drivers(service)
        if drivers_avail:
            service.driver = f"{drivers_avail[0].name}-{drivers_avail[0].version}"
            logger.debug(f"Driver match: {service.driver}")
        created = await crud.db_create(db=db,
                                       table=models.get_table("service"),
                                       values=service.dict())
        if created and drivers_avail:
            if not drivers.is_driver_deployed(drivers_avail[0]):
                driver_deployed = drivers.deploy_driver(drivers_avail[0])
                if not driver_deployed:
                    created["driver"] = None
                    await put_service(created["id"],
                                      schemas.ServiceCreate(**created), db)
    except sqlite3.IntegrityError as exc:
        raise errors.DataException(
            service, getattr(service, exc.args[0].split('.')[-1]),
            errors.CONFLICT_ERROR)
    return created
예제 #3
0
async def delete_service(service_id: str,
                         db: databases.Database = Depends(models.get_db)):
    table = models.get_table('service')
    deleted = await crud.db_delete(db=db, table=table, obj_id=service_id)
    if not deleted:
        raise errors.DataException(schemas.Service, service_id,
                                   errors.NOTFOUND_ERROR)
    return deleted
예제 #4
0
async def get_service(service_id: str,
                      db: databases.Database = Depends(models.get_db)):
    queried = await crud.db_get(db=db,
                                table=models.get_table("service"),
                                obj_id=service_id)
    if queried is None:
        raise errors.DataException(schemas.Service, service_id,
                                   errors.NOTFOUND_ERROR)
    return queried