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';
18 selector: 'cd-rgw-multisite-migrate',
19 templateUrl: './rgw-multisite-migrate.component.html',
20 styleUrls: ['./rgw-multisite-migrate.component.scss']
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;
28 submitAction = new EventEmitter();
30 multisiteMigrateForm: CdFormGroup;
32 realmList: RgwRealm[];
33 multisiteInfo: object[] = [];
35 zonegroupList: RgwZonegroup[];
36 zonegroupNames: string[];
39 zonegroup: RgwZonegroup;
41 newZonegroupName: any;
43 bsModalRef: NgbModalRef;
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
60 this.multisiteMigrateForm = new CdFormGroup({
61 realmName: new FormControl(null, {
64 CdValidators.custom('uniqueName', (realmName: string) => {
65 return this.realmNames && this.zoneNames.indexOf(realmName) !== -1;
69 zonegroupName: new FormControl(null, {
72 CdValidators.custom('uniqueName', (zonegroupName: string) => {
73 return this.zonegroupNames && this.zoneNames.indexOf(zonegroupName) !== -1;
77 zoneName: new FormControl(null, {
80 CdValidators.custom('uniqueName', (zoneName: string) => {
81 return this.zoneNames && this.zoneNames.indexOf(zoneName) !== -1;
85 zone_endpoints: new FormControl([], {
87 CdValidators.custom('endpoint', (value: string) => {
88 if (_.isEmpty(value)) {
91 if (value.includes(',')) {
92 value.split(',').forEach((url: string) => {
94 !this.endpoints.test(url) && !this.ipv4Rgx.test(url) && !this.ipv6Rgx.test(url)
99 !this.endpoints.test(value) &&
100 !this.ipv4Rgx.test(value) &&
101 !this.ipv6Rgx.test(value)
110 zonegroup_endpoints: new FormControl(
113 CdValidators.custom('endpoint', (value: string) => {
114 if (_.isEmpty(value)) {
117 if (value.includes(',')) {
118 value.split(',').forEach((url: string) => {
120 !this.endpoints.test(url) && !this.ipv4Rgx.test(url) && !this.ipv6Rgx.test(url)
125 !this.endpoints.test(value) &&
126 !this.ipv4Rgx.test(value) &&
127 !this.ipv6Rgx.test(value)
136 users: new FormControl(null)
142 this.multisiteInfo[0] !== undefined && this.multisiteInfo[0].hasOwnProperty('realms')
143 ? this.multisiteInfo[0]['realms']
145 this.realmNames = this.realmList.map((realm) => {
146 return realm['name'];
149 this.multisiteInfo[1] !== undefined && this.multisiteInfo[1].hasOwnProperty('zonegroups')
150 ? this.multisiteInfo[1]['zonegroups']
152 this.zonegroupNames = this.zonegroupList.map((zonegroup) => {
153 return zonegroup['name'];
156 this.multisiteInfo[2] !== undefined && this.multisiteInfo[2].hasOwnProperty('zones')
157 ? this.multisiteInfo[2]['zones']
159 this.zoneNames = this.zoneList.map((zone) => {
162 this.rgwZoneService.getUserList('default').subscribe((users: any) => {
163 this.users = users.filter((user: any) => user['system'] === true);
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(
180 this.notificationService.show(
181 NotificationType.success,
182 $localize`${this.actionLabels.MIGRATE} done successfully`
184 this.submitAction.emit();
185 this.activeModal.close();
188 this.notificationService.show(NotificationType.error, $localize`Migration failed`);
193 checkUrlArray(endpoints: string) {
194 let endpointsArray = [];
195 if (endpoints.includes(',')) {
196 endpointsArray = endpoints.split(',');
198 endpointsArray.push(endpoints);
200 return endpointsArray;