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 {
17 pythonExample: string;
19 CLIGetExample: string;
20 CLIUpdateHeader: string;
21 CLIUpdateExample: string;
27 COLLECTION = 'collection',
28 STORAGE_CLASSES_CONFIRMED = 'storage_classes_confirmed'
32 CONTAINER_REQUEST = 'container_request',
33 OUTPUT_NAME = 'output_name'
38 DELETE_AT = 'delete_at'
41 export const openAdvancedTabDialog = (uuid: string) =>
42 (dispatch: Dispatch<any>, getState: () => RootState) => {
43 const { resources } = getState();
44 const kind = extractUuidKind(uuid);
45 const data = getResource(uuid)(resources);
47 if (kind === ResourceKind.COLLECTION) {
48 const dataCollection: AdvancedTabDialogData = {
49 pythonHeader: pythonHeader(CollectionData.COLLECTION),
50 pythonExample: pythonExample(data.uuid, GroupContentsResourcePrefix.COLLECTION),
51 CLIGetHeader: CLIGetHeader(CollectionData.COLLECTION),
52 CLIGetExample: CLIGetExample(data.uuid, GroupContentsResourcePrefix.COLLECTION),
53 CLIUpdateHeader: CLIUpdateHeader(CollectionData.COLLECTION, CollectionData.STORAGE_CLASSES_CONFIRMED),
54 CLIUpdateExample: CLIUpdateExample(data.uuid, CollectionData.COLLECTION, data.storageClassesConfirmed, CollectionData.STORAGE_CLASSES_CONFIRMED),
55 curlHeader: curlHeader(CollectionData.COLLECTION, CollectionData.STORAGE_CLASSES_CONFIRMED),
56 curlExample: curlExample(data.uuid, GroupContentsResourcePrefix.COLLECTION, data.storageClassesConfirmed, CollectionData.COLLECTION, CollectionData.STORAGE_CLASSES_CONFIRMED)
58 dispatch(dialogActions.OPEN_DIALOG({ id: ADVANCED_TAB_DIALOG, data: dataCollection }));
59 } else if (kind === ResourceKind.PROCESS) {
60 const dataProcess: AdvancedTabDialogData = {
61 pythonHeader: pythonHeader(ProcessData.CONTAINER_REQUEST),
62 pythonExample: pythonExample(data.uuid, GroupContentsResourcePrefix.PROCESS),
63 CLIGetHeader: CLIGetHeader(ProcessData.CONTAINER_REQUEST),
64 CLIGetExample: CLIGetExample(data.uuid, GroupContentsResourcePrefix.PROCESS),
65 CLIUpdateHeader: CLIUpdateHeader(ProcessData.CONTAINER_REQUEST, ProcessData.OUTPUT_NAME),
66 CLIUpdateExample: CLIUpdateExample(data.uuid, ProcessData.CONTAINER_REQUEST, data.outputName, ProcessData.OUTPUT_NAME),
67 curlHeader: curlHeader(ProcessData.CONTAINER_REQUEST, ProcessData.OUTPUT_NAME),
68 curlExample: curlExample(data.uuid, GroupContentsResourcePrefix.PROCESS, data.outputName, ProcessData.CONTAINER_REQUEST, ProcessData.OUTPUT_NAME)
70 dispatch(dialogActions.OPEN_DIALOG({ id: ADVANCED_TAB_DIALOG, data: dataProcess }));
71 } else if (kind === ResourceKind.PROJECT) {
72 const dataProject: AdvancedTabDialogData = {
73 pythonHeader: pythonHeader(ProjectData.GROUP),
74 pythonExample: pythonExample(data.uuid, GroupContentsResourcePrefix.PROJECT),
75 CLIGetHeader: CLIGetHeader(ProjectData.GROUP),
76 CLIGetExample: CLIGetExample(data.uuid, GroupContentsResourcePrefix.PROJECT),
77 CLIUpdateHeader: CLIUpdateHeader(ProjectData.GROUP, ProjectData.DELETE_AT),
78 CLIUpdateExample: CLIUpdateExample(data.uuid, ProjectData.GROUP, data.deleteAt, ProjectData.DELETE_AT),
79 curlHeader: curlHeader(ProjectData.GROUP, ProjectData.DELETE_AT),
80 curlExample: curlExample(data.uuid, GroupContentsResourcePrefix.PROJECT, data.deleteAt, ProjectData.GROUP, ProjectData.DELETE_AT)
82 dispatch(dialogActions.OPEN_DIALOG({ id: ADVANCED_TAB_DIALOG, data: dataProject }));
85 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Could not open advanced tab for this resource.", hideDuration: 2000, kind: SnackbarKind.ERROR }));
89 const pythonHeader = (resourceKind: string) =>
90 `An example python command to get a ${resourceKind} using its uuid:`;
92 const pythonExample = (uuid: string, resourcePrefix: string) => {
93 const pythonExample = `import arvados
95 x = arvados.api().${resourcePrefix}().get(uuid='${uuid}').execute()`;
100 const CLIGetHeader = (resourceKind: string) =>
101 `An example arv command to get a ${resourceKind} using its uuid:`;
103 const CLIGetExample = (uuid: string, resourcePrefix: string) => {
104 const cliGetExample = `arv ${resourcePrefix} get \\
107 return cliGetExample;
110 const CLIUpdateHeader = (resourceKind: string, resourceName: string) =>
111 `An example arv command to update the "${resourceName}" attribute for the current ${resourceKind}:`;
113 const CLIUpdateExample = (uuid: string, resourceKind: string, resource: string | string[], resourceName: string) => {
114 const CLIUpdateCollectionExample = `arv ${resourceKind} update \\
116 --${resourceKind} '{"${resourceName}":${resource}}'`;
118 return CLIUpdateCollectionExample;
121 const curlHeader = (resourceKind: string, resource: string) =>
122 `An example curl command to update the "${resource}" attribute for the current ${resourceKind}:`;
124 const curlExample = (uuid: string, resourcePrefix: string, resource: string | string[], resourceKind: string, resourceName: string) => {
125 const curlExample = `curl -X PUT \\
126 -H "Authorization: OAuth2 $ARVADOS_API_TOKEN" \\
127 --data-urlencode ${resourceKind}@/dev/stdin \\
128 https://$ARVADOS_API_HOST/arvados/v1/${resourcePrefix}/${uuid} \\
131 "${resourceName}": ${resource}