]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
e2687300dc9b5591eb382b9eef899dd00848fbcc
[ceph-ci.git] /
1 import { Component, OnInit } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
3
4 import * as _ from 'lodash';
5 import { BsModalRef } from 'ngx-bootstrap/modal';
6
7 import { IscsiService } from '../../../shared/api/iscsi.service';
8 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
9
10 @Component({
11   selector: 'cd-iscsi-target-image-settings-modal',
12   templateUrl: './iscsi-target-image-settings-modal.component.html',
13   styleUrls: ['./iscsi-target-image-settings-modal.component.scss']
14 })
15 export class IscsiTargetImageSettingsModalComponent implements OnInit {
16   image: string;
17   imagesSettings: any;
18   disk_default_controls: any;
19   disk_controls_limits: any;
20   backstores: any;
21
22   settingsForm: CdFormGroup;
23   helpText: any;
24
25   constructor(public modalRef: BsModalRef, public iscsiService: IscsiService) {}
26
27   ngOnInit() {
28     this.helpText = this.iscsiService.imageAdvancedSettings;
29
30     const fg = {
31       backstore: new FormControl(this.imagesSettings[this.image]['backstore'])
32     };
33     _.forEach(this.backstores, (backstore) => {
34       const model = this.imagesSettings[this.image][backstore] || {};
35       _.forIn(this.disk_default_controls[backstore], (_value, key) => {
36         const validators = [];
37         if (this.disk_controls_limits && key in this.disk_controls_limits[backstore]) {
38           if ('min' in this.disk_controls_limits[backstore][key]) {
39             validators.push(Validators.min(this.disk_controls_limits[backstore][key]['min']));
40           }
41           if ('max' in this.disk_controls_limits[backstore][key]) {
42             validators.push(Validators.max(this.disk_controls_limits[backstore][key]['max']));
43           }
44         }
45         fg[key] = new FormControl(model[key], {
46           validators: validators
47         });
48       });
49     });
50
51     this.settingsForm = new CdFormGroup(fg);
52   }
53
54   save() {
55     const backstore = this.settingsForm.controls['backstore'].value;
56     const settings = {};
57     _.forIn(this.settingsForm.controls, (control, key) => {
58       if (
59         !(control.value === '' || control.value === null) &&
60         key in this.disk_default_controls[this.settingsForm.value['backstore']]
61       ) {
62         settings[key] = control.value;
63         // If one setting belongs to multiple backstores, we have to update it in all backstores
64         _.forEach(this.backstores, (currentBackstore) => {
65           if (currentBackstore !== backstore) {
66             const model = this.imagesSettings[this.image][currentBackstore] || {};
67             if (key in model) {
68               this.imagesSettings[this.image][currentBackstore][key] = control.value;
69             }
70           }
71         });
72       }
73     });
74     this.imagesSettings[this.image]['backstore'] = backstore;
75     this.imagesSettings[this.image][backstore] = settings;
76     this.imagesSettings = { ...this.imagesSettings };
77     this.modalRef.hide();
78   }
79 }