1 import { Component, EventEmitter, OnInit, Output } from '@angular/core';
2 import { Validators } from '@angular/forms';
4 import { I18n } from '@ngx-translate/i18n-polyfill';
5 import * as _ from 'lodash';
6 import { BsModalRef } from 'ngx-bootstrap/modal';
8 import { CrushRuleService } from '../../../shared/api/crush-rule.service';
9 import { CrushNodeSelectionClass } from '../../../shared/classes/crush.node.selection.class';
10 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
11 import { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
12 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
13 import { CdValidators } from '../../../shared/forms/cd-validators';
14 import { CrushNode } from '../../../shared/models/crush-node';
15 import { FinishedTask } from '../../../shared/models/finished-task';
16 import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
19 selector: 'cd-crush-rule-form-modal',
20 templateUrl: './crush-rule-form-modal.component.html',
21 styleUrls: ['./crush-rule-form-modal.component.scss']
23 export class CrushRuleFormModalComponent extends CrushNodeSelectionClass implements OnInit {
25 submitAction = new EventEmitter();
27 tooltips = this.crushRuleService.formTooltips;
35 private formBuilder: CdFormBuilder,
36 public bsModalRef: BsModalRef,
37 private taskWrapper: TaskWrapperService,
38 private crushRuleService: CrushRuleService,
40 public actionLabels: ActionLabelsI18n
43 this.action = this.actionLabels.CREATE;
44 this.resource = this.i18n('Crush Rule');
49 this.form = this.formBuilder.group({
55 Validators.pattern('[A-Za-z0-9_-]+'),
58 (value: any) => this.names && this.names.indexOf(value) !== -1
63 root: null, // Replaced with first root
64 // failure_domain: string
65 failure_domain: '', // Replaced with most common type
66 // device_class: string
67 device_class: '' // Replaced with device type if only one exists beneath domain
74 .subscribe(({ names, nodes }: { names: string[]; nodes: CrushNode[] }) => {
75 this.initCrushNodeSelection(
77 this.form.get('root'),
78 this.form.get('failure_domain'),
79 this.form.get('device_class')
86 if (this.form.invalid) {
87 this.form.setErrors({ cdSubmitButton: true });
90 const rule = _.cloneDeep(this.form.value);
91 rule.root = rule.root.name;
92 if (rule.device_class === '') {
93 delete rule.device_class;
97 task: new FinishedTask('crushRule/create', rule),
98 call: this.crushRuleService.create(rule)
103 this.form.setErrors({ cdSubmitButton: true });
106 this.bsModalRef.hide();
107 this.submitAction.emit(rule);