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