1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { dialogActions } from '~/store/dialog/dialog-actions';
6 import { RootState } from '~/store/store';
7 import { Dispatch } from 'redux';
8 import { ResourceKind, extractUuidKind } from '~/models/resource';
9 import { getResource } from '~/store/resources/resources';
10 import { GroupContentsResourcePrefix } from '~/services/groups-service/groups-service';
11 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
13 export const ADVANCED_TAB_DIALOG = 'advancedTabDialog';
15 export interface AdvancedTabDialogData {
18 pythonExample: string;
20 CLIGetExample: string;
21 CLIUpdateHeader: string;
22 CLIUpdateExample: string;
28 COLLECTION = 'collection',
29 STORAGE_CLASSES_CONFIRMED = 'storage_classes_confirmed'
33 CONTAINER_REQUEST = 'container_request',
34 OUTPUT_NAME = 'output_name'
39 DELETE_AT = 'delete_at'
42 export const openAdvancedTabDialog = (uuid: string) =>
43 (dispatch: Dispatch<any>, getState: () => RootState) => {
44 const { resources } = getState();
45 const kind = extractUuidKind(uuid);
46 const data = getResource(uuid)(resources);
48 if (kind === ResourceKind.COLLECTION) {
49 const dataCollection: AdvancedTabDialogData = {
51 pythonHeader: pythonHeader(CollectionData.COLLECTION),
52 pythonExample: pythonExample(data.uuid, GroupContentsResourcePrefix.COLLECTION),
53 CLIGetHeader: CLIGetHeader(CollectionData.COLLECTION),
54 CLIGetExample: CLIGetExample(data.uuid, GroupContentsResourcePrefix.COLLECTION),
55 CLIUpdateHeader: CLIUpdateHeader(CollectionData.COLLECTION, CollectionData.STORAGE_CLASSES_CONFIRMED),
56 CLIUpdateExample: CLIUpdateExample(data.uuid, CollectionData.COLLECTION, data.storageClassesConfirmed, CollectionData.STORAGE_CLASSES_CONFIRMED),
57 curlHeader: curlHeader(CollectionData.COLLECTION, CollectionData.STORAGE_CLASSES_CONFIRMED),
58 curlExample: curlExample(data.uuid, GroupContentsResourcePrefix.COLLECTION, data.storageClassesConfirmed, CollectionData.COLLECTION, CollectionData.STORAGE_CLASSES_CONFIRMED)
60 dispatch(dialogActions.OPEN_DIALOG({ id: ADVANCED_TAB_DIALOG, data: dataCollection }));
61 } else if (kind === ResourceKind.PROCESS) {
62 const dataProcess: AdvancedTabDialogData = {
64 pythonHeader: pythonHeader(ProcessData.CONTAINER_REQUEST),
65 pythonExample: pythonExample(data.uuid, GroupContentsResourcePrefix.PROCESS),
66 CLIGetHeader: CLIGetHeader(ProcessData.CONTAINER_REQUEST),
67 CLIGetExample: CLIGetExample(data.uuid, GroupContentsResourcePrefix.PROCESS),
68 CLIUpdateHeader: CLIUpdateHeader(ProcessData.CONTAINER_REQUEST, ProcessData.OUTPUT_NAME),
69 CLIUpdateExample: CLIUpdateExample(data.uuid, ProcessData.CONTAINER_REQUEST, data.outputName, ProcessData.OUTPUT_NAME),
70 curlHeader: curlHeader(ProcessData.CONTAINER_REQUEST, ProcessData.OUTPUT_NAME),
71 curlExample: curlExample(data.uuid, GroupContentsResourcePrefix.PROCESS, data.outputName, ProcessData.CONTAINER_REQUEST, ProcessData.OUTPUT_NAME)
73 dispatch(dialogActions.OPEN_DIALOG({ id: ADVANCED_TAB_DIALOG, data: dataProcess }));
74 } else if (kind === ResourceKind.PROJECT) {
75 const dataProject: AdvancedTabDialogData = {
77 pythonHeader: pythonHeader(ProjectData.GROUP),
78 pythonExample: pythonExample(data.uuid, GroupContentsResourcePrefix.PROJECT),
79 CLIGetHeader: CLIGetHeader(ProjectData.GROUP),
80 CLIGetExample: CLIGetExample(data.uuid, GroupContentsResourcePrefix.PROJECT),
81 CLIUpdateHeader: CLIUpdateHeader(ProjectData.GROUP, ProjectData.DELETE_AT),
82 CLIUpdateExample: CLIUpdateExample(data.uuid, ProjectData.GROUP, data.deleteAt, ProjectData.DELETE_AT),
83 curlHeader: curlHeader(ProjectData.GROUP, ProjectData.DELETE_AT),
84 curlExample: curlExample(data.uuid, GroupContentsResourcePrefix.PROJECT, data.deleteAt, ProjectData.GROUP, ProjectData.DELETE_AT)
86 dispatch(dialogActions.OPEN_DIALOG({ id: ADVANCED_TAB_DIALOG, data: dataProject }));
89 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Could not open advanced tab for this resource.", hideDuration: 2000, kind: SnackbarKind.ERROR }));
93 const pythonHeader = (resourceKind: string) =>
94 `An example python command to get a ${resourceKind} using its uuid:`;
96 const pythonExample = (uuid: string, resourcePrefix: string) => {
97 const pythonExample = `import arvados
99 x = arvados.api().${resourcePrefix}().get(uuid='${uuid}').execute()`;
101 return pythonExample;
104 const CLIGetHeader = (resourceKind: string) =>
105 `An example arv command to get a ${resourceKind} using its uuid:`;
107 const CLIGetExample = (uuid: string, resourcePrefix: string) => {
108 const cliGetExample = `arv ${resourcePrefix} get \\
111 return cliGetExample;
114 const CLIUpdateHeader = (resourceKind: string, resourceName: string) =>
115 `An example arv command to update the "${resourceName}" attribute for the current ${resourceKind}:`;
117 const CLIUpdateExample = (uuid: string, resourceKind: string, resource: string | string[], resourceName: string) => {
118 const CLIUpdateCollectionExample = `arv ${resourceKind} update \\
120 --${resourceKind} '{"${resourceName}":${resource}}'`;
122 return CLIUpdateCollectionExample;
125 const curlHeader = (resourceKind: string, resource: string) =>
126 `An example curl command to update the "${resource}" attribute for the current ${resourceKind}:`;
128 const curlExample = (uuid: string, resourcePrefix: string, resource: string | string[], resourceKind: string, resourceName: string) => {
129 const curlExample = `curl -X PUT \\
130 -H "Authorization: OAuth2 $ARVADOS_API_TOKEN" \\
131 --data-urlencode ${resourceKind}@/dev/stdin \\
132 https://$ARVADOS_API_HOST/arvados/v1/${resourcePrefix}/${uuid} \\
135 "${resourceName}": ${resource}