Exemplo n.º 1
0
async def delete_user_resource(
    response: Response,
    user=Depends(get_current_workspace_owner_or_researcher_user),
    user_resource=Depends(get_user_resource_by_id_from_path),
    workspace_service=Depends(get_workspace_service_by_id_from_path),
    user_resource_repo=Depends(get_repository(UserResourceRepository)),
    operations_repo=Depends(get_repository(OperationRepository)),
    resource_template_repo=Depends(get_repository(ResourceTemplateRepository))
) -> OperationInResponse:
    validate_user_is_workspace_owner_or_resource_owner(user, user_resource)

    if user_resource.isEnabled:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=strings.USER_RESOURCE_NEEDS_TO_BE_DISABLED_BEFORE_DELETION)

    resource_template = resource_template_repo.get_template_by_name_and_version(
        user_resource.templateName, user_resource.templateVersion,
        ResourceType.UserResource, workspace_service.templateName)

    operation = await send_uninstall_message(
        resource=user_resource,
        resource_repo=user_resource_repo,
        operations_repo=operations_repo,
        resource_type=ResourceType.UserResource,
        resource_template_repo=resource_template_repo,
        user=user,
        resource_template=resource_template)

    response.headers["Location"] = construct_location_header(operation)

    return OperationInResponse(operation=operation)
Exemplo n.º 2
0
async def patch_user_resource(
    user_resource_patch: ResourcePatch,
    response: Response,
    user=Depends(get_current_workspace_owner_or_researcher_user),
    user_resource=Depends(get_user_resource_by_id_from_path),
    workspace_service=Depends(get_workspace_service_by_id_from_path),
    user_resource_repo=Depends(get_repository(UserResourceRepository)),
    resource_template_repo=Depends(get_repository(ResourceTemplateRepository)),
    operations_repo=Depends(get_repository(OperationRepository)),
    etag: str = Header(...)
) -> OperationInResponse:
    validate_user_is_workspace_owner_or_resource_owner(user, user_resource)

    try:
        patched_user_resource, resource_template = user_resource_repo.patch_user_resource(
            user_resource, user_resource_patch, etag, resource_template_repo,
            workspace_service.templateName, user)
        operation = await send_resource_request_message(
            resource=patched_user_resource,
            operations_repo=operations_repo,
            resource_repo=user_resource_repo,
            user=user,
            resource_template=resource_template,
            resource_template_repo=resource_template_repo,
            action=RequestAction.Upgrade)

        response.headers["Location"] = construct_location_header(operation)
        return OperationInResponse(operation=operation)
    except CosmosAccessConditionFailedError:
        raise HTTPException(status_code=status.HTTP_409_CONFLICT,
                            detail=strings.ETAG_CONFLICT)
    except ValidationError as v:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
                            detail=v.message)
Exemplo n.º 3
0
async def create_workspace(
    workspace_create: WorkspaceInCreate,
    response: Response,
    user=Depends(get_current_admin_user),
    workspace_repo=Depends(get_repository(WorkspaceRepository)),
    resource_template_repo=Depends(get_repository(ResourceTemplateRepository)),
    operations_repo=Depends(get_repository(OperationRepository))
) -> OperationInResponse:
    try:
        # TODO: This requires Directory.ReadAll ( Application.Read.All ) to be enabled in the Azure AD application to enable a users workspaces to be listed. This should be made optional.
        auth_info = extract_auth_information(workspace_create.properties)
        workspace, resource_template = workspace_repo.create_workspace_item(
            workspace_create, auth_info, user.id)
    except (ValidationError, ValueError) as e:
        logging.error(f"Failed to create workspace model instance: {e}")
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
                            detail=str(e))

    operation = await save_and_deploy_resource(
        resource=workspace,
        resource_repo=workspace_repo,
        operations_repo=operations_repo,
        resource_template_repo=resource_template_repo,
        user=user,
        resource_template=resource_template)
    response.headers["Location"] = construct_location_header(operation)

    return OperationInResponse(operation=operation)
Exemplo n.º 4
0
async def create_user_resource(
    response: Response,
    user_resource_create: UserResourceInCreate,
    user_resource_repo=Depends(get_repository(UserResourceRepository)),
    resource_template_repo=Depends(get_repository(ResourceTemplateRepository)),
    operations_repo=Depends(get_repository(OperationRepository)),
    user=Depends(get_current_workspace_owner_or_researcher_user),
    workspace=Depends(get_deployed_workspace_by_id_from_path),
    workspace_service=Depends(get_deployed_workspace_service_by_id_from_path)
) -> OperationInResponse:

    try:
        user_resource, resource_template = user_resource_repo.create_user_resource_item(
            user_resource_create, workspace.id, workspace_service.id,
            workspace_service.templateName, user.id)
    except (ValidationError, ValueError) as e:
        logging.error(f"Failed create user resource model instance: {e}")
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
                            detail=str(e))

    operation = await save_and_deploy_resource(
        resource=user_resource,
        resource_repo=user_resource_repo,
        operations_repo=operations_repo,
        resource_template_repo=resource_template_repo,
        user=user,
        resource_template=resource_template)
    response.headers["Location"] = construct_location_header(operation)

    return OperationInResponse(operation=operation)
Exemplo n.º 5
0
async def create_shared_service(
    response: Response,
    shared_service_input: SharedServiceInCreate,
    user=Depends(get_current_admin_user),
    shared_services_repo=Depends(get_repository(SharedServiceRepository)),
    resource_template_repo=Depends(get_repository(ResourceTemplateRepository)),
    operations_repo=Depends(get_repository(OperationRepository))
) -> OperationInResponse:
    try:
        shared_service, resource_template = shared_services_repo.create_shared_service_item(
            shared_service_input)
    except (ValidationError, ValueError) as e:
        logging.error(f"Failed create shared service model instance: {e}")
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
                            detail=str(e))
    except DuplicateEntity as e:
        logging.error(f"Shared service already exists: {e}")
        raise HTTPException(status_code=status.HTTP_409_CONFLICT,
                            detail=str(e))

    operation = await save_and_deploy_resource(
        resource=shared_service,
        resource_repo=shared_services_repo,
        operations_repo=operations_repo,
        resource_template_repo=resource_template_repo,
        user=user,
        resource_template=resource_template)
    response.headers["Location"] = construct_location_header(operation)

    return OperationInResponse(operation=operation)
Exemplo n.º 6
0
async def delete_shared_service(
    response: Response,
    user=Depends(get_current_admin_user),
    shared_service=Depends(get_shared_service_by_id_from_path),
    operations_repo=Depends(get_repository(OperationRepository)),
    shared_service_repo=Depends(get_repository(SharedServiceRepository)),
    resource_template_repo=Depends(get_repository(ResourceTemplateRepository))
) -> OperationInResponse:
    if shared_service.isEnabled:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=strings.SHARED_SERVICE_NEEDS_TO_BE_DISABLED_BEFORE_DELETION)

    resource_template = resource_template_repo.get_template_by_name_and_version(
        shared_service.templateName, shared_service.templateVersion,
        ResourceType.SharedService)

    operation = await send_uninstall_message(
        resource=shared_service,
        resource_repo=shared_service_repo,
        operations_repo=operations_repo,
        resource_type=ResourceType.SharedService,
        resource_template_repo=resource_template_repo,
        user=user,
        resource_template=resource_template)

    response.headers["Location"] = construct_location_header(operation)

    return OperationInResponse(operation=operation)
Exemplo n.º 7
0
async def invoke_action_on_workspace_service(
    response: Response,
    action: str,
    user=Depends(get_current_workspace_owner_user),
    workspace_service=Depends(get_workspace_service_by_id_from_path),
    resource_template_repo=Depends(get_repository(ResourceTemplateRepository)),
    operations_repo=Depends(get_repository(OperationRepository)),
    workspace_service_repo=Depends(get_repository(WorkspaceServiceRepository))
) -> OperationInResponse:
    operation = await send_custom_action_message(
        resource=workspace_service,
        resource_repo=workspace_service_repo,
        custom_action=action,
        resource_type=ResourceType.WorkspaceService,
        operations_repo=operations_repo,
        resource_template_repo=resource_template_repo,
        user=user)

    response.headers["Location"] = construct_location_header(operation)

    return OperationInResponse(operation=operation)
Exemplo n.º 8
0
async def invoke_action_on_user_resource(
    response: Response,
    action: str,
    user_resource=Depends(get_user_resource_by_id_from_path),
    workspace_service=Depends(get_workspace_service_by_id_from_path),
    resource_template_repo=Depends(get_repository(ResourceTemplateRepository)),
    user_resource_repo=Depends(get_repository(UserResourceRepository)),
    operations_repo=Depends(get_repository(OperationRepository)),
    user=Depends(get_current_workspace_owner_or_researcher_user)
) -> OperationInResponse:
    validate_user_is_workspace_owner_or_resource_owner(user, user_resource)
    operation = await send_custom_action_message(
        resource=user_resource,
        resource_repo=user_resource_repo,
        custom_action=action,
        resource_type=ResourceType.UserResource,
        operations_repo=operations_repo,
        resource_template_repo=resource_template_repo,
        user=user,
        parent_service_name=workspace_service.templateName)

    response.headers["Location"] = construct_location_header(operation)

    return OperationInResponse(operation=operation)
Exemplo n.º 9
0
async def delete_workspace(
    response: Response,
    user=Depends(get_current_admin_user),
    workspace=Depends(get_workspace_by_id_from_path),
    operations_repo=Depends(get_repository(OperationRepository)),
    workspace_repo=Depends(get_repository(WorkspaceRepository)),
    workspace_service_repo=Depends(get_repository(WorkspaceServiceRepository)),
    resource_template_repo=Depends(get_repository(ResourceTemplateRepository))
) -> OperationInResponse:
    if workspace.isEnabled:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=strings.WORKSPACE_NEEDS_TO_BE_DISABLED_BEFORE_DELETION)
    if len(
            workspace_service_repo.get_active_workspace_services_for_workspace(
                workspace.id)) > 0:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=strings.
            WORKSPACE_SERVICES_NEED_TO_BE_DELETED_BEFORE_WORKSPACE)

    resource_template = resource_template_repo.get_template_by_name_and_version(
        workspace.templateName, workspace.templateVersion,
        ResourceType.Workspace)

    operation = await send_uninstall_message(
        resource=workspace,
        resource_repo=workspace_repo,
        operations_repo=operations_repo,
        resource_type=ResourceType.Workspace,
        resource_template_repo=resource_template_repo,
        user=user,
        resource_template=resource_template)

    response.headers["Location"] = construct_location_header(operation)
    return OperationInResponse(operation=operation)
Exemplo n.º 10
0
async def retrieve_user_resource_operations_by_user_resource_id_and_operation_id(
        user_resource=Depends(get_user_resource_by_id_from_path),
        user=Depends(get_current_workspace_owner_or_researcher_user),
        operation=Depends(get_operation_by_id_from_path)) -> OperationInList:
    validate_user_is_workspace_owner_or_resource_owner(user, user_resource)
    return OperationInResponse(operation=operation)
Exemplo n.º 11
0
async def retrieve_workspace_service_operation_by_workspace_service_id_and_operation_id(
        workspace_service=Depends(get_workspace_service_by_id_from_path),
        operation=Depends(get_operation_by_id_from_path)) -> OperationInList:
    return OperationInResponse(operation=operation)