1 import { Component, OnInit } from '@angular/core';
3 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
4 import { forkJoin, Observable } from 'rxjs';
6 import { PoolService } from '~/app/shared/api/pool.service';
7 import { RbdService } from '~/app/shared/api/rbd.service';
8 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
9 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
10 import { Icons } from '~/app/shared/enum/icons.enum';
11 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
12 import { CdTableAction } from '~/app/shared/models/cd-table-action';
13 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
14 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
15 import { Permission } from '~/app/shared/models/permissions';
16 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
17 import { ModalService } from '~/app/shared/services/modal.service';
18 import { NotificationService } from '~/app/shared/services/notification.service';
19 import { TaskListService } from '~/app/shared/services/task-list.service';
20 import { RbdNamespaceFormModalComponent } from '../rbd-namespace-form/rbd-namespace-form-modal.component';
23 selector: 'cd-rbd-namespace-list',
24 templateUrl: './rbd-namespace-list.component.html',
25 styleUrls: ['./rbd-namespace-list.component.scss'],
26 providers: [TaskListService]
28 export class RbdNamespaceListComponent implements OnInit {
29 columns: CdTableColumn[];
31 modalRef: NgbModalRef;
32 permission: Permission;
33 selection = new CdTableSelection();
34 tableActions: CdTableAction[];
37 private authStorageService: AuthStorageService,
38 private rbdService: RbdService,
39 private poolService: PoolService,
40 private modalService: ModalService,
41 private notificationService: NotificationService,
42 public actionLabels: ActionLabelsI18n
44 this.permission = this.authStorageService.getPermissions().rbdImage;
45 const createAction: CdTableAction = {
48 click: () => this.createModal(),
49 name: this.actionLabels.CREATE
51 const deleteAction: CdTableAction = {
54 click: () => this.deleteModal(),
55 name: this.actionLabels.DELETE,
56 disable: () => this.getDeleteDisableDesc()
58 this.tableActions = [createAction, deleteAction];
64 name: $localize`Namespace`,
69 name: $localize`Pool`,
74 name: $localize`Total images`,
83 this.poolService.list(['pool_name', 'type', 'application_metadata']).then((pools: any) => {
85 (pool: any) => this.rbdService.isRBDPool(pool) && pool.type === 'replicated'
87 const promises: Observable<any>[] = [];
88 pools.forEach((pool: any) => {
89 promises.push(this.rbdService.listNamespaces(pool['pool_name']));
91 if (promises.length > 0) {
92 forkJoin(promises).subscribe((data: Array<Array<string>>) => {
93 const result: any[] = [];
94 for (let i = 0; i < data.length; i++) {
95 const namespaces = data[i];
96 const pool_name = pools[i]['pool_name'];
97 namespaces.forEach((namespace: any) => {
99 id: `${pool_name}/${namespace.namespace}`,
101 namespace: namespace.namespace,
102 num_images: namespace.num_images
106 this.namespaces = result;
109 this.namespaces = [];
114 updateSelection(selection: CdTableSelection) {
115 this.selection = selection;
119 this.modalRef = this.modalService.show(RbdNamespaceFormModalComponent);
120 this.modalRef.componentInstance.onSubmit.subscribe(() => {
126 const pool = this.selection.first().pool;
127 const namespace = this.selection.first().namespace;
128 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
129 itemDescription: 'Namespace',
130 itemNames: [`${pool}/${namespace}`],
132 this.rbdService.deleteNamespace(pool, namespace).subscribe(
134 this.notificationService.show(
135 NotificationType.success,
136 $localize`Deleted namespace '${pool}/${namespace}'`
138 this.modalRef.close();
142 this.modalRef.componentInstance.stopLoadingSpinner();
148 getDeleteDisableDesc(): string | boolean {
149 const first = this.selection.first();
151 if (first?.num_images > 0) {
152 return $localize`Namespace contains images`;
155 return !this.selection?.first();