advanced-tab-curl-cli-python-examples
[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     kind: string;
17     pythonHeader: string;
18     pythonExample: string;
19     CLIGetHeader: string;
20     CLIGetExample: string;
21     CLIUpdateHeader: string;
22     CLIUpdateExample: string;
23     curlHeader: string;
24     curlExample: string;
25 }
26
27 enum CollectionData {
28     COLLECTION = 'collection',
29     STORAGE_CLASSES_CONFIRMED = 'storage_classes_confirmed'
30 }
31
32 enum ProcessData {
33     CONTAINER_REQUEST = 'container_request',
34     OUTPUT_NAME = 'output_name'
35 }
36
37 enum ProjectData {
38     GROUP = 'group',
39     DELETE_AT = 'delete_at'
40 }
41
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);
47         if (data) {
48             if (kind === ResourceKind.COLLECTION) {
49                 const dataCollection: AdvancedTabDialogData = {
50                     kind,
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)
59                 };
60                 dispatch(dialogActions.OPEN_DIALOG({ id: ADVANCED_TAB_DIALOG, data: dataCollection }));
61             } else if (kind === ResourceKind.PROCESS) {
62                 const dataProcess: AdvancedTabDialogData = {
63                     kind,
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)
72                 };
73                 dispatch(dialogActions.OPEN_DIALOG({ id: ADVANCED_TAB_DIALOG, data: dataProcess }));
74             } else if (kind === ResourceKind.PROJECT) {
75                 const dataProject: AdvancedTabDialogData = {
76                     kind,
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)
85                 };
86                 dispatch(dialogActions.OPEN_DIALOG({ id: ADVANCED_TAB_DIALOG, data: dataProject }));
87             }
88         } else {
89             dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Could not open advanced tab for this resource.", hideDuration: 2000, kind: SnackbarKind.ERROR }));
90         }
91     };
92
93 const pythonHeader = (resourceKind: string) =>
94     `An example python command to get a ${resourceKind} using its uuid:`;
95
96 const pythonExample = (uuid: string, resourcePrefix: string) => {
97     const pythonExample = `import arvados
98
99  x = arvados.api().${resourcePrefix}().get(uuid='${uuid}').execute()`;
100
101     return pythonExample;
102 };
103
104 const CLIGetHeader = (resourceKind: string) =>
105     `An example arv command to get a ${resourceKind} using its uuid:`;
106
107 const CLIGetExample = (uuid: string, resourcePrefix: string) => {
108     const cliGetExample = `arv ${resourcePrefix} get \\
109  --uuid ${uuid}`;
110
111     return cliGetExample;
112 };
113
114 const CLIUpdateHeader = (resourceKind: string, resourceName: string) =>
115     `An example arv command to update the "${resourceName}" attribute for the current ${resourceKind}:`;
116
117 const CLIUpdateExample = (uuid: string, resourceKind: string, resource: string | string[], resourceName: string) => {
118     const CLIUpdateCollectionExample = `arv ${resourceKind} update \\ 
119  --uuid ${uuid} \\
120  --${resourceKind} '{"${resourceName}":${resource}}'`;
121
122     return CLIUpdateCollectionExample;
123 };
124
125 const curlHeader = (resourceKind: string, resource: string) =>
126     `An example curl command to update the "${resource}" attribute for the current ${resourceKind}:`;
127
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} \\
133  <<EOF
134 {
135   "${resourceName}": ${resource}
136 }
137 EOF`;
138
139     return curlExample;
140 };