Merge branch 'master' into 13969-advanced-tab
[arvados-workbench2.git] / src / store / advanced-tab / advanced-tab.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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';
12
13 export const ADVANCED_TAB_DIALOG = 'advancedTabDialog';
14
15 export interface AdvancedTabDialogData {
16     pythonHeader: string;
17     pythonExample: string;
18     CLIGetHeader: string;
19     CLIGetExample: string;
20     CLIUpdateHeader: string;
21     CLIUpdateExample: string;
22     curlHeader: string;
23     curlExample: string;
24 }
25
26 enum CollectionData {
27     COLLECTION = 'collection',
28     STORAGE_CLASSES_CONFIRMED = 'storage_classes_confirmed'
29 }
30
31 enum ProcessData {
32     CONTAINER_REQUEST = 'container_request',
33     OUTPUT_NAME = 'output_name'
34 }
35
36 enum ProjectData {
37     GROUP = 'group',
38     DELETE_AT = 'delete_at'
39 }
40
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);
46         if (data) {
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)
57                 };
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)
69                 };
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)
81                 };
82                 dispatch(dialogActions.OPEN_DIALOG({ id: ADVANCED_TAB_DIALOG, data: dataProject }));
83             }
84         } else {
85             dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Could not open advanced tab for this resource.", hideDuration: 2000, kind: SnackbarKind.ERROR }));
86         }
87     };
88
89 const pythonHeader = (resourceKind: string) =>
90     `An example python command to get a ${resourceKind} using its uuid:`;
91
92 const pythonExample = (uuid: string, resourcePrefix: string) => {
93     const pythonExample = `import arvados
94
95  x = arvados.api().${resourcePrefix}().get(uuid='${uuid}').execute()`;
96
97     return pythonExample;
98 };
99
100 const CLIGetHeader = (resourceKind: string) =>
101     `An example arv command to get a ${resourceKind} using its uuid:`;
102
103 const CLIGetExample = (uuid: string, resourcePrefix: string) => {
104     const cliGetExample = `arv ${resourcePrefix} get \\
105  --uuid ${uuid}`;
106
107     return cliGetExample;
108 };
109
110 const CLIUpdateHeader = (resourceKind: string, resourceName: string) =>
111     `An example arv command to update the "${resourceName}" attribute for the current ${resourceKind}:`;
112
113 const CLIUpdateExample = (uuid: string, resourceKind: string, resource: string | string[], resourceName: string) => {
114     const CLIUpdateCollectionExample = `arv ${resourceKind} update \\ 
115  --uuid ${uuid} \\
116  --${resourceKind} '{"${resourceName}":${resource}}'`;
117
118     return CLIUpdateCollectionExample;
119 };
120
121 const curlHeader = (resourceKind: string, resource: string) =>
122     `An example curl command to update the "${resource}" attribute for the current ${resourceKind}:`;
123
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} \\
129  <<EOF
130 {
131   "${resourceName}": ${resource}
132 }
133 EOF`;
134
135     return curlExample;
136 };