]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/pool/crush-rule-form-modal/crush-rule-form-modal.component.ts
import 15.2.0 Octopus source
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / pool / crush-rule-form-modal / crush-rule-form-modal.component.ts
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';
9 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
10 import { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
11 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
12 import { CdValidators } from '../../../shared/forms/cd-validators';
13 import { CrushNode } from '../../../shared/models/crush-node';
14 import { FinishedTask } from '../../../shared/models/finished-task';
15 import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
16
17 @Component({
18 selector: 'cd-crush-rule-form-modal',
19 templateUrl: './crush-rule-form-modal.component.html',
20 styleUrls: ['./crush-rule-form-modal.component.scss']
21 })
22 export class CrushRuleFormModalComponent implements OnInit {
23 @Output()
24 submitAction = new EventEmitter();
25
26 buckets: CrushNode[] = [];
27 failureDomains: { [type: string]: CrushNode[] } = {};
28 devices: string[] = [];
29 tooltips = this.crushRuleService.formTooltips;
30
31 form: CdFormGroup;
32 names: string[];
33 action: string;
34 resource: string;
35
36 private nodes: CrushNode[] = [];
37 private easyNodes: { [id: number]: CrushNode } = {};
38
39 constructor(
40 private formBuilder: CdFormBuilder,
41 public bsModalRef: BsModalRef,
42 private taskWrapper: TaskWrapperService,
43 private crushRuleService: CrushRuleService,
44 private i18n: I18n,
45 public actionLabels: ActionLabelsI18n
46 ) {
47 this.action = this.actionLabels.CREATE;
48 this.resource = this.i18n('Crush Rule');
49 this.createForm();
50 }
51
52 createForm() {
53 this.form = this.formBuilder.group({
54 // name: string
55 name: [
56 '',
57 [
58 Validators.required,
59 Validators.pattern('[A-Za-z0-9_-]+'),
60 CdValidators.custom(
61 'uniqueName',
62 (value: any) => this.names && this.names.indexOf(value) !== -1
63 )
64 ]
65 ],
66 // root: CrushNode
67 root: null, // Replaced with first root
68 // failure_domain: string
69 failure_domain: '', // Replaced with most common type
70 // device_class: string
71 device_class: '' // Replaced with device type if only one exists beneath domain
72 });
73 }
74
75 ngOnInit() {
76 this.crushRuleService
77 .getInfo()
78 .subscribe(({ names, nodes }: { names: string[]; nodes: CrushNode[] }) => {
79 this.nodes = nodes;
80 nodes.forEach((node) => {
81 this.easyNodes[node.id] = node;
82 });
83 this.buckets = _.sortBy(nodes.filter((n) => n.children), 'name');
84 this.names = names;
85 this.preSelectRoot();
86 });
87 this.form.get('root').valueChanges.subscribe((root: CrushNode) => this.updateRoot(root));
88 this.form
89 .get('failure_domain')
90 .valueChanges.subscribe((domain: string) => this.updateDevices(domain));
91 }
92
93 private preSelectRoot() {
94 const rootNode = this.nodes.find((node) => node.type === 'root');
95 this.form.silentSet('root', rootNode);
96 this.updateRoot(rootNode);
97 }
98
99 private updateRoot(rootNode: CrushNode) {
100 const nodes = this.getSubNodes(rootNode);
101 const domains = {};
102 nodes.forEach((node) => {
103 if (!domains[node.type]) {
104 domains[node.type] = [];
105 }
106 domains[node.type].push(node);
107 });
108 Object.keys(domains).forEach((type) => {
109 if (domains[type].length <= 1) {
110 delete domains[type];
111 }
112 });
113 this.failureDomains = domains;
114 this.updateFailureDomain();
115 }
116
117 private getSubNodes(node: CrushNode): CrushNode[] {
118 let subNodes = [node]; // Includes parent node
119 if (!node.children) {
120 return subNodes;
121 }
122 node.children.forEach((id) => {
123 const childNode = this.easyNodes[id];
124 subNodes = subNodes.concat(this.getSubNodes(childNode));
125 });
126 return subNodes;
127 }
128
129 private updateFailureDomain() {
130 let failureDomain = this.getIncludedCustomValue(
131 'failure_domain',
132 Object.keys(this.failureDomains)
133 );
134 if (failureDomain === '') {
135 failureDomain = this.setMostCommonDomain();
136 }
137 this.updateDevices(failureDomain);
138 }
139
140 private getIncludedCustomValue(controlName: string, includedIn: string[]) {
141 const control = this.form.get(controlName);
142 return control.dirty && includedIn.includes(control.value) ? control.value : '';
143 }
144
145 private setMostCommonDomain(): string {
146 let winner = { n: 0, type: '' };
147 Object.keys(this.failureDomains).forEach((type) => {
148 const n = this.failureDomains[type].length;
149 if (winner.n < n) {
150 winner = { n, type };
151 }
152 });
153 this.form.silentSet('failure_domain', winner.type);
154 return winner.type;
155 }
156
157 updateDevices(failureDomain: string) {
158 const subNodes = _.flatten(
159 this.failureDomains[failureDomain].map((node) => this.getSubNodes(node))
160 );
161 this.devices = _.uniq(subNodes.filter((n) => n.device_class).map((n) => n.device_class)).sort();
162 const device =
163 this.devices.length === 1
164 ? this.devices[0]
165 : this.getIncludedCustomValue('device_class', this.devices);
166 this.form.get('device_class').setValue(device);
167 }
168
169 failureDomainKeys(): string[] {
170 return Object.keys(this.failureDomains).sort();
171 }
172
173 onSubmit() {
174 if (this.form.invalid) {
175 this.form.setErrors({ cdSubmitButton: true });
176 return;
177 }
178 const rule = _.cloneDeep(this.form.value);
179 rule.root = rule.root.name;
180 if (rule.device_class === '') {
181 delete rule.device_class;
182 }
183 this.taskWrapper
184 .wrapTaskAroundCall({
185 task: new FinishedTask('crushRule/create', rule),
186 call: this.crushRuleService.create(rule)
187 })
188 .subscribe(
189 undefined,
190 () => {
191 this.form.setErrors({ cdSubmitButton: true });
192 },
193 () => {
194 this.bsModalRef.hide();
195 this.submitAction.emit(rule);
196 }
197 );
198 }
199 }