]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
0afb2442735cf8fe2ab5a89a91c10e8adeb277a9
[ceph-ci.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         );
81         this.names = names;
82       });
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         error: () => {
102           this.form.setErrors({ cdSubmitButton: true });
103         },
104         complete: () => {
105           this.activeModal.close();
106           this.submitAction.emit(rule);
107         }
108       });
109   }
110 }