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.selection.first() || !_.isUndefined(this.getDeleteDisableDesc()),
58 disableDesc: () => this.getDeleteDisableDesc()
60 this.tableActions = [createAction, deleteAction];
66 name: $localize`Namespace`,
71 name: $localize`Pool`,
76 name: $localize`Total images`,
85 this.poolService.list(['pool_name', 'type', 'application_metadata']).then((pools: any) => {
87 (pool: any) => this.rbdService.isRBDPool(pool) && pool.type === 'replicated'
89 const promisses: Observable<any>[] = [];
90 pools.forEach((pool: any) => {
91 promisses.push(this.rbdService.listNamespaces(pool['pool_name']));
93 if (promisses.length > 0) {
94 forkJoin(promisses).subscribe((data: Array<Array<string>>) => {
95 const result: any[] = [];
96 for (let i = 0; i < data.length; i++) {
97 const namespaces = data[i];
98 const pool_name = pools[i]['pool_name'];
99 namespaces.forEach((namespace: any) => {
101 id: `${pool_name}/${namespace.namespace}`,
103 namespace: namespace.namespace,
104 num_images: namespace.num_images
108 this.namespaces = result;
111 this.namespaces = [];
116 updateSelection(selection: CdTableSelection) {
117 this.selection = selection;
121 this.modalRef = this.modalService.show(RbdNamespaceFormModalComponent);
122 this.modalRef.componentInstance.onSubmit.subscribe(() => {
128 const pool = this.selection.first().pool;
129 const namespace = this.selection.first().namespace;
130 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
131 itemDescription: 'Namespace',
132 itemNames: [`${pool}/${namespace}`],
134 this.rbdService.deleteNamespace(pool, namespace).subscribe(
136 this.notificationService.show(
137 NotificationType.success,
138 $localize`Deleted namespace '${pool}/${namespace}'`
140 this.modalRef.close();
144 this.modalRef.componentInstance.stopLoadingSpinner();
150 getDeleteDisableDesc(): string | undefined {
151 const first = this.selection.first();
153 if (first.num_images > 0) {
154 return $localize`Namespace contains images`;