]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-discovery-modal/iscsi-target-discovery-modal.component.ts
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / block / iscsi-target-discovery-modal / iscsi-target-discovery-modal.component.ts
CommitLineData
11fdf7f2
TL
1import { Component, OnInit } from '@angular/core';
2import { FormControl, Validators } from '@angular/forms';
3
4import { I18n } from '@ngx-translate/i18n-polyfill';
5import { BsModalRef } from 'ngx-bootstrap/modal';
6
7import { IscsiService } from '../../../shared/api/iscsi.service';
8import { NotificationType } from '../../../shared/enum/notification-type.enum';
9import { CdFormGroup } from '../../../shared/forms/cd-form-group';
10import { CdValidators } from '../../../shared/forms/cd-validators';
81eedcae
TL
11import { Permission } from '../../../shared/models/permissions';
12import { AuthStorageService } from '../../../shared/services/auth-storage.service';
11fdf7f2
TL
13import { NotificationService } from '../../../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})
20export class IscsiTargetDiscoveryModalComponent implements OnInit {
21 discoveryForm: CdFormGroup;
81eedcae
TL
22 permission: Permission;
23 hasPermission: boolean;
11fdf7f2
TL
24
25 USER_REGEX = /[\w\.:@_-]{8,64}/;
26 PASSWORD_REGEX = /[\w@\-_\/]{12,16}/;
27
28 constructor(
81eedcae 29 private authStorageService: AuthStorageService,
11fdf7f2
TL
30 public bsModalRef: BsModalRef,
31 private iscsiService: IscsiService,
32 private notificationService: NotificationService,
33 private i18n: I18n
34 ) {
81eedcae
TL
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() {
11fdf7f2 47 this.discoveryForm = new CdFormGroup({
81eedcae
TL
48 user: new FormControl({ value: '', disabled: !this.hasPermission }),
49 password: new FormControl({ value: '', disabled: !this.hasPermission }),
50 mutual_user: new FormControl({ value: '', disabled: !this.hasPermission }),
51 mutual_password: new FormControl({ value: '', disabled: !this.hasPermission })
11fdf7f2
TL
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
11fdf7f2
TL
109 submitAction() {
110 this.iscsiService.updateDiscovery(this.discoveryForm.value).subscribe(
111 () => {
112 this.notificationService.show(
113 NotificationType.success,
114 this.i18n('Updated discovery authentication')
115 );
116 this.bsModalRef.hide();
117 },
118 () => {
92f5a8d4 119 this.discoveryForm.setErrors({ cdSubmitButton: true });
11fdf7f2
TL
120 }
121 );
122 }
123}