]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
d9ad56a5bf4da977eb538cf3ce96b71ddbf539ff
[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 } 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       username: new UntypedFormControl(null, {
139         validators: [Validators.required]
140       })
141     });
142   }
143
144   ngOnInit(): void {
145     this.realmList =
146       this.multisiteInfo[0] !== undefined && this.multisiteInfo[0].hasOwnProperty('realms')
147         ? this.multisiteInfo[0]['realms']
148         : [];
149     this.realmNames = this.realmList.map((realm) => {
150       return realm['name'];
151     });
152     this.zonegroupList =
153       this.multisiteInfo[1] !== undefined && this.multisiteInfo[1].hasOwnProperty('zonegroups')
154         ? this.multisiteInfo[1]['zonegroups']
155         : [];
156     this.zonegroupNames = this.zonegroupList.map((zonegroup) => {
157       return zonegroup['name'];
158     });
159     this.zoneList =
160       this.multisiteInfo[2] !== undefined && this.multisiteInfo[2].hasOwnProperty('zones')
161         ? this.multisiteInfo[2]['zones']
162         : [];
163     this.zoneNames = this.zoneList.map((zone) => {
164       return zone['name'];
165     });
166   }
167
168   submit() {
169     const values = this.multisiteMigrateForm.value;
170     this.realm = new RgwRealm();
171     this.realm.name = values['realmName'];
172     this.zonegroup = new RgwZonegroup();
173     this.zonegroup.name = values['zonegroupName'];
174     this.zonegroup.endpoints = values['zonegroup_endpoints'];
175     this.zone = new RgwZone();
176     this.zone.name = values['zoneName'];
177     this.zone.endpoints = values['zone_endpoints'];
178     this.rgwMultisiteService
179       .migrate(this.realm, this.zonegroup, this.zone, values['username'])
180       .subscribe(
181         () => {
182           this.rgwMultisiteService.setRestartGatewayMessage(false);
183           this.notificationService.show(
184             NotificationType.success,
185             $localize`Migration done successfully`
186           );
187           this.submitAction.emit();
188           this.activeModal.close();
189         },
190         () => {
191           this.notificationService.show(NotificationType.error, $localize`Migration failed`);
192         }
193       );
194   }
195 }