]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
8d449f3ce79fd299fcd89ef25e43e168f0755593
[ceph.git] /
1 import { Component, OnInit } from '@angular/core';
2
3 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
4 import * as _ from 'lodash';
5 import { forkJoin, Observable } from 'rxjs';
6
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';
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: NgbModalRef;
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: ModalService,
42     private notificationService: NotificationService,
43     public actionLabels: ActionLabelsI18n
44   ) {
45     this.permission = this.authStorageService.getPermissions().rbdImage;
46     const createAction: CdTableAction = {
47       permission: 'create',
48       icon: Icons.add,
49       click: () => this.createModal(),
50       name: this.actionLabels.CREATE
51     };
52     const deleteAction: CdTableAction = {
53       permission: 'delete',
54       icon: Icons.destroy,
55       click: () => this.deleteModal(),
56       name: this.actionLabels.DELETE,
57       disable: () => !this.selection.first() || !_.isUndefined(this.getDeleteDisableDesc()),
58       disableDesc: () => this.getDeleteDisableDesc()
59     };
60     this.tableActions = [createAction, deleteAction];
61   }
62
63   ngOnInit() {
64     this.columns = [
65       {
66         name: $localize`Namespace`,
67         prop: 'namespace',
68         flexGrow: 1
69       },
70       {
71         name: $localize`Pool`,
72         prop: 'pool',
73         flexGrow: 1
74       },
75       {
76         name: $localize`Total images`,
77         prop: 'num_images',
78         flexGrow: 1
79       }
80     ];
81     this.refresh();
82   }
83
84   refresh() {
85     this.poolService.list(['pool_name', 'type', 'application_metadata']).then((pools: any) => {
86       pools = pools.filter(
87         (pool: any) => this.rbdService.isRBDPool(pool) && pool.type === 'replicated'
88       );
89       const promisses: Observable<any>[] = [];
90       pools.forEach((pool: any) => {
91         promisses.push(this.rbdService.listNamespaces(pool['pool_name']));
92       });
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) => {
100               result.push({
101                 id: `${pool_name}/${namespace.namespace}`,
102                 pool: pool_name,
103                 namespace: namespace.namespace,
104                 num_images: namespace.num_images
105               });
106             });
107           }
108           this.namespaces = result;
109         });
110       } else {
111         this.namespaces = [];
112       }
113     });
114   }
115
116   updateSelection(selection: CdTableSelection) {
117     this.selection = selection;
118   }
119
120   createModal() {
121     this.modalRef = this.modalService.show(RbdNamespaceFormModalComponent);
122     this.modalRef.componentInstance.onSubmit.subscribe(() => {
123       this.refresh();
124     });
125   }
126
127   deleteModal() {
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}`],
133       submitAction: () =>
134         this.rbdService.deleteNamespace(pool, namespace).subscribe(
135           () => {
136             this.notificationService.show(
137               NotificationType.success,
138               $localize`Deleted namespace '${pool}/${namespace}'`
139             );
140             this.modalRef.close();
141             this.refresh();
142           },
143           () => {
144             this.modalRef.componentInstance.stopLoadingSpinner();
145           }
146         )
147     });
148   }
149
150   getDeleteDisableDesc(): string | undefined {
151     const first = this.selection.first();
152     if (first) {
153       if (first.num_images > 0) {
154         return $localize`Namespace contains images`;
155       }
156     }
157
158     return undefined;
159   }
160 }