]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
567c5f096dd9775fefc438d3ed3d578b6d728f95
[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 { 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';
14
15 @Component({
16   selector: 'cd-rgw-multisite-zone-form',
17   templateUrl: './rgw-multisite-zone-form.component.html',
18   styleUrls: ['./rgw-multisite-zone-form.component.scss']
19 })
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;
24   action: string;
25   multisiteZoneForm: CdFormGroup;
26   editing = false;
27   resource: string;
28   realm: RgwRealm;
29   zonegroup: RgwZonegroup;
30   zone: RgwZone;
31   defaultsInfo: string[] = [];
32   multisiteInfo: object[] = [];
33   zonegroupList: RgwZonegroup[] = [];
34   zoneList: RgwZone[] = [];
35   zoneNames: string[];
36   users: string[];
37
38   constructor(
39     public activeModal: NgbActiveModal,
40     public actionLabels: ActionLabelsI18n,
41     public rgwZoneService: RgwZoneService,
42     public rgwZoneGroupService: RgwZonegroupService,
43     public notificationService: NotificationService,
44     public rgwUserService: RgwUserService
45   ) {
46     this.action = this.editing
47       ? this.actionLabels.EDIT + this.resource
48       : this.actionLabels.CREATE + this.resource;
49     this.createForm();
50   }
51
52   createForm() {
53     this.multisiteZoneForm = new CdFormGroup({
54       zoneName: new FormControl(null, {
55         validators: [
56           Validators.required,
57           CdValidators.custom('uniqueName', (zoneName: string) => {
58             return this.zoneNames && this.zoneNames.indexOf(zoneName) !== -1;
59           })
60         ]
61       }),
62       default_zone: new FormControl(false),
63       master_zone: new FormControl(false),
64       selectedZonegroup: new FormControl(null),
65       zone_endpoints: new FormControl(null, {
66         validators: [
67           CdValidators.custom('endpoint', (value: string) => {
68             if (_.isEmpty(value)) {
69               return false;
70             } else {
71               if (value.includes(',')) {
72                 value.split(',').forEach((url: string) => {
73                   return (
74                     !this.endpoints.test(url) && !this.ipv4Rgx.test(url) && !this.ipv6Rgx.test(url)
75                   );
76                 });
77               } else {
78                 return (
79                   !this.endpoints.test(value) &&
80                   !this.ipv4Rgx.test(value) &&
81                   !this.ipv6Rgx.test(value)
82                 );
83               }
84               return false;
85             }
86           })
87         ]
88       }),
89       users: new FormControl(null)
90     });
91   }
92
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();
100       }
101     });
102   }
103
104   ngOnInit(): void {
105     this.zonegroupList =
106       this.multisiteInfo[1] !== undefined && this.multisiteInfo[1].hasOwnProperty('zonegroups')
107         ? this.multisiteInfo[1]['zonegroups']
108         : [];
109     this.zoneList =
110       this.multisiteInfo[2] !== undefined && this.multisiteInfo[2].hasOwnProperty('zones')
111         ? this.multisiteInfo[2]['zones']
112         : [];
113     this.zoneNames = this.zoneList.map((zone) => {
114       return zone['name'];
115     });
116     if (this.action === 'create') {
117       this.multisiteZoneForm
118         .get('selectedZonegroup')
119         .setValue(this.defaultsInfo['defaultZonegroupName']);
120       this.onZoneGroupChange(this.defaultsInfo['defaultZonegroupName']);
121     }
122     this.rgwUserService.list().subscribe((users: any) => {
123       this.users = users.filter((user: any) => user.keys.length !== 0);
124     });
125   }
126
127   submit() {
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'];
133     this.rgwZoneService
134       .create(
135         this.zone,
136         this.zonegroup,
137         values['default_zone'],
138         values['master_zone'],
139         values['zone_endpoints'],
140         values['users']
141       )
142       .subscribe(
143         () => {
144           this.notificationService.show(
145             NotificationType.success,
146             $localize`Zone: '${values['zoneName']}' created successfully`
147           );
148           this.activeModal.close();
149         },
150         () => {
151           this.multisiteZoneForm.setErrors({ cdSubmitButton: true });
152         }
153       );
154   }
155 }