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 { RgwUserService } from '~/app/shared/api/rgw-user.service';
6 import { RgwZoneService } from '~/app/shared/api/rgw-zone.service';
7 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
8 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
9 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
10 import { CdValidators } from '~/app/shared/forms/cd-validators';
11 import { NotificationService } from '~/app/shared/services/notification.service';
12 import { RgwRealm, RgwZone, RgwZonegroup } from '../models/rgw-multisite';
15 selector: 'cd-rgw-multisite-zone-form',
16 templateUrl: './rgw-multisite-zone-form.component.html',
17 styleUrls: ['./rgw-multisite-zone-form.component.scss']
19 export class RgwMultisiteZoneFormComponent implements OnInit {
20 readonly endpoints = /^((https?:\/\/)|(www.))(?:([a-zA-Z]+)|(\d+\.\d+.\d+.\d+)):\d{2,4}$/;
21 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;
22 readonly ipv6Rgx = /^(?:[a-f0-9]{1,4}:){7}[a-f0-9]{1,4}$/i;
24 multisiteZoneForm: CdFormGroup;
28 zonegroup: RgwZonegroup;
30 defaultsInfo: string[] = [];
31 multisiteInfo: object[] = [];
32 zonegroupList: RgwZonegroup[] = [];
33 zoneList: RgwZone[] = [];
38 public activeModal: NgbActiveModal,
39 public actionLabels: ActionLabelsI18n,
40 public rgwZoneService: RgwZoneService,
41 public notificationService: NotificationService,
42 public rgwUserService: RgwUserService
44 this.action = this.editing
45 ? this.actionLabels.EDIT + this.resource
46 : this.actionLabels.CREATE + this.resource;
51 this.multisiteZoneForm = new CdFormGroup({
52 zoneName: new FormControl(null, {
55 CdValidators.custom('uniqueName', (zoneName: string) => {
56 return this.zoneNames && this.zoneNames.indexOf(zoneName) !== -1;
60 default_zone: new FormControl(false),
61 master_zone: new FormControl(false),
62 selectedZonegroup: new FormControl(null),
63 zone_endpoints: new FormControl(null, {
65 CdValidators.custom('endpoint', (value: string) => {
66 if (_.isEmpty(value)) {
69 if (value.includes(',')) {
70 value.split(',').forEach((url: string) => {
72 !this.endpoints.test(url) && !this.ipv4Rgx.test(url) && !this.ipv6Rgx.test(url)
77 !this.endpoints.test(value) &&
78 !this.ipv4Rgx.test(value) &&
79 !this.ipv6Rgx.test(value)
87 users: new FormControl(null)
93 this.multisiteInfo[1] !== undefined && this.multisiteInfo[1].hasOwnProperty('zonegroups')
94 ? this.multisiteInfo[1]['zonegroups']
97 this.multisiteInfo[2] !== undefined && this.multisiteInfo[2].hasOwnProperty('zones')
98 ? this.multisiteInfo[2]['zones']
100 this.zoneNames = this.zoneList.map((zone) => {
103 if (this.action === 'create') {
104 this.multisiteZoneForm
105 .get('selectedZonegroup')
106 .setValue(this.defaultsInfo['defaultZonegroupName']);
108 this.rgwUserService.list().subscribe((users: any) => {
109 this.users = users.filter((user: any) => user.keys.length !== 0);
114 const values = this.multisiteZoneForm.value;
115 this.zonegroup = new RgwZonegroup();
116 this.zonegroup.name = values['selectedZonegroup'];
117 this.zone = new RgwZone();
118 this.zone.name = values['zoneName'];
123 values['default_zone'],
124 values['master_zone'],
125 values['zone_endpoints'],
130 this.notificationService.show(
131 NotificationType.success,
132 $localize`Zone: '${values['zoneName']}' created successfully`
134 this.activeModal.close();
137 this.multisiteZoneForm.setErrors({ cdSubmitButton: true });