]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
715ee5035c81dc6fd18fa496544b05b96c01e27f
[ceph-ci.git] /
1 import { Component, OnInit } from '@angular/core';
2 import { ValidatorFn, Validators } from '@angular/forms';
3 import { ActivatedRoute, Router } from '@angular/router';
4
5 import * as _ from 'lodash';
6 import { forkJoin as observableForkJoin } from 'rxjs';
7
8 import { MgrModuleService } from '../../../../shared/api/mgr-module.service';
9 import { NotificationType } from '../../../../shared/enum/notification-type.enum';
10 import { CdForm } from '../../../../shared/forms/cd-form';
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 { NotificationService } from '../../../../shared/services/notification.service';
15
16 @Component({
17   selector: 'cd-mgr-module-form',
18   templateUrl: './mgr-module-form.component.html',
19   styleUrls: ['./mgr-module-form.component.scss']
20 })
21 export class MgrModuleFormComponent extends CdForm implements OnInit {
22   mgrModuleForm: CdFormGroup;
23   moduleName = '';
24   moduleOptions: any[] = [];
25
26   constructor(
27     private route: ActivatedRoute,
28     private router: Router,
29     private formBuilder: CdFormBuilder,
30     private mgrModuleService: MgrModuleService,
31     private notificationService: NotificationService
32   ) {
33     super();
34   }
35
36   ngOnInit() {
37     this.route.params.subscribe((params: { name: string }) => {
38       this.moduleName = decodeURIComponent(params.name);
39       const observables = [
40         this.mgrModuleService.getOptions(this.moduleName),
41         this.mgrModuleService.getConfig(this.moduleName)
42       ];
43       observableForkJoin(observables).subscribe(
44         (resp: object) => {
45           this.moduleOptions = resp[0];
46           // Create the form dynamically.
47           this.createForm();
48           // Set the form field values.
49           this.mgrModuleForm.setValue(resp[1]);
50           this.loadingReady();
51         },
52         (_error) => {
53           this.loadingError();
54         }
55       );
56     });
57   }
58
59   getValidators(moduleOption: any): ValidatorFn[] {
60     const result = [];
61     switch (moduleOption.type) {
62       case 'addr':
63         result.push(CdValidators.ip());
64         break;
65       case 'uint':
66       case 'int':
67       case 'size':
68       case 'secs':
69         result.push(CdValidators.number());
70         result.push(Validators.required);
71         if (_.isNumber(moduleOption.min)) {
72           result.push(Validators.min(moduleOption.min));
73         }
74         if (_.isNumber(moduleOption.max)) {
75           result.push(Validators.max(moduleOption.max));
76         }
77         break;
78       case 'str':
79         if (_.isNumber(moduleOption.min)) {
80           result.push(Validators.minLength(moduleOption.min));
81         }
82         if (_.isNumber(moduleOption.max)) {
83           result.push(Validators.maxLength(moduleOption.max));
84         }
85         break;
86       case 'float':
87         result.push(Validators.required);
88         result.push(CdValidators.decimalNumber());
89         break;
90       case 'uuid':
91         result.push(CdValidators.uuid());
92         break;
93     }
94     return result;
95   }
96
97   createForm() {
98     const controlsConfig = {};
99     _.forEach(this.moduleOptions, (moduleOption) => {
100       controlsConfig[moduleOption.name] = [
101         moduleOption.default_value,
102         this.getValidators(moduleOption)
103       ];
104     });
105     this.mgrModuleForm = this.formBuilder.group(controlsConfig);
106   }
107
108   goToListView() {
109     this.router.navigate(['/mgr-modules']);
110   }
111
112   onSubmit() {
113     // Exit immediately if the form isn't dirty.
114     if (this.mgrModuleForm.pristine) {
115       this.goToListView();
116       return;
117     }
118     const config = {};
119     _.forEach(this.moduleOptions, (moduleOption) => {
120       const control = this.mgrModuleForm.get(moduleOption.name);
121       // Append the option only if the value has been modified.
122       if (control.dirty && control.valid) {
123         config[moduleOption.name] = control.value;
124       }
125     });
126     this.mgrModuleService.updateConfig(this.moduleName, config).subscribe(
127       () => {
128         this.notificationService.show(
129           NotificationType.success,
130           $localize`Updated options for module '${this.moduleName}'.`
131         );
132         this.goToListView();
133       },
134       () => {
135         // Reset the 'Submit' button.
136         this.mgrModuleForm.setErrors({ cdSubmitButton: true });
137       }
138     );
139   }
140 }