1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { AxiosInstance } from "axios";
6 import { snakeCase } from "lodash";
7 import { Resource } from "models/resource";
8 import { ApiActions } from "services/api/api-actions";
9 import { CommonService } from "services/common-service/common-service";
11 export enum CommonResourceServiceError {
12 UNIQUE_NAME_VIOLATION = 'UniqueNameViolation',
13 OWNERSHIP_CYCLE = 'OwnershipCycle',
14 MODIFYING_CONTAINER_REQUEST_FINAL_STATE = 'ModifyingContainerRequestFinalState',
15 NAME_HAS_ALREADY_BEEN_TAKEN = 'NameHasAlreadyBeenTaken',
20 export class CommonResourceService<T extends Resource> extends CommonService<T> {
21 constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions, readOnlyFields: string[] = []) {
22 super(serverApi, resourceType, actions, readOnlyFields.concat([
29 create(data?: Partial<T>, showErrors?: boolean) {
31 if (data !== undefined) {
32 this.readOnlyFields.forEach( field => delete data[field] );
34 [this.resourceType.slice(0, -1)]: CommonService.mapKeys(snakeCase)(data),
37 return super.create(payload, showErrors);
40 update(uuid: string, data: Partial<T>, showErrors?: boolean, select?: string[]) {
42 if (data !== undefined) {
43 this.readOnlyFields.forEach( field => delete data[field] );
45 [this.resourceType.slice(0, -1)]: CommonService.mapKeys(snakeCase)(data),
47 if (select !== undefined && select.length > 0) {
48 payload.select = ['uuid', ...select.map(field => snakeCase(field))];
51 return super.update(uuid, payload, showErrors);
55 export const getCommonResourceServiceError = (errorResponse: any) => {
56 if ('errors' in errorResponse) {
57 const error = errorResponse.errors.join('');
59 case /UniqueViolation/.test(error):
60 return CommonResourceServiceError.UNIQUE_NAME_VIOLATION;
61 case /ownership cycle/.test(error):
62 return CommonResourceServiceError.OWNERSHIP_CYCLE;
63 case /Mounts cannot be modified in state 'Final'/.test(error):
64 return CommonResourceServiceError.MODIFYING_CONTAINER_REQUEST_FINAL_STATE;
65 case /Name has already been taken/.test(error):
66 return CommonResourceServiceError.NAME_HAS_ALREADY_BEEN_TAKEN;
68 return CommonResourceServiceError.UNKNOWN;
71 return CommonResourceServiceError.NONE;