]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
37324979bf9e68e1f95427f33e730c97a6ecd02e
[ceph-ci.git] /
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 { CrushNodeSelectionClass } from '../../../shared/classes/crush.node.selection.class';
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 })
23 export class CrushRuleFormModalComponent extends CrushNodeSelectionClass implements OnInit {
24   @Output()
25   submitAction = new EventEmitter();
26
27   tooltips = this.crushRuleService.formTooltips;
28
29   form: CdFormGroup;
30   names: string[];
31   action: string;
32   resource: string;
33
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   ) {
42     super();
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[] }) => {
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.bsModalRef.hide();
106           this.submitAction.emit(rule);
107         }
108       });
109   }
110 }