1 import { Component, OnInit } from '@angular/core';
2 import { UntypedFormArray, UntypedFormControl, Validators } from '@angular/forms';
4 import { CdFormBuilder } from '~/app/shared/forms/cd-form-builder';
5 import { ActionLabelsI18n, URLVerbs } from '~/app/shared/constants/app.constants';
6 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
7 import { CdValidators } from '~/app/shared/forms/cd-validators';
8 import { Permission } from '~/app/shared/models/permissions';
9 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
10 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
11 import { FinishedTask } from '~/app/shared/models/finished-task';
12 import { ActivatedRoute, Router } from '@angular/router';
13 import { NvmeofService } from '~/app/shared/api/nvmeof.service';
16 selector: 'cd-nvmeof-initiators-form',
17 templateUrl: './nvmeof-initiators-form.component.html',
18 styleUrls: ['./nvmeof-initiators-form.component.scss']
20 export class NvmeofInitiatorsFormComponent implements OnInit {
21 permission: Permission;
22 initiatorForm: CdFormGroup;
26 remove: boolean = false;
28 removeHosts: { name: string; value: boolean; id: number }[] = [];
31 private authStorageService: AuthStorageService,
32 public actionLabels: ActionLabelsI18n,
33 private nvmeofService: NvmeofService,
34 private taskWrapperService: TaskWrapperService,
35 private router: Router,
36 private route: ActivatedRoute,
37 private formBuilder: CdFormBuilder
39 this.permission = this.authStorageService.getPermissions().nvmeof;
40 this.resource = $localize`Initiator`;
41 this.pageURL = 'block/nvmeof/subsystems';
44 NQN_REGEX = /^nqn\.(19|20)\d\d-(0[1-9]|1[0-2])\.\D{2,3}(\.[A-Za-z0-9-]+)+(:[A-Za-z0-9-\.]+(:[A-Za-z0-9-\.]+)*)$/;
45 NQN_REGEX_UUID = /^nqn\.2014-08\.org\.nvmexpress:uuid:[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
48 customNQNValidator = CdValidators.custom(
51 !!nqnInput && !(this.NQN_REGEX.test(nqnInput) || this.NQN_REGEX_UUID.test(nqnInput))
56 this.action = this.actionLabels.ADD;
57 this.route.params.subscribe((params: { subsystem_nqn: string }) => {
58 this.subsystemNQN = params.subsystem_nqn;
63 this.initiatorForm = new CdFormGroup({
64 allowAnyHost: new UntypedFormControl(false),
65 addHost: new CdFormGroup({
66 addHostCheck: new UntypedFormControl(false),
67 addedHosts: this.formBuilder.array(
72 (hosts: string[]) => !!hosts.length && new Set(hosts)?.size !== hosts.length
80 get addedHosts(): UntypedFormArray {
81 return this.initiatorForm.get('addHost.addedHosts') as UntypedFormArray;
86 newHostFormGroup = this.formBuilder.control('', [this.customNQNValidator, Validators.required]);
87 this.addedHosts.push(newHostFormGroup);
90 removeHost(index: number) {
91 this.addedHosts.removeAt(index);
95 const addHostCheck = this.initiatorForm.get('addHost.addHostCheck').value;
97 while (this.addedHosts.length !== 0) {
98 this.addedHosts.removeAt(0);
106 const component = this;
107 const allowAnyHost: boolean = this.initiatorForm.getValue('allowAnyHost');
108 const hosts: string[] = this.addedHosts.value;
109 let taskUrl = `nvmeof/initiator/${URLVerbs.ADD}`;
112 host_nqn: hosts.join(',')
117 request['host_nqn'] = hosts.join(',');
119 this.taskWrapperService
120 .wrapTaskAroundCall({
121 task: new FinishedTask(taskUrl, {
122 nqn: this.subsystemNQN
124 call: this.nvmeofService.addInitiators(this.subsystemNQN, request)
128 component.initiatorForm.setErrors({ cdSubmitButton: true });
131 this.router.navigate([this.pageURL, { outlets: { modal: null } }]);