import { ErasureCodeProfile } from '../../../shared/models/erasure-code-profile';
import { FinishedTask } from '../../../shared/models/finished-task';
import { Permission } from '../../../shared/models/permissions';
+import { PoolFormInfo } from '../../../shared/models/pool-form-info';
import { DimlessBinaryPipe } from '../../../shared/pipes/dimless-binary.pipe';
import { AuthStorageService } from '../../../shared/services/auth-storage.service';
import { FormatterService } from '../../../shared/services/formatter.service';
import { ErasureCodeProfileFormComponent } from '../erasure-code-profile-form/erasure-code-profile-form.component';
import { Pool } from '../pool';
import { PoolFormData } from './pool-form-data';
-import { PoolFormInfo } from './pool-form-info';
+
+interface FormFieldDescription {
+ externalFieldName: string;
+ formControlName: string;
+ attr?: string;
+ replaceFn?: Function;
+ editable?: boolean;
+ resetValue?: any;
+}
@Component({
selector: 'cd-pool-form',
export class PoolFormComponent implements OnInit {
permission: Permission;
form: CdFormGroup;
- compressionForm: CdFormGroup;
ecProfiles: ErasureCodeProfile[];
info: PoolFormInfo;
routeParamsSubscribe: any;
) {
this.editing = this.router.url.startsWith('/pool/edit');
this.authenticate();
- this.createForms();
+ this.createForm();
}
authenticate() {
}
}
- private createForms() {
- this.compressionForm = new CdFormGroup({
+ private createForm() {
+ const compressionForm = new CdFormGroup({
mode: new FormControl('none'),
algorithm: new FormControl(''),
minBlobSize: new FormControl('', {
updateOn: 'blur'
})
});
+
this.form = new CdFormGroup(
{
name: new FormControl('', {
validators: [Validators.required, Validators.min(1)]
}),
ecOverwrites: new FormControl(false),
- compression: this.compressionForm
+ compression: compressionForm
},
CdValidators.custom('form', () => null)
);
}
private setCompressionValidators() {
- CdValidators.validateIf(this.form.get('minBlobSize'), () => this.activatedCompression(), [
+ CdValidators.validateIf(this.form.get('minBlobSize'), () => this.hasCompressionEnabled(), [
Validators.min(0),
CdValidators.custom('maximum', (size) =>
this.oddBlobSize(size, this.form.getValue('maxBlobSize'))
)
]);
- CdValidators.validateIf(this.form.get('maxBlobSize'), () => this.activatedCompression(), [
+ CdValidators.validateIf(this.form.get('maxBlobSize'), () => this.hasCompressionEnabled(), [
Validators.min(0),
CdValidators.custom('minimum', (size) =>
this.oddBlobSize(this.form.getValue('minBlobSize'), size)
)
]);
- CdValidators.validateIf(this.form.get('ratio'), () => this.activatedCompression(), [
+ CdValidators.validateIf(this.form.get('ratio'), () => this.hasCompressionEnabled(), [
Validators.min(0),
Validators.max(1)
]);
return Boolean(minimum && maximum && minimum >= maximum);
}
- activatedCompression() {
+ hasCompressionEnabled() {
return this.form.getValue('mode') && this.form.get('mode').value.toLowerCase() !== 'none';
}
this.form.setErrors({ cdSubmitButton: true });
return;
}
+
const pool = { pool: this.form.getValue('name') };
- this.extendByItemsForSubmit(pool, [
- { api: 'pool_type', name: 'poolType' },
- { api: 'pg_num', name: 'pgNum', edit: true },
+
+ this.assignFormFields(pool, [
+ { externalFieldName: 'pool_type', formControlName: 'poolType' },
+ { externalFieldName: 'pg_num', formControlName: 'pgNum', editable: true },
this.form.getValue('poolType') === 'replicated'
- ? { api: 'size', name: 'size' }
- : { api: 'erasure_code_profile', name: 'erasureProfile', attr: 'name' },
- { api: 'rule_name', name: 'crushRule', attr: 'rule_name' }
+ ? { externalFieldName: 'size', formControlName: 'size' }
+ : {
+ externalFieldName: 'erasure_code_profile',
+ formControlName: 'erasureProfile',
+ attr: 'name'
+ },
+ { externalFieldName: 'rule_name', formControlName: 'crushRule', attr: 'rule_name' }
]);
+
if (this.info.is_all_bluestore) {
- this.extendByItemForSubmit(pool, {
- api: 'flags',
- name: 'ecOverwrites',
- fn: () => ['ec_overwrites']
+ this.assignFormField(pool, {
+ externalFieldName: 'flags',
+ formControlName: 'ecOverwrites',
+ replaceFn: () => ['ec_overwrites']
});
+
if (this.form.getValue('mode') !== 'none') {
- this.extendByItemsForSubmit(pool, [
+ this.assignFormFields(pool, [
{
- api: 'compression_mode',
- name: 'mode',
- edit: true,
- fn: (value) => this.activatedCompression() && value
+ externalFieldName: 'compression_mode',
+ formControlName: 'mode',
+ editable: true,
+ replaceFn: (value) => this.hasCompressionEnabled() && value
},
- { api: 'compression_algorithm', name: 'algorithm', edit: true },
{
- api: 'compression_min_blob_size',
- name: 'minBlobSize',
- fn: this.formatter.toBytes,
- edit: true,
+ externalFieldName: 'compression_algorithm',
+ formControlName: 'algorithm',
+ editable: true
+ },
+ {
+ externalFieldName: 'compression_min_blob_size',
+ formControlName: 'minBlobSize',
+ replaceFn: this.formatter.toBytes,
+ editable: true,
resetValue: 0
},
{
- api: 'compression_max_blob_size',
- name: 'maxBlobSize',
- fn: this.formatter.toBytes,
- edit: true,
+ externalFieldName: 'compression_max_blob_size',
+ formControlName: 'maxBlobSize',
+ replaceFn: this.formatter.toBytes,
+ editable: true,
resetValue: 0
},
- { api: 'compression_required_ratio', name: 'ratio', edit: true, resetValue: 0 }
+ {
+ externalFieldName: 'compression_required_ratio',
+ formControlName: 'ratio',
+ editable: true,
+ resetValue: 0
+ }
]);
} else if (this.editing) {
- this.extendByItemsForSubmit(pool, [
+ this.assignFormFields(pool, [
{
- api: 'compression_mode',
- name: 'mode',
- edit: true,
- fn: () => 'unset'
+ externalFieldName: 'compression_mode',
+ formControlName: 'mode',
+ editable: true,
+ replaceFn: () => 'unset'
}
]);
}
this.triggerApiTask(pool);
}
- private extendByItemsForSubmit(pool, items: any[]) {
- items.forEach((item) => this.extendByItemForSubmit(pool, item));
+ /**
+ * Retrieves the values for the given form field descriptions and assigns the values to the given
+ * object. This method differentiates between `add` and `edit` mode and acts differently on one or
+ * the other.
+ */
+ private assignFormFields(pool: object, formFieldDescription: FormFieldDescription[]): void {
+ formFieldDescription.forEach((item) => this.assignFormField(pool, item));
}
- private extendByItemForSubmit(
- pool,
+ /**
+ * Retrieves the value for the given form field description and assigns the values to the given
+ * object. This method differentiates between `add` and `edit` mode and acts differently on one or
+ * the other.
+ */
+ private assignFormField(
+ pool: object,
{
- api,
- name,
+ externalFieldName,
+ formControlName,
attr,
- fn,
- edit,
+ replaceFn,
+ editable,
resetValue
- }: {
- api: string;
- name: string;
- attr?: string;
- fn?: Function;
- edit?: boolean;
- resetValue?: any;
- }
- ) {
- if (this.editing && (!edit || this.form.get(name).pristine)) {
+ }: FormFieldDescription
+ ): void {
+ if (this.editing && (!editable || this.form.get(formControlName).pristine)) {
return;
}
- const value = this.form.getValue(name);
- let apiValue = fn ? fn(value) : attr ? _.get(value, attr) : value;
+ const value = this.form.getValue(formControlName);
+ let apiValue = replaceFn ? replaceFn(value) : attr ? _.get(value, attr) : value;
if (!value || !apiValue) {
- if (edit && !_.isUndefined(resetValue)) {
+ if (editable && !_.isUndefined(resetValue)) {
apiValue = resetValue;
} else {
return;
}
}
- pool[api] = apiValue;
+ pool[externalFieldName] = apiValue;
}
private triggerApiTask(pool) {