]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
8e7812d7194125c26e4d8397c8537b16f726986b
[ceph.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 { ModalService } from '~/app/shared/services/modal.service';
18 import { NotificationService } from '~/app/shared/services/notification.service';
19 import { TaskListService } from '~/app/shared/services/task-list.service';
20 import { RbdNamespaceFormModalComponent } from '../rbd-namespace-form/rbd-namespace-form-modal.component';
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 modalService: ModalService,
41     private notificationService: NotificationService,
42     public actionLabels: ActionLabelsI18n
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     this.modalRef = this.modalService.show(RbdNamespaceFormModalComponent);
120     this.modalRef.componentInstance.onSubmit.subscribe(() => {
121       this.refresh();
122     });
123   }
124
125   deleteModal() {
126     const pool = this.selection.first().pool;
127     const namespace = this.selection.first().namespace;
128     this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
129       itemDescription: 'Namespace',
130       itemNames: [`${pool}/${namespace}`],
131       submitAction: () =>
132         this.rbdService.deleteNamespace(pool, namespace).subscribe(
133           () => {
134             this.notificationService.show(
135               NotificationType.success,
136               $localize`Deleted namespace '${pool}/${namespace}'`
137             );
138             this.modalRef.close();
139             this.refresh();
140           },
141           () => {
142             this.modalRef.componentInstance.stopLoadingSpinner();
143           }
144         )
145     });
146   }
147
148   getDeleteDisableDesc(): string | boolean {
149     const first = this.selection.first();
150
151     if (first?.num_images > 0) {
152       return $localize`Namespace contains images`;
153     }
154
155     return !this.selection?.first();
156   }
157 }