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[];
35 public activeModal: NgbActiveModal,
36 public actionLabels: ActionLabelsI18n,
37 public rgwZonegroupService: RgwZonegroupService,
38 public notificationService: NotificationService
40 this.action = this.editing
41 ? this.actionLabels.EDIT + this.resource
42 : this.actionLabels.CREATE + this.resource;
47 this.multisiteZonegroupForm = new CdFormGroup({
48 default_zonegroup: new FormControl(false),
49 zonegroupName: new FormControl(null, {
52 CdValidators.custom('uniqueName', (zonegroupName: string) => {
53 return this.zonegroupNames && this.zonegroupNames.indexOf(zonegroupName) !== -1;
57 master_zonegroup: new FormControl(false),
58 selectedRealm: new FormControl(null),
59 zonegroup_endpoints: new FormControl(null, [
60 CdValidators.custom('endpoint', (value: string) => {
61 if (_.isEmpty(value)) {
64 if (value.includes(',')) {
65 value.split(',').forEach((url: string) => {
67 !this.endpoints.test(url) && !this.ipv4Rgx.test(url) && !this.ipv6Rgx.test(url)
72 !this.endpoints.test(value) &&
73 !this.ipv4Rgx.test(value) &&
74 !this.ipv6Rgx.test(value)
87 this.multisiteInfo[0] !== undefined && this.multisiteInfo[0].hasOwnProperty('realms')
88 ? this.multisiteInfo[0]['realms']
91 this.multisiteInfo[1] !== undefined && this.multisiteInfo[1].hasOwnProperty('zonegroups')
92 ? this.multisiteInfo[1]['zonegroups']
94 this.zonegroupNames = this.zonegroupList.map((zonegroup) => {
95 return zonegroup['name'];
97 if (this.action === 'create' && this.defaultsInfo['defaultRealmName'] !== null) {
98 this.multisiteZonegroupForm
100 .setValue(this.defaultsInfo['defaultRealmName']);
105 const values = this.multisiteZonegroupForm.value;
106 this.realm = new RgwRealm();
107 this.realm.name = values['selectedRealm'];
108 this.zonegroup = new RgwZonegroup();
109 this.zonegroup.name = values['zonegroupName'];
110 this.zonegroup.endpoints = this.checkUrlArray(values['zonegroup_endpoints']);
111 this.rgwZonegroupService
112 .create(this.realm, this.zonegroup, values['default_zonegroup'], values['master_zonegroup'])
115 this.notificationService.show(
116 NotificationType.success,
117 $localize`Zonegroup: '${values['zonegroupName']}' created successfully`
119 this.activeModal.close();
122 this.multisiteZonegroupForm.setErrors({ cdSubmitButton: true });
127 checkUrlArray(endpoints: string) {
128 let endpointsArray = [];
129 if (endpoints.includes(',')) {
130 endpointsArray = endpoints.split(',');
132 endpointsArray.push(endpoints);
134 return endpointsArray;