]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
da099cb123337ba332560d1364989468051e74f4
[ceph-ci.git] /
1 import { Component, OnInit } from '@angular/core';
2
3 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
4 import { forkJoin, Observable } from 'rxjs';
5
6 import { PoolService } from '~/app/shared/api/pool.service';
7 import { RbdService } from '~/app/shared/api/rbd.service';
8 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
9 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
10 import { Icons } from '~/app/shared/enum/icons.enum';
11 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
12 import { CdTableAction } from '~/app/shared/models/cd-table-action';
13 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
14 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
15 import { Permission } from '~/app/shared/models/permissions';
16 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
17 import { NotificationService } from '~/app/shared/services/notification.service';
18 import { TaskListService } from '~/app/shared/services/task-list.service';
19 import { RbdNamespaceFormModalComponent } from '../rbd-namespace-form/rbd-namespace-form-modal.component';
20 import { ModalCdsService } from '~/app/shared/services/modal-cds.service';
21
22 @Component({
23   selector: 'cd-rbd-namespace-list',
24   templateUrl: './rbd-namespace-list.component.html',
25   styleUrls: ['./rbd-namespace-list.component.scss'],
26   providers: [TaskListService]
27 })
28 export class RbdNamespaceListComponent implements OnInit {
29   columns: CdTableColumn[];
30   namespaces: any;
31   modalRef: NgbModalRef;
32   permission: Permission;
33   selection = new CdTableSelection();
34   tableActions: CdTableAction[];
35
36   constructor(
37     private authStorageService: AuthStorageService,
38     private rbdService: RbdService,
39     private poolService: PoolService,
40     private notificationService: NotificationService,
41     public actionLabels: ActionLabelsI18n,
42     private cdsModalService: ModalCdsService
43   ) {
44     this.permission = this.authStorageService.getPermissions().rbdImage;
45     const createAction: CdTableAction = {
46       permission: 'create',
47       icon: Icons.add,
48       click: () => this.createModal(),
49       name: this.actionLabels.CREATE
50     };
51     const deleteAction: CdTableAction = {
52       permission: 'delete',
53       icon: Icons.destroy,
54       click: () => this.deleteModal(),
55       name: this.actionLabels.DELETE,
56       disable: () => this.getDeleteDisableDesc()
57     };
58     this.tableActions = [createAction, deleteAction];
59   }
60
61   ngOnInit() {
62     this.columns = [
63       {
64         name: $localize`Namespace`,
65         prop: 'namespace',
66         flexGrow: 1
67       },
68       {
69         name: $localize`Pool`,
70         prop: 'pool',
71         flexGrow: 1
72       },
73       {
74         name: $localize`Total images`,
75         prop: 'num_images',
76         flexGrow: 1
77       }
78     ];
79     this.refresh();
80   }
81
82   refresh() {
83     this.poolService.list(['pool_name', 'type', 'application_metadata']).then((pools: any) => {
84       pools = pools.filter(
85         (pool: any) => this.rbdService.isRBDPool(pool) && pool.type === 'replicated'
86       );
87       const promises: Observable<any>[] = [];
88       pools.forEach((pool: any) => {
89         promises.push(this.rbdService.listNamespaces(pool['pool_name']));
90       });
91       if (promises.length > 0) {
92         forkJoin(promises).subscribe((data: Array<Array<string>>) => {
93           const result: any[] = [];
94           for (let i = 0; i < data.length; i++) {
95             const namespaces = data[i];
96             const pool_name = pools[i]['pool_name'];
97             namespaces.forEach((namespace: any) => {
98               result.push({
99                 id: `${pool_name}/${namespace.namespace}`,
100                 pool: pool_name,
101                 namespace: namespace.namespace,
102                 num_images: namespace.num_images
103               });
104             });
105           }
106           this.namespaces = result;
107         });
108       } else {
109         this.namespaces = [];
110       }
111     });
112   }
113
114   updateSelection(selection: CdTableSelection) {
115     this.selection = selection;
116   }
117
118   createModal() {
119     const modalRef = this.cdsModalService.show(RbdNamespaceFormModalComponent);
120     modalRef.onSubmit?.subscribe(() => this.refresh());
121   }
122
123   deleteModal() {
124     const pool = this.selection.first().pool;
125     const namespace = this.selection.first().namespace;
126     const modalRef = this.cdsModalService.show(CriticalConfirmationModalComponent, {
127       itemDescription: 'Namespace',
128       itemNames: [`${pool}/${namespace}`],
129       submitAction: () =>
130         this.rbdService.deleteNamespace(pool, namespace).subscribe(
131           () => {
132             this.notificationService.show(
133               NotificationType.success,
134               $localize`Deleted namespace '${pool}/${namespace}'`
135             );
136             this.cdsModalService.dismissAll();
137             this.refresh();
138           },
139           () => {
140             this.cdsModalService.stopLoadingSpinner(modalRef.deletionForm);
141           }
142         )
143     });
144   }
145
146   getDeleteDisableDesc(): string | boolean {
147     const first = this.selection.first();
148
149     if (first?.num_images > 0) {
150       return $localize`Namespace contains images`;
151     }
152
153     return !this.selection?.first();
154   }
155 }