]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
36d2e70f0d579359045301ad2c8c04aff7fbebef
[ceph.git] /
1 import { Component, OnInit } from '@angular/core';
2 import { AbstractControl, FormControl } from '@angular/forms';
3
4 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
5 import * as _ from 'lodash';
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 activeModal: NgbActiveModal, public iscsiService: IscsiService) {}
27
28   ngOnInit() {
29     const fg: Record<string, FormControl> = {
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: string, setting: string) {
45     if (this.disk_controls_limits) {
46       return this.disk_controls_limits[backstore][setting];
47     }
48     // backward compatibility
49     return { type: 'int' };
50   }
51
52   save() {
53     const backstore = this.settingsForm.controls['backstore'].value;
54     const lun = this.settingsForm.controls['lun'].value;
55     const wwn = this.settingsForm.controls['wwn'].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]['lun'] = lun;
76     this.imagesSettings[this.image]['wwn'] = wwn;
77     this.imagesSettings[this.image][backstore] = settings;
78     this.imagesSettings = { ...this.imagesSettings };
79     this.control.updateValueAndValidity({ emitEvent: false });
80     this.activeModal.close();
81   }
82 }