1 import { Component, EventEmitter, OnInit, Output } from '@angular/core';
2 import { Validators } from '@angular/forms';
4 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
5 import _ from 'lodash';
7 import { CrushRuleService } from '~/app/shared/api/crush-rule.service';
8 import { CrushNodeSelectionClass } from '~/app/shared/classes/crush.node.selection.class';
9 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
10 import { CdFormBuilder } from '~/app/shared/forms/cd-form-builder';
11 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
12 import { CdValidators } from '~/app/shared/forms/cd-validators';
13 import { CrushNode } from '~/app/shared/models/crush-node';
14 import { FinishedTask } from '~/app/shared/models/finished-task';
15 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
18 selector: 'cd-crush-rule-form-modal',
19 templateUrl: './crush-rule-form-modal.component.html',
20 styleUrls: ['./crush-rule-form-modal.component.scss']
22 export class CrushRuleFormModalComponent extends CrushNodeSelectionClass implements OnInit {
24 submitAction = new EventEmitter();
26 tooltips = this.crushRuleService.formTooltips;
34 private formBuilder: CdFormBuilder,
35 public activeModal: NgbActiveModal,
36 private taskWrapper: TaskWrapperService,
37 private crushRuleService: CrushRuleService,
38 public actionLabels: ActionLabelsI18n
41 this.action = this.actionLabels.CREATE;
42 this.resource = $localize`Crush Rule`;
47 this.form = this.formBuilder.group({
53 Validators.pattern('[A-Za-z0-9_-]+'),
56 (value: any) => this.names && this.names.indexOf(value) !== -1
61 root: null, // Replaced with first root
62 // failure_domain: string
63 failure_domain: '', // Replaced with most common type
64 // device_class: string
65 device_class: '' // Replaced with device type if only one exists beneath domain
72 .subscribe(({ names, nodes }: { names: string[]; nodes: CrushNode[] }) => {
73 this.initCrushNodeSelection(
75 this.form.get('root'),
76 this.form.get('failure_domain'),
77 this.form.get('device_class')
84 if (this.form.invalid) {
85 this.form.setErrors({ cdSubmitButton: true });
88 const rule = _.cloneDeep(this.form.value);
89 rule.root = rule.root.name;
90 if (rule.device_class === '') {
91 delete rule.device_class;
95 task: new FinishedTask('crushRule/create', rule),
96 call: this.crushRuleService.create(rule)
100 this.form.setErrors({ cdSubmitButton: true });
103 this.activeModal.close();
104 this.submitAction.emit(rule);