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';
19 selector: 'cd-rgw-multisite-migrate',
20 templateUrl: './rgw-multisite-migrate.component.html',
21 styleUrls: ['./rgw-multisite-migrate.component.scss']
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;
29 submitAction = new EventEmitter();
31 multisiteMigrateForm: CdFormGroup;
33 realmList: RgwRealm[];
34 multisiteInfo: object[] = [];
36 zonegroupList: RgwZonegroup[];
37 zonegroupNames: string[];
40 zonegroup: RgwZonegroup;
42 newZonegroupName: any;
44 bsModalRef: NgbModalRef;
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
62 this.multisiteMigrateForm = new CdFormGroup({
63 realmName: new UntypedFormControl(null, {
66 CdValidators.custom('uniqueName', (realmName: string) => {
67 return this.realmNames && this.zoneNames.indexOf(realmName) !== -1;
71 zonegroupName: new UntypedFormControl(null, {
74 CdValidators.custom('uniqueName', (zonegroupName: string) => {
75 return this.zonegroupNames && this.zoneNames.indexOf(zonegroupName) !== -1;
79 zoneName: new UntypedFormControl(null, {
82 CdValidators.custom('uniqueName', (zoneName: string) => {
83 return this.zoneNames && this.zoneNames.indexOf(zoneName) !== -1;
87 zone_endpoints: new UntypedFormControl([], {
89 CdValidators.custom('endpoint', (value: string) => {
90 if (_.isEmpty(value)) {
93 if (value.includes(',')) {
94 value.split(',').forEach((url: string) => {
96 !this.endpoints.test(url) && !this.ipv4Rgx.test(url) && !this.ipv6Rgx.test(url)
101 !this.endpoints.test(value) &&
102 !this.ipv4Rgx.test(value) &&
103 !this.ipv6Rgx.test(value)
112 zonegroup_endpoints: new UntypedFormControl(
115 CdValidators.custom('endpoint', (value: string) => {
116 if (_.isEmpty(value)) {
119 if (value.includes(',')) {
120 value.split(',').forEach((url: string) => {
122 !this.endpoints.test(url) && !this.ipv4Rgx.test(url) && !this.ipv6Rgx.test(url)
127 !this.endpoints.test(value) &&
128 !this.ipv4Rgx.test(value) &&
129 !this.ipv6Rgx.test(value)
138 username: new UntypedFormControl(null, {
139 validators: [Validators.required]
146 this.multisiteInfo[0] !== undefined && this.multisiteInfo[0].hasOwnProperty('realms')
147 ? this.multisiteInfo[0]['realms']
149 this.realmNames = this.realmList.map((realm) => {
150 return realm['name'];
153 this.multisiteInfo[1] !== undefined && this.multisiteInfo[1].hasOwnProperty('zonegroups')
154 ? this.multisiteInfo[1]['zonegroups']
156 this.zonegroupNames = this.zonegroupList.map((zonegroup) => {
157 return zonegroup['name'];
160 this.multisiteInfo[2] !== undefined && this.multisiteInfo[2].hasOwnProperty('zones')
161 ? this.multisiteInfo[2]['zones']
163 this.zoneNames = this.zoneList.map((zone) => {
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'])
182 this.rgwMultisiteService.setRestartGatewayMessage(false);
183 this.notificationService.show(
184 NotificationType.success,
185 $localize`Migration done successfully`
187 this.submitAction.emit();
188 this.activeModal.close();
191 this.notificationService.show(NotificationType.error, $localize`Migration failed`);