summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-image-settings-modal/iscsi-target-image-settings-modal.component.ts
blob: e9c9c7d90daae3e8a2918383e58d3d027c668c2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { Component, OnInit } from '@angular/core';
import { AbstractControl, FormControl } from '@angular/forms';

import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import _ from 'lodash';

import { IscsiService } from '~/app/shared/api/iscsi.service';
import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
import { CdFormGroup } from '~/app/shared/forms/cd-form-group';

@Component({
  selector: 'cd-iscsi-target-image-settings-modal',
  templateUrl: './iscsi-target-image-settings-modal.component.html',
  styleUrls: ['./iscsi-target-image-settings-modal.component.scss']
})
export class IscsiTargetImageSettingsModalComponent implements OnInit {
  image: string;
  imagesSettings: any;
  api_version: number;
  disk_default_controls: any;
  disk_controls_limits: any;
  backstores: any;
  control: AbstractControl;

  settingsForm: CdFormGroup;

  constructor(
    public activeModal: NgbActiveModal,
    public iscsiService: IscsiService,
    public actionLabels: ActionLabelsI18n
  ) {}

  ngOnInit() {
    const fg: Record<string, FormControl> = {
      backstore: new FormControl(this.imagesSettings[this.image]['backstore']),
      lun: new FormControl(this.imagesSettings[this.image]['lun']),
      wwn: new FormControl(this.imagesSettings[this.image]['wwn'])
    };
    _.forEach(this.backstores, (backstore) => {
      const model = this.imagesSettings[this.image][backstore] || {};
      _.forIn(this.disk_default_controls[backstore], (_value, key) => {
        fg[key] = new FormControl(model[key]);
      });
    });

    this.settingsForm = new CdFormGroup(fg);
  }

  getDiskControlLimits(backstore: string, setting: string) {
    if (this.disk_controls_limits) {
      return this.disk_controls_limits[backstore][setting];
    }
    // backward compatibility
    return { type: 'int' };
  }

  save() {
    const backstore = this.settingsForm.controls['backstore'].value;
    const lun = this.settingsForm.controls['lun'].value;
    const wwn = this.settingsForm.controls['wwn'].value;
    const settings = {};
    _.forIn(this.settingsForm.controls, (control, key) => {
      if (
        !(control.value === '' || control.value === null) &&
        key in this.disk_default_controls[this.settingsForm.value['backstore']]
      ) {
        settings[key] = control.value;
        // If one setting belongs to multiple backstores, we have to update it in all backstores
        _.forEach(this.backstores, (currentBackstore) => {
          if (currentBackstore !== backstore) {
            const model = this.imagesSettings[this.image][currentBackstore] || {};
            if (key in model) {
              this.imagesSettings[this.image][currentBackstore][key] = control.value;
            }
          }
        });
      }
    });
    this.imagesSettings[this.image]['backstore'] = backstore;
    this.imagesSettings[this.image]['lun'] = lun;
    this.imagesSettings[this.image]['wwn'] = wwn;
    this.imagesSettings[this.image][backstore] = settings;
    this.imagesSettings = { ...this.imagesSettings };
    this.control.updateValueAndValidity({ emitEvent: false });
    this.activeModal.close();
  }
}