]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
792674da3e4840cf1683b8b56c19dd6438ffdcc3
[ceph-ci.git] /
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';
12
13 @Component({
14   selector: 'cd-rgw-multisite-zonegroup-form',
15   templateUrl: './rgw-multisite-zonegroup-form.component.html',
16   styleUrls: ['./rgw-multisite-zonegroup-form.component.scss']
17 })
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;
22   action: string;
23   multisiteZonegroupForm: CdFormGroup;
24   editing = false;
25   resource: string;
26   realm: RgwRealm;
27   zonegroup: RgwZonegroup;
28   defaultsInfo: string[] = [];
29   multisiteInfo: object[] = [];
30   realmList: RgwRealm[] = [];
31   zonegroupList: RgwZonegroup[] = [];
32   zonegroupNames: string[];
33
34   constructor(
35     public activeModal: NgbActiveModal,
36     public actionLabels: ActionLabelsI18n,
37     public rgwZonegroupService: RgwZonegroupService,
38     public notificationService: NotificationService
39   ) {
40     this.action = this.editing
41       ? this.actionLabels.EDIT + this.resource
42       : this.actionLabels.CREATE + this.resource;
43     this.createForm();
44   }
45
46   createForm() {
47     this.multisiteZonegroupForm = new CdFormGroup({
48       default_zonegroup: new FormControl(false),
49       zonegroupName: new FormControl(null, {
50         validators: [
51           Validators.required,
52           CdValidators.custom('uniqueName', (zonegroupName: string) => {
53             return this.zonegroupNames && this.zonegroupNames.indexOf(zonegroupName) !== -1;
54           })
55         ]
56       }),
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)) {
62             return false;
63           } else {
64             if (value.includes(',')) {
65               value.split(',').forEach((url: string) => {
66                 return (
67                   !this.endpoints.test(url) && !this.ipv4Rgx.test(url) && !this.ipv6Rgx.test(url)
68                 );
69               });
70             } else {
71               return (
72                 !this.endpoints.test(value) &&
73                 !this.ipv4Rgx.test(value) &&
74                 !this.ipv6Rgx.test(value)
75               );
76             }
77             return false;
78           }
79         }),
80         Validators.required
81       ])
82     });
83   }
84
85   ngOnInit(): void {
86     this.realmList =
87       this.multisiteInfo[0] !== undefined && this.multisiteInfo[0].hasOwnProperty('realms')
88         ? this.multisiteInfo[0]['realms']
89         : [];
90     this.zonegroupList =
91       this.multisiteInfo[1] !== undefined && this.multisiteInfo[1].hasOwnProperty('zonegroups')
92         ? this.multisiteInfo[1]['zonegroups']
93         : [];
94     this.zonegroupNames = this.zonegroupList.map((zonegroup) => {
95       return zonegroup['name'];
96     });
97     if (this.action === 'create' && this.defaultsInfo['defaultRealmName'] !== null) {
98       this.multisiteZonegroupForm
99         .get('selectedRealm')
100         .setValue(this.defaultsInfo['defaultRealmName']);
101     }
102   }
103
104   submit() {
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'])
113       .subscribe(
114         () => {
115           this.notificationService.show(
116             NotificationType.success,
117             $localize`Zonegroup: '${values['zonegroupName']}' created successfully`
118           );
119           this.activeModal.close();
120         },
121         () => {
122           this.multisiteZonegroupForm.setErrors({ cdSubmitButton: true });
123         }
124       );
125   }
126
127   checkUrlArray(endpoints: string) {
128     let endpointsArray = [];
129     if (endpoints.includes(',')) {
130       endpointsArray = endpoints.split(',');
131     } else {
132       endpointsArray.push(endpoints);
133     }
134     return endpointsArray;
135   }
136 }