]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
3918aabfce195ea182d85ca4398bfe04fd541f10
[ceph.git] /
1 import { Component, OnInit } from '@angular/core';
2 import { FormControl } 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
24   constructor(public modalRef: BsModalRef, public iscsiService: IscsiService) {}
25
26   ngOnInit() {
27     const fg = {
28       backstore: new FormControl(this.imagesSettings[this.image]['backstore'])
29     };
30     _.forEach(this.backstores, (backstore) => {
31       const model = this.imagesSettings[this.image][backstore] || {};
32       _.forIn(this.disk_default_controls[backstore], (_value, key) => {
33         fg[key] = new FormControl(model[key]);
34       });
35     });
36
37     this.settingsForm = new CdFormGroup(fg);
38   }
39
40   getDiskControlLimits(backstore, setting) {
41     return this.disk_controls_limits[backstore][setting];
42   }
43
44   save() {
45     const backstore = this.settingsForm.controls['backstore'].value;
46     const settings = {};
47     _.forIn(this.settingsForm.controls, (control, key) => {
48       if (
49         !(control.value === '' || control.value === null) &&
50         key in this.disk_default_controls[this.settingsForm.value['backstore']]
51       ) {
52         settings[key] = control.value;
53         // If one setting belongs to multiple backstores, we have to update it in all backstores
54         _.forEach(this.backstores, (currentBackstore) => {
55           if (currentBackstore !== backstore) {
56             const model = this.imagesSettings[this.image][currentBackstore] || {};
57             if (key in model) {
58               this.imagesSettings[this.image][currentBackstore][key] = control.value;
59             }
60           }
61         });
62       }
63     });
64     this.imagesSettings[this.image]['backstore'] = backstore;
65     this.imagesSettings[this.image][backstore] = settings;
66     this.imagesSettings = { ...this.imagesSettings };
67     this.modalRef.hide();
68   }
69 }