]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
308b09d721641a74d96b5a7331ff59e6be42aa5f
[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 = this.crushRuleService.formTooltips;
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.crushRuleService
71       .getInfo()
72       .subscribe(({ names, nodes }: { names: string[]; nodes: CrushNode[] }) => {
73         this.initCrushNodeSelection(
74           nodes,
75           this.form.get('root'),
76           this.form.get('failure_domain'),
77           this.form.get('device_class')
78         );
79         this.names = names;
80       });
81   }
82
83   onSubmit() {
84     if (this.form.invalid) {
85       this.form.setErrors({ cdSubmitButton: true });
86       return;
87     }
88     const rule = _.cloneDeep(this.form.value);
89     rule.root = rule.root.name;
90     if (rule.device_class === '') {
91       delete rule.device_class;
92     }
93     this.taskWrapper
94       .wrapTaskAroundCall({
95         task: new FinishedTask('crushRule/create', rule),
96         call: this.crushRuleService.create(rule)
97       })
98       .subscribe({
99         error: () => {
100           this.form.setErrors({ cdSubmitButton: true });
101         },
102         complete: () => {
103           this.activeModal.close();
104           this.submitAction.emit(rule);
105         }
106       });
107   }
108 }