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