]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
7d712ba2d5cd797e851ea0e52dd56b401925cef4
[ceph.git] /
1 import { Component, OnInit } from '@angular/core';
2 import { ActivatedRoute, Router } from '@angular/router';
3
4 import { ActionLabelsI18n, URLVerbs } from '~/app/shared/constants/app.constants';
5 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
6 import { NvmeofSubsystem } from '~/app/shared/models/nvmeof';
7 import { Permission } from '~/app/shared/models/permissions';
8 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
9 import { ListWithDetails } from '~/app/shared/classes/list-with-details.class';
10 import { CdTableAction } from '~/app/shared/models/cd-table-action';
11 import { Icons } from '~/app/shared/enum/icons.enum';
12 import { DeleteConfirmationModalComponent } from '~/app/shared/components/delete-confirmation-modal/delete-confirmation-modal.component';
13 import { FinishedTask } from '~/app/shared/models/finished-task';
14 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
15 import { NvmeofService } from '~/app/shared/api/nvmeof.service';
16 import { ModalCdsService } from '~/app/shared/services/modal-cds.service';
17 import { CephServiceSpec } from '~/app/shared/models/service.interface';
18
19 type ComboBoxItem = {
20   content: string;
21   selected?: boolean;
22 };
23
24 const BASE_URL = 'block/nvmeof/subsystems';
25
26 @Component({
27   selector: 'cd-nvmeof-subsystems',
28   templateUrl: './nvmeof-subsystems.component.html',
29   styleUrls: ['./nvmeof-subsystems.component.scss']
30 })
31 export class NvmeofSubsystemsComponent extends ListWithDetails implements OnInit {
32   subsystems: NvmeofSubsystem[] = [];
33   subsystemsColumns: any;
34   permission: Permission;
35   selection = new CdTableSelection();
36   tableActions: CdTableAction[];
37   subsystemDetails: any[];
38   gwGroups: ComboBoxItem[] = [];
39   group: string = null;
40
41   constructor(
42     private nvmeofService: NvmeofService,
43     private authStorageService: AuthStorageService,
44     public actionLabels: ActionLabelsI18n,
45     private router: Router,
46     private modalService: ModalCdsService,
47     private taskWrapper: TaskWrapperService,
48     private route: ActivatedRoute
49   ) {
50     super();
51     this.permission = this.authStorageService.getPermissions().nvmeof;
52   }
53
54   ngOnInit() {
55     this.route.queryParams.subscribe((params) => {
56       if (params?.['group']) this.onGroupSelection({ content: params?.['group'] });
57     });
58     this.getGatewayGroups();
59     this.subsystemsColumns = [
60       {
61         name: $localize`NQN`,
62         prop: 'nqn'
63       },
64       {
65         name: $localize`# Namespaces`,
66         prop: 'namespace_count'
67       },
68       {
69         name: $localize`# Maximum Allowed Namespaces`,
70         prop: 'max_namespaces'
71       }
72     ];
73     this.tableActions = [
74       {
75         name: this.actionLabels.CREATE,
76         permission: 'create',
77         icon: Icons.add,
78         click: () =>
79           this.router.navigate([BASE_URL, { outlets: { modal: [URLVerbs.CREATE] } }], {
80             queryParams: { group: this.group }
81           }),
82         canBePrimary: (selection: CdTableSelection) => !selection.hasSelection,
83         disable: () => !this.group
84       },
85       {
86         name: this.actionLabels.DELETE,
87         permission: 'delete',
88         icon: Icons.destroy,
89         click: () => this.deleteSubsystemModal()
90       }
91     ];
92   }
93
94   // Subsystems
95   updateSelection(selection: CdTableSelection) {
96     this.selection = selection;
97   }
98
99   getSubsystems() {
100     if (this.group) {
101       this.nvmeofService
102         .listSubsystems(this.group)
103         .subscribe((subsystems: NvmeofSubsystem[] | NvmeofSubsystem) => {
104           if (Array.isArray(subsystems)) this.subsystems = subsystems;
105           else this.subsystems = [subsystems];
106         });
107     } else {
108       this.subsystems = [];
109     }
110   }
111
112   deleteSubsystemModal() {
113     const subsystem = this.selection.first();
114     this.modalService.show(DeleteConfirmationModalComponent, {
115       itemDescription: 'Subsystem',
116       itemNames: [subsystem.nqn],
117       actionDescription: 'delete',
118       submitActionObservable: () =>
119         this.taskWrapper.wrapTaskAroundCall({
120           task: new FinishedTask('nvmeof/subsystem/delete', { nqn: subsystem.nqn }),
121           call: this.nvmeofService.deleteSubsystem(subsystem.nqn, this.group)
122         })
123     });
124   }
125
126   // Gateway groups
127   onGroupSelection(selected: ComboBoxItem) {
128     selected.selected = true;
129     this.group = selected.content;
130     this.getSubsystems();
131   }
132
133   onGroupClear() {
134     this.group = null;
135     this.getSubsystems();
136   }
137
138   getGatewayGroups() {
139     this.nvmeofService.listGatewayGroups().subscribe((response: CephServiceSpec[][]) => {
140       if (response?.[0].length) {
141         this.gwGroups = response[0].map((group: CephServiceSpec) => {
142           return {
143             content: group?.spec?.group
144           };
145         });
146       }
147       // Select first group if no group is selected
148       if (!this.group && this.gwGroups.length) this.onGroupSelection(this.gwGroups[0]);
149     });
150   }
151 }