]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
3dd514ee46166b905f5773e4a97340242c269a95
[ceph.git] /
1 import { Component, OnInit } from '@angular/core';
2
3 import { I18n } from '@ngx-translate/i18n-polyfill';
4 import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
5
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';
22
23 @Component({
24   selector: 'cd-rbd-namespace-list',
25   templateUrl: './rbd-namespace-list.component.html',
26   styleUrls: ['./rbd-namespace-list.component.scss'],
27   providers: [TaskListService]
28 })
29 export class RbdNamespaceListComponent implements OnInit {
30   columns: CdTableColumn[];
31   namespaces: any;
32   modalRef: BsModalRef;
33   permission: Permission;
34   selection = new CdTableSelection();
35   tableActions: CdTableAction[];
36
37   constructor(
38     private authStorageService: AuthStorageService,
39     private rbdService: RbdService,
40     private poolService: PoolService,
41     private modalService: BsModalService,
42     private notificationService: NotificationService,
43     private i18n: I18n,
44     public actionLabels: ActionLabelsI18n
45   ) {
46     this.permission = this.authStorageService.getPermissions().rbdImage;
47     const createAction: CdTableAction = {
48       permission: 'create',
49       icon: Icons.add,
50       click: () => this.createModal(),
51       name: this.actionLabels.CREATE
52     };
53     const deleteAction: CdTableAction = {
54       permission: 'delete',
55       icon: Icons.destroy,
56       click: () => this.deleteModal(),
57       name: this.actionLabels.DELETE,
58       disable: () => !this.selection.first() || !_.isUndefined(this.getDeleteDisableDesc()),
59       disableDesc: () => this.getDeleteDisableDesc()
60     };
61     this.tableActions = [createAction, deleteAction];
62   }
63
64   ngOnInit() {
65     this.columns = [
66       {
67         name: this.i18n('Namespace'),
68         prop: 'namespace',
69         flexGrow: 1
70       },
71       {
72         name: this.i18n('Pool'),
73         prop: 'pool',
74         flexGrow: 1
75       },
76       {
77         name: this.i18n('Total images'),
78         prop: 'num_images',
79         flexGrow: 1
80       }
81     ];
82     this.refresh();
83   }
84
85   refresh() {
86     this.poolService.list(['pool_name', 'type', 'application_metadata']).then((pools: any) => {
87       pools = pools.filter(
88         (pool: any) => this.rbdService.isRBDPool(pool) && pool.type === 'replicated'
89       );
90       const promisses: Observable<any>[] = [];
91       pools.forEach((pool: any) => {
92         promisses.push(this.rbdService.listNamespaces(pool['pool_name']));
93       });
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) => {
101               result.push({
102                 id: `${pool_name}/${namespace.namespace}`,
103                 pool: pool_name,
104                 namespace: namespace.namespace,
105                 num_images: namespace.num_images
106               });
107             });
108           }
109           this.namespaces = result;
110         });
111       } else {
112         this.namespaces = [];
113       }
114     });
115   }
116
117   updateSelection(selection: CdTableSelection) {
118     this.selection = selection;
119   }
120
121   createModal() {
122     this.modalRef = this.modalService.show(RbdNamespaceFormComponent);
123     this.modalRef.content.onSubmit.subscribe(() => {
124       this.refresh();
125     });
126   }
127
128   deleteModal() {
129     const pool = this.selection.first().pool;
130     const namespace = this.selection.first().namespace;
131     this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
132       initialState: {
133         itemDescription: 'Namespace',
134         itemNames: [`${pool}/${namespace}`],
135         submitAction: () =>
136           this.rbdService.deleteNamespace(pool, namespace).subscribe(
137             () => {
138               this.notificationService.show(
139                 NotificationType.success,
140                 this.i18n(`Deleted namespace '{{pool}}/{{namespace}}'`, {
141                   pool: pool,
142                   namespace: namespace
143                 })
144               );
145               this.modalRef.hide();
146               this.refresh();
147             },
148             () => {
149               this.modalRef.content.stopLoadingSpinner();
150             }
151           )
152       }
153     });
154   }
155
156   getDeleteDisableDesc(): string | undefined {
157     const first = this.selection.first();
158     if (first) {
159       if (first.num_images > 0) {
160         return this.i18n('Namespace contains images');
161       }
162     }
163
164     return undefined;
165   }
166 }