]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-ci.git/blob
7e5b064f37929604320cd593ae4dbf45af4e3cf1
[ceph-ci.git] /
1 import { Component, OnInit } from '@angular/core';
2 import { UntypedFormControl, Validators } from '@angular/forms';
3 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
4
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 { MAX_NAMESPACE, NvmeofService } from '~/app/shared/api/nvmeof.service';
14
15 @Component({
16   selector: 'cd-nvmeof-subsystems-form',
17   templateUrl: './nvmeof-subsystems-form.component.html',
18   styleUrls: ['./nvmeof-subsystems-form.component.scss']
19 })
20 export class NvmeofSubsystemsFormComponent implements OnInit {
21   permission: Permission;
22   subsystemForm: CdFormGroup;
23   action: string;
24   resource: string;
25   pageURL: string;
26   defaultMaxNamespace: number = MAX_NAMESPACE;
27   group: string;
28
29   constructor(
30     private authStorageService: AuthStorageService,
31     public actionLabels: ActionLabelsI18n,
32     public activeModal: NgbActiveModal,
33     private nvmeofService: NvmeofService,
34     private taskWrapperService: TaskWrapperService,
35     private router: Router,
36     private route: ActivatedRoute
37   ) {
38     this.permission = this.authStorageService.getPermissions().nvmeof;
39     this.resource = $localize`Subsystem`;
40     this.pageURL = 'block/nvmeof/subsystems';
41   }
42
43   DEFAULT_NQN = 'nqn.2001-07.com.ceph:' + Date.now();
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}$/;
46
47   customNQNValidator = CdValidators.custom(
48     'pattern',
49     (nqnInput: string) =>
50       !!nqnInput && !(this.NQN_REGEX.test(nqnInput) || this.NQN_REGEX_UUID.test(nqnInput))
51   );
52
53   ngOnInit() {
54     this.route.queryParams.subscribe((params) => {
55       this.group = params?.['group'];
56     });
57     this.createForm();
58     this.action = this.actionLabels.CREATE;
59   }
60
61   createForm() {
62     this.subsystemForm = new CdFormGroup({
63       nqn: new UntypedFormControl(this.DEFAULT_NQN, {
64         validators: [
65           this.customNQNValidator,
66           Validators.required,
67           this.customNQNValidator,
68           CdValidators.custom(
69             'maxLength',
70             (nqnInput: string) => new TextEncoder().encode(nqnInput).length > 223
71           )
72         ],
73         asyncValidators: [
74           CdValidators.unique(
75             this.nvmeofService.isSubsystemPresent,
76             this.nvmeofService,
77             null,
78             null,
79             this.group
80           )
81         ]
82       }),
83       max_namespaces: new UntypedFormControl(this.defaultMaxNamespace, {
84         validators: [
85           CdValidators.number(false),
86           Validators.max(this.defaultMaxNamespace),
87           Validators.min(1)
88         ]
89       })
90     });
91   }
92
93   onSubmit() {
94     const component = this;
95     const nqn: string = this.subsystemForm.getValue('nqn');
96     const max_namespaces: number = Number(this.subsystemForm.getValue('max_namespaces'));
97     let taskUrl = `nvmeof/subsystem/${URLVerbs.CREATE}`;
98
99     const request = {
100       nqn,
101       enable_ha: true,
102       gw_group: this.group,
103       max_namespaces
104     };
105
106     if (!max_namespaces) {
107       delete request.max_namespaces;
108     }
109     this.taskWrapperService
110       .wrapTaskAroundCall({
111         task: new FinishedTask(taskUrl, {
112           nqn: nqn
113         }),
114         call: this.nvmeofService.createSubsystem(request)
115       })
116       .subscribe({
117         error() {
118           component.subsystemForm.setErrors({ cdSubmitButton: true });
119         },
120         complete: () => {
121           this.router.navigate([this.pageURL, { outlets: { modal: null } }]);
122         }
123       });
124   }
125 }