]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
b65ad62bdb4b14223519a17f1cd3db2760a7a36e
[ceph-ci.git] /
1 import { Component, OnInit } from '@angular/core';
2 import { UntypedFormControl, Validators } from '@angular/forms';
3 import { ActivatedRoute, Router } from '@angular/router';
4 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
5 import {
6   NamespaceCreateRequest,
7   NamespaceEditRequest,
8   NvmeofService
9 } from '~/app/shared/api/nvmeof.service';
10 import { ActionLabelsI18n, URLVerbs } from '~/app/shared/constants/app.constants';
11 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
12 import { FinishedTask } from '~/app/shared/models/finished-task';
13 import { NvmeofSubsystemNamespace } from '~/app/shared/models/nvmeof';
14 import { Permission } from '~/app/shared/models/permissions';
15 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
16 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
17 import { Pool } from '../../pool/pool';
18 import { PoolService } from '~/app/shared/api/pool.service';
19 import { RbdService } from '~/app/shared/api/rbd.service';
20 import { FormatterService } from '~/app/shared/services/formatter.service';
21 import { Observable } from 'rxjs';
22 import { CdValidators } from '~/app/shared/forms/cd-validators';
23 import { DimlessBinaryPipe } from '~/app/shared/pipes/dimless-binary.pipe';
24
25 @Component({
26   selector: 'cd-nvmeof-namespaces-form',
27   templateUrl: './nvmeof-namespaces-form.component.html',
28   styleUrls: ['./nvmeof-namespaces-form.component.scss']
29 })
30 export class NvmeofNamespacesFormComponent implements OnInit {
31   action: string;
32   permission: Permission;
33   poolPermission: Permission;
34   resource: string;
35   pageURL: string;
36   edit: boolean = false;
37   nsForm: CdFormGroup;
38   subsystemNQN: string;
39   rbdPools: Array<Pool> = null;
40   units: Array<string> = ['KiB', 'MiB', 'GiB', 'TiB'];
41   nsid: string;
42   currentBytes: number;
43   invalidSizeError: boolean;
44   group: string;
45
46   constructor(
47     public actionLabels: ActionLabelsI18n,
48     private authStorageService: AuthStorageService,
49     private taskWrapperService: TaskWrapperService,
50     private nvmeofService: NvmeofService,
51     private poolService: PoolService,
52     private rbdService: RbdService,
53     private router: Router,
54     private route: ActivatedRoute,
55     public activeModal: NgbActiveModal,
56     public formatterService: FormatterService,
57     public dimlessBinaryPipe: DimlessBinaryPipe
58   ) {
59     this.permission = this.authStorageService.getPermissions().nvmeof;
60     this.poolPermission = this.authStorageService.getPermissions().pool;
61     this.resource = $localize`Namespace`;
62     this.pageURL = 'block/nvmeof/subsystems';
63   }
64
65   init() {
66     this.route.queryParams.subscribe((params) => {
67       this.group = params?.['group'];
68     });
69     this.createForm();
70     this.action = this.actionLabels.CREATE;
71     this.route.params.subscribe((params: { subsystem_nqn: string; nsid: string }) => {
72       this.subsystemNQN = params.subsystem_nqn;
73       this.nsid = params?.nsid;
74     });
75   }
76
77   initForEdit() {
78     this.edit = true;
79     this.action = this.actionLabels.EDIT;
80     this.nvmeofService
81       .getNamespace(this.subsystemNQN, this.nsid, this.group)
82       .subscribe((res: NvmeofSubsystemNamespace) => {
83         const convertedSize = this.dimlessBinaryPipe.transform(res.rbd_image_size).split(' ');
84         this.currentBytes = res.rbd_image_size;
85         this.nsForm.get('image').setValue(res.rbd_image_name);
86         this.nsForm.get('pool').setValue(res.rbd_pool_name);
87         this.nsForm.get('unit').setValue(convertedSize[1]);
88         this.nsForm.get('image_size').setValue(convertedSize[0]);
89         this.nsForm.get('image_size').addValidators(Validators.required);
90         this.nsForm.get('image').disable();
91         this.nsForm.get('pool').disable();
92       });
93   }
94
95   initForCreate() {
96     this.poolService.getList().subscribe((resp: Pool[]) => {
97       this.rbdPools = resp.filter(this.rbdService.isRBDPool);
98     });
99   }
100
101   ngOnInit() {
102     this.init();
103     if (this.router.url.includes('subsystems/(modal:edit')) {
104       this.initForEdit();
105     } else {
106       this.initForCreate();
107     }
108   }
109
110   createForm() {
111     this.nsForm = new CdFormGroup({
112       image: new UntypedFormControl(`nvme_ns_image:${Date.now()}`, {
113         validators: [Validators.required, Validators.pattern(/^[^@/]+?$/)]
114       }),
115       pool: new UntypedFormControl(null, {
116         validators: [Validators.required]
117       }),
118       image_size: new UntypedFormControl(1, [CdValidators.number(false), Validators.min(1)]),
119       unit: new UntypedFormControl(this.units[2])
120     });
121   }
122
123   buildRequest(): NamespaceCreateRequest | NamespaceEditRequest {
124     const image_size = this.nsForm.getValue('image_size');
125     const image_size_unit = this.nsForm.getValue('unit');
126     const request = {} as NamespaceCreateRequest | NamespaceEditRequest;
127     request['gw_group'] = this.group;
128     if (image_size) {
129       const key: string = this.edit ? 'rbd_image_size' : 'size';
130       const value: number = this.formatterService.toBytes(image_size + image_size_unit);
131       request[key] = value;
132     }
133     if (!this.edit) {
134       const image = this.nsForm.getValue('image');
135       const pool = this.nsForm.getValue('pool');
136       request['rbd_image_name'] = image;
137       request['rbd_pool'] = pool;
138     }
139     return request;
140   }
141
142   validateSize() {
143     const unit = this.nsForm.getValue('unit');
144     const image_size = this.nsForm.getValue('image_size');
145     if (image_size && unit) {
146       const bytes = this.formatterService.toBytes(image_size + unit);
147       return bytes <= this.currentBytes;
148     }
149     return null;
150   }
151
152   onSubmit() {
153     if (this.validateSize()) {
154       this.invalidSizeError = true;
155       this.nsForm.setErrors({ cdSubmitButton: true });
156     } else {
157       this.invalidSizeError = false;
158       const component = this;
159       const taskUrl: string = `nvmeof/namespace/${this.edit ? URLVerbs.EDIT : URLVerbs.CREATE}`;
160       const request = this.buildRequest();
161       let action: Observable<any>;
162
163       if (this.edit) {
164         action = this.taskWrapperService.wrapTaskAroundCall({
165           task: new FinishedTask(taskUrl, {
166             nqn: this.subsystemNQN,
167             nsid: this.nsid
168           }),
169           call: this.nvmeofService.updateNamespace(
170             this.subsystemNQN,
171             this.nsid,
172             request as NamespaceEditRequest
173           )
174         });
175       } else {
176         action = this.taskWrapperService.wrapTaskAroundCall({
177           task: new FinishedTask(taskUrl, {
178             nqn: this.subsystemNQN
179           }),
180           call: this.nvmeofService.createNamespace(
181             this.subsystemNQN,
182             request as NamespaceCreateRequest
183           )
184         });
185       }
186
187       action.subscribe({
188         error() {
189           component.nsForm.setErrors({ cdSubmitButton: true });
190         },
191         complete: () => {
192           this.router.navigate([this.pageURL, { outlets: { modal: null } }]);
193         }
194       });
195     }
196   }
197 }