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