1 import { Component, OnInit } from '@angular/core';
2 import { Step } from 'carbon-components-angular';
3 import { InitiatorRequest, NvmeofService } from '~/app/shared/api/nvmeof.service';
4 import { FinishedTask } from '~/app/shared/models/finished-task';
5 import { HOST_TYPE, NvmeofSubsystemInitiator } from '~/app/shared/models/nvmeof';
6 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
7 import { ActivatedRoute, Router } from '@angular/router';
8 import { SubsystemPayload } from '../nvmeof-subsystems-form/nvmeof-subsystems-form.component';
11 selector: 'cd-nvmeof-initiators-form',
12 templateUrl: './nvmeof-initiators-form.component.html',
13 styleUrls: ['./nvmeof-initiators-form.component.scss'],
16 export class NvmeofInitiatorsFormComponent implements OnInit {
18 subsystemNQN!: string;
19 isSubmitLoading = false;
20 existingHosts: string[] = [];
24 label: $localize`Host access control`,
29 title = $localize`Add Initiator`;
30 description = $localize`Allow specific hosts to run NVMe/TCP commands to the NVMe subsystem.`;
31 pageURL = 'block/nvmeof/subsystems';
34 private nvmeofService: NvmeofService,
35 private taskWrapperService: TaskWrapperService,
36 private router: Router,
37 private route: ActivatedRoute
41 this.route.queryParams.subscribe((params) => {
42 this.group = params?.['group'];
44 this.route.parent.params.subscribe((params: any) => {
45 if (params.subsystem_nqn) {
46 this.subsystemNQN = params.subsystem_nqn;
49 this.route.params.subscribe((params: any) => {
50 if (!this.subsystemNQN && params.subsystem_nqn) {
51 this.subsystemNQN = params.subsystem_nqn;
53 this.fetchExistingHosts();
57 fetchExistingHosts() {
58 if (!this.subsystemNQN || !this.group) return;
60 .getInitiators(this.subsystemNQN, this.group)
61 .subscribe((response: NvmeofSubsystemInitiator[] | { hosts: NvmeofSubsystemInitiator[] }) => {
62 const initiators = Array.isArray(response) ? response : response?.hosts || [];
63 this.existingHosts = initiators.map((i) => i.nqn);
67 onSubmit(payload: SubsystemPayload) {
68 this.isSubmitLoading = true;
69 const taskUrl = `nvmeof/initiator/add`;
71 const request: InitiatorRequest = {
72 host_nqn: payload.hostType === HOST_TYPE.ALL ? '*' : payload.addedHosts.join(','),
75 this.taskWrapperService
77 task: new FinishedTask(taskUrl, {
78 nqn: this.subsystemNQN
80 call: this.nvmeofService.addInitiators(this.subsystemNQN, request)
84 this.isSubmitLoading = false;
88 this.isSubmitLoading = false;
89 this.router.navigate([{ outlets: { modal: null } }], {
90 relativeTo: this.route.parent,
91 queryParamsHandling: 'preserve'