]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-discovery-modal/iscsi-target-discovery-modal.component.ts
update ceph source to reef 18.2.1
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / block / iscsi-target-discovery-modal / iscsi-target-discovery-modal.component.ts
1 import { Component, OnInit } from '@angular/core';
2 import { UntypedFormControl, Validators } from '@angular/forms';
3
4 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
5
6 import { IscsiService } from '~/app/shared/api/iscsi.service';
7 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
8 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
9 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
10 import { CdValidators } from '~/app/shared/forms/cd-validators';
11 import { Permission } from '~/app/shared/models/permissions';
12 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
13 import { NotificationService } from '~/app/shared/services/notification.service';
14
15 @Component({
16 selector: 'cd-iscsi-target-discovery-modal',
17 templateUrl: './iscsi-target-discovery-modal.component.html',
18 styleUrls: ['./iscsi-target-discovery-modal.component.scss']
19 })
20 export class IscsiTargetDiscoveryModalComponent implements OnInit {
21 discoveryForm: CdFormGroup;
22 permission: Permission;
23 hasPermission: boolean;
24
25 USER_REGEX = /^[\w\.:@_-]{8,64}$/;
26 PASSWORD_REGEX = /^[\w@\-_\/]{12,16}$/;
27
28 constructor(
29 private authStorageService: AuthStorageService,
30 public activeModal: NgbActiveModal,
31 public actionLabels: ActionLabelsI18n,
32 private iscsiService: IscsiService,
33 private notificationService: NotificationService
34 ) {
35 this.permission = this.authStorageService.getPermissions().iscsi;
36 }
37
38 ngOnInit() {
39 this.hasPermission = this.permission.update;
40 this.createForm();
41 this.iscsiService.getDiscovery().subscribe((auth) => {
42 this.discoveryForm.patchValue(auth);
43 });
44 }
45
46 createForm() {
47 this.discoveryForm = new CdFormGroup({
48 user: new UntypedFormControl({ value: '', disabled: !this.hasPermission }),
49 password: new UntypedFormControl({ value: '', disabled: !this.hasPermission }),
50 mutual_user: new UntypedFormControl({ value: '', disabled: !this.hasPermission }),
51 mutual_password: new UntypedFormControl({ value: '', disabled: !this.hasPermission })
52 });
53
54 CdValidators.validateIf(
55 this.discoveryForm.get('user'),
56 () =>
57 this.discoveryForm.getValue('password') ||
58 this.discoveryForm.getValue('mutual_user') ||
59 this.discoveryForm.getValue('mutual_password'),
60 [Validators.required],
61 [Validators.pattern(this.USER_REGEX)],
62 [
63 this.discoveryForm.get('password'),
64 this.discoveryForm.get('mutual_user'),
65 this.discoveryForm.get('mutual_password')
66 ]
67 );
68
69 CdValidators.validateIf(
70 this.discoveryForm.get('password'),
71 () =>
72 this.discoveryForm.getValue('user') ||
73 this.discoveryForm.getValue('mutual_user') ||
74 this.discoveryForm.getValue('mutual_password'),
75 [Validators.required],
76 [Validators.pattern(this.PASSWORD_REGEX)],
77 [
78 this.discoveryForm.get('user'),
79 this.discoveryForm.get('mutual_user'),
80 this.discoveryForm.get('mutual_password')
81 ]
82 );
83
84 CdValidators.validateIf(
85 this.discoveryForm.get('mutual_user'),
86 () => this.discoveryForm.getValue('mutual_password'),
87 [Validators.required],
88 [Validators.pattern(this.USER_REGEX)],
89 [
90 this.discoveryForm.get('user'),
91 this.discoveryForm.get('password'),
92 this.discoveryForm.get('mutual_password')
93 ]
94 );
95
96 CdValidators.validateIf(
97 this.discoveryForm.get('mutual_password'),
98 () => this.discoveryForm.getValue('mutual_user'),
99 [Validators.required],
100 [Validators.pattern(this.PASSWORD_REGEX)],
101 [
102 this.discoveryForm.get('user'),
103 this.discoveryForm.get('password'),
104 this.discoveryForm.get('mutual_user')
105 ]
106 );
107 }
108
109 submitAction() {
110 this.iscsiService.updateDiscovery(this.discoveryForm.value).subscribe(
111 () => {
112 this.notificationService.show(
113 NotificationType.success,
114 $localize`Updated discovery authentication`
115 );
116 this.activeModal.close();
117 },
118 () => {
119 this.discoveryForm.setErrors({ cdSubmitButton: true });
120 }
121 );
122 }
123 }