]>
Commit | Line | Data |
---|---|---|
11fdf7f2 | 1 | import { Component, OnInit } from '@angular/core'; |
eafe8130 | 2 | import { AbstractControl, FormControl } from '@angular/forms'; |
11fdf7f2 | 3 | |
f67539c2 TL |
4 | import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; |
5 | import _ from 'lodash'; | |
11fdf7f2 | 6 | |
f67539c2 TL |
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'; | |
11fdf7f2 TL |
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; | |
eafe8130 | 19 | api_version: number; |
11fdf7f2 | 20 | disk_default_controls: any; |
eafe8130 | 21 | disk_controls_limits: any; |
11fdf7f2 | 22 | backstores: any; |
eafe8130 | 23 | control: AbstractControl; |
11fdf7f2 | 24 | |
eafe8130 | 25 | settingsForm: CdFormGroup; |
11fdf7f2 | 26 | |
f67539c2 TL |
27 | constructor( |
28 | public activeModal: NgbActiveModal, | |
29 | public iscsiService: IscsiService, | |
30 | public actionLabels: ActionLabelsI18n | |
31 | ) {} | |
11fdf7f2 TL |
32 | |
33 | ngOnInit() { | |
9f95a23c | 34 | const fg: Record<string, FormControl> = { |
eafe8130 TL |
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 | }; | |
11fdf7f2 | 39 | _.forEach(this.backstores, (backstore) => { |
eafe8130 TL |
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 | }); | |
11fdf7f2 | 44 | }); |
eafe8130 TL |
45 | |
46 | this.settingsForm = new CdFormGroup(fg); | |
47 | } | |
48 | ||
9f95a23c | 49 | getDiskControlLimits(backstore: string, setting: string) { |
eafe8130 TL |
50 | if (this.disk_controls_limits) { |
51 | return this.disk_controls_limits[backstore][setting]; | |
52 | } | |
53 | // backward compatibility | |
54 | return { type: 'int' }; | |
11fdf7f2 TL |
55 | } |
56 | ||
57 | save() { | |
eafe8130 TL |
58 | const backstore = this.settingsForm.controls['backstore'].value; |
59 | const lun = this.settingsForm.controls['lun'].value; | |
60 | const wwn = this.settingsForm.controls['wwn'].value; | |
11fdf7f2 | 61 | const settings = {}; |
eafe8130 TL |
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 | }); | |
11fdf7f2 TL |
77 | } |
78 | }); | |
79 | this.imagesSettings[this.image]['backstore'] = backstore; | |
eafe8130 TL |
80 | this.imagesSettings[this.image]['lun'] = lun; |
81 | this.imagesSettings[this.image]['wwn'] = wwn; | |
11fdf7f2 TL |
82 | this.imagesSettings[this.image][backstore] = settings; |
83 | this.imagesSettings = { ...this.imagesSettings }; | |
eafe8130 | 84 | this.control.updateValueAndValidity({ emitEvent: false }); |
f67539c2 | 85 | this.activeModal.close(); |
11fdf7f2 TL |
86 | } |
87 | } |