]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
e9c9c7d90daae3e8a2918383e58d3d027c668c2c
[ceph-ci.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 _ from 'lodash';
6
7 import { IscsiService } from '~/app/shared/api/iscsi.service';
8 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
9 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
10
11 @Component({
12   selector: 'cd-iscsi-target-image-settings-modal',
13   templateUrl: './iscsi-target-image-settings-modal.component.html',
14   styleUrls: ['./iscsi-target-image-settings-modal.component.scss']
15 })
16 export class IscsiTargetImageSettingsModalComponent implements OnInit {
17   image: string;
18   imagesSettings: any;
19   api_version: number;
20   disk_default_controls: any;
21   disk_controls_limits: any;
22   backstores: any;
23   control: AbstractControl;
24
25   settingsForm: CdFormGroup;
26
27   constructor(
28     public activeModal: NgbActiveModal,
29     public iscsiService: IscsiService,
30     public actionLabels: ActionLabelsI18n
31   ) {}
32
33   ngOnInit() {
34     const fg: Record<string, FormControl> = {
35       backstore: new FormControl(this.imagesSettings[this.image]['backstore']),
36       lun: new FormControl(this.imagesSettings[this.image]['lun']),
37       wwn: new FormControl(this.imagesSettings[this.image]['wwn'])
38     };
39     _.forEach(this.backstores, (backstore) => {
40       const model = this.imagesSettings[this.image][backstore] || {};
41       _.forIn(this.disk_default_controls[backstore], (_value, key) => {
42         fg[key] = new FormControl(model[key]);
43       });
44     });
45
46     this.settingsForm = new CdFormGroup(fg);
47   }
48
49   getDiskControlLimits(backstore: string, setting: string) {
50     if (this.disk_controls_limits) {
51       return this.disk_controls_limits[backstore][setting];
52     }
53     // backward compatibility
54     return { type: 'int' };
55   }
56
57   save() {
58     const backstore = this.settingsForm.controls['backstore'].value;
59     const lun = this.settingsForm.controls['lun'].value;
60     const wwn = this.settingsForm.controls['wwn'].value;
61     const settings = {};
62     _.forIn(this.settingsForm.controls, (control, key) => {
63       if (
64         !(control.value === '' || control.value === null) &&
65         key in this.disk_default_controls[this.settingsForm.value['backstore']]
66       ) {
67         settings[key] = control.value;
68         // If one setting belongs to multiple backstores, we have to update it in all backstores
69         _.forEach(this.backstores, (currentBackstore) => {
70           if (currentBackstore !== backstore) {
71             const model = this.imagesSettings[this.image][currentBackstore] || {};
72             if (key in model) {
73               this.imagesSettings[this.image][currentBackstore][key] = control.value;
74             }
75           }
76         });
77       }
78     });
79     this.imagesSettings[this.image]['backstore'] = backstore;
80     this.imagesSettings[this.image]['lun'] = lun;
81     this.imagesSettings[this.image]['wwn'] = wwn;
82     this.imagesSettings[this.image][backstore] = settings;
83     this.imagesSettings = { ...this.imagesSettings };
84     this.control.updateValueAndValidity({ emitEvent: false });
85     this.activeModal.close();
86   }
87 }