]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
345d47e7421d1508be01c2a226252afbf3d21cec
[ceph.git] /
1 import { RbdService } from '~/app/shared/api/rbd.service';
2 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
3 import { Icons } from '~/app/shared/enum/icons.enum';
4 import { CdTableAction } from '~/app/shared/models/cd-table-action';
5 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
6
7 export class RbdSnapshotActionsModel {
8   create: CdTableAction;
9   rename: CdTableAction;
10   protect: CdTableAction;
11   unprotect: CdTableAction;
12   clone: CdTableAction;
13   copy: CdTableAction;
14   rollback: CdTableAction;
15   deleteSnap: CdTableAction;
16   ordering: CdTableAction[];
17
18   cloneFormatVersion = 1;
19
20   constructor(
21     actionLabels: ActionLabelsI18n,
22     public featuresName: string[],
23     rbdService: RbdService
24   ) {
25     rbdService.cloneFormatVersion().subscribe((version: number) => {
26       this.cloneFormatVersion = version;
27     });
28
29     this.create = {
30       permission: 'create',
31       icon: Icons.add,
32       name: actionLabels.CREATE
33     };
34     this.rename = {
35       permission: 'update',
36       icon: Icons.edit,
37       name: actionLabels.RENAME,
38       disable: (selection: CdTableSelection) =>
39         this.disableForMirrorSnapshot(selection) || !selection.hasSingleSelection
40     };
41     this.protect = {
42       permission: 'update',
43       icon: Icons.lock,
44       visible: (selection: CdTableSelection) =>
45         selection.hasSingleSelection && !selection.first().is_protected,
46       name: actionLabels.PROTECT,
47       disable: (selection: CdTableSelection) =>
48         this.disableForMirrorSnapshot(selection) ||
49         this.getDisableDesc(selection, this.featuresName)
50     };
51     this.unprotect = {
52       permission: 'update',
53       icon: Icons.unlock,
54       visible: (selection: CdTableSelection) =>
55         selection.hasSingleSelection && selection.first().is_protected,
56       name: actionLabels.UNPROTECT,
57       disable: (selection: CdTableSelection) => this.disableForMirrorSnapshot(selection)
58     };
59     this.clone = {
60       permission: 'create',
61       canBePrimary: (selection: CdTableSelection) => selection.hasSingleSelection,
62       disable: (selection: CdTableSelection) =>
63         this.getDisableDesc(selection, this.featuresName) ||
64         this.disableForMirrorSnapshot(selection),
65       icon: Icons.clone,
66       name: actionLabels.CLONE
67     };
68     this.copy = {
69       permission: 'create',
70       canBePrimary: (selection: CdTableSelection) => selection.hasSingleSelection,
71       disable: (selection: CdTableSelection) =>
72         !selection.hasSingleSelection ||
73         selection.first().cdExecuting ||
74         this.disableForMirrorSnapshot(selection),
75       icon: Icons.copy,
76       name: actionLabels.COPY
77     };
78     this.rollback = {
79       permission: 'update',
80       icon: Icons.undo,
81       name: actionLabels.ROLLBACK,
82       disable: (selection: CdTableSelection) =>
83         this.disableForMirrorSnapshot(selection) || !selection.hasSingleSelection
84     };
85     this.deleteSnap = {
86       permission: 'delete',
87       icon: Icons.destroy,
88       disable: (selection: CdTableSelection) => {
89         const first = selection.first();
90         return (
91           !selection.hasSingleSelection ||
92           first.cdExecuting ||
93           first.is_protected ||
94           this.disableForMirrorSnapshot(selection)
95         );
96       },
97       name: actionLabels.DELETE
98     };
99
100     this.ordering = [
101       this.create,
102       this.rename,
103       this.protect,
104       this.unprotect,
105       this.clone,
106       this.copy,
107       this.rollback,
108       this.deleteSnap
109     ];
110   }
111
112   getDisableDesc(selection: CdTableSelection, featuresName: string[]): boolean | string {
113     if (selection.hasSingleSelection && !selection.first().cdExecuting) {
114       if (!featuresName?.includes('layering')) {
115         return $localize`The layering feature needs to be enabled on parent image`;
116       }
117
118       if (this.cloneFormatVersion === 1 && !selection.first().is_protected) {
119         return $localize`Snapshot must be protected in order to clone.`;
120       }
121
122       return false;
123     }
124
125     return true;
126   }
127
128   disableForMirrorSnapshot(selection: CdTableSelection) {
129     return (
130       selection.hasSingleSelection &&
131       selection.first().mirror_mode === 'snapshot' &&
132       selection.first().name.includes('.mirror.')
133     );
134   }
135 }