]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/blob
f30b61ea5e5f3199a2d6bf94f8b70b74ada151b8
[ceph.git] /
1 import { Component, EventEmitter, OnInit, Output } from '@angular/core';
2 import { Validators } from '@angular/forms';
3
4 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
5 import _ from 'lodash';
6
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';
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 extends CrushNodeSelectionClass implements OnInit {
23   @Output()
24   submitAction = new EventEmitter();
25
26   tooltips!: Record<string, string>;
27
28   form: CdFormGroup;
29   names: string[];
30   action: string;
31   resource: string;
32
33   constructor(
34     private formBuilder: CdFormBuilder,
35     public activeModal: NgbActiveModal,
36     private taskWrapper: TaskWrapperService,
37     private crushRuleService: CrushRuleService,
38     public actionLabels: ActionLabelsI18n
39   ) {
40     super();
41     this.action = this.actionLabels.CREATE;
42     this.resource = $localize`Crush Rule`;
43     this.createForm();
44   }
45
46   createForm() {
47     this.form = this.formBuilder.group({
48       // name: string
49       name: [
50         '',
51         [
52           Validators.required,
53           Validators.pattern('[A-Za-z0-9_-]+'),
54           CdValidators.custom(
55             'uniqueName',
56             (value: any) => this.names && this.names.indexOf(value) !== -1
57           )
58         ]
59       ],
60       // root: CrushNode
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
66     });
67   }
68
69   ngOnInit() {
70     this.tooltips = this.crushRuleService.formTooltips;
71
72     this.crushRuleService
73       .getInfo()
74       .subscribe(({ names, nodes }: { names: string[]; nodes: CrushNode[] }) => {
75         this.initCrushNodeSelection(
76           nodes,
77           this.form.get('root'),
78           this.form.get('failure_domain'),
79           this.form.get('device_class'),
80           false
81         );
82         this.names = names;
83       });
84   }
85
86   onSubmit() {
87     if (this.form.invalid) {
88       this.form.setErrors({ cdSubmitButton: true });
89       return;
90     }
91     const rule = _.cloneDeep(this.form.value);
92     rule.root = rule.root.name;
93     if (rule.device_class === '') {
94       delete rule.device_class;
95     }
96     this.taskWrapper
97       .wrapTaskAroundCall({
98         task: new FinishedTask('crushRule/create', rule),
99         call: this.crushRuleService.create(rule)
100       })
101       .subscribe({
102         error: () => {
103           this.form.setErrors({ cdSubmitButton: true });
104         },
105         complete: () => {
106           this.activeModal.close();
107           this.submitAction.emit(rule);
108         }
109       });
110   }
111 }