1 import { Component, OnInit } from '@angular/core';
2 import { AbstractControl, FormControl } from '@angular/forms';
4 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
5 import * as _ from 'lodash';
7 import { IscsiService } from '../../../shared/api/iscsi.service';
8 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
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']
15 export class IscsiTargetImageSettingsModalComponent implements OnInit {
19 disk_default_controls: any;
20 disk_controls_limits: any;
22 control: AbstractControl;
24 settingsForm: CdFormGroup;
26 constructor(public activeModal: NgbActiveModal, public iscsiService: IscsiService) {}
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'])
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]);
41 this.settingsForm = new CdFormGroup(fg);
44 getDiskControlLimits(backstore: string, setting: string) {
45 if (this.disk_controls_limits) {
46 return this.disk_controls_limits[backstore][setting];
48 // backward compatibility
49 return { type: 'int' };
53 const backstore = this.settingsForm.controls['backstore'].value;
54 const lun = this.settingsForm.controls['lun'].value;
55 const wwn = this.settingsForm.controls['wwn'].value;
57 _.forIn(this.settingsForm.controls, (control, key) => {
59 !(control.value === '' || control.value === null) &&
60 key in this.disk_default_controls[this.settingsForm.value['backstore']]
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] || {};
68 this.imagesSettings[this.image][currentBackstore][key] = control.value;
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();