]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
8b40111b8c8cb655586b4ea90c863ada4af02ea0
[ceph-ci.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) => this.disableForMirrorSnapshot(selection)
48     };
49     this.unprotect = {
50       permission: 'update',
51       icon: Icons.unlock,
52       visible: (selection: CdTableSelection) =>
53         selection.hasSingleSelection && selection.first().is_protected,
54       name: actionLabels.UNPROTECT,
55       disable: (selection: CdTableSelection) => this.disableForMirrorSnapshot(selection)
56     };
57     this.clone = {
58       permission: 'create',
59       canBePrimary: (selection: CdTableSelection) => selection.hasSingleSelection,
60       disable: (selection: CdTableSelection) =>
61         this.getCloneDisableDesc(selection, this.featuresName) ||
62         this.disableForMirrorSnapshot(selection),
63       icon: Icons.clone,
64       name: actionLabels.CLONE
65     };
66     this.copy = {
67       permission: 'create',
68       canBePrimary: (selection: CdTableSelection) => selection.hasSingleSelection,
69       disable: (selection: CdTableSelection) =>
70         !selection.hasSingleSelection ||
71         selection.first().cdExecuting ||
72         this.disableForMirrorSnapshot(selection),
73       icon: Icons.copy,
74       name: actionLabels.COPY
75     };
76     this.rollback = {
77       permission: 'update',
78       icon: Icons.undo,
79       name: actionLabels.ROLLBACK,
80       disable: (selection: CdTableSelection) =>
81         this.disableForMirrorSnapshot(selection) || !selection.hasSingleSelection
82     };
83     this.deleteSnap = {
84       permission: 'delete',
85       icon: Icons.destroy,
86       disable: (selection: CdTableSelection) => {
87         const first = selection.first();
88         return (
89           !selection.hasSingleSelection ||
90           first.cdExecuting ||
91           first.is_protected ||
92           this.disableForMirrorSnapshot(selection)
93         );
94       },
95       name: actionLabels.DELETE
96     };
97
98     this.ordering = [
99       this.create,
100       this.rename,
101       this.protect,
102       this.unprotect,
103       this.clone,
104       this.copy,
105       this.rollback,
106       this.deleteSnap
107     ];
108   }
109
110   getCloneDisableDesc(selection: CdTableSelection, featuresName: string[]): boolean | string {
111     if (selection.hasSingleSelection && !selection.first().cdExecuting) {
112       if (!featuresName?.includes('layering')) {
113         return $localize`Parent image must support Layering`;
114       }
115
116       if (this.cloneFormatVersion === 1 && !selection.first().is_protected) {
117         return $localize`Snapshot must be protected in order to clone.`;
118       }
119
120       return false;
121     }
122
123     return true;
124   }
125
126   disableForMirrorSnapshot(selection: CdTableSelection) {
127     return (
128       selection.hasSingleSelection &&
129       selection.first().mirror_mode === 'snapshot' &&
130       selection.first().name.includes('.mirror.')
131     );
132   }
133 }