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 { RgwZonegroupService } from '~/app/shared/api/rgw-zonegroup.service';
8 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
9 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
10 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
11 import { CdValidators } from '~/app/shared/forms/cd-validators';
12 import { NotificationService } from '~/app/shared/services/notification.service';
13 import { RgwRealm, RgwZone, RgwZonegroup } from '../models/rgw-multisite';
16 selector: 'cd-rgw-multisite-zone-form',
17 templateUrl: './rgw-multisite-zone-form.component.html',
18 styleUrls: ['./rgw-multisite-zone-form.component.scss']
20 export class RgwMultisiteZoneFormComponent implements OnInit {
21 readonly endpoints = /^((https?:\/\/)|(www.))(?:([a-zA-Z]+)|(\d+\.\d+.\d+.\d+)):\d{2,4}$/;
22 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;
23 readonly ipv6Rgx = /^(?:[a-f0-9]{1,4}:){7}[a-f0-9]{1,4}$/i;
25 multisiteZoneForm: CdFormGroup;
29 zonegroup: RgwZonegroup;
31 defaultsInfo: string[] = [];
32 multisiteInfo: object[] = [];
33 zonegroupList: RgwZonegroup[] = [];
34 zoneList: RgwZone[] = [];
39 public activeModal: NgbActiveModal,
40 public actionLabels: ActionLabelsI18n,
41 public rgwZoneService: RgwZoneService,
42 public rgwZoneGroupService: RgwZonegroupService,
43 public notificationService: NotificationService,
44 public rgwUserService: RgwUserService
46 this.action = this.editing
47 ? this.actionLabels.EDIT + this.resource
48 : this.actionLabels.CREATE + this.resource;
53 this.multisiteZoneForm = new CdFormGroup({
54 zoneName: new FormControl(null, {
57 CdValidators.custom('uniqueName', (zoneName: string) => {
58 return this.zoneNames && this.zoneNames.indexOf(zoneName) !== -1;
62 default_zone: new FormControl(false),
63 master_zone: new FormControl(false),
64 selectedZonegroup: new FormControl(null),
65 zone_endpoints: new FormControl(null, {
67 CdValidators.custom('endpoint', (value: string) => {
68 if (_.isEmpty(value)) {
71 if (value.includes(',')) {
72 value.split(',').forEach((url: string) => {
74 !this.endpoints.test(url) && !this.ipv4Rgx.test(url) && !this.ipv6Rgx.test(url)
79 !this.endpoints.test(value) &&
80 !this.ipv4Rgx.test(value) &&
81 !this.ipv6Rgx.test(value)
89 users: new FormControl(null)
93 onZoneGroupChange(zonegroupName: string) {
94 let zg = new RgwZonegroup();
95 zg.name = zonegroupName;
96 this.rgwZoneGroupService.get(zg).subscribe((zonegroup: RgwZonegroup) => {
97 if (_.isEmpty(zonegroup.master_zone)) {
98 this.multisiteZoneForm.get('master_zone').setValue(true);
99 this.multisiteZoneForm.get('master_zone').disable();
106 this.multisiteInfo[1] !== undefined && this.multisiteInfo[1].hasOwnProperty('zonegroups')
107 ? this.multisiteInfo[1]['zonegroups']
110 this.multisiteInfo[2] !== undefined && this.multisiteInfo[2].hasOwnProperty('zones')
111 ? this.multisiteInfo[2]['zones']
113 this.zoneNames = this.zoneList.map((zone) => {
116 if (this.action === 'create') {
117 this.multisiteZoneForm
118 .get('selectedZonegroup')
119 .setValue(this.defaultsInfo['defaultZonegroupName']);
120 this.onZoneGroupChange(this.defaultsInfo['defaultZonegroupName']);
122 this.rgwUserService.list().subscribe((users: any) => {
123 this.users = users.filter((user: any) => user.keys.length !== 0);
128 const values = this.multisiteZoneForm.value;
129 this.zonegroup = new RgwZonegroup();
130 this.zonegroup.name = values['selectedZonegroup'];
131 this.zone = new RgwZone();
132 this.zone.name = values['zoneName'];
137 values['default_zone'],
138 values['master_zone'],
139 values['zone_endpoints'],
144 this.notificationService.show(
145 NotificationType.success,
146 $localize`Zone: '${values['zoneName']}' created successfully`
148 this.activeModal.close();
151 this.multisiteZoneForm.setErrors({ cdSubmitButton: true });