1 import { Component, OnInit } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
4 import * as _ from 'lodash';
5 import { BsModalRef } from 'ngx-bootstrap/modal';
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 {
18 disk_default_controls: any;
19 disk_controls_limits: any;
22 settingsForm: CdFormGroup;
25 constructor(public modalRef: BsModalRef, public iscsiService: IscsiService) {}
28 this.helpText = this.iscsiService.imageAdvancedSettings;
31 backstore: new FormControl(this.imagesSettings[this.image]['backstore'])
33 _.forEach(this.backstores, (backstore) => {
34 const model = this.imagesSettings[this.image][backstore] || {};
35 _.forIn(this.disk_default_controls[backstore], (_value, key) => {
36 const validators = [];
37 if (this.disk_controls_limits && key in this.disk_controls_limits[backstore]) {
38 if ('min' in this.disk_controls_limits[backstore][key]) {
39 validators.push(Validators.min(this.disk_controls_limits[backstore][key]['min']));
41 if ('max' in this.disk_controls_limits[backstore][key]) {
42 validators.push(Validators.max(this.disk_controls_limits[backstore][key]['max']));
45 fg[key] = new FormControl(model[key], {
46 validators: validators
51 this.settingsForm = new CdFormGroup(fg);
55 const backstore = this.settingsForm.controls['backstore'].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][backstore] = settings;
76 this.imagesSettings = { ...this.imagesSettings };