summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-discovery-modal/iscsi-target-discovery-modal.component.ts
blob: 68958cfaa2b01ed0bd4bc63fb079aebe5f850357 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { Component, OnInit } from '@angular/core';
import { FormControl, Validators } from '@angular/forms';

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

import { IscsiService } from '~/app/shared/api/iscsi.service';
import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
import { NotificationType } from '~/app/shared/enum/notification-type.enum';
import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
import { CdValidators } from '~/app/shared/forms/cd-validators';
import { Permission } from '~/app/shared/models/permissions';
import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
import { NotificationService } from '~/app/shared/services/notification.service';

@Component({
  selector: 'cd-iscsi-target-discovery-modal',
  templateUrl: './iscsi-target-discovery-modal.component.html',
  styleUrls: ['./iscsi-target-discovery-modal.component.scss']
})
export class IscsiTargetDiscoveryModalComponent implements OnInit {
  discoveryForm: CdFormGroup;
  permission: Permission;
  hasPermission: boolean;

  USER_REGEX = /^[\w\.:@_-]{8,64}$/;
  PASSWORD_REGEX = /^[\w@\-_\/]{12,16}$/;

  constructor(
    private authStorageService: AuthStorageService,
    public activeModal: NgbActiveModal,
    public actionLabels: ActionLabelsI18n,
    private iscsiService: IscsiService,
    private notificationService: NotificationService
  ) {
    this.permission = this.authStorageService.getPermissions().iscsi;
  }

  ngOnInit() {
    this.hasPermission = this.permission.update;
    this.createForm();
    this.iscsiService.getDiscovery().subscribe((auth) => {
      this.discoveryForm.patchValue(auth);
    });
  }

  createForm() {
    this.discoveryForm = new CdFormGroup({
      user: new FormControl({ value: '', disabled: !this.hasPermission }),
      password: new FormControl({ value: '', disabled: !this.hasPermission }),
      mutual_user: new FormControl({ value: '', disabled: !this.hasPermission }),
      mutual_password: new FormControl({ value: '', disabled: !this.hasPermission })
    });

    CdValidators.validateIf(
      this.discoveryForm.get('user'),
      () =>
        this.discoveryForm.getValue('password') ||
        this.discoveryForm.getValue('mutual_user') ||
        this.discoveryForm.getValue('mutual_password'),
      [Validators.required],
      [Validators.pattern(this.USER_REGEX)],
      [
        this.discoveryForm.get('password'),
        this.discoveryForm.get('mutual_user'),
        this.discoveryForm.get('mutual_password')
      ]
    );

    CdValidators.validateIf(
      this.discoveryForm.get('password'),
      () =>
        this.discoveryForm.getValue('user') ||
        this.discoveryForm.getValue('mutual_user') ||
        this.discoveryForm.getValue('mutual_password'),
      [Validators.required],
      [Validators.pattern(this.PASSWORD_REGEX)],
      [
        this.discoveryForm.get('user'),
        this.discoveryForm.get('mutual_user'),
        this.discoveryForm.get('mutual_password')
      ]
    );

    CdValidators.validateIf(
      this.discoveryForm.get('mutual_user'),
      () => this.discoveryForm.getValue('mutual_password'),
      [Validators.required],
      [Validators.pattern(this.USER_REGEX)],
      [
        this.discoveryForm.get('user'),
        this.discoveryForm.get('password'),
        this.discoveryForm.get('mutual_password')
      ]
    );

    CdValidators.validateIf(
      this.discoveryForm.get('mutual_password'),
      () => this.discoveryForm.getValue('mutual_user'),
      [Validators.required],
      [Validators.pattern(this.PASSWORD_REGEX)],
      [
        this.discoveryForm.get('user'),
        this.discoveryForm.get('password'),
        this.discoveryForm.get('mutual_user')
      ]
    );
  }

  submitAction() {
    this.iscsiService.updateDiscovery(this.discoveryForm.value).subscribe(
      () => {
        this.notificationService.show(
          NotificationType.success,
          $localize`Updated discovery authentication`
        );
        this.activeModal.close();
      },
      () => {
        this.discoveryForm.setErrors({ cdSubmitButton: true });
      }
    );
  }
}