1 import { Component, OnInit } from '@angular/core';
3 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
4 import * as _ from 'lodash';
5 import { forkJoin, Observable } from 'rxjs';
7 import { PoolService } from '../../../shared/api/pool.service';
8 import { RbdService } from '../../../shared/api/rbd.service';
9 import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
10 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
11 import { Icons } from '../../../shared/enum/icons.enum';
12 import { NotificationType } from '../../../shared/enum/notification-type.enum';
13 import { CdTableAction } from '../../../shared/models/cd-table-action';
14 import { CdTableColumn } from '../../../shared/models/cd-table-column';
15 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
16 import { Permission } from '../../../shared/models/permissions';
17 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
18 import { ModalService } from '../../../shared/services/modal.service';
19 import { NotificationService } from '../../../shared/services/notification.service';
20 import { TaskListService } from '../../../shared/services/task-list.service';
21 import { RbdNamespaceFormModalComponent } from '../rbd-namespace-form/rbd-namespace-form-modal.component';
24 selector: 'cd-rbd-namespace-list',
25 templateUrl: './rbd-namespace-list.component.html',
26 styleUrls: ['./rbd-namespace-list.component.scss'],
27 providers: [TaskListService]
29 export class RbdNamespaceListComponent implements OnInit {
30 columns: CdTableColumn[];
32 modalRef: NgbModalRef;
33 permission: Permission;
34 selection = new CdTableSelection();
35 tableActions: CdTableAction[];
38 private authStorageService: AuthStorageService,
39 private rbdService: RbdService,
40 private poolService: PoolService,
41 private modalService: ModalService,
42 private notificationService: NotificationService,
43 public actionLabels: ActionLabelsI18n
45 this.permission = this.authStorageService.getPermissions().rbdImage;
46 const createAction: CdTableAction = {
49 click: () => this.createModal(),
50 name: this.actionLabels.CREATE
52 const deleteAction: CdTableAction = {
55 click: () => this.deleteModal(),
56 name: this.actionLabels.DELETE,
57 disable: () => this.getDeleteDisableDesc()
59 this.tableActions = [createAction, deleteAction];
65 name: $localize`Namespace`,
70 name: $localize`Pool`,
75 name: $localize`Total images`,
84 this.poolService.list(['pool_name', 'type', 'application_metadata']).then((pools: any) => {
86 (pool: any) => this.rbdService.isRBDPool(pool) && pool.type === 'replicated'
88 const promisses: Observable<any>[] = [];
89 pools.forEach((pool: any) => {
90 promisses.push(this.rbdService.listNamespaces(pool['pool_name']));
92 if (promisses.length > 0) {
93 forkJoin(promisses).subscribe((data: Array<Array<string>>) => {
94 const result: any[] = [];
95 for (let i = 0; i < data.length; i++) {
96 const namespaces = data[i];
97 const pool_name = pools[i]['pool_name'];
98 namespaces.forEach((namespace: any) => {
100 id: `${pool_name}/${namespace.namespace}`,
102 namespace: namespace.namespace,
103 num_images: namespace.num_images
107 this.namespaces = result;
110 this.namespaces = [];
115 updateSelection(selection: CdTableSelection) {
116 this.selection = selection;
120 this.modalRef = this.modalService.show(RbdNamespaceFormModalComponent);
121 this.modalRef.componentInstance.onSubmit.subscribe(() => {
127 const pool = this.selection.first().pool;
128 const namespace = this.selection.first().namespace;
129 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
130 itemDescription: 'Namespace',
131 itemNames: [`${pool}/${namespace}`],
133 this.rbdService.deleteNamespace(pool, namespace).subscribe(
135 this.notificationService.show(
136 NotificationType.success,
137 $localize`Deleted namespace '${pool}/${namespace}'`
139 this.modalRef.close();
143 this.modalRef.componentInstance.stopLoadingSpinner();
149 getDeleteDisableDesc(): string | boolean {
150 const first = this.selection.first();
152 if (first?.num_images > 0) {
153 return $localize`Namespace contains images`;
156 return !this.selection?.first();