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';
6 NamespaceCreateRequest,
7 NamespaceUpdateRequest,
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';
26 selector: 'cd-nvmeof-namespaces-form',
27 templateUrl: './nvmeof-namespaces-form.component.html',
28 styleUrls: ['./nvmeof-namespaces-form.component.scss']
30 export class NvmeofNamespacesFormComponent implements OnInit {
32 permission: Permission;
33 poolPermission: Permission;
36 edit: boolean = false;
39 rbdPools: Array<Pool> = null;
40 units: Array<string> = ['KiB', 'MiB', 'GiB', 'TiB'];
43 invalidSizeError: boolean;
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
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';
66 this.route.queryParams.subscribe((params) => {
67 this.group = params?.['group'];
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;
79 this.action = this.actionLabels.EDIT;
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();
96 this.poolService.getList().subscribe((resp: Pool[]) => {
97 this.rbdPools = resp.filter(this.rbdService.isRBDPool);
103 if (this.router.url.includes('subsystems/(modal:edit')) {
106 this.initForCreate();
111 this.nsForm = new CdFormGroup({
112 image: new UntypedFormControl(`nvme_ns_image:${Date.now()}`, {
113 validators: [Validators.required, Validators.pattern(/^[^@/]+?$/)]
115 pool: new UntypedFormControl(null, {
116 validators: [Validators.required]
118 image_size: new UntypedFormControl(1, [CdValidators.number(false), Validators.min(1)]),
119 unit: new UntypedFormControl(this.units[2])
123 updateRequest(rbdImageSize: number): NamespaceUpdateRequest {
124 const request: NamespaceUpdateRequest = {
125 gw_group: this.group,
126 rbd_image_size: rbdImageSize
131 createRequest(rbdImageSize: number): NamespaceCreateRequest {
132 const image = this.nsForm.getValue('image');
133 const pool = this.nsForm.getValue('pool');
134 const request: NamespaceCreateRequest = {
135 gw_group: this.group,
136 rbd_image_name: image,
140 request['rbd_image_size'] = rbdImageSize;
146 const image_size = this.nsForm.getValue('image_size');
147 let rbdImageSize: number = null;
149 const image_size_unit = this.nsForm.getValue('unit');
150 const value: number = this.formatterService.toBytes(image_size + image_size_unit);
151 rbdImageSize = value;
154 return this.updateRequest(rbdImageSize);
156 return this.createRequest(rbdImageSize);
161 const unit = this.nsForm.getValue('unit');
162 const image_size = this.nsForm.getValue('image_size');
163 if (image_size && unit) {
164 const bytes = this.formatterService.toBytes(image_size + unit);
165 return bytes <= this.currentBytes;
171 if (this.validateSize()) {
172 this.invalidSizeError = true;
173 this.nsForm.setErrors({ cdSubmitButton: true });
175 this.invalidSizeError = false;
176 const component = this;
177 const taskUrl: string = `nvmeof/namespace/${this.edit ? URLVerbs.EDIT : URLVerbs.CREATE}`;
178 const request: NamespaceCreateRequest | NamespaceUpdateRequest = this.buildRequest();
179 let action: Observable<any>;
182 action = this.taskWrapperService.wrapTaskAroundCall({
183 task: new FinishedTask(taskUrl, {
184 nqn: this.subsystemNQN,
187 call: this.nvmeofService.updateNamespace(
190 request as NamespaceUpdateRequest
194 action = this.taskWrapperService.wrapTaskAroundCall({
195 task: new FinishedTask(taskUrl, {
196 nqn: this.subsystemNQN
198 call: this.nvmeofService.createNamespace(
200 request as NamespaceCreateRequest
207 component.nsForm.setErrors({ cdSubmitButton: true });
210 this.router.navigate([this.pageURL, { outlets: { modal: null } }]);