1 import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
3 import { I18n } from '@ngx-translate/i18n-polyfill';
4 import * as _ from 'lodash';
5 import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
7 import { RbdService } from '../../../shared/api/rbd.service';
8 import { ListWithDetails } from '../../../shared/classes/list-with-details.class';
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 { ActionLabelsI18n } from '../../../shared/constants/app.constants';
12 import { TableComponent } from '../../../shared/datatable/table/table.component';
13 import { CellTemplate } from '../../../shared/enum/cell-template.enum';
14 import { Icons } from '../../../shared/enum/icons.enum';
15 import { ViewCacheStatus } from '../../../shared/enum/view-cache-status.enum';
16 import { CdTableAction } from '../../../shared/models/cd-table-action';
17 import { CdTableColumn } from '../../../shared/models/cd-table-column';
18 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
19 import { FinishedTask } from '../../../shared/models/finished-task';
20 import { ImageSpec } from '../../../shared/models/image-spec';
21 import { Permission } from '../../../shared/models/permissions';
22 import { Task } from '../../../shared/models/task';
23 import { DimlessBinaryPipe } from '../../../shared/pipes/dimless-binary.pipe';
24 import { DimlessPipe } from '../../../shared/pipes/dimless.pipe';
25 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
26 import { TaskListService } from '../../../shared/services/task-list.service';
27 import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
28 import { URLBuilderService } from '../../../shared/services/url-builder.service';
29 import { RbdParentModel } from '../rbd-form/rbd-parent.model';
30 import { RbdTrashMoveModalComponent } from '../rbd-trash-move-modal/rbd-trash-move-modal.component';
31 import { RBDImageFormat, RbdModel } from './rbd-model';
33 const BASE_URL = 'block/rbd';
36 selector: 'cd-rbd-list',
37 templateUrl: './rbd-list.component.html',
38 styleUrls: ['./rbd-list.component.scss'],
41 { provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }
44 export class RbdListComponent extends ListWithDetails implements OnInit {
45 @ViewChild(TableComponent, { static: true })
46 table: TableComponent;
47 @ViewChild('usageTpl', { static: false })
48 usageTpl: TemplateRef<any>;
49 @ViewChild('parentTpl', { static: true })
50 parentTpl: TemplateRef<any>;
51 @ViewChild('nameTpl', { static: false })
52 nameTpl: TemplateRef<any>;
53 @ViewChild('flattenTpl', { static: true })
54 flattenTpl: TemplateRef<any>;
55 @ViewChild('deleteTpl', { static: true })
56 deleteTpl: TemplateRef<any>;
58 permission: Permission;
59 tableActions: CdTableAction[];
61 columns: CdTableColumn[];
63 viewCacheStatusList: any[];
64 selection = new CdTableSelection();
69 'rbd/create': (metadata: object) =>
70 this.createRbdFromTask(metadata['pool_name'], metadata['namespace'], metadata['image_name']),
71 'rbd/delete': (metadata: object) => this.createRbdFromTaskImageSpec(metadata['image_spec']),
72 'rbd/clone': (metadata: object) =>
73 this.createRbdFromTask(
74 metadata['child_pool_name'],
75 metadata['child_namespace'],
76 metadata['child_image_name']
78 'rbd/copy': (metadata: object) =>
79 this.createRbdFromTask(
80 metadata['dest_pool_name'],
81 metadata['dest_namespace'],
82 metadata['dest_image_name']
86 private createRbdFromTaskImageSpec(imageSpecStr: string): RbdModel {
87 const imageSpec = ImageSpec.fromString(imageSpecStr);
88 return this.createRbdFromTask(imageSpec.poolName, imageSpec.namespace, imageSpec.imageName);
91 private createRbdFromTask(pool: string, namespace: string, name: string): RbdModel {
92 const model = new RbdModel();
94 model.unique_id = '-1';
96 model.namespace = namespace;
97 model.pool_name = pool;
98 model.image_format = RBDImageFormat.V2;
103 private authStorageService: AuthStorageService,
104 private rbdService: RbdService,
105 private dimlessBinaryPipe: DimlessBinaryPipe,
106 private dimlessPipe: DimlessPipe,
107 private modalService: BsModalService,
108 private taskWrapper: TaskWrapperService,
109 private taskListService: TaskListService,
111 private urlBuilder: URLBuilderService,
112 public actionLabels: ActionLabelsI18n
115 this.permission = this.authStorageService.getPermissions().rbdImage;
116 const getImageUri = () =>
117 this.selection.first() &&
119 this.selection.first().pool_name,
120 this.selection.first().namespace,
121 this.selection.first().name
123 const addAction: CdTableAction = {
124 permission: 'create',
126 routerLink: () => this.urlBuilder.getCreate(),
127 canBePrimary: (selection: CdTableSelection) => !selection.hasSingleSelection,
128 name: this.actionLabels.CREATE
130 const editAction: CdTableAction = {
131 permission: 'update',
133 routerLink: () => this.urlBuilder.getEdit(getImageUri()),
134 name: this.actionLabels.EDIT
136 const deleteAction: CdTableAction = {
137 permission: 'delete',
139 click: () => this.deleteRbdModal(),
140 name: this.actionLabels.DELETE,
141 disable: (selection: CdTableSelection) =>
142 !this.selection.first() ||
143 !this.selection.hasSingleSelection ||
144 this.hasClonedSnapshots(selection.first()),
145 disableDesc: () => this.getDeleteDisableDesc()
147 const copyAction: CdTableAction = {
148 permission: 'create',
149 canBePrimary: (selection: CdTableSelection) => selection.hasSingleSelection,
150 disable: (selection: CdTableSelection) =>
151 !selection.hasSingleSelection || selection.first().cdExecuting,
153 routerLink: () => `/block/rbd/copy/${getImageUri()}`,
154 name: this.actionLabels.COPY
156 const flattenAction: CdTableAction = {
157 permission: 'update',
158 disable: (selection: CdTableSelection) =>
159 !selection.hasSingleSelection || selection.first().cdExecuting || !selection.first().parent,
161 click: () => this.flattenRbdModal(),
162 name: this.actionLabels.FLATTEN
164 const moveAction: CdTableAction = {
165 permission: 'delete',
167 click: () => this.trashRbdModal(),
168 name: this.actionLabels.TRASH,
169 disable: (selection: CdTableSelection) =>
170 !selection.first() ||
171 !selection.hasSingleSelection ||
172 selection.first().image_format === RBDImageFormat.V1
174 this.tableActions = [
187 name: this.i18n('Name'),
190 cellTransformation: CellTemplate.executing
193 name: this.i18n('Pool'),
198 name: this.i18n('Namespace'),
203 name: this.i18n('Size'),
206 cellClass: 'text-right',
207 pipe: this.dimlessBinaryPipe
210 name: this.i18n('Objects'),
213 cellClass: 'text-right',
214 pipe: this.dimlessPipe
217 name: this.i18n('Object size'),
220 cellClass: 'text-right',
221 pipe: this.dimlessBinaryPipe
224 name: this.i18n('Provisioned'),
226 cellClass: 'text-center',
228 pipe: this.dimlessBinaryPipe
231 name: this.i18n('Total provisioned'),
232 prop: 'total_disk_usage',
233 cellClass: 'text-center',
235 pipe: this.dimlessBinaryPipe
238 name: this.i18n('Parent'),
241 cellTemplate: this.parentTpl
245 const itemFilter = (entry: Record<string, any>, task: Task) => {
246 let taskImageSpec: string;
249 taskImageSpec = new ImageSpec(
250 task.metadata['dest_pool_name'],
251 task.metadata['dest_namespace'],
252 task.metadata['dest_image_name']
256 taskImageSpec = new ImageSpec(
257 task.metadata['child_pool_name'],
258 task.metadata['child_namespace'],
259 task.metadata['child_image_name']
263 taskImageSpec = new ImageSpec(
264 task.metadata['pool_name'],
265 task.metadata['namespace'],
266 task.metadata['image_name']
270 taskImageSpec = task.metadata['image_spec'];
274 taskImageSpec === new ImageSpec(entry.pool_name, entry.namespace, entry.name).toString()
278 const taskFilter = (task: Task) => {
287 ].includes(task.name);
290 this.taskListService.init(
291 () => this.rbdService.list(),
292 (resp) => this.prepareResponse(resp),
293 (images) => (this.images = images),
294 () => this.onFetchError(),
302 this.table.reset(); // Disable loading indicator.
303 this.viewCacheStatusList = [{ status: ViewCacheStatus.ValueException }];
306 prepareResponse(resp: any[]): any[] {
307 let images: any[] = [];
308 const viewCacheStatusMap = {};
309 resp.forEach((pool) => {
310 if (_.isUndefined(viewCacheStatusMap[pool.status])) {
311 viewCacheStatusMap[pool.status] = [];
313 viewCacheStatusMap[pool.status].push(pool.pool_name);
314 images = images.concat(pool.value);
316 const viewCacheStatusList: any[] = [];
317 _.forEach(viewCacheStatusMap, (value: any, key) => {
318 viewCacheStatusList.push({
319 status: parseInt(key, 10),
321 (value.length > 1 ? 'pools ' : 'pool ') +
323 value.join('</strong>, <strong>') +
327 this.viewCacheStatusList = viewCacheStatusList;
331 updateSelection(selection: CdTableSelection) {
332 this.selection = selection;
336 const poolName = this.selection.first().pool_name;
337 const namespace = this.selection.first().namespace;
338 const imageName = this.selection.first().name;
339 const imageSpec = new ImageSpec(poolName, namespace, imageName);
341 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
343 itemDescription: 'RBD',
344 itemNames: [imageSpec],
345 bodyTemplate: this.deleteTpl,
347 hasSnapshots: this.hasSnapshots(),
348 snapshots: this.listProtectedSnapshots()
350 submitActionObservable: () =>
351 this.taskWrapper.wrapTaskAroundCall({
352 task: new FinishedTask('rbd/delete', {
353 image_spec: imageSpec.toString()
355 call: this.rbdService.delete(imageSpec)
362 const initialState = {
363 poolName: this.selection.first().pool_name,
364 namespace: this.selection.first().namespace,
365 imageName: this.selection.first().name,
366 hasSnapshots: this.hasSnapshots()
368 this.modalRef = this.modalService.show(RbdTrashMoveModalComponent, { initialState });
371 flattenRbd(imageSpec: ImageSpec) {
373 .wrapTaskAroundCall({
374 task: new FinishedTask('rbd/flatten', {
375 image_spec: imageSpec.toString()
377 call: this.rbdService.flatten(imageSpec)
379 .subscribe(undefined, undefined, () => {
380 this.modalRef.hide();
385 const poolName = this.selection.first().pool_name;
386 const namespace = this.selection.first().namespace;
387 const imageName = this.selection.first().name;
388 const parent: RbdParentModel = this.selection.first().parent;
389 const parentImageSpec = new ImageSpec(
391 parent.pool_namespace,
394 const childImageSpec = new ImageSpec(poolName, namespace, imageName);
396 const initialState = {
397 titleText: 'RBD flatten',
398 buttonText: 'Flatten',
399 bodyTpl: this.flattenTpl,
401 parent: `${parentImageSpec}@${parent.snap_name}`,
402 child: childImageSpec.toString()
405 this.flattenRbd(childImageSpec);
409 this.modalRef = this.modalService.show(ConfirmationModalComponent, { initialState });
413 const snapshots = this.selection.first()['snapshots'] || [];
414 return snapshots.length > 0;
417 hasClonedSnapshots(image: object) {
418 const snapshots = image['snapshots'] || [];
419 return snapshots.some((snap: object) => snap['children'] && snap['children'].length > 0);
422 listProtectedSnapshots() {
423 const first = this.selection.first();
424 const snapshots = first['snapshots'];
425 return snapshots.reduce((accumulator: string[], snap: object) => {
426 if (snap['is_protected']) {
427 accumulator.push(snap['name']);
433 getDeleteDisableDesc(): string {
434 const first = this.selection.first();
435 if (first && this.hasClonedSnapshots(first)) {
437 'This RBD has cloned snapshots. Please delete related RBDs before deleting this RBD.'