Merge branch '14502_admin_compute_nodes'
[arvados-workbench2.git] / src / store / workbench / workbench-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from 'redux';
6 import { RootState } from "~/store/store";
7 import { loadDetailsPanel } from '~/store/details-panel/details-panel-action';
8 import { snackbarActions } from '~/store/snackbar/snackbar-actions';
9 import { loadFavoritePanel } from '~/store/favorite-panel/favorite-panel-action';
10 import { openProjectPanel, projectPanelActions, setIsProjectPanelTrashed } from '~/store/project-panel/project-panel-action';
11 import { activateSidePanelTreeItem, initSidePanelTree, SidePanelTreeCategory, loadSidePanelTreeProjects } from '~/store/side-panel-tree/side-panel-tree-actions';
12 import { loadResource, updateResources } from '~/store/resources/resources-actions';
13 import { favoritePanelActions } from '~/store/favorite-panel/favorite-panel-action';
14 import { projectPanelColumns } from '~/views/project-panel/project-panel';
15 import { favoritePanelColumns } from '~/views/favorite-panel/favorite-panel';
16 import { matchRootRoute } from '~/routes/routes';
17 import { setSidePanelBreadcrumbs, setProcessBreadcrumbs, setSharedWithMeBreadcrumbs, setTrashBreadcrumbs, setBreadcrumbs } from '~/store/breadcrumbs/breadcrumbs-actions';
18 import { navigateToProject } from '~/store/navigation/navigation-action';
19 import { MoveToFormDialogData } from '~/store/move-to-dialog/move-to-dialog';
20 import { ServiceRepository } from '~/services/services';
21 import { getResource } from '~/store/resources/resources';
22 import { getProjectPanelCurrentUuid } from '~/store/project-panel/project-panel-action';
23 import * as projectCreateActions from '~/store/projects/project-create-actions';
24 import * as projectMoveActions from '~/store/projects/project-move-actions';
25 import * as projectUpdateActions from '~/store/projects/project-update-actions';
26 import * as collectionCreateActions from '~/store/collections/collection-create-actions';
27 import * as collectionCopyActions from '~/store/collections/collection-copy-actions';
28 import * as collectionUpdateActions from '~/store/collections/collection-update-actions';
29 import * as collectionMoveActions from '~/store/collections/collection-move-actions';
30 import * as processesActions from '~/store/processes/processes-actions';
31 import * as processMoveActions from '~/store/processes/process-move-actions';
32 import * as processUpdateActions from '~/store/processes/process-update-actions';
33 import * as processCopyActions from '~/store/processes/process-copy-actions';
34 import { trashPanelColumns } from "~/views/trash-panel/trash-panel";
35 import { loadTrashPanel, trashPanelActions } from "~/store/trash-panel/trash-panel-action";
36 import { initProcessLogsPanel } from '~/store/process-logs-panel/process-logs-panel-actions';
37 import { loadProcessPanel } from '~/store/process-panel/process-panel-actions';
38 import { sharedWithMePanelActions } from '~/store/shared-with-me-panel/shared-with-me-panel-actions';
39 import { loadSharedWithMePanel } from '~/store/shared-with-me-panel/shared-with-me-panel-actions';
40 import { CopyFormDialogData } from '~/store/copy-dialog/copy-dialog';
41 import { loadWorkflowPanel, workflowPanelActions } from '~/store/workflow-panel/workflow-panel-actions';
42 import { loadSshKeysPanel } from '~/store/auth/auth-action';
43 import { workflowPanelColumns } from '~/views/workflow-panel/workflow-panel-view';
44 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions';
45 import { getProgressIndicator } from '~/store/progress-indicator/progress-indicator-reducer';
46 import { ResourceKind, extractUuidKind } from '~/models/resource';
47 import { FilterBuilder } from '~/services/api/filter-builder';
48 import { GroupContentsResource } from '~/services/groups-service/groups-service';
49 import { unionize, ofType, UnionOf, MatchCases } from '~/common/unionize';
50 import { loadRunProcessPanel } from '~/store/run-process-panel/run-process-panel-actions';
51 import { loadCollectionFiles } from '~/store/collection-panel/collection-panel-files/collection-panel-files-actions';
52 import { SnackbarKind } from '~/store/snackbar/snackbar-actions';
53 import { collectionPanelActions } from "~/store/collection-panel/collection-panel-action";
54 import { CollectionResource } from "~/models/collection";
55 import { searchResultsPanelActions, loadSearchResultsPanel } from '~/store/search-results-panel/search-results-panel-actions';
56 import { searchResultsPanelColumns } from '~/views/search-results-panel/search-results-panel-view';
57 import { loadVirtualMachinesPanel } from '~/store/virtual-machines/virtual-machines-actions';
58 import { loadRepositoriesPanel } from '~/store/repositories/repositories-actions';
59 import { loadKeepServicesPanel } from '~/store/keep-services/keep-services-actions';
60 import { loadComputeNodesPanel } from '~/store/compute-nodes/compute-nodes-actions';
61
62 export const WORKBENCH_LOADING_SCREEN = 'workbenchLoadingScreen';
63
64 export const isWorkbenchLoading = (state: RootState) => {
65     const progress = getProgressIndicator(WORKBENCH_LOADING_SCREEN)(state.progressIndicator);
66     return progress ? progress.working : false;
67 };
68
69 const handleFirstTimeLoad = (action: any) =>
70     async (dispatch: Dispatch<any>, getState: () => RootState) => {
71         try {
72             await dispatch(action);
73         } finally {
74             if (isWorkbenchLoading(getState())) {
75                 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
76             }
77         }
78     };
79
80
81 export const loadWorkbench = () =>
82     async (dispatch: Dispatch, getState: () => RootState) => {
83         dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
84         const { auth, router } = getState();
85         const { user } = auth;
86         if (user) {
87             const userResource = await dispatch<any>(loadResource(user.uuid));
88             if (userResource) {
89                 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
90                 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
91                 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
92                 dispatch(sharedWithMePanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
93                 dispatch(workflowPanelActions.SET_COLUMNS({ columns: workflowPanelColumns }));
94                 dispatch(searchResultsPanelActions.SET_COLUMNS({ columns: searchResultsPanelColumns }));
95                 dispatch<any>(initSidePanelTree());
96                 if (router.location) {
97                     const match = matchRootRoute(router.location.pathname);
98                     if (match) {
99                         dispatch(navigateToProject(userResource.uuid));
100                     }
101                 }
102             } else {
103                 dispatch(userIsNotAuthenticated);
104             }
105         } else {
106             dispatch(userIsNotAuthenticated);
107         }
108     };
109
110 export const loadFavorites = () =>
111     handleFirstTimeLoad(
112         (dispatch: Dispatch) => {
113             dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
114             dispatch<any>(loadFavoritePanel());
115             dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
116         });
117
118 export const loadTrash = () =>
119     handleFirstTimeLoad(
120         (dispatch: Dispatch) => {
121             dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
122             dispatch<any>(loadTrashPanel());
123             dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
124         });
125
126 export const loadProject = (uuid: string) =>
127     handleFirstTimeLoad(
128         async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
129             const userUuid = services.authService.getUuid();
130             dispatch(setIsProjectPanelTrashed(false));
131             if (userUuid) {
132                 if (userUuid !== uuid) {
133                     const match = await loadGroupContentsResource({ uuid, userUuid, services });
134                     match({
135                         OWNED: async project => {
136                             await dispatch(activateSidePanelTreeItem(uuid));
137                             dispatch<any>(setSidePanelBreadcrumbs(uuid));
138                             dispatch(finishLoadingProject(project));
139                         },
140                         SHARED: project => {
141                             dispatch<any>(setSharedWithMeBreadcrumbs(uuid));
142                             dispatch(activateSidePanelTreeItem(uuid));
143                             dispatch(finishLoadingProject(project));
144                         },
145                         TRASHED: project => {
146                             dispatch<any>(setTrashBreadcrumbs(uuid));
147                             dispatch(setIsProjectPanelTrashed(true));
148                             dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
149                             dispatch(finishLoadingProject(project));
150                         }
151                     });
152                 } else {
153                     await dispatch(activateSidePanelTreeItem(userUuid));
154                     dispatch<any>(setSidePanelBreadcrumbs(userUuid));
155                     dispatch(finishLoadingProject(userUuid));
156                 }
157             }
158         });
159
160 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
161     async (dispatch: Dispatch) => {
162         const newProject = await dispatch<any>(projectCreateActions.createProject(data));
163         if (newProject) {
164             dispatch(snackbarActions.OPEN_SNACKBAR({
165                 message: "Project has been successfully created.",
166                 hideDuration: 2000
167             }));
168             await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
169             dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
170         }
171     };
172
173 export const moveProject = (data: MoveToFormDialogData) =>
174     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
175         try {
176             const oldProject = getResource(data.uuid)(getState().resources);
177             const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
178             const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
179             if (movedProject) {
180                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
181                 if (oldProject) {
182                     await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
183                 }
184                 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
185             }
186         } catch (e) {
187             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
188         }
189     };
190
191 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
192     async (dispatch: Dispatch) => {
193         const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
194         if (updatedProject) {
195             dispatch(snackbarActions.OPEN_SNACKBAR({
196                 message: "Project has been successfully updated.",
197                 hideDuration: 2000
198             }));
199             await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
200             dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
201         }
202     };
203
204 export const loadCollection = (uuid: string) =>
205     handleFirstTimeLoad(
206         async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
207             const userUuid = services.authService.getUuid();
208             if (userUuid) {
209                 const match = await loadGroupContentsResource({ uuid, userUuid, services });
210                 match({
211                     OWNED: async collection => {
212                         dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
213                         dispatch(updateResources([collection]));
214                         await dispatch(activateSidePanelTreeItem(collection.ownerUuid));
215                         dispatch(setSidePanelBreadcrumbs(collection.ownerUuid));
216                         dispatch(loadCollectionFiles(collection.uuid));
217                     },
218                     SHARED: collection => {
219                         dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
220                         dispatch(updateResources([collection]));
221                         dispatch<any>(setSharedWithMeBreadcrumbs(collection.ownerUuid));
222                         dispatch(activateSidePanelTreeItem(collection.ownerUuid));
223                         dispatch(loadCollectionFiles(collection.uuid));
224                     },
225                     TRASHED: collection => {
226                         dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
227                         dispatch(updateResources([collection]));
228                         dispatch(setTrashBreadcrumbs(''));
229                         dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
230                         dispatch(loadCollectionFiles(collection.uuid));
231                     },
232
233                 });
234             }
235         });
236
237 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
238     async (dispatch: Dispatch) => {
239         const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
240         if (collection) {
241             dispatch(snackbarActions.OPEN_SNACKBAR({
242                 message: "Collection has been successfully created.",
243                 hideDuration: 2000
244             }));
245             dispatch<any>(updateResources([collection]));
246             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
247         }
248     };
249
250 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
251     async (dispatch: Dispatch) => {
252         const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
253         if (collection) {
254             dispatch(snackbarActions.OPEN_SNACKBAR({
255                 message: "Collection has been successfully updated.",
256                 hideDuration: 2000
257             }));
258             dispatch<any>(updateResources([collection]));
259             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
260         }
261     };
262
263 export const copyCollection = (data: CopyFormDialogData) =>
264     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
265         try {
266             const copyToProject = getResource(data.ownerUuid)(getState().resources);
267             const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
268             if (copyToProject && collection) {
269                 dispatch<any>(reloadProjectMatchingUuid([copyToProject.uuid]));
270                 dispatch(snackbarActions.OPEN_SNACKBAR({
271                     message: 'Collection has been copied.',
272                     hideDuration: 3000,
273                     kind: SnackbarKind.SUCCESS,
274                     link: collection.ownerUuid
275                 }));
276             }
277         } catch (e) {
278             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000, kind: SnackbarKind.ERROR }));
279         }
280     };
281
282 export const moveCollection = (data: MoveToFormDialogData) =>
283     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
284         try {
285             const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
286             dispatch<any>(updateResources([collection]));
287             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
288             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
289         } catch (e) {
290             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
291         }
292     };
293
294 export const loadProcess = (uuid: string) =>
295     handleFirstTimeLoad(
296         async (dispatch: Dispatch, getState: () => RootState) => {
297             dispatch<any>(loadProcessPanel(uuid));
298             const process = await dispatch<any>(processesActions.loadProcess(uuid));
299             await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
300             dispatch<any>(setProcessBreadcrumbs(uuid));
301             dispatch(loadDetailsPanel(uuid));
302         });
303
304 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) =>
305     async (dispatch: Dispatch) => {
306         try {
307             const process = await dispatch<any>(processUpdateActions.updateProcess(data));
308             if (process) {
309                 dispatch(snackbarActions.OPEN_SNACKBAR({
310                     message: "Process has been successfully updated.",
311                     hideDuration: 2000
312                 }));
313                 dispatch<any>(updateResources([process]));
314                 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
315             }
316         } catch (e) {
317             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
318         }
319     };
320
321 export const moveProcess = (data: MoveToFormDialogData) =>
322     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
323         try {
324             const process = await dispatch<any>(processMoveActions.moveProcess(data));
325             dispatch<any>(updateResources([process]));
326             dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
327             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
328         } catch (e) {
329             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
330         }
331     };
332
333 export const copyProcess = (data: CopyFormDialogData) =>
334     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
335         try {
336             const process = await dispatch<any>(processCopyActions.copyProcess(data));
337             dispatch<any>(updateResources([process]));
338             dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
339             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
340         } catch (e) {
341             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
342         }
343     };
344
345 export const loadProcessLog = (uuid: string) =>
346     handleFirstTimeLoad(
347         async (dispatch: Dispatch) => {
348             const process = await dispatch<any>(processesActions.loadProcess(uuid));
349             dispatch<any>(setProcessBreadcrumbs(uuid));
350             dispatch<any>(initProcessLogsPanel(uuid));
351             await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
352         });
353
354 export const resourceIsNotLoaded = (uuid: string) =>
355     snackbarActions.OPEN_SNACKBAR({
356         message: `Resource identified by ${uuid} is not loaded.`
357     });
358
359 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
360     message: 'User is not authenticated'
361 });
362
363 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
364     message: 'Could not load user'
365 });
366
367 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
368     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
369         const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
370         if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
371             dispatch<any>(loadProject(currentProjectPanelUuid));
372         }
373     };
374
375 export const loadSharedWithMe = handleFirstTimeLoad(async (dispatch: Dispatch) => {
376     dispatch<any>(loadSharedWithMePanel());
377     await dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
378     await dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
379 });
380
381 export const loadRunProcess = handleFirstTimeLoad(
382     async (dispatch: Dispatch) => {
383         await dispatch<any>(loadRunProcessPanel());
384     }
385 );
386
387 export const loadWorkflow = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
388     dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.WORKFLOWS));
389     await dispatch(loadWorkflowPanel());
390     dispatch(setSidePanelBreadcrumbs(SidePanelTreeCategory.WORKFLOWS));
391 });
392
393 export const loadSearchResults = handleFirstTimeLoad(
394     async (dispatch: Dispatch<any>) => {
395         await dispatch(loadSearchResultsPanel());
396     });
397
398 export const loadVirtualMachines = handleFirstTimeLoad(
399     async (dispatch: Dispatch<any>) => {
400         await dispatch(loadVirtualMachinesPanel());
401         dispatch(setBreadcrumbs([{ label: 'Virtual Machines' }]));
402     });
403     
404 export const loadRepositories = handleFirstTimeLoad(
405     async (dispatch: Dispatch<any>) => {
406         await dispatch(loadRepositoriesPanel());
407         dispatch(setBreadcrumbs([{ label: 'Repositories' }]));
408     });
409
410 export const loadSshKeys = handleFirstTimeLoad(
411     async (dispatch: Dispatch<any>) => {
412         await dispatch(loadSshKeysPanel());
413     });
414
415 export const loadKeepServices = handleFirstTimeLoad(
416     async (dispatch: Dispatch<any>) => {
417         await dispatch(loadKeepServicesPanel());
418     });
419
420 export const loadComputeNodes = handleFirstTimeLoad(
421     async (dispatch: Dispatch<any>) => {
422         await dispatch(loadComputeNodesPanel());
423     });
424
425 const finishLoadingProject = (project: GroupContentsResource | string) =>
426     async (dispatch: Dispatch<any>) => {
427         const uuid = typeof project === 'string' ? project : project.uuid;
428         dispatch(openProjectPanel(uuid));
429         dispatch(loadDetailsPanel(uuid));
430         if (typeof project !== 'string') {
431             dispatch(updateResources([project]));
432         }
433     };
434
435 const loadGroupContentsResource = async (params: {
436     uuid: string,
437     userUuid: string,
438     services: ServiceRepository
439 }) => {
440     const filters = new FilterBuilder()
441         .addEqual('uuid', params.uuid)
442         .getFilters();
443     const { items } = await params.services.groupsService.contents(params.userUuid, {
444         filters,
445         recursive: true,
446         includeTrash: true,
447     });
448     const resource = items.shift();
449     let handler: GroupContentsHandler;
450     if (resource) {
451         handler = (resource.kind === ResourceKind.COLLECTION || resource.kind === ResourceKind.PROJECT) && resource.isTrashed
452             ? groupContentsHandlers.TRASHED(resource)
453             : groupContentsHandlers.OWNED(resource);
454     } else {
455         const kind = extractUuidKind(params.uuid);
456         let resource: GroupContentsResource;
457         if (kind === ResourceKind.COLLECTION) {
458             resource = await params.services.collectionService.get(params.uuid);
459         } else if (kind === ResourceKind.PROJECT) {
460             resource = await params.services.projectService.get(params.uuid);
461         } else {
462             resource = await params.services.containerRequestService.get(params.uuid);
463         }
464         handler = groupContentsHandlers.SHARED(resource);
465     }
466     return (cases: MatchCases<typeof groupContentsHandlersRecord, GroupContentsHandler, void>) =>
467         groupContentsHandlers.match(handler, cases);
468
469 };
470
471 const groupContentsHandlersRecord = {
472     TRASHED: ofType<GroupContentsResource>(),
473     SHARED: ofType<GroupContentsResource>(),
474     OWNED: ofType<GroupContentsResource>(),
475 };
476
477 const groupContentsHandlers = unionize(groupContentsHandlersRecord);
478
479 type GroupContentsHandler = UnionOf<typeof groupContentsHandlers>;