]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
e186195c5c71f2e378d5e74eeac6cc84031b22b5
[ceph.git] /
1 import { Component, Input, OnChanges, OnInit, TemplateRef, ViewChild } from '@angular/core';
2
3 import { I18n } from '@ngx-translate/i18n-polyfill';
4 import * as moment from 'moment';
5 import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
6 import { of } from 'rxjs';
7
8 import { RbdService } from '../../../shared/api/rbd.service';
9 import { ConfirmationModalComponent } from '../../../shared/components/confirmation-modal/confirmation-modal.component';
10 import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
11 import { CellTemplate } from '../../../shared/enum/cell-template.enum';
12 import { CdTableAction } from '../../../shared/models/cd-table-action';
13 import { CdTableColumn } from '../../../shared/models/cd-table-column';
14 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
15 import { ExecutingTask } from '../../../shared/models/executing-task';
16 import { FinishedTask } from '../../../shared/models/finished-task';
17 import { Permission } from '../../../shared/models/permissions';
18 import { CdDatePipe } from '../../../shared/pipes/cd-date.pipe';
19 import { DimlessBinaryPipe } from '../../../shared/pipes/dimless-binary.pipe';
20 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
21 import { NotificationService } from '../../../shared/services/notification.service';
22 import { SummaryService } from '../../../shared/services/summary.service';
23 import { TaskListService } from '../../../shared/services/task-list.service';
24 import { TaskManagerService } from '../../../shared/services/task-manager.service';
25 import { RbdSnapshotFormComponent } from '../rbd-snapshot-form/rbd-snapshot-form.component';
26 import { RbdSnapshotActionsModel } from './rbd-snapshot-actions.model';
27 import { RbdSnapshotModel } from './rbd-snapshot.model';
28
29 @Component({
30   selector: 'cd-rbd-snapshot-list',
31   templateUrl: './rbd-snapshot-list.component.html',
32   styleUrls: ['./rbd-snapshot-list.component.scss'],
33   providers: [TaskListService]
34 })
35 export class RbdSnapshotListComponent implements OnInit, OnChanges {
36   @Input()
37   snapshots: RbdSnapshotModel[] = [];
38   @Input()
39   poolName: string;
40   @Input()
41   rbdName: string;
42   @ViewChild('nameTpl')
43   nameTpl: TemplateRef<any>;
44   @ViewChild('protectTpl')
45   protectTpl: TemplateRef<any>;
46   @ViewChild('rollbackTpl')
47   rollbackTpl: TemplateRef<any>;
48
49   permission: Permission;
50   selection = new CdTableSelection();
51   tableActions: CdTableAction[];
52
53   data: RbdSnapshotModel[];
54
55   columns: CdTableColumn[];
56
57   modalRef: BsModalRef;
58
59   builders = {
60     'rbd/snap/create': (metadata) => {
61       const model = new RbdSnapshotModel();
62       model.name = metadata['snapshot_name'];
63       return model;
64     }
65   };
66
67   constructor(
68     private authStorageService: AuthStorageService,
69     private modalService: BsModalService,
70     private dimlessBinaryPipe: DimlessBinaryPipe,
71     private cdDatePipe: CdDatePipe,
72     private rbdService: RbdService,
73     private taskManagerService: TaskManagerService,
74     private notificationService: NotificationService,
75     private summaryService: SummaryService,
76     private taskListService: TaskListService,
77     private i18n: I18n
78   ) {
79     this.permission = this.authStorageService.getPermissions().rbdImage;
80     const actions = new RbdSnapshotActionsModel(this.i18n);
81     actions.create.click = () => this.openCreateSnapshotModal();
82     actions.rename.click = () => this.openEditSnapshotModal();
83     actions.protect.click = () => this.toggleProtection();
84     actions.unprotect.click = () => this.toggleProtection();
85     const getImageUri = () =>
86       this.selection.first() &&
87       `${encodeURI(this.poolName)}/${encodeURI(this.rbdName)}/${encodeURI(
88         this.selection.first().name
89       )}`;
90     actions.clone.routerLink = () => `/block/rbd/clone/${getImageUri()}`;
91     actions.copy.routerLink = () => `/block/rbd/copy/${getImageUri()}`;
92     actions.rollback.click = () => this.rollbackModal();
93     actions.deleteSnap.click = () => this.deleteSnapshotModal();
94     this.tableActions = actions.ordering;
95   }
96
97   ngOnInit() {
98     this.columns = [
99       {
100         name: this.i18n('Name'),
101         prop: 'name',
102         cellTransformation: CellTemplate.executing,
103         flexGrow: 2
104       },
105       {
106         name: this.i18n('Size'),
107         prop: 'size',
108         flexGrow: 1,
109         cellClass: 'text-right',
110         pipe: this.dimlessBinaryPipe
111       },
112       {
113         name: this.i18n('Provisioned'),
114         prop: 'disk_usage',
115         flexGrow: 1,
116         cellClass: 'text-right',
117         pipe: this.dimlessBinaryPipe
118       },
119       {
120         name: this.i18n('State'),
121         prop: 'is_protected',
122         flexGrow: 1,
123         cellClass: 'text-center',
124         cellTemplate: this.protectTpl
125       },
126       {
127         name: this.i18n('Created'),
128         prop: 'timestamp',
129         flexGrow: 1,
130         pipe: this.cdDatePipe
131       }
132     ];
133   }
134
135   ngOnChanges() {
136     const itemFilter = (entry, task) => {
137       return entry.name === task.metadata['snapshot_name'];
138     };
139
140     const taskFilter = (task) => {
141       return (
142         ['rbd/snap/create', 'rbd/snap/delete', 'rbd/snap/edit', 'rbd/snap/rollback'].includes(
143           task.name
144         ) &&
145         this.poolName === task.metadata['pool_name'] &&
146         this.rbdName === task.metadata['image_name']
147       );
148     };
149
150     this.taskListService.init(
151       () => of(this.snapshots),
152       null,
153       (items) => (this.data = items),
154       () => (this.data = this.snapshots),
155       taskFilter,
156       itemFilter,
157       this.builders
158     );
159   }
160
161   private openSnapshotModal(taskName: string, snapName: string = null) {
162     this.modalRef = this.modalService.show(RbdSnapshotFormComponent);
163     this.modalRef.content.poolName = this.poolName;
164     this.modalRef.content.imageName = this.rbdName;
165     if (snapName) {
166       this.modalRef.content.setEditing();
167     } else {
168       // Auto-create a name for the snapshot: <image_name>_<timestamp_ISO_8601>
169       // https://en.wikipedia.org/wiki/ISO_8601
170       snapName = `${this.rbdName}-${moment()
171         .utc()
172         .format('YYYYMMDD[T]HHmmss[Z]')}`;
173     }
174     this.modalRef.content.setSnapName(snapName);
175     this.modalRef.content.onSubmit.subscribe((snapshotName: string) => {
176       const executingTask = new ExecutingTask();
177       executingTask.name = taskName;
178       executingTask.metadata = {
179         image_name: this.rbdName,
180         pool_name: this.poolName,
181         snapshot_name: snapshotName
182       };
183       this.summaryService.addRunningTask(executingTask);
184       this.ngOnChanges();
185     });
186   }
187
188   openCreateSnapshotModal() {
189     this.openSnapshotModal('rbd/snap/create');
190   }
191
192   openEditSnapshotModal() {
193     this.openSnapshotModal('rbd/snap/edit', this.selection.first().name);
194   }
195
196   toggleProtection() {
197     const snapshotName = this.selection.first().name;
198     const isProtected = this.selection.first().is_protected;
199     const finishedTask = new FinishedTask();
200     finishedTask.name = 'rbd/snap/edit';
201     finishedTask.metadata = {
202       pool_name: this.poolName,
203       image_name: this.rbdName,
204       snapshot_name: snapshotName
205     };
206     this.rbdService
207       .protectSnapshot(this.poolName, this.rbdName, snapshotName, !isProtected)
208       .toPromise()
209       .then((resp) => {
210         const executingTask = new ExecutingTask();
211         executingTask.name = finishedTask.name;
212         executingTask.metadata = finishedTask.metadata;
213         this.summaryService.addRunningTask(executingTask);
214         this.ngOnChanges();
215         this.taskManagerService.subscribe(
216           finishedTask.name,
217           finishedTask.metadata,
218           (asyncFinishedTask: FinishedTask) => {
219             this.notificationService.notifyTask(asyncFinishedTask);
220           }
221         );
222       });
223   }
224
225   _asyncTask(task: string, taskName: string, snapshotName: string) {
226     const finishedTask = new FinishedTask();
227     finishedTask.name = taskName;
228     finishedTask.metadata = {
229       pool_name: this.poolName,
230       image_name: this.rbdName,
231       snapshot_name: snapshotName
232     };
233     this.rbdService[task](this.poolName, this.rbdName, snapshotName)
234       .toPromise()
235       .then(() => {
236         const executingTask = new ExecutingTask();
237         executingTask.name = finishedTask.name;
238         executingTask.metadata = finishedTask.metadata;
239         this.summaryService.addRunningTask(executingTask);
240         this.modalRef.hide();
241         this.ngOnChanges();
242         this.taskManagerService.subscribe(
243           executingTask.name,
244           executingTask.metadata,
245           (asyncFinishedTask: FinishedTask) => {
246             this.notificationService.notifyTask(asyncFinishedTask);
247           }
248         );
249       })
250       .catch((resp) => {
251         this.modalRef.content.stopLoadingSpinner();
252       });
253   }
254
255   rollbackModal() {
256     const snapshotName = this.selection.selected[0].name;
257     const initialState = {
258       titleText: this.i18n('RBD snapshot rollback'),
259       buttonText: this.i18n('Rollback'),
260       bodyTpl: this.rollbackTpl,
261       bodyData: {
262         snapName: `${this.poolName}/${this.rbdName}@${snapshotName}`
263       },
264       onSubmit: () => {
265         this._asyncTask('rollbackSnapshot', 'rbd/snap/rollback', snapshotName);
266       }
267     };
268
269     this.modalRef = this.modalService.show(ConfirmationModalComponent, { initialState });
270   }
271
272   deleteSnapshotModal() {
273     const snapshotName = this.selection.selected[0].name;
274     this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
275       initialState: {
276         itemDescription: this.i18n('RBD snapshot'),
277         submitAction: () => this._asyncTask('deleteSnapshot', 'rbd/snap/delete', snapshotName)
278       }
279     });
280   }
281
282   updateSelection(selection: CdTableSelection) {
283     this.selection = selection;
284   }
285 }