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