]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-ci.git/blob
c725199d13f13029dff4f8cfb0635dde230280d6
[ceph-ci.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 { Permissions } 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, GroupsComboboxItem } 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 const BASE_URL = 'block/nvmeof/subsystems';
20 const DEFAULT_PLACEHOLDER = $localize`Enter group name`;
21
22 @Component({
23   selector: 'cd-nvmeof-subsystems',
24   templateUrl: './nvmeof-subsystems.component.html',
25   styleUrls: ['./nvmeof-subsystems.component.scss'],
26   standalone: false
27 })
28 export class NvmeofSubsystemsComponent extends ListWithDetails implements OnInit {
29   subsystems: NvmeofSubsystem[] = [];
30   subsystemsColumns: any;
31   permissions: Permissions;
32   selection = new CdTableSelection();
33   tableActions: CdTableAction[];
34   subsystemDetails: any[];
35   gwGroups: GroupsComboboxItem[] = [];
36   group: string = null;
37   gwGroupsEmpty: boolean = false;
38   gwGroupPlaceholder: string = DEFAULT_PLACEHOLDER;
39
40   constructor(
41     private nvmeofService: NvmeofService,
42     private authStorageService: AuthStorageService,
43     public actionLabels: ActionLabelsI18n,
44     private router: Router,
45     private modalService: ModalCdsService,
46     private taskWrapper: TaskWrapperService,
47     private route: ActivatedRoute
48   ) {
49     super();
50     this.permissions = this.authStorageService.getPermissions();
51   }
52
53   ngOnInit() {
54     this.route.queryParams.subscribe((params) => {
55       if (params?.['group']) this.onGroupSelection({ content: params?.['group'] });
56     });
57     this.setGatewayGroups();
58     this.subsystemsColumns = [
59       {
60         name: $localize`NQN`,
61         prop: 'nqn'
62       },
63       {
64         name: $localize`# Namespaces`,
65         prop: 'namespace_count'
66       },
67       {
68         name: $localize`# Maximum Allowed Namespaces`,
69         prop: 'max_namespaces'
70       }
71     ];
72     this.tableActions = [
73       {
74         name: this.actionLabels.CREATE,
75         permission: 'create',
76         icon: Icons.add,
77         click: () =>
78           this.router.navigate([BASE_URL, { outlets: { modal: [URLVerbs.CREATE] } }], {
79             queryParams: { group: this.group }
80           }),
81         canBePrimary: (selection: CdTableSelection) => !selection.hasSelection,
82         disable: () => !this.group
83       },
84       {
85         name: this.actionLabels.DELETE,
86         permission: 'delete',
87         icon: Icons.destroy,
88         click: () => this.deleteSubsystemModal()
89       }
90     ];
91   }
92
93   // Subsystems
94   updateSelection(selection: CdTableSelection) {
95     this.selection = selection;
96   }
97
98   getSubsystems() {
99     if (this.group) {
100       this.nvmeofService
101         .listSubsystems(this.group)
102         .subscribe((subsystems: NvmeofSubsystem[] | NvmeofSubsystem) => {
103           if (Array.isArray(subsystems)) this.subsystems = subsystems;
104           else this.subsystems = [subsystems];
105         });
106     } else {
107       this.subsystems = [];
108     }
109   }
110
111   deleteSubsystemModal() {
112     const subsystem = this.selection.first();
113     this.modalService.show(DeleteConfirmationModalComponent, {
114       itemDescription: 'Subsystem',
115       itemNames: [subsystem.nqn],
116       actionDescription: 'delete',
117       submitActionObservable: () =>
118         this.taskWrapper.wrapTaskAroundCall({
119           task: new FinishedTask('nvmeof/subsystem/delete', { nqn: subsystem.nqn }),
120           call: this.nvmeofService.deleteSubsystem(subsystem.nqn, this.group)
121         })
122     });
123   }
124
125   // Gateway groups
126   onGroupSelection(selected: GroupsComboboxItem) {
127     selected.selected = true;
128     this.group = selected.content;
129     this.getSubsystems();
130   }
131
132   onGroupClear() {
133     this.group = null;
134     this.getSubsystems();
135   }
136
137   setGatewayGroups() {
138     this.nvmeofService.listGatewayGroups().subscribe((response: CephServiceSpec[][]) => {
139       if (response?.[0]?.length) {
140         this.gwGroups = this.nvmeofService.formatGwGroupsList(response);
141       } else this.gwGroups = [];
142       // Select first group if no group is selected
143       if (!this.group && this.gwGroups.length) {
144         this.onGroupSelection(this.gwGroups[0]);
145         this.gwGroupsEmpty = false;
146         this.gwGroupPlaceholder = DEFAULT_PLACEHOLDER;
147       } else {
148         this.gwGroupsEmpty = true;
149         this.gwGroupPlaceholder = $localize`No groups available`;
150       }
151     });
152   }
153 }