1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from "redux";
6 import { RootState } from '~/store/store';
7 import { ServiceRepository } from "~/services/services";
8 import { navigateToUserVirtualMachines, navigateToAdminVirtualMachines, navigateToRootProject } from "~/store/navigation/navigation-action";
9 import { bindDataExplorerActions } from '~/store/data-explorer/data-explorer-action';
10 import { formatDate } from "~/common/formatters";
11 import { unionize, ofType, UnionOf } from "~/common/unionize";
12 import { VirtualMachineLogins } from '~/models/virtual-machines';
13 import { FilterBuilder } from "~/services/api/filter-builder";
14 import { ListResults } from "~/services/common-service/common-service";
15 import { dialogActions } from '~/store/dialog/dialog-actions';
16 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
18 export const virtualMachinesActions = unionize({
19 SET_REQUESTED_DATE: ofType<string>(),
20 SET_VIRTUAL_MACHINES: ofType<ListResults<any>>(),
21 SET_LOGINS: ofType<VirtualMachineLogins>(),
22 SET_LINKS: ofType<ListResults<any>>()
25 export type VirtualMachineActions = UnionOf<typeof virtualMachinesActions>;
27 export const VIRTUAL_MACHINES_PANEL = 'virtualMachinesPanel';
28 export const VIRTUAL_MACHINE_ATTRIBUTES_DIALOG = 'virtualMachineAttributesDialog';
29 export const VIRTUAL_MACHINE_REMOVE_DIALOG = 'virtualMachineRemoveDialog';
31 export const openUserVirtualMachines = () =>
32 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
33 dispatch<any>(navigateToUserVirtualMachines);
36 export const openAdminVirtualMachines = () =>
37 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
38 const user = getState().auth.user;
39 if (user && user.isAdmin) {
40 dispatch<any>(navigateToAdminVirtualMachines);
42 dispatch<any>(navigateToRootProject);
43 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "You don't have permissions to view this page", hideDuration: 2000, kind: SnackbarKind.ERROR }));
47 export const openVirtualMachineAttributes = (uuid: string) =>
48 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
49 const virtualMachineData = getState().virtualMachines.virtualMachines.items.find(it => it.uuid === uuid);
50 dispatch(dialogActions.OPEN_DIALOG({ id: VIRTUAL_MACHINE_ATTRIBUTES_DIALOG, data: { virtualMachineData } }));
53 const loadRequestedDate = () =>
54 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
55 const date = services.virtualMachineService.getRequestedDate();
56 dispatch(virtualMachinesActions.SET_REQUESTED_DATE(date));
59 export const loadVirtualMachinesAdminData = () =>
60 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
61 dispatch<any>(loadRequestedDate());
62 const virtualMachines = await services.virtualMachineService.list();
63 dispatch(virtualMachinesActions.SET_VIRTUAL_MACHINES(virtualMachines));
64 const getAllLogins = await services.virtualMachineService.getAllLogins();
65 dispatch(virtualMachinesActions.SET_LOGINS(getAllLogins));
68 export const loadVirtualMachinesUserData = () =>
69 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
70 dispatch<any>(loadRequestedDate());
71 const virtualMachines = await services.virtualMachineService.list();
72 const virtualMachinesUuids = virtualMachines.items.map(it => it.uuid);
73 const links = await services.linkService.list({
74 filters: new FilterBuilder()
75 .addIn("headUuid", virtualMachinesUuids)
78 dispatch(virtualMachinesActions.SET_VIRTUAL_MACHINES(virtualMachines));
79 dispatch(virtualMachinesActions.SET_LINKS(links));
82 export const saveRequestedDate = () =>
83 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
84 const date = formatDate((new Date).toISOString());
85 services.virtualMachineService.saveRequestedDate(date);
86 dispatch<any>(loadRequestedDate());
89 export const openRemoveVirtualMachineDialog = (uuid: string) =>
90 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
91 dispatch(dialogActions.OPEN_DIALOG({
92 id: VIRTUAL_MACHINE_REMOVE_DIALOG,
94 title: 'Remove virtual machine',
95 text: 'Are you sure you want to remove this virtual machine?',
96 confirmButtonLabel: 'Remove',
102 export const removeVirtualMachine = (uuid: string) =>
103 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
104 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
105 await services.virtualMachineService.delete(uuid);
106 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
107 dispatch<any>(loadVirtualMachinesAdminData());
110 const virtualMachinesBindedActions = bindDataExplorerActions(VIRTUAL_MACHINES_PANEL);
112 export const loadVirtualMachinesPanel = () =>
113 (dispatch: Dispatch) => {
114 dispatch(virtualMachinesBindedActions.REQUEST_ITEMS());