]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
5debb52c4d3abb565c94f99d24c9a06ca6e80b0f
[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 { Router } from '@angular/router';
13 import { 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
27   constructor(
28     private authStorageService: AuthStorageService,
29     public actionLabels: ActionLabelsI18n,
30     public activeModal: NgbActiveModal,
31     private nvmeofService: NvmeofService,
32     private taskWrapperService: TaskWrapperService,
33     private router: Router
34   ) {
35     this.permission = this.authStorageService.getPermissions().nvmeof;
36     this.resource = $localize`Subsystem`;
37     this.pageURL = 'block/nvmeof/subsystems';
38   }
39
40   DEFAULT_NQN = 'nqn.2001-07.com.ceph:' + Date.now();
41   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-\.]+)*)$/;
42   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}$/;
43
44   customNQNValidator = CdValidators.custom(
45     'pattern',
46     (nqnInput: string) =>
47       !!nqnInput && !(this.NQN_REGEX.test(nqnInput) || this.NQN_REGEX_UUID.test(nqnInput))
48   );
49
50   ngOnInit() {
51     this.createForm();
52     this.action = this.actionLabels.CREATE;
53   }
54
55   createForm() {
56     this.subsystemForm = new CdFormGroup({
57       nqn: new UntypedFormControl(this.DEFAULT_NQN, {
58         validators: [
59           this.customNQNValidator,
60           Validators.required,
61           this.customNQNValidator,
62           CdValidators.custom(
63             'maxLength',
64             (nqnInput: string) => new TextEncoder().encode(nqnInput).length > 223
65           )
66         ],
67         asyncValidators: [
68           CdValidators.unique(this.nvmeofService.isSubsystemPresent, this.nvmeofService)
69         ]
70       }),
71       max_namespaces: new UntypedFormControl(256, {
72         validators: [CdValidators.number(false), Validators.max(256), Validators.min(1)]
73       })
74     });
75   }
76
77   onSubmit() {
78     const component = this;
79     const nqn: string = this.subsystemForm.getValue('nqn');
80     const max_namespaces: number = Number(this.subsystemForm.getValue('max_namespaces'));
81     let taskUrl = `nvmeof/subsystem/${URLVerbs.CREATE}`;
82
83     const request = {
84       nqn,
85       max_namespaces,
86       enable_ha: true
87     };
88
89     if (!max_namespaces) {
90       delete request.max_namespaces;
91     }
92     this.taskWrapperService
93       .wrapTaskAroundCall({
94         task: new FinishedTask(taskUrl, {
95           nqn: nqn
96         }),
97         call: this.nvmeofService.createSubsystem(request)
98       })
99       .subscribe({
100         error() {
101           component.subsystemForm.setErrors({ cdSubmitButton: true });
102         },
103         complete: () => {
104           this.router.navigate([this.pageURL, { outlets: { modal: null } }]);
105         }
106       });
107   }
108 }