]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/blob
fa89a1699f5da4c26b103f8fb1381aa3cfffa262
[ceph.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 { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
8 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
9 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
10 import { CdValidators } from '~/app/shared/forms/cd-validators';
11 import { NotificationService } from '~/app/shared/services/notification.service';
12 import { RgwRealm, RgwZone, RgwZonegroup } from '../models/rgw-multisite';
13
14 @Component({
15   selector: 'cd-rgw-multisite-zone-form',
16   templateUrl: './rgw-multisite-zone-form.component.html',
17   styleUrls: ['./rgw-multisite-zone-form.component.scss']
18 })
19 export class RgwMultisiteZoneFormComponent implements OnInit {
20   readonly endpoints = /^((https?:\/\/)|(www.))(?:([a-zA-Z]+)|(\d+\.\d+.\d+.\d+)):\d{2,4}$/;
21   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;
22   readonly ipv6Rgx = /^(?:[a-f0-9]{1,4}:){7}[a-f0-9]{1,4}$/i;
23   action: string;
24   multisiteZoneForm: CdFormGroup;
25   editing = false;
26   resource: string;
27   realm: RgwRealm;
28   zonegroup: RgwZonegroup;
29   zone: RgwZone;
30   defaultsInfo: string[] = [];
31   multisiteInfo: object[] = [];
32   zonegroupList: RgwZonegroup[] = [];
33   zoneList: RgwZone[] = [];
34   zoneNames: string[];
35   users: string[];
36
37   constructor(
38     public activeModal: NgbActiveModal,
39     public actionLabels: ActionLabelsI18n,
40     public rgwZoneService: RgwZoneService,
41     public notificationService: NotificationService,
42     public rgwUserService: RgwUserService
43   ) {
44     this.action = this.editing
45       ? this.actionLabels.EDIT + this.resource
46       : this.actionLabels.CREATE + this.resource;
47     this.createForm();
48   }
49
50   createForm() {
51     this.multisiteZoneForm = new CdFormGroup({
52       zoneName: new FormControl(null, {
53         validators: [
54           Validators.required,
55           CdValidators.custom('uniqueName', (zoneName: string) => {
56             return this.zoneNames && this.zoneNames.indexOf(zoneName) !== -1;
57           })
58         ]
59       }),
60       default_zone: new FormControl(false),
61       master_zone: new FormControl(false),
62       selectedZonegroup: new FormControl(null),
63       zone_endpoints: new FormControl(null, {
64         validators: [
65           CdValidators.custom('endpoint', (value: string) => {
66             if (_.isEmpty(value)) {
67               return false;
68             } else {
69               if (value.includes(',')) {
70                 value.split(',').forEach((url: string) => {
71                   return (
72                     !this.endpoints.test(url) && !this.ipv4Rgx.test(url) && !this.ipv6Rgx.test(url)
73                   );
74                 });
75               } else {
76                 return (
77                   !this.endpoints.test(value) &&
78                   !this.ipv4Rgx.test(value) &&
79                   !this.ipv6Rgx.test(value)
80                 );
81               }
82               return false;
83             }
84           })
85         ]
86       }),
87       users: new FormControl(null)
88     });
89   }
90
91   ngOnInit(): void {
92     this.zonegroupList =
93       this.multisiteInfo[1] !== undefined && this.multisiteInfo[1].hasOwnProperty('zonegroups')
94         ? this.multisiteInfo[1]['zonegroups']
95         : [];
96     this.zoneList =
97       this.multisiteInfo[2] !== undefined && this.multisiteInfo[2].hasOwnProperty('zones')
98         ? this.multisiteInfo[2]['zones']
99         : [];
100     this.zoneNames = this.zoneList.map((zone) => {
101       return zone['name'];
102     });
103     if (this.action === 'create') {
104       this.multisiteZoneForm
105         .get('selectedZonegroup')
106         .setValue(this.defaultsInfo['defaultZonegroupName']);
107     }
108     this.rgwUserService.list().subscribe((users: any) => {
109       this.users = users.filter((user: any) => user.keys.length !== 0);
110     });
111   }
112
113   submit() {
114     const values = this.multisiteZoneForm.value;
115     this.zonegroup = new RgwZonegroup();
116     this.zonegroup.name = values['selectedZonegroup'];
117     this.zone = new RgwZone();
118     this.zone.name = values['zoneName'];
119     this.rgwZoneService
120       .create(
121         this.zone,
122         this.zonegroup,
123         values['default_zone'],
124         values['master_zone'],
125         values['zone_endpoints'],
126         values['users']
127       )
128       .subscribe(
129         () => {
130           this.notificationService.show(
131             NotificationType.success,
132             $localize`Zone: '${values['zoneName']}' created successfully`
133           );
134           this.activeModal.close();
135         },
136         () => {
137           this.multisiteZoneForm.setErrors({ cdSubmitButton: true });
138         }
139       );
140   }
141 }