]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
3a701a1bc0ebf20b7afbac2f01429e76bb89b773
[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 (zgp.is_master === true && !_.isEmpty(zgp.realm_id)) {
97         this.isMaster = true;
98       }
99     });
100     if (!this.isMaster) {
101       this.multisiteZonegroupForm.get('master_zonegroup').setValue(true);
102       this.multisiteZonegroupForm.get('master_zonegroup').disable();
103     }
104     this.zonegroupNames = this.zonegroupList.map((zonegroup) => {
105       return zonegroup['name'];
106     });
107     if (this.action === 'create' && this.defaultsInfo['defaultRealmName'] !== null) {
108       this.multisiteZonegroupForm
109         .get('selectedRealm')
110         .setValue(this.defaultsInfo['defaultRealmName']);
111     }
112   }
113
114   submit() {
115     const values = this.multisiteZonegroupForm.value;
116     this.realm = new RgwRealm();
117     this.realm.name = values['selectedRealm'];
118     this.zonegroup = new RgwZonegroup();
119     this.zonegroup.name = values['zonegroupName'];
120     this.zonegroup.endpoints = this.checkUrlArray(values['zonegroup_endpoints']);
121     this.rgwZonegroupService
122       .create(this.realm, this.zonegroup, values['default_zonegroup'], values['master_zonegroup'])
123       .subscribe(
124         () => {
125           this.notificationService.show(
126             NotificationType.success,
127             $localize`Zonegroup: '${values['zonegroupName']}' created successfully`
128           );
129           this.activeModal.close();
130         },
131         () => {
132           this.multisiteZonegroupForm.setErrors({ cdSubmitButton: true });
133         }
134       );
135   }
136
137   checkUrlArray(endpoints: string) {
138     let endpointsArray = [];
139     if (endpoints.includes(',')) {
140       endpointsArray = endpoints.split(',');
141     } else {
142       endpointsArray.push(endpoints);
143     }
144     return endpointsArray;
145   }
146 }