]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
c36c636ce305bc4f990075e61649f7ac0012247e
[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   isMaster = false;
34
35   constructor(
36     public activeModal: NgbActiveModal,
37     public actionLabels: ActionLabelsI18n,
38     public rgwZonegroupService: RgwZonegroupService,
39     public notificationService: NotificationService
40   ) {
41     this.action = this.editing
42       ? this.actionLabels.EDIT + this.resource
43       : this.actionLabels.CREATE + this.resource;
44     this.createForm();
45   }
46
47   createForm() {
48     this.multisiteZonegroupForm = new CdFormGroup({
49       default_zonegroup: new FormControl(false),
50       zonegroupName: new FormControl(null, {
51         validators: [
52           Validators.required,
53           CdValidators.custom('uniqueName', (zonegroupName: string) => {
54             return this.zonegroupNames && this.zonegroupNames.indexOf(zonegroupName) !== -1;
55           })
56         ]
57       }),
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)) {
63             return false;
64           } else {
65             if (value.includes(',')) {
66               value.split(',').forEach((url: string) => {
67                 return (
68                   !this.endpoints.test(url) && !this.ipv4Rgx.test(url) && !this.ipv6Rgx.test(url)
69                 );
70               });
71             } else {
72               return (
73                 !this.endpoints.test(value) &&
74                 !this.ipv4Rgx.test(value) &&
75                 !this.ipv6Rgx.test(value)
76               );
77             }
78             return false;
79           }
80         }),
81         Validators.required
82       ])
83     });
84   }
85
86   ngOnInit(): void {
87     this.realmList =
88       this.multisiteInfo[0] !== undefined && this.multisiteInfo[0].hasOwnProperty('realms')
89         ? this.multisiteInfo[0]['realms']
90         : [];
91     this.zonegroupList =
92       this.multisiteInfo[1] !== undefined && this.multisiteInfo[1].hasOwnProperty('zonegroups')
93         ? this.multisiteInfo[1]['zonegroups']
94         : [];
95     this.zonegroupList.forEach((zgp: any) => {
96       if (
97         zgp.is_master === true &&
98         !_.isEmpty(zgp.realm_id) &&
99         zgp.realm_id === this.defaultsInfo['defaultRealmName']
100       ) {
101         this.isMaster = true;
102       }
103     });
104     if (!this.isMaster) {
105       this.multisiteZonegroupForm.get('master_zonegroup').setValue(true);
106       this.multisiteZonegroupForm.get('master_zonegroup').disable();
107     }
108     this.zonegroupNames = this.zonegroupList.map((zonegroup) => {
109       return zonegroup['name'];
110     });
111     if (this.action === 'create' && this.defaultsInfo['defaultRealmName'] !== null) {
112       this.multisiteZonegroupForm
113         .get('selectedRealm')
114         .setValue(this.defaultsInfo['defaultRealmName']);
115     }
116   }
117
118   submit() {
119     const values = this.multisiteZonegroupForm.value;
120     this.realm = new RgwRealm();
121     this.realm.name = values['selectedRealm'];
122     this.zonegroup = new RgwZonegroup();
123     this.zonegroup.name = values['zonegroupName'];
124     this.zonegroup.endpoints = this.checkUrlArray(values['zonegroup_endpoints']);
125     this.rgwZonegroupService
126       .create(this.realm, this.zonegroup, values['default_zonegroup'], values['master_zonegroup'])
127       .subscribe(
128         () => {
129           this.notificationService.show(
130             NotificationType.success,
131             $localize`Zonegroup: '${values['zonegroupName']}' created successfully`
132           );
133           this.activeModal.close();
134         },
135         () => {
136           this.multisiteZonegroupForm.setErrors({ cdSubmitButton: true });
137         }
138       );
139   }
140
141   checkUrlArray(endpoints: string) {
142     let endpointsArray = [];
143     if (endpoints.includes(',')) {
144       endpointsArray = endpoints.split(',');
145     } else {
146       endpointsArray.push(endpoints);
147     }
148     return endpointsArray;
149   }
150 }