1 import { Component, EventEmitter, OnInit } from '@angular/core';
2 import { FormControl, ValidatorFn, Validators } from '@angular/forms';
3 import { ActivatedRoute, Router } from '@angular/router';
5 import { I18n } from '@ngx-translate/i18n-polyfill';
6 import * as _ from 'lodash';
7 import { Observable } from 'rxjs';
9 import { PoolService } from '../../../shared/api/pool.service';
10 import { RbdService } from '../../../shared/api/rbd.service';
11 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
12 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
14 RbdConfigurationEntry,
15 RbdConfigurationSourceField
16 } from '../../../shared/models/configuration';
17 import { FinishedTask } from '../../../shared/models/finished-task';
18 import { Permission } from '../../../shared/models/permissions';
19 import { DimlessBinaryPipe } from '../../../shared/pipes/dimless-binary.pipe';
20 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
21 import { FormatterService } from '../../../shared/services/formatter.service';
22 import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
23 import { RbdImageFeature } from './rbd-feature.interface';
24 import { RbdFormCloneRequestModel } from './rbd-form-clone-request.model';
25 import { RbdFormCopyRequestModel } from './rbd-form-copy-request.model';
26 import { RbdFormCreateRequestModel } from './rbd-form-create-request.model';
27 import { RbdFormEditRequestModel } from './rbd-form-edit-request.model';
28 import { RbdFormMode } from './rbd-form-mode.enum';
29 import { RbdFormResponseModel } from './rbd-form-response.model';
32 selector: 'cd-rbd-form',
33 templateUrl: './rbd-form.component.html',
34 styleUrls: ['./rbd-form.component.scss']
36 export class RbdFormComponent implements OnInit {
37 poolPermission: Permission;
39 getDirtyConfigurationValues: (
40 includeLocalField?: boolean,
41 localField?: RbdConfigurationSourceField
42 ) => RbdConfigurationEntry[];
44 pools: Array<string> = null;
45 allPools: Array<string> = null;
46 dataPools: Array<string> = null;
47 allDataPools: Array<string> = null;
48 features: { [key: string]: RbdImageFeature };
49 featuresList: RbdImageFeature[] = [];
50 initializeConfigData = new EventEmitter<{
51 initialData: RbdConfigurationEntry[];
52 sourceType: RbdConfigurationSourceField;
57 advancedEnabled = false;
59 public rbdFormMode = RbdFormMode;
62 response: RbdFormResponseModel;
65 defaultObjectSize = '4 MiB';
67 objectSizes: Array<string> = [
87 private authStorageService: AuthStorageService,
88 private route: ActivatedRoute,
89 private poolService: PoolService,
90 private rbdService: RbdService,
91 private formatter: FormatterService,
92 private taskWrapper: TaskWrapperService,
93 private dimlessBinaryPipe: DimlessBinaryPipe,
95 public actionLabels: ActionLabelsI18n,
98 this.poolPermission = this.authStorageService.getPermissions().pool;
99 this.resource = this.i18n('RBD');
102 desc: this.i18n('Deep flatten'),
108 desc: this.i18n('Layering'),
114 desc: this.i18n('Exclusive lock'),
120 desc: this.i18n('Object map (requires exclusive-lock)'),
121 requires: 'exclusive-lock',
127 desc: this.i18n('Journaling (requires exclusive-lock)'),
128 requires: 'exclusive-lock',
134 desc: this.i18n('Fast diff (interlocked with object-map)'),
135 requires: 'object-map',
138 interlockedWith: 'object-map',
142 this.featuresList = this.objToArray(this.features);
146 objToArray(obj: { [key: string]: any }) {
147 return _.map(obj, (o, key) => Object.assign(o, { key: key }));
151 this.rbdForm = new CdFormGroup(
153 parent: new FormControl(''),
154 name: new FormControl('', {
155 validators: [Validators.required, Validators.pattern(/^[^@/]+?$/)]
157 pool: new FormControl(null, {
158 validators: [Validators.required]
160 useDataPool: new FormControl(false),
161 dataPool: new FormControl(null),
162 size: new FormControl(null, {
165 obj_size: new FormControl(this.defaultObjectSize),
166 features: new CdFormGroup(
167 this.featuresList.reduce((acc, e) => {
168 acc[e.key] = new FormControl({ value: false, disabled: !!e.initDisabled });
172 stripingUnit: new FormControl(null),
173 stripingCount: new FormControl(null, {
177 this.validateRbdForm(this.formatter)
182 this.rbdForm.get('parent').disable();
183 this.rbdForm.get('pool').disable();
184 this.rbdForm.get('useDataPool').disable();
185 this.rbdForm.get('dataPool').disable();
186 this.rbdForm.get('obj_size').disable();
187 this.rbdForm.get('stripingUnit').disable();
188 this.rbdForm.get('stripingCount').disable();
192 this.rbdForm.get('parent').disable();
193 this.rbdForm.get('size').disable();
197 this.rbdForm.get('parent').disable();
198 this.rbdForm.get('size').disable();
202 if (this.router.url.startsWith('/block/rbd/edit')) {
203 this.mode = this.rbdFormMode.editing;
204 this.action = this.actionLabels.EDIT;
205 this.disableForEdit();
206 } else if (this.router.url.startsWith('/block/rbd/clone')) {
207 this.mode = this.rbdFormMode.cloning;
208 this.disableForClone();
209 this.action = this.actionLabels.CLONE;
210 } else if (this.router.url.startsWith('/block/rbd/copy')) {
211 this.mode = this.rbdFormMode.copying;
212 this.action = this.actionLabels.COPY;
213 this.disableForCopy();
215 this.action = this.actionLabels.CREATE;
218 this.mode === this.rbdFormMode.editing ||
219 this.mode === this.rbdFormMode.cloning ||
220 this.mode === this.rbdFormMode.copying
222 this.route.params.subscribe((params: { pool: string; name: string; snap: string }) => {
223 const poolName = decodeURIComponent(params.pool);
224 const rbdName = decodeURIComponent(params.name);
226 this.snapName = decodeURIComponent(params.snap);
228 this.rbdService.get(poolName, rbdName).subscribe((resp: RbdFormResponseModel) => {
229 this.setResponse(resp, this.snapName);
234 this.rbdService.defaultFeatures().subscribe((defaultFeatures: Array<string>) => {
235 this.setFeatures(defaultFeatures);
238 if (this.mode !== this.rbdFormMode.editing && this.poolPermission.read) {
240 .list(['pool_name', 'type', 'flags_names', 'application_metadata'])
243 const dataPools = [];
244 for (const pool of resp) {
245 if (_.indexOf(pool.application_metadata, 'rbd') !== -1) {
246 if (!pool.pool_name.includes('/')) {
247 if (pool.type === 'replicated') {
249 dataPools.push(pool);
251 pool.type === 'erasure' &&
252 pool.flags_names.indexOf('ec_overwrites') !== -1
254 dataPools.push(pool);
260 this.allPools = pools;
261 this.dataPools = dataPools;
262 this.allDataPools = dataPools;
263 if (this.pools.length === 1) {
264 const poolName = this.pools[0]['pool_name'];
265 this.rbdForm.get('pool').setValue(poolName);
266 this.onPoolChange(poolName);
270 _.each(this.features, (feature) => {
274 .valueChanges.subscribe((value) => this.featureFormUpdate(feature.key, value));
278 onPoolChange(selectedPoolName) {
279 const newDataPools = this.allDataPools.filter((dataPool: any) => {
280 return dataPool.pool_name !== selectedPoolName;
282 if (this.rbdForm.getValue('dataPool') === selectedPoolName) {
283 this.rbdForm.get('dataPool').setValue(null);
285 this.dataPools = newDataPools;
288 onUseDataPoolChange() {
289 if (!this.rbdForm.getValue('useDataPool')) {
290 this.rbdForm.get('dataPool').setValue(null);
291 this.onDataPoolChange(null);
295 onDataPoolChange(selectedDataPoolName) {
296 const newPools = this.allPools.filter((pool: any) => {
297 return pool.pool_name !== selectedDataPoolName;
299 if (this.rbdForm.getValue('pool') === selectedDataPoolName) {
300 this.rbdForm.get('pool').setValue(null);
302 this.pools = newPools;
305 validateRbdForm(formatter: FormatterService): ValidatorFn {
306 return (formGroup: CdFormGroup) => {
308 const useDataPoolControl = formGroup.get('useDataPool');
309 const dataPoolControl = formGroup.get('dataPool');
310 let dataPoolControlErrors = null;
311 if (useDataPoolControl.value && dataPoolControl.value == null) {
312 dataPoolControlErrors = { required: true };
314 dataPoolControl.setErrors(dataPoolControlErrors);
316 const sizeControl = formGroup.get('size');
317 const objectSizeControl = formGroup.get('obj_size');
318 const objectSizeInBytes = formatter.toBytes(
319 objectSizeControl.value != null ? objectSizeControl.value : this.defaultObjectSize
321 const stripingCountControl = formGroup.get('stripingCount');
322 const stripingCount = stripingCountControl.value != null ? stripingCountControl.value : 1;
323 let sizeControlErrors = null;
324 if (sizeControl.value === null) {
325 sizeControlErrors = { required: true };
327 const sizeInBytes = formatter.toBytes(sizeControl.value);
328 if (stripingCount * objectSizeInBytes > sizeInBytes) {
329 sizeControlErrors = { invalidSizeObject: true };
332 sizeControl.setErrors(sizeControlErrors);
334 const stripingUnitControl = formGroup.get('stripingUnit');
335 let stripingUnitControlErrors = null;
336 if (stripingUnitControl.value === null && stripingCountControl.value !== null) {
337 stripingUnitControlErrors = { required: true };
338 } else if (stripingUnitControl.value !== null) {
339 const stripingUnitInBytes = formatter.toBytes(stripingUnitControl.value);
340 if (stripingUnitInBytes > objectSizeInBytes) {
341 stripingUnitControlErrors = { invalidStripingUnit: true };
344 stripingUnitControl.setErrors(stripingUnitControlErrors);
346 let stripingCountControlErrors = null;
347 if (stripingCountControl.value === null && stripingUnitControl.value !== null) {
348 stripingCountControlErrors = { required: true };
349 } else if (stripingCount < 1) {
350 stripingCountControlErrors = { min: true };
352 stripingCountControl.setErrors(stripingCountControlErrors);
357 protected getDependendChildFeatures(featureKey: string) {
358 return _.filter(this.features, (f) => f.requires === featureKey) || [];
361 deepBoxCheck(key, checked) {
362 const childFeatures = this.getDependendChildFeatures(key);
363 childFeatures.forEach((feature) => {
364 const featureControl = this.rbdForm.get(feature.key);
366 featureControl.enable({ emitEvent: false });
368 featureControl.disable({ emitEvent: false });
369 featureControl.setValue(false, { emitEvent: false });
370 this.deepBoxCheck(feature.key, checked);
373 const featureFormGroup = this.rbdForm.get('features');
374 if (this.mode === this.rbdFormMode.editing && featureFormGroup.get(feature.key).enabled) {
375 if (this.response.features_name.indexOf(feature.key) !== -1 && !feature.allowDisable) {
376 featureFormGroup.get(feature.key).disable();
378 this.response.features_name.indexOf(feature.key) === -1 &&
381 featureFormGroup.get(feature.key).disable();
387 interlockCheck(key, checked) {
388 // Adds a compatibility layer for Ceph cluster where the feature interlock of features hasn't
389 // been implemented yet. It disables the feature interlock for images which only have one of
390 // both interlocked features (at the time of this writing: object-map and fast-diff) enabled.
391 const feature = this.featuresList.find((f) => f.key === key);
393 // Ignore `create` page
394 const hasInterlockedFeature = feature.interlockedWith != null;
395 const dependentInterlockedFeature = this.featuresList.find(
396 (f) => f.interlockedWith === feature.key
398 const isOriginFeatureEnabled = !!this.response.features_name.find((e) => e === feature.key); // in this case: fast-diff
399 if (hasInterlockedFeature) {
400 const isLinkedEnabled = !!this.response.features_name.find(
401 (e) => e === feature.interlockedWith
402 ); // depends: object-map
403 if (isOriginFeatureEnabled !== isLinkedEnabled) {
404 return; // Ignore incompatible setting because it's from a previous cluster version
406 } else if (dependentInterlockedFeature) {
407 const isOtherInterlockedFeatureEnabled = !!this.response.features_name.find(
408 (e) => e === dependentInterlockedFeature.key
410 if (isOtherInterlockedFeatureEnabled !== isOriginFeatureEnabled) {
411 return; // Ignore incompatible setting because it's from a previous cluster version
417 _.filter(this.features, (f) => f.interlockedWith === key).forEach((f) =>
418 this.rbdForm.get(f.key).setValue(true, { emitEvent: false })
421 if (feature.interlockedWith) {
422 // Don't skip emitting the event here, as it prevents `fast-diff` from
423 // becoming disabled when manually unchecked. This is because it
424 // triggers an update on `object-map` and if `object-map` doesn't emit,
425 // `fast-diff` will not be automatically disabled.
428 .get(feature.interlockedWith)
434 featureFormUpdate(key, checked) {
436 const required = this.features[key].requires;
437 if (required && !this.rbdForm.getValue(required)) {
438 this.rbdForm.get(`features.${key}`).setValue(false);
442 this.deepBoxCheck(key, checked);
443 this.interlockCheck(key, checked);
446 setFeatures(features: Array<string>) {
447 const featuresControl = this.rbdForm.get('features');
448 _.forIn(this.features, (feature) => {
449 if (features.indexOf(feature.key) !== -1) {
450 featuresControl.get(feature.key).setValue(true);
452 this.featureFormUpdate(feature.key, featuresControl.get(feature.key).value);
456 setResponse(response: RbdFormResponseModel, snapName: string) {
457 this.response = response;
458 if (this.mode === this.rbdFormMode.cloning) {
459 this.rbdForm.get('parent').setValue(`${response.pool_name}/${response.name}@${snapName}`);
460 } else if (this.mode === this.rbdFormMode.copying) {
462 this.rbdForm.get('parent').setValue(`${response.pool_name}/${response.name}@${snapName}`);
464 this.rbdForm.get('parent').setValue(`${response.pool_name}/${response.name}`);
466 } else if (response.parent) {
467 const parent = response.parent;
470 .setValue(`${parent.pool_name}/${parent.image_name}@${parent.snap_name}`);
472 if (this.mode === this.rbdFormMode.editing) {
473 this.rbdForm.get('name').setValue(response.name);
475 this.rbdForm.get('pool').setValue(response.pool_name);
476 if (response.data_pool) {
477 this.rbdForm.get('useDataPool').setValue(true);
478 this.rbdForm.get('dataPool').setValue(response.data_pool);
480 this.rbdForm.get('size').setValue(this.dimlessBinaryPipe.transform(response.size));
481 this.rbdForm.get('obj_size').setValue(this.dimlessBinaryPipe.transform(response.obj_size));
482 this.setFeatures(response.features_name);
485 .setValue(this.dimlessBinaryPipe.transform(response.stripe_unit));
486 this.rbdForm.get('stripingCount').setValue(response.stripe_count);
489 this.initializeConfigData.emit({
490 initialData: this.response.configuration,
491 sourceType: RbdConfigurationSourceField.image
496 const request = new RbdFormCreateRequestModel();
497 request.pool_name = this.rbdForm.getValue('pool');
498 request.name = this.rbdForm.getValue('name');
499 request.size = this.formatter.toBytes(this.rbdForm.getValue('size'));
500 request.obj_size = this.formatter.toBytes(this.rbdForm.getValue('obj_size'));
501 _.forIn(this.features, (feature) => {
502 if (this.rbdForm.getValue(feature.key)) {
503 request.features.push(feature.key);
508 request.stripe_unit = this.formatter.toBytes(this.rbdForm.getValue('stripingUnit'));
509 request.stripe_count = this.rbdForm.getValue('stripingCount');
510 request.data_pool = this.rbdForm.getValue('dataPool');
513 request.configuration = this.getDirtyConfigurationValues();
518 createAction(): Observable<any> {
519 const request = this.createRequest();
520 return this.taskWrapper.wrapTaskAroundCall({
521 task: new FinishedTask('rbd/create', {
522 pool_name: request.pool_name,
523 image_name: request.name
525 call: this.rbdService.create(request)
530 const request = new RbdFormEditRequestModel();
531 request.name = this.rbdForm.getValue('name');
532 request.size = this.formatter.toBytes(this.rbdForm.getValue('size'));
533 _.forIn(this.features, (feature) => {
534 if (this.rbdForm.getValue(feature.key)) {
535 request.features.push(feature.key);
539 request.configuration = this.getDirtyConfigurationValues();
544 cloneRequest(): RbdFormCloneRequestModel {
545 const request = new RbdFormCloneRequestModel();
546 request.child_pool_name = this.rbdForm.getValue('pool');
547 request.child_image_name = this.rbdForm.getValue('name');
548 request.obj_size = this.formatter.toBytes(this.rbdForm.getValue('obj_size'));
549 _.forIn(this.features, (feature) => {
550 if (this.rbdForm.getValue(feature.key)) {
551 request.features.push(feature.key);
556 request.stripe_unit = this.formatter.toBytes(this.rbdForm.getValue('stripingUnit'));
557 request.stripe_count = this.rbdForm.getValue('stripingCount');
558 request.data_pool = this.rbdForm.getValue('dataPool');
561 request.configuration = this.getDirtyConfigurationValues(
563 RbdConfigurationSourceField.image
569 editAction(): Observable<any> {
570 return this.taskWrapper.wrapTaskAroundCall({
571 task: new FinishedTask('rbd/edit', {
572 pool_name: this.response.pool_name,
573 image_name: this.response.name
575 call: this.rbdService.update(this.response.pool_name, this.response.name, this.editRequest())
579 cloneAction(): Observable<any> {
580 const request = this.cloneRequest();
581 return this.taskWrapper.wrapTaskAroundCall({
582 task: new FinishedTask('rbd/clone', {
583 parent_pool_name: this.response.pool_name,
584 parent_image_name: this.response.name,
585 parent_snap_name: this.snapName,
586 child_pool_name: request.child_pool_name,
587 child_image_name: request.child_image_name
589 call: this.rbdService.cloneSnapshot(
590 this.response.pool_name,
598 copyRequest(): RbdFormCopyRequestModel {
599 const request = new RbdFormCopyRequestModel();
601 request.snapshot_name = this.snapName;
603 request.dest_pool_name = this.rbdForm.getValue('pool');
604 request.dest_image_name = this.rbdForm.getValue('name');
605 request.obj_size = this.formatter.toBytes(this.rbdForm.getValue('obj_size'));
606 _.forIn(this.features, (feature) => {
607 if (this.rbdForm.getValue(feature.key)) {
608 request.features.push(feature.key);
613 request.stripe_unit = this.formatter.toBytes(this.rbdForm.getValue('stripingUnit'));
614 request.stripe_count = this.rbdForm.getValue('stripingCount');
615 request.data_pool = this.rbdForm.getValue('dataPool');
618 request.configuration = this.getDirtyConfigurationValues(
620 RbdConfigurationSourceField.image
626 copyAction(): Observable<any> {
627 const request = this.copyRequest();
629 return this.taskWrapper.wrapTaskAroundCall({
630 task: new FinishedTask('rbd/copy', {
631 src_pool_name: this.response.pool_name,
632 src_image_name: this.response.name,
633 dest_pool_name: request.dest_pool_name,
634 dest_image_name: request.dest_image_name
636 call: this.rbdService.copy(this.response.pool_name, this.response.name, request)
641 let action: Observable<any>;
643 if (this.mode === this.rbdFormMode.editing) {
644 action = this.editAction();
645 } else if (this.mode === this.rbdFormMode.cloning) {
646 action = this.cloneAction();
647 } else if (this.mode === this.rbdFormMode.copying) {
648 action = this.copyAction();
650 action = this.createAction();
655 () => this.rbdForm.setErrors({ cdSubmitButton: true }),
656 () => this.router.navigate(['/block/rbd'])