Merge branch 'master' into 14452-my-account
[arvados-workbench2.git] / src / store / compute-nodes / compute-nodes-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 { unionize, ofType, UnionOf } from "~/common/unionize";
7 import { RootState } from '~/store/store';
8 import { setBreadcrumbs } from '~/store/breadcrumbs/breadcrumbs-actions';
9 import { ServiceRepository } from "~/services/services";
10 import { NodeResource } from '~/models/node';
11 import { dialogActions } from '~/store/dialog/dialog-actions';
12 import { snackbarActions } from '~/store/snackbar/snackbar-actions';
13 import { navigateToRootProject } from '~/store/navigation/navigation-action';
14
15 export const computeNodesActions = unionize({
16     SET_COMPUTE_NODES: ofType<NodeResource[]>(),
17     REMOVE_COMPUTE_NODE: ofType<string>()
18 });
19
20 export type ComputeNodesActions = UnionOf<typeof computeNodesActions>;
21
22 export const COMPUTE_NODE_REMOVE_DIALOG = 'computeNodeRemoveDialog';
23 export const COMPUTE_NODE_ATTRIBUTES_DIALOG = 'computeNodeAttributesDialog';
24
25 export const loadComputeNodesPanel = () =>
26     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
27         const user = getState().auth.user;
28         if (user && user.isAdmin) {
29             try {
30                 dispatch(setBreadcrumbs([{ label: 'Compute Nodes' }]));
31                 const response = await services.nodeService.list();
32                 dispatch(computeNodesActions.SET_COMPUTE_NODES(response.items));
33             } catch (e) {
34                 return;
35             }
36         } else {
37             dispatch(navigateToRootProject);
38             dispatch(snackbarActions.OPEN_SNACKBAR({ message: "You don't have permissions to view this page", hideDuration: 2000 }));
39         }
40     };
41
42 export const openComputeNodeAttributesDialog = (uuid: string) =>
43     (dispatch: Dispatch, getState: () => RootState) => {
44         const computeNode = getState().computeNodes.find(node => node.uuid === uuid);
45         dispatch(dialogActions.OPEN_DIALOG({ id: COMPUTE_NODE_ATTRIBUTES_DIALOG, data: { computeNode } }));
46     };
47
48 export const openComputeNodeRemoveDialog = (uuid: string) =>
49     (dispatch: Dispatch, getState: () => RootState) => {
50         dispatch(dialogActions.OPEN_DIALOG({
51             id: COMPUTE_NODE_REMOVE_DIALOG,
52             data: {
53                 title: 'Remove compute node',
54                 text: 'Are you sure you want to remove this compute node?',
55                 confirmButtonLabel: 'Remove',
56                 uuid
57             }
58         }));
59     };
60
61 export const removeComputeNode = (uuid: string) =>
62     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
63         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
64         try {
65             await services.nodeService.delete(uuid);
66             dispatch(computeNodesActions.REMOVE_COMPUTE_NODE(uuid));
67             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Compute node has been successfully removed.', hideDuration: 2000 }));
68         } catch (e) {
69             return;
70         }
71     };