]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
1 | import { Component, EventEmitter, OnInit, Output } from '@angular/core'; |
2 | import { Validators } from '@angular/forms'; | |
3 | ||
4 | import { I18n } from '@ngx-translate/i18n-polyfill'; | |
5 | import * as _ from 'lodash'; | |
6 | import { BsModalRef } from 'ngx-bootstrap/modal'; | |
7 | ||
8 | import { CrushRuleService } from '../../../shared/api/crush-rule.service'; | |
e306af50 | 9 | import { CrushNodeSelectionClass } from '../../../shared/classes/crush.node.selection.class'; |
9f95a23c TL |
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'; | |
17 | ||
18 | @Component({ | |
19 | selector: 'cd-crush-rule-form-modal', | |
20 | templateUrl: './crush-rule-form-modal.component.html', | |
21 | styleUrls: ['./crush-rule-form-modal.component.scss'] | |
22 | }) | |
e306af50 | 23 | export class CrushRuleFormModalComponent extends CrushNodeSelectionClass implements OnInit { |
9f95a23c TL |
24 | @Output() |
25 | submitAction = new EventEmitter(); | |
26 | ||
9f95a23c TL |
27 | tooltips = this.crushRuleService.formTooltips; |
28 | ||
29 | form: CdFormGroup; | |
30 | names: string[]; | |
31 | action: string; | |
32 | resource: string; | |
33 | ||
9f95a23c TL |
34 | constructor( |
35 | private formBuilder: CdFormBuilder, | |
36 | public bsModalRef: BsModalRef, | |
37 | private taskWrapper: TaskWrapperService, | |
38 | private crushRuleService: CrushRuleService, | |
39 | private i18n: I18n, | |
40 | public actionLabels: ActionLabelsI18n | |
41 | ) { | |
e306af50 | 42 | super(); |
9f95a23c TL |
43 | this.action = this.actionLabels.CREATE; |
44 | this.resource = this.i18n('Crush Rule'); | |
45 | this.createForm(); | |
46 | } | |
47 | ||
48 | createForm() { | |
49 | this.form = this.formBuilder.group({ | |
50 | // name: string | |
51 | name: [ | |
52 | '', | |
53 | [ | |
54 | Validators.required, | |
55 | Validators.pattern('[A-Za-z0-9_-]+'), | |
56 | CdValidators.custom( | |
57 | 'uniqueName', | |
58 | (value: any) => this.names && this.names.indexOf(value) !== -1 | |
59 | ) | |
60 | ] | |
61 | ], | |
62 | // root: CrushNode | |
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 | |
68 | }); | |
69 | } | |
70 | ||
71 | ngOnInit() { | |
72 | this.crushRuleService | |
73 | .getInfo() | |
74 | .subscribe(({ names, nodes }: { names: string[]; nodes: CrushNode[] }) => { | |
e306af50 TL |
75 | this.initCrushNodeSelection( |
76 | nodes, | |
77 | this.form.get('root'), | |
78 | this.form.get('failure_domain'), | |
79 | this.form.get('device_class') | |
801d1391 | 80 | ); |
9f95a23c | 81 | this.names = names; |
9f95a23c | 82 | }); |
9f95a23c TL |
83 | } |
84 | ||
85 | onSubmit() { | |
86 | if (this.form.invalid) { | |
87 | this.form.setErrors({ cdSubmitButton: true }); | |
88 | return; | |
89 | } | |
90 | const rule = _.cloneDeep(this.form.value); | |
91 | rule.root = rule.root.name; | |
92 | if (rule.device_class === '') { | |
93 | delete rule.device_class; | |
94 | } | |
95 | this.taskWrapper | |
96 | .wrapTaskAroundCall({ | |
97 | task: new FinishedTask('crushRule/create', rule), | |
98 | call: this.crushRuleService.create(rule) | |
99 | }) | |
100 | .subscribe( | |
101 | undefined, | |
102 | () => { | |
103 | this.form.setErrors({ cdSubmitButton: true }); | |
104 | }, | |
105 | () => { | |
106 | this.bsModalRef.hide(); | |
107 | this.submitAction.emit(rule); | |
108 | } | |
109 | ); | |
110 | } | |
111 | } |