1 import { Component, OnInit } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
3 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
4 import _ from 'lodash';
5 import { RgwZonegroupService } from '~/app/shared/api/rgw-zonegroup.service';
6 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
7 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
8 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
9 import { CdValidators } from '~/app/shared/forms/cd-validators';
10 import { NotificationService } from '~/app/shared/services/notification.service';
11 import { RgwRealm, RgwZonegroup } from '../models/rgw-multisite';
14 selector: 'cd-rgw-multisite-zonegroup-form',
15 templateUrl: './rgw-multisite-zonegroup-form.component.html',
16 styleUrls: ['./rgw-multisite-zonegroup-form.component.scss']
18 export class RgwMultisiteZonegroupFormComponent implements OnInit {
19 readonly endpoints = /^((https?:\/\/)|(www.))(?:([a-zA-Z]+)|(\d+\.\d+.\d+.\d+)):\d{2,4}$/;
20 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;
21 readonly ipv6Rgx = /^(?:[a-f0-9]{1,4}:){7}[a-f0-9]{1,4}$/i;
23 multisiteZonegroupForm: CdFormGroup;
27 zonegroup: RgwZonegroup;
28 defaultsInfo: string[] = [];
29 multisiteInfo: object[] = [];
30 realmList: RgwRealm[] = [];
31 zonegroupList: RgwZonegroup[] = [];
32 zonegroupNames: string[];
36 public activeModal: NgbActiveModal,
37 public actionLabels: ActionLabelsI18n,
38 public rgwZonegroupService: RgwZonegroupService,
39 public notificationService: NotificationService
41 this.action = this.editing
42 ? this.actionLabels.EDIT + this.resource
43 : this.actionLabels.CREATE + this.resource;
48 this.multisiteZonegroupForm = new CdFormGroup({
49 default_zonegroup: new FormControl(false),
50 zonegroupName: new FormControl(null, {
53 CdValidators.custom('uniqueName', (zonegroupName: string) => {
54 return this.zonegroupNames && this.zonegroupNames.indexOf(zonegroupName) !== -1;
58 master_zonegroup: new FormControl(false),
59 selectedRealm: new FormControl(null),
60 zonegroup_endpoints: new FormControl(null, [
61 CdValidators.custom('endpoint', (value: string) => {
62 if (_.isEmpty(value)) {
65 if (value.includes(',')) {
66 value.split(',').forEach((url: string) => {
68 !this.endpoints.test(url) && !this.ipv4Rgx.test(url) && !this.ipv6Rgx.test(url)
73 !this.endpoints.test(value) &&
74 !this.ipv4Rgx.test(value) &&
75 !this.ipv6Rgx.test(value)
88 this.multisiteInfo[0] !== undefined && this.multisiteInfo[0].hasOwnProperty('realms')
89 ? this.multisiteInfo[0]['realms']
92 this.multisiteInfo[1] !== undefined && this.multisiteInfo[1].hasOwnProperty('zonegroups')
93 ? this.multisiteInfo[1]['zonegroups']
95 this.zonegroupList.forEach((zgp: any) => {
96 if (zgp.is_master === true && !_.isEmpty(zgp.realm_id)) {
100 if (!this.isMaster) {
101 this.multisiteZonegroupForm.get('master_zonegroup').setValue(true);
102 this.multisiteZonegroupForm.get('master_zonegroup').disable();
104 this.zonegroupNames = this.zonegroupList.map((zonegroup) => {
105 return zonegroup['name'];
107 if (this.action === 'create' && this.defaultsInfo['defaultRealmName'] !== null) {
108 this.multisiteZonegroupForm
109 .get('selectedRealm')
110 .setValue(this.defaultsInfo['defaultRealmName']);
115 const values = this.multisiteZonegroupForm.value;
116 this.realm = new RgwRealm();
117 this.realm.name = values['selectedRealm'];
118 this.zonegroup = new RgwZonegroup();
119 this.zonegroup.name = values['zonegroupName'];
120 this.zonegroup.endpoints = this.checkUrlArray(values['zonegroup_endpoints']);
121 this.rgwZonegroupService
122 .create(this.realm, this.zonegroup, values['default_zonegroup'], values['master_zonegroup'])
125 this.notificationService.show(
126 NotificationType.success,
127 $localize`Zonegroup: '${values['zonegroupName']}' created successfully`
129 this.activeModal.close();
132 this.multisiteZonegroupForm.setErrors({ cdSubmitButton: true });
137 checkUrlArray(endpoints: string) {
138 let endpointsArray = [];
139 if (endpoints.includes(',')) {
140 endpointsArray = endpoints.split(',');
142 endpointsArray.push(endpoints);
144 return endpointsArray;