]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-ci.git/blob
6f6b8f8896f4725ce969c53c6ae7f2af8bfd1f1e
[ceph-ci.git] /
1 import { Component, OnInit } from '@angular/core';
2 import { UntypedFormArray, UntypedFormControl, Validators } from '@angular/forms';
3
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 { Icons } from '~/app/shared/enum/icons.enum';
9 import { Permission } from '~/app/shared/models/permissions';
10 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
11 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
12 import { FinishedTask } from '~/app/shared/models/finished-task';
13 import { ActivatedRoute, Router } from '@angular/router';
14 import { InitiatorRequest, NvmeofService } from '~/app/shared/api/nvmeof.service';
15
16 @Component({
17   selector: 'cd-nvmeof-initiators-form',
18   templateUrl: './nvmeof-initiators-form.component.html',
19   styleUrls: ['./nvmeof-initiators-form.component.scss'],
20   standalone: false
21 })
22 export class NvmeofInitiatorsFormComponent implements OnInit {
23   icons = Icons;
24   permission: Permission;
25   initiatorForm: CdFormGroup;
26   action: string;
27   resource: string;
28   pageURL: string;
29   remove: boolean = false;
30   subsystemNQN: string;
31   removeHosts: { name: string; value: boolean; id: number }[] = [];
32   group: string;
33
34   constructor(
35     private authStorageService: AuthStorageService,
36     public actionLabels: ActionLabelsI18n,
37     private nvmeofService: NvmeofService,
38     private taskWrapperService: TaskWrapperService,
39     private router: Router,
40     private route: ActivatedRoute,
41     private formBuilder: CdFormBuilder
42   ) {
43     this.permission = this.authStorageService.getPermissions().nvmeof;
44     this.resource = $localize`Initiator`;
45     this.pageURL = 'block/nvmeof/subsystems';
46   }
47
48   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-\.]+)*)$/;
49   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}$/;
50   ALLOW_ALL_HOST = '*';
51
52   customNQNValidator = CdValidators.custom(
53     'pattern',
54     (nqnInput: string) =>
55       !!nqnInput && !(this.NQN_REGEX.test(nqnInput) || this.NQN_REGEX_UUID.test(nqnInput))
56   );
57
58   ngOnInit() {
59     this.route.queryParams.subscribe((params) => {
60       this.group = params?.['group'];
61     });
62     this.createForm();
63     this.action = this.actionLabels.ADD;
64     this.route.params.subscribe((params: { subsystem_nqn: string }) => {
65       this.subsystemNQN = params.subsystem_nqn;
66     });
67   }
68
69   createForm() {
70     this.initiatorForm = new CdFormGroup({
71       allowAnyHost: new UntypedFormControl(false),
72       addHost: new CdFormGroup({
73         addHostCheck: new UntypedFormControl(false),
74         addedHosts: this.formBuilder.array(
75           [],
76           [
77             CdValidators.custom(
78               'duplicate',
79               (hosts: string[]) => !!hosts.length && new Set(hosts)?.size !== hosts.length
80             )
81           ]
82         )
83       })
84     });
85   }
86
87   get addedHosts(): UntypedFormArray {
88     return this.initiatorForm.get('addHost.addedHosts') as UntypedFormArray;
89   }
90
91   addHost() {
92     let newHostFormGroup;
93     newHostFormGroup = this.formBuilder.control('', [this.customNQNValidator, Validators.required]);
94     this.addedHosts.push(newHostFormGroup);
95   }
96
97   removeHost(index: number) {
98     this.addedHosts.removeAt(index);
99   }
100
101   setAddHostCheck() {
102     const addHostCheck = this.initiatorForm.get('addHost.addHostCheck').value;
103     if (!addHostCheck) {
104       while (this.addedHosts.length !== 0) {
105         this.addedHosts.removeAt(0);
106       }
107     } else {
108       this.addHost();
109     }
110   }
111
112   onSubmit() {
113     const component = this;
114     const allowAnyHost: boolean = this.initiatorForm.getValue('allowAnyHost');
115     const hosts: string[] = this.addedHosts.value;
116     let taskUrl = `nvmeof/initiator/${URLVerbs.ADD}`;
117
118     const request: InitiatorRequest = {
119       host_nqn: hosts.join(','),
120       gw_group: this.group
121     };
122
123     if (allowAnyHost) {
124       hosts.push('*');
125       request['host_nqn'] = hosts.join(',');
126     }
127     this.taskWrapperService
128       .wrapTaskAroundCall({
129         task: new FinishedTask(taskUrl, {
130           nqn: this.subsystemNQN
131         }),
132         call: this.nvmeofService.addSubsystemInitiators(this.subsystemNQN, request)
133       })
134       .subscribe({
135         error() {
136           component.initiatorForm.setErrors({ cdSubmitButton: true });
137         },
138         complete: () => {
139           this.router.navigate([this.pageURL, { outlets: { modal: null } }]);
140         }
141       });
142   }
143 }