1 import { Component, Input, OnChanges, OnInit, TemplateRef, ViewChild } from '@angular/core';
2 import { Router } from '@angular/router';
3 import { NvmeofService } from '~/app/shared/api/nvmeof.service';
4 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-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';
10 import { NvmeofSubsystemInitiator } from '~/app/shared/models/nvmeof';
11 import { Permission } from '~/app/shared/models/permissions';
12 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
13 import { ModalCdsService } from '~/app/shared/services/modal-cds.service';
14 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
16 const BASE_URL = 'block/nvmeof/subsystems';
19 selector: 'cd-nvmeof-initiators-list',
20 templateUrl: './nvmeof-initiators-list.component.html',
21 styleUrls: ['./nvmeof-initiators-list.component.scss']
23 export class NvmeofInitiatorsListComponent implements OnInit, OnChanges {
27 @ViewChild('hostTpl', { static: true })
28 hostTpl: TemplateRef<any>;
30 initiatorColumns: any;
31 tableActions: CdTableAction[];
32 selection = new CdTableSelection();
33 permission: Permission;
34 initiators: NvmeofSubsystemInitiator[] = [];
37 public actionLabels: ActionLabelsI18n,
38 private authStorageService: AuthStorageService,
39 private nvmeofService: NvmeofService,
40 private modalService: ModalCdsService,
41 private router: Router,
42 private taskWrapper: TaskWrapperService
44 this.permission = this.authStorageService.getPermissions().nvmeof;
48 this.initiatorColumns = [
50 name: $localize`Initiator`,
52 cellTemplate: this.hostTpl
57 name: this.actionLabels.ADD,
61 this.router.navigate([
63 { outlets: { modal: [URLVerbs.ADD, this.subsystemNQN, 'initiator'] } }
65 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
68 name: this.actionLabels.REMOVE,
71 click: () => this.removeInitiatorModal(),
72 disable: () => !this.selection.hasSelection,
73 canBePrimary: (selection: CdTableSelection) => selection.hasSelection
78 getAllowAllHostIndex() {
79 return this.selection.selected.findIndex((selected) => selected.nqn === '*');
83 this.listInitiators();
86 updateSelection(selection: CdTableSelection) {
87 this.selection = selection;
92 .getInitiators(this.subsystemNQN)
93 .subscribe((initiators: NvmeofSubsystemInitiator[]) => {
94 this.initiators = initiators;
99 return this.selection.selected.map((selected) => selected.nqn);
102 removeInitiatorModal() {
103 const hostNQNs = this.getSelectedNQNs();
104 const allowAllHostIndex = this.getAllowAllHostIndex();
105 const host_nqn = hostNQNs.join(',');
106 let itemNames = hostNQNs;
107 if (allowAllHostIndex !== -1) {
108 hostNQNs.splice(allowAllHostIndex, 1);
109 itemNames = [...hostNQNs, $localize`Allow any host(*)`];
111 this.modalService.show(CriticalConfirmationModalComponent, {
112 itemDescription: 'Initiator',
114 actionDescription: 'remove',
115 submitActionObservable: () =>
116 this.taskWrapper.wrapTaskAroundCall({
117 task: new FinishedTask('nvmeof/initiator/remove', {
118 nqn: this.subsystemNQN,
119 plural: itemNames.length > 1
121 call: this.nvmeofService.removeInitiators(this.subsystemNQN, { host_nqn })