]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
df121a4a5b4bad5aa4c9f0837043217b4f1aab0c
[ceph-ci.git] /
1 import { Component, EventEmitter, OnInit, Output } from '@angular/core';
2 import { UntypedFormControl, Validators } from '@angular/forms';
3 import { NgbActiveModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
4 import _ from 'lodash';
5 import { RgwMultisiteService } from '~/app/shared/api/rgw-multisite.service';
6 import { RgwRealmService } from '~/app/shared/api/rgw-realm.service';
7 import { RgwZoneService } from '~/app/shared/api/rgw-zone.service';
8 import { RgwZonegroupService } from '~/app/shared/api/rgw-zonegroup.service';
9 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
10 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
11 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
12 import { CdValidators } from '~/app/shared/forms/cd-validators';
13 import { NotificationService } from '~/app/shared/services/notification.service';
14 import { RgwRealm, RgwZone, RgwZonegroup, SystemKey } from '../models/rgw-multisite';
15 import { ModalService } from '~/app/shared/services/modal.service';
16 import { RgwDaemonService } from '~/app/shared/api/rgw-daemon.service';
17
18 @Component({
19   selector: 'cd-rgw-multisite-migrate',
20   templateUrl: './rgw-multisite-migrate.component.html',
21   styleUrls: ['./rgw-multisite-migrate.component.scss']
22 })
23 export class RgwMultisiteMigrateComponent implements OnInit {
24   readonly endpoints = /^((https?:\/\/)|(www.))(?:([a-zA-Z]+)|(\d+\.\d+.\d+.\d+)):\d{2,4}$/;
25   readonly ipv4Rgx = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/i;
26   readonly ipv6Rgx = /^(?:[a-f0-9]{1,4}:){7}[a-f0-9]{1,4}$/i;
27
28   @Output()
29   submitAction = new EventEmitter();
30
31   multisiteMigrateForm: CdFormGroup;
32   zoneNames: string[];
33   realmList: RgwRealm[];
34   multisiteInfo: object[] = [];
35   realmNames: string[];
36   zonegroupList: RgwZonegroup[];
37   zonegroupNames: string[];
38   zoneList: RgwZone[];
39   realm: RgwRealm;
40   zonegroup: RgwZonegroup;
41   zone: RgwZone;
42   newZonegroupName: any;
43   newZoneName: any;
44   bsModalRef: NgbModalRef;
45   users: any;
46
47   constructor(
48     public activeModal: NgbActiveModal,
49     public actionLabels: ActionLabelsI18n,
50     public rgwMultisiteService: RgwMultisiteService,
51     public rgwZoneService: RgwZoneService,
52     public notificationService: NotificationService,
53     public rgwZonegroupService: RgwZonegroupService,
54     public rgwRealmService: RgwRealmService,
55     public rgwDaemonService: RgwDaemonService,
56     public modalService: ModalService
57   ) {
58     this.createForm();
59   }
60
61   createForm() {
62     this.multisiteMigrateForm = new CdFormGroup({
63       realmName: new UntypedFormControl(null, {
64         validators: [
65           Validators.required,
66           CdValidators.custom('uniqueName', (realmName: string) => {
67             return this.realmNames && this.zoneNames.indexOf(realmName) !== -1;
68           })
69         ]
70       }),
71       zonegroupName: new UntypedFormControl(null, {
72         validators: [
73           Validators.required,
74           CdValidators.custom('uniqueName', (zonegroupName: string) => {
75             return this.zonegroupNames && this.zoneNames.indexOf(zonegroupName) !== -1;
76           })
77         ]
78       }),
79       zoneName: new UntypedFormControl(null, {
80         validators: [
81           Validators.required,
82           CdValidators.custom('uniqueName', (zoneName: string) => {
83             return this.zoneNames && this.zoneNames.indexOf(zoneName) !== -1;
84           })
85         ]
86       }),
87       zone_endpoints: new UntypedFormControl([], {
88         validators: [
89           CdValidators.custom('endpoint', (value: string) => {
90             if (_.isEmpty(value)) {
91               return false;
92             } else {
93               if (value.includes(',')) {
94                 value.split(',').forEach((url: string) => {
95                   return (
96                     !this.endpoints.test(url) && !this.ipv4Rgx.test(url) && !this.ipv6Rgx.test(url)
97                   );
98                 });
99               } else {
100                 return (
101                   !this.endpoints.test(value) &&
102                   !this.ipv4Rgx.test(value) &&
103                   !this.ipv6Rgx.test(value)
104                 );
105               }
106               return false;
107             }
108           }),
109           Validators.required
110         ]
111       }),
112       zonegroup_endpoints: new UntypedFormControl(
113         [],
114         [
115           CdValidators.custom('endpoint', (value: string) => {
116             if (_.isEmpty(value)) {
117               return false;
118             } else {
119               if (value.includes(',')) {
120                 value.split(',').forEach((url: string) => {
121                   return (
122                     !this.endpoints.test(url) && !this.ipv4Rgx.test(url) && !this.ipv6Rgx.test(url)
123                   );
124                 });
125               } else {
126                 return (
127                   !this.endpoints.test(value) &&
128                   !this.ipv4Rgx.test(value) &&
129                   !this.ipv6Rgx.test(value)
130                 );
131               }
132               return false;
133             }
134           }),
135           Validators.required
136         ]
137       ),
138       access_key: new UntypedFormControl(null),
139       secret_key: new UntypedFormControl(null)
140     });
141   }
142
143   ngOnInit(): void {
144     this.realmList =
145       this.multisiteInfo[0] !== undefined && this.multisiteInfo[0].hasOwnProperty('realms')
146         ? this.multisiteInfo[0]['realms']
147         : [];
148     this.realmNames = this.realmList.map((realm) => {
149       return realm['name'];
150     });
151     this.zonegroupList =
152       this.multisiteInfo[1] !== undefined && this.multisiteInfo[1].hasOwnProperty('zonegroups')
153         ? this.multisiteInfo[1]['zonegroups']
154         : [];
155     this.zonegroupNames = this.zonegroupList.map((zonegroup) => {
156       return zonegroup['name'];
157     });
158     this.zoneList =
159       this.multisiteInfo[2] !== undefined && this.multisiteInfo[2].hasOwnProperty('zones')
160         ? this.multisiteInfo[2]['zones']
161         : [];
162     this.zoneNames = this.zoneList.map((zone) => {
163       return zone['name'];
164     });
165   }
166
167   submit() {
168     const values = this.multisiteMigrateForm.value;
169     this.realm = new RgwRealm();
170     this.realm.name = values['realmName'];
171     this.zonegroup = new RgwZonegroup();
172     this.zonegroup.name = values['zonegroupName'];
173     this.zonegroup.endpoints = values['zonegroup_endpoints'];
174     this.zone = new RgwZone();
175     this.zone.name = values['zoneName'];
176     this.zone.endpoints = values['zone_endpoints'];
177     this.zone.system_key = new SystemKey();
178     this.zone.system_key.access_key = values['access_key'];
179     this.zone.system_key.secret_key = values['secret_key'];
180     this.rgwMultisiteService.migrate(this.realm, this.zonegroup, this.zone).subscribe(
181       () => {
182         this.notificationService.show(
183           NotificationType.success,
184           $localize`${this.actionLabels.MIGRATE} done successfully`
185         );
186         this.notificationService.show(NotificationType.success, `Daemon restart scheduled`);
187         this.submitAction.emit();
188         this.activeModal.close();
189       },
190       () => {
191         this.notificationService.show(NotificationType.error, $localize`Migration failed`);
192       }
193     );
194   }
195 }