1 import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
3 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
4 import _ from 'lodash';
6 import { RbdService } from '~/app/shared/api/rbd.service';
7 import { ListWithDetails } from '~/app/shared/classes/list-with-details.class';
8 import { TableStatusViewCache } from '~/app/shared/classes/table-status-view-cache';
9 import { ConfirmationModalComponent } from '~/app/shared/components/confirmation-modal/confirmation-modal.component';
10 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
11 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
12 import { TableComponent } from '~/app/shared/datatable/table/table.component';
13 import { Icons } from '~/app/shared/enum/icons.enum';
14 import { ViewCacheStatus } from '~/app/shared/enum/view-cache-status.enum';
15 import { CdTableAction } from '~/app/shared/models/cd-table-action';
16 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
17 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
18 import { FinishedTask } from '~/app/shared/models/finished-task';
19 import { ImageSpec } from '~/app/shared/models/image-spec';
20 import { Permission } from '~/app/shared/models/permissions';
21 import { Task } from '~/app/shared/models/task';
22 import { DimlessBinaryPipe } from '~/app/shared/pipes/dimless-binary.pipe';
23 import { DimlessPipe } from '~/app/shared/pipes/dimless.pipe';
24 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
25 import { ModalService } from '~/app/shared/services/modal.service';
26 import { TaskListService } from '~/app/shared/services/task-list.service';
27 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
28 import { URLBuilderService } from '~/app/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')
48 usageTpl: TemplateRef<any>;
49 @ViewChild('parentTpl', { static: true })
50 parentTpl: TemplateRef<any>;
52 nameTpl: TemplateRef<any>;
53 @ViewChild('flattenTpl', { static: true })
54 flattenTpl: TemplateRef<any>;
55 @ViewChild('deleteTpl', { static: true })
56 deleteTpl: TemplateRef<any>;
57 @ViewChild('removingStatTpl', { static: true })
58 removingStatTpl: TemplateRef<any>;
59 @ViewChild('provisionedNotAvailableTooltipTpl', { static: true })
60 provisionedNotAvailableTooltipTpl: TemplateRef<any>;
61 @ViewChild('totalProvisionedNotAvailableTooltipTpl', { static: true })
62 totalProvisionedNotAvailableTooltipTpl: TemplateRef<any>;
64 permission: Permission;
65 tableActions: CdTableAction[];
67 columns: CdTableColumn[];
69 tableStatus = new TableStatusViewCache();
70 selection = new CdTableSelection();
73 modalRef: NgbModalRef;
76 'rbd/create': (metadata: object) =>
77 this.createRbdFromTask(metadata['pool_name'], metadata['namespace'], metadata['image_name']),
78 'rbd/delete': (metadata: object) => this.createRbdFromTaskImageSpec(metadata['image_spec']),
79 'rbd/clone': (metadata: object) =>
80 this.createRbdFromTask(
81 metadata['child_pool_name'],
82 metadata['child_namespace'],
83 metadata['child_image_name']
85 'rbd/copy': (metadata: object) =>
86 this.createRbdFromTask(
87 metadata['dest_pool_name'],
88 metadata['dest_namespace'],
89 metadata['dest_image_name']
93 private createRbdFromTaskImageSpec(imageSpecStr: string): RbdModel {
94 const imageSpec = ImageSpec.fromString(imageSpecStr);
95 return this.createRbdFromTask(imageSpec.poolName, imageSpec.namespace, imageSpec.imageName);
98 private createRbdFromTask(pool: string, namespace: string, name: string): RbdModel {
99 const model = new RbdModel();
101 model.unique_id = '-1';
103 model.namespace = namespace;
104 model.pool_name = pool;
105 model.image_format = RBDImageFormat.V2;
110 private authStorageService: AuthStorageService,
111 private rbdService: RbdService,
112 private dimlessBinaryPipe: DimlessBinaryPipe,
113 private dimlessPipe: DimlessPipe,
114 private modalService: ModalService,
115 private taskWrapper: TaskWrapperService,
116 public taskListService: TaskListService,
117 private urlBuilder: URLBuilderService,
118 public actionLabels: ActionLabelsI18n
121 this.permission = this.authStorageService.getPermissions().rbdImage;
122 const getImageUri = () =>
123 this.selection.first() &&
125 this.selection.first().pool_name,
126 this.selection.first().namespace,
127 this.selection.first().name
129 const addAction: CdTableAction = {
130 permission: 'create',
132 routerLink: () => this.urlBuilder.getCreate(),
133 canBePrimary: (selection: CdTableSelection) => !selection.hasSingleSelection,
134 name: this.actionLabels.CREATE
136 const editAction: CdTableAction = {
137 permission: 'update',
139 routerLink: () => this.urlBuilder.getEdit(getImageUri()),
140 name: this.actionLabels.EDIT,
141 disable: (selection: CdTableSelection) =>
142 this.getRemovingStatusDesc(selection) || this.getInvalidNameDisable(selection)
144 const deleteAction: CdTableAction = {
145 permission: 'delete',
147 click: () => this.deleteRbdModal(),
148 name: this.actionLabels.DELETE,
149 disable: (selection: CdTableSelection) => this.getDeleteDisableDesc(selection)
151 const copyAction: CdTableAction = {
152 permission: 'create',
153 canBePrimary: (selection: CdTableSelection) => selection.hasSingleSelection,
154 disable: (selection: CdTableSelection) =>
155 this.getRemovingStatusDesc(selection) ||
156 this.getInvalidNameDisable(selection) ||
157 !!selection.first().cdExecuting,
159 routerLink: () => `/block/rbd/copy/${getImageUri()}`,
160 name: this.actionLabels.COPY
162 const flattenAction: CdTableAction = {
163 permission: 'update',
164 disable: (selection: CdTableSelection) =>
165 this.getRemovingStatusDesc(selection) ||
166 this.getInvalidNameDisable(selection) ||
167 selection.first().cdExecuting ||
168 !selection.first().parent,
170 click: () => this.flattenRbdModal(),
171 name: this.actionLabels.FLATTEN
173 const moveAction: CdTableAction = {
174 permission: 'delete',
176 click: () => this.trashRbdModal(),
177 name: this.actionLabels.TRASH,
178 disable: (selection: CdTableSelection) =>
179 this.getRemovingStatusDesc(selection) ||
180 this.getInvalidNameDisable(selection) ||
181 selection.first().image_format === RBDImageFormat.V1
183 this.tableActions = [
196 name: $localize`Name`,
199 cellTemplate: this.removingStatTpl
202 name: $localize`Pool`,
207 name: $localize`Namespace`,
212 name: $localize`Size`,
215 cellClass: 'text-right',
216 pipe: this.dimlessBinaryPipe
219 name: $localize`Objects`,
222 cellClass: 'text-right',
223 pipe: this.dimlessPipe
226 name: $localize`Object size`,
229 cellClass: 'text-right',
230 pipe: this.dimlessBinaryPipe
233 name: $localize`Provisioned`,
235 cellClass: 'text-center',
237 pipe: this.dimlessBinaryPipe,
238 cellTemplate: this.provisionedNotAvailableTooltipTpl
241 name: $localize`Total provisioned`,
242 prop: 'total_disk_usage',
243 cellClass: 'text-center',
245 pipe: this.dimlessBinaryPipe,
246 cellTemplate: this.totalProvisionedNotAvailableTooltipTpl
249 name: $localize`Parent`,
252 cellTemplate: this.parentTpl
256 const itemFilter = (entry: Record<string, any>, task: Task) => {
257 let taskImageSpec: string;
260 taskImageSpec = new ImageSpec(
261 task.metadata['dest_pool_name'],
262 task.metadata['dest_namespace'],
263 task.metadata['dest_image_name']
267 taskImageSpec = new ImageSpec(
268 task.metadata['child_pool_name'],
269 task.metadata['child_namespace'],
270 task.metadata['child_image_name']
274 taskImageSpec = new ImageSpec(
275 task.metadata['pool_name'],
276 task.metadata['namespace'],
277 task.metadata['image_name']
281 taskImageSpec = task.metadata['image_spec'];
285 taskImageSpec === new ImageSpec(entry.pool_name, entry.namespace, entry.name).toString()
289 const taskFilter = (task: Task) => {
298 ].includes(task.name);
301 this.taskListService.init(
302 () => this.rbdService.list(),
303 (resp) => this.prepareResponse(resp),
304 (images) => (this.images = images),
305 () => this.onFetchError(),
313 this.table.reset(); // Disable loading indicator.
314 this.tableStatus = new TableStatusViewCache(ViewCacheStatus.ValueException);
317 prepareResponse(resp: any[]): any[] {
318 let images: any[] = [];
319 const viewCacheStatusMap = {};
321 resp.forEach((pool) => {
322 if (_.isUndefined(viewCacheStatusMap[pool.status])) {
323 viewCacheStatusMap[pool.status] = [];
325 viewCacheStatusMap[pool.status].push(pool.pool_name);
326 images = images.concat(pool.value);
330 if (viewCacheStatusMap[ViewCacheStatus.ValueException]) {
331 status = ViewCacheStatus.ValueException;
332 } else if (viewCacheStatusMap[ViewCacheStatus.ValueStale]) {
333 status = ViewCacheStatus.ValueStale;
334 } else if (viewCacheStatusMap[ViewCacheStatus.ValueNone]) {
335 status = ViewCacheStatus.ValueNone;
340 (viewCacheStatusMap[status].length > 1 ? 'pools ' : 'pool ') +
341 viewCacheStatusMap[status].join();
343 this.tableStatus = new TableStatusViewCache(status, statusFor);
345 this.tableStatus = new TableStatusViewCache();
351 updateSelection(selection: CdTableSelection) {
352 this.selection = selection;
356 const poolName = this.selection.first().pool_name;
357 const namespace = this.selection.first().namespace;
358 const imageName = this.selection.first().name;
359 const imageSpec = new ImageSpec(poolName, namespace, imageName);
361 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
362 itemDescription: 'RBD',
363 itemNames: [imageSpec],
364 bodyTemplate: this.deleteTpl,
366 hasSnapshots: this.hasSnapshots(),
367 snapshots: this.listProtectedSnapshots()
369 submitActionObservable: () =>
370 this.taskWrapper.wrapTaskAroundCall({
371 task: new FinishedTask('rbd/delete', {
372 image_spec: imageSpec.toString()
374 call: this.rbdService.delete(imageSpec)
380 const initialState = {
381 poolName: this.selection.first().pool_name,
382 namespace: this.selection.first().namespace,
383 imageName: this.selection.first().name,
384 hasSnapshots: this.hasSnapshots()
386 this.modalRef = this.modalService.show(RbdTrashMoveModalComponent, initialState);
389 flattenRbd(imageSpec: ImageSpec) {
391 .wrapTaskAroundCall({
392 task: new FinishedTask('rbd/flatten', {
393 image_spec: imageSpec.toString()
395 call: this.rbdService.flatten(imageSpec)
399 this.modalRef.close();
405 const poolName = this.selection.first().pool_name;
406 const namespace = this.selection.first().namespace;
407 const imageName = this.selection.first().name;
408 const parent: RbdParentModel = this.selection.first().parent;
409 const parentImageSpec = new ImageSpec(
411 parent.pool_namespace,
414 const childImageSpec = new ImageSpec(poolName, namespace, imageName);
416 const initialState = {
417 titleText: 'RBD flatten',
418 buttonText: 'Flatten',
419 bodyTpl: this.flattenTpl,
421 parent: `${parentImageSpec}@${parent.snap_name}`,
422 child: childImageSpec.toString()
425 this.flattenRbd(childImageSpec);
429 this.modalRef = this.modalService.show(ConfirmationModalComponent, initialState);
433 const snapshots = this.selection.first()['snapshots'] || [];
434 return snapshots.length > 0;
437 hasClonedSnapshots(image: object) {
438 const snapshots = image['snapshots'] || [];
439 return snapshots.some((snap: object) => snap['children'] && snap['children'].length > 0);
442 listProtectedSnapshots() {
443 const first = this.selection.first();
444 const snapshots = first['snapshots'];
445 return snapshots.reduce((accumulator: string[], snap: object) => {
446 if (snap['is_protected']) {
447 accumulator.push(snap['name']);
453 getDeleteDisableDesc(selection: CdTableSelection): string | boolean {
454 const first = selection.first();
456 if (first && this.hasClonedSnapshots(first)) {
457 return $localize`This RBD has cloned snapshots. Please delete related RBDs before deleting this RBD.`;
460 return this.getInvalidNameDisable(selection) || this.hasClonedSnapshots(selection.first());
463 getInvalidNameDisable(selection: CdTableSelection): string | boolean {
464 const first = selection.first();
466 if (first?.name?.match(/[@/]/)) {
467 return $localize`This RBD image has an invalid name and can't be managed by ceph.`;
470 return !selection.first() || !selection.hasSingleSelection;
473 getRemovingStatusDesc(selection: CdTableSelection): string | boolean {
474 const first = selection.first();
475 if (first?.source === 'REMOVING') {
476 return $localize`Action not possible for an RBD in status 'Removing'`;