]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
cd66c7cf9c476a082b234001321870fc9f97389a
[ceph-ci.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.getDeleteDisableDesc()
58     };
59     this.tableActions = [createAction, deleteAction];
60   }
61
62   ngOnInit() {
63     this.columns = [
64       {
65         name: $localize`Namespace`,
66         prop: 'namespace',
67         flexGrow: 1
68       },
69       {
70         name: $localize`Pool`,
71         prop: 'pool',
72         flexGrow: 1
73       },
74       {
75         name: $localize`Total images`,
76         prop: 'num_images',
77         flexGrow: 1
78       }
79     ];
80     this.refresh();
81   }
82
83   refresh() {
84     this.poolService.list(['pool_name', 'type', 'application_metadata']).then((pools: any) => {
85       pools = pools.filter(
86         (pool: any) => this.rbdService.isRBDPool(pool) && pool.type === 'replicated'
87       );
88       const promisses: Observable<any>[] = [];
89       pools.forEach((pool: any) => {
90         promisses.push(this.rbdService.listNamespaces(pool['pool_name']));
91       });
92       if (promisses.length > 0) {
93         forkJoin(promisses).subscribe((data: Array<Array<string>>) => {
94           const result: any[] = [];
95           for (let i = 0; i < data.length; i++) {
96             const namespaces = data[i];
97             const pool_name = pools[i]['pool_name'];
98             namespaces.forEach((namespace: any) => {
99               result.push({
100                 id: `${pool_name}/${namespace.namespace}`,
101                 pool: pool_name,
102                 namespace: namespace.namespace,
103                 num_images: namespace.num_images
104               });
105             });
106           }
107           this.namespaces = result;
108         });
109       } else {
110         this.namespaces = [];
111       }
112     });
113   }
114
115   updateSelection(selection: CdTableSelection) {
116     this.selection = selection;
117   }
118
119   createModal() {
120     this.modalRef = this.modalService.show(RbdNamespaceFormModalComponent);
121     this.modalRef.componentInstance.onSubmit.subscribe(() => {
122       this.refresh();
123     });
124   }
125
126   deleteModal() {
127     const pool = this.selection.first().pool;
128     const namespace = this.selection.first().namespace;
129     this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
130       itemDescription: 'Namespace',
131       itemNames: [`${pool}/${namespace}`],
132       submitAction: () =>
133         this.rbdService.deleteNamespace(pool, namespace).subscribe(
134           () => {
135             this.notificationService.show(
136               NotificationType.success,
137               $localize`Deleted namespace '${pool}/${namespace}'`
138             );
139             this.modalRef.close();
140             this.refresh();
141           },
142           () => {
143             this.modalRef.componentInstance.stopLoadingSpinner();
144           }
145         )
146     });
147   }
148
149   getDeleteDisableDesc(): string | boolean {
150     const first = this.selection.first();
151
152     if (first?.num_images > 0) {
153       return $localize`Namespace contains images`;
154     }
155
156     return !this.selection?.first();
157   }
158 }