1 import { Component, Input, OnInit, TemplateRef, ViewChild } from '@angular/core';
2 import { ActivatedRoute, Router } from '@angular/router';
3 import { NvmeofService } from '~/app/shared/api/nvmeof.service';
4 import { DeleteConfirmationModalComponent } from '~/app/shared/components/delete-confirmation-modal/delete-confirmation-modal.component';
5 import { ActionLabelsI18n, URLVerbs } from '~/app/shared/constants/app.constants';
6 import { Icons } from '~/app/shared/enum/icons.enum';
7 import { CdTableAction } from '~/app/shared/models/cd-table-action';
8 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
9 import { FinishedTask } from '~/app/shared/models/finished-task';
12 NvmeofSubsystemInitiator,
13 getSubsystemAuthStatus
14 } from '~/app/shared/models/nvmeof';
15 import { Permission } from '~/app/shared/models/permissions';
16 import { NvmeofSubsystemAuthType } from '~/app/shared/enum/nvmeof.enum';
17 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
18 import { ModalCdsService } from '~/app/shared/services/modal-cds.service';
19 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
21 const BASE_URL = 'block/nvmeof/subsystems';
24 selector: 'cd-nvmeof-initiators-list',
25 templateUrl: './nvmeof-initiators-list.component.html',
26 styleUrls: ['./nvmeof-initiators-list.component.scss'],
29 export class NvmeofInitiatorsListComponent implements OnInit {
35 @ViewChild('dhchapTpl', { static: true })
36 dhchapTpl: TemplateRef<any>;
38 initiatorColumns: any;
39 tableActions: CdTableAction[];
40 selection = new CdTableSelection();
41 permission: Permission;
42 initiators: NvmeofSubsystemInitiator[] = [];
43 subsystem: NvmeofSubsystem;
45 authType = NvmeofSubsystemAuthType;
48 public actionLabels: ActionLabelsI18n,
49 private authStorageService: AuthStorageService,
50 private nvmeofService: NvmeofService,
51 private modalService: ModalCdsService,
52 private router: Router,
53 private taskWrapper: TaskWrapperService,
54 private route: ActivatedRoute
56 this.permission = this.authStorageService.getPermissions().nvmeof;
60 if (!this.subsystemNQN || !this.group) {
61 this.route.parent?.params.subscribe((params) => {
62 if (params['subsystem_nqn']) {
63 this.subsystemNQN = params['subsystem_nqn'];
67 this.route.queryParams.subscribe((qp) => {
69 this.group = qp['group'];
77 this.initiatorColumns = [
79 name: $localize`Host NQN`,
83 name: $localize`DHCHAP key`,
85 cellTemplate: this.dhchapTpl
90 name: this.actionLabels.ADD,
95 [BASE_URL, { outlets: { modal: [URLVerbs.ADD, this.subsystemNQN, 'initiator'] } }],
96 { queryParams: { group: this.group } }
98 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection,
99 disable: () => this.hasAllHostsAllowed()
102 name: this.actionLabels.REMOVE,
103 permission: 'delete',
105 click: () => this.removeInitiatorModal(),
106 disable: () => !this.selection.hasSelection,
107 canBePrimary: (selection: CdTableSelection) => selection.hasSelection
112 private fetchIfReady() {
113 if (this.subsystemNQN && this.group) {
114 this.listInitiators();
119 getAllowAllHostIndex() {
120 return this.selection.selected.findIndex((selected) => selected.nqn === '*');
123 hasAllHostsAllowed(): boolean {
124 return this.initiators.some((initiator) => initiator.nqn === '*');
128 this.router.navigate(
129 [BASE_URL, { outlets: { modal: [URLVerbs.ADD, this.subsystemNQN, 'initiator'] } }],
130 { queryParams: { group: this.group } }
134 updateSelection(selection: CdTableSelection) {
135 this.selection = selection;
140 .getInitiators(this.subsystemNQN, this.group)
141 .subscribe((initiators: NvmeofSubsystemInitiator[]) => {
142 this.initiators = initiators;
143 this.updateAuthStatus();
148 this.nvmeofService.getSubsystem(this.subsystemNQN, this.group).subscribe((subsystem: any) => {
149 this.subsystem = subsystem;
150 this.updateAuthStatus();
155 if (this.subsystem && this.initiators) {
156 this.authStatus = getSubsystemAuthStatus(this.subsystem, this.initiators);
161 return this.selection.selected.map((selected) => selected.nqn);
164 removeInitiatorModal() {
165 const hostNQNs = this.getSelectedNQNs();
166 const allowAllHostIndex = this.getAllowAllHostIndex();
167 const host_nqn = hostNQNs.join(',');
168 let itemNames = hostNQNs;
169 if (allowAllHostIndex !== -1) {
170 hostNQNs.splice(allowAllHostIndex, 1);
171 itemNames = [...hostNQNs, $localize`Allow any host(*)`];
173 this.modalService.show(DeleteConfirmationModalComponent, {
174 itemDescription: 'Initiator',
176 actionDescription: 'remove',
177 submitActionObservable: () =>
178 this.taskWrapper.wrapTaskAroundCall({
179 task: new FinishedTask('nvmeof/initiator/remove', {
180 nqn: this.subsystemNQN,
181 plural: itemNames.length > 1
183 call: this.nvmeofService.removeSubsystemInitiators(this.subsystemNQN, {