1 import { Component, OnInit } from '@angular/core';
2 import { AbstractControl, FormControl } from '@angular/forms';
4 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
5 import _ from 'lodash';
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';
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']
16 export class IscsiTargetImageSettingsModalComponent implements OnInit {
20 disk_default_controls: any;
21 disk_controls_limits: any;
23 control: AbstractControl;
25 settingsForm: CdFormGroup;
28 public activeModal: NgbActiveModal,
29 public iscsiService: IscsiService,
30 public actionLabels: ActionLabelsI18n
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'])
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]);
46 this.settingsForm = new CdFormGroup(fg);
49 getDiskControlLimits(backstore: string, setting: string) {
50 if (this.disk_controls_limits) {
51 return this.disk_controls_limits[backstore][setting];
53 // backward compatibility
54 return { type: 'int' };
58 const backstore = this.settingsForm.controls['backstore'].value;
59 const lun = this.settingsForm.controls['lun'].value;
60 const wwn = this.settingsForm.controls['wwn'].value;
62 _.forIn(this.settingsForm.controls, (control, key) => {
64 !(control.value === '' || control.value === null) &&
65 key in this.disk_default_controls[this.settingsForm.value['backstore']]
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] || {};
73 this.imagesSettings[this.image][currentBackstore][key] = control.value;
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();