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