1 import { Component, Input, OnChanges, OnInit, TemplateRef, ViewChild } from '@angular/core';
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';
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';
30 selector: 'cd-rbd-snapshot-list',
31 templateUrl: './rbd-snapshot-list.component.html',
32 styleUrls: ['./rbd-snapshot-list.component.scss'],
33 providers: [TaskListService]
35 export class RbdSnapshotListComponent implements OnInit, OnChanges {
37 snapshots: RbdSnapshotModel[] = [];
43 nameTpl: TemplateRef<any>;
44 @ViewChild('protectTpl')
45 protectTpl: TemplateRef<any>;
46 @ViewChild('rollbackTpl')
47 rollbackTpl: TemplateRef<any>;
49 permission: Permission;
50 selection = new CdTableSelection();
51 tableActions: CdTableAction[];
53 data: RbdSnapshotModel[];
55 columns: CdTableColumn[];
60 'rbd/snap/create': (metadata) => {
61 const model = new RbdSnapshotModel();
62 model.name = metadata['snapshot_name'];
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,
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
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;
100 name: this.i18n('Name'),
102 cellTransformation: CellTemplate.executing,
106 name: this.i18n('Size'),
109 cellClass: 'text-right',
110 pipe: this.dimlessBinaryPipe
113 name: this.i18n('Provisioned'),
116 cellClass: 'text-right',
117 pipe: this.dimlessBinaryPipe
120 name: this.i18n('State'),
121 prop: 'is_protected',
123 cellClass: 'text-center',
124 cellTemplate: this.protectTpl
127 name: this.i18n('Created'),
130 pipe: this.cdDatePipe
136 const itemFilter = (entry, task) => {
137 return entry.name === task.metadata['snapshot_name'];
140 const taskFilter = (task) => {
142 ['rbd/snap/create', 'rbd/snap/delete', 'rbd/snap/edit', 'rbd/snap/rollback'].includes(
145 this.poolName === task.metadata['pool_name'] &&
146 this.rbdName === task.metadata['image_name']
150 this.taskListService.init(
151 () => of(this.snapshots),
153 (items) => (this.data = items),
154 () => (this.data = this.snapshots),
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;
166 this.modalRef.content.setEditing();
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()
172 .format('YYYYMMDD[T]HHmmss[Z]')}`;
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
183 this.summaryService.addRunningTask(executingTask);
188 openCreateSnapshotModal() {
189 this.openSnapshotModal('rbd/snap/create');
192 openEditSnapshotModal() {
193 this.openSnapshotModal('rbd/snap/edit', this.selection.first().name);
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
207 .protectSnapshot(this.poolName, this.rbdName, snapshotName, !isProtected)
210 const executingTask = new ExecutingTask();
211 executingTask.name = finishedTask.name;
212 executingTask.metadata = finishedTask.metadata;
213 this.summaryService.addRunningTask(executingTask);
215 this.taskManagerService.subscribe(
217 finishedTask.metadata,
218 (asyncFinishedTask: FinishedTask) => {
219 this.notificationService.notifyTask(asyncFinishedTask);
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
233 this.rbdService[task](this.poolName, this.rbdName, snapshotName)
236 const executingTask = new ExecutingTask();
237 executingTask.name = finishedTask.name;
238 executingTask.metadata = finishedTask.metadata;
239 this.summaryService.addRunningTask(executingTask);
240 this.modalRef.hide();
242 this.taskManagerService.subscribe(
244 executingTask.metadata,
245 (asyncFinishedTask: FinishedTask) => {
246 this.notificationService.notifyTask(asyncFinishedTask);
251 this.modalRef.content.stopLoadingSpinner();
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,
262 snapName: `${this.poolName}/${this.rbdName}@${snapshotName}`
265 this._asyncTask('rollbackSnapshot', 'rbd/snap/rollback', snapshotName);
269 this.modalRef = this.modalService.show(ConfirmationModalComponent, { initialState });
272 deleteSnapshotModal() {
273 const snapshotName = this.selection.selected[0].name;
274 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
276 itemDescription: this.i18n('RBD snapshot'),
277 submitAction: () => this._asyncTask('deleteSnapshot', 'rbd/snap/delete', snapshotName)
282 updateSelection(selection: CdTableSelection) {
283 this.selection = selection;