1 import { Component, OnInit } from '@angular/core';
3 import { I18n } from '@ngx-translate/i18n-polyfill';
4 import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
6 import * as _ from 'lodash';
7 import { forkJoin, Observable } from 'rxjs';
8 import { PoolService } from '../../../shared/api/pool.service';
9 import { RbdService } from '../../../shared/api/rbd.service';
10 import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
11 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
12 import { Icons } from '../../../shared/enum/icons.enum';
13 import { NotificationType } from '../../../shared/enum/notification-type.enum';
14 import { CdTableAction } from '../../../shared/models/cd-table-action';
15 import { CdTableColumn } from '../../../shared/models/cd-table-column';
16 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
17 import { Permission } from '../../../shared/models/permissions';
18 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
19 import { NotificationService } from '../../../shared/services/notification.service';
20 import { TaskListService } from '../../../shared/services/task-list.service';
21 import { RbdNamespaceFormComponent } from '../rbd-namespace-form/rbd-namespace-form.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[];
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: BsModalService,
42 private notificationService: NotificationService,
44 public actionLabels: ActionLabelsI18n
46 this.permission = this.authStorageService.getPermissions().rbdImage;
47 const createAction: CdTableAction = {
50 click: () => this.createModal(),
51 name: this.actionLabels.CREATE
53 const deleteAction: CdTableAction = {
56 click: () => this.deleteModal(),
57 name: this.actionLabels.DELETE,
58 disable: () => !this.selection.first() || !_.isUndefined(this.getDeleteDisableDesc()),
59 disableDesc: () => this.getDeleteDisableDesc()
61 this.tableActions = [createAction, deleteAction];
67 name: this.i18n('Namespace'),
72 name: this.i18n('Pool'),
77 name: this.i18n('Total images'),
86 this.poolService.list(['pool_name', 'type', 'application_metadata']).then((pools: any) => {
88 (pool: any) => this.rbdService.isRBDPool(pool) && pool.type === 'replicated'
90 const promisses: Observable<any>[] = [];
91 pools.forEach((pool: any) => {
92 promisses.push(this.rbdService.listNamespaces(pool['pool_name']));
94 if (promisses.length > 0) {
95 forkJoin(promisses).subscribe((data: Array<Array<string>>) => {
96 const result: any[] = [];
97 for (let i = 0; i < data.length; i++) {
98 const namespaces = data[i];
99 const pool_name = pools[i]['pool_name'];
100 namespaces.forEach((namespace: any) => {
102 id: `${pool_name}/${namespace.namespace}`,
104 namespace: namespace.namespace,
105 num_images: namespace.num_images
109 this.namespaces = result;
112 this.namespaces = [];
117 updateSelection(selection: CdTableSelection) {
118 this.selection = selection;
122 this.modalRef = this.modalService.show(RbdNamespaceFormComponent);
123 this.modalRef.content.onSubmit.subscribe(() => {
129 const pool = this.selection.first().pool;
130 const namespace = this.selection.first().namespace;
131 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
133 itemDescription: 'Namespace',
134 itemNames: [`${pool}/${namespace}`],
136 this.rbdService.deleteNamespace(pool, namespace).subscribe(
138 this.notificationService.show(
139 NotificationType.success,
140 this.i18n(`Deleted namespace '{{pool}}/{{namespace}}'`, {
145 this.modalRef.hide();
149 this.modalRef.content.stopLoadingSpinner();
156 getDeleteDisableDesc(): string | undefined {
157 const first = this.selection.first();
159 if (first.num_images > 0) {
160 return this.i18n('Namespace contains images');