1 import { Component, OnInit } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
3 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
4 import { RgwRealmService } from '~/app/shared/api/rgw-realm.service';
5 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
6 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
7 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
8 import { CdValidators } from '~/app/shared/forms/cd-validators';
9 import { NotificationService } from '~/app/shared/services/notification.service';
10 import { RgwRealm } from '../models/rgw-multisite';
11 import { DocService } from '~/app/shared/services/doc.service';
14 selector: 'cd-rgw-multisite-realm-form',
15 templateUrl: './rgw-multisite-realm-form.component.html',
16 styleUrls: ['./rgw-multisite-realm-form.component.scss']
18 export class RgwMultisiteRealmFormComponent implements OnInit {
20 multisiteRealmForm: CdFormGroup;
24 multisiteInfo: object[] = [];
26 realmList: RgwRealm[] = [];
27 zonegroupList: RgwRealm[] = [];
31 defaultsInfo: string[];
32 defaultRealmDisabled = false;
36 public activeModal: NgbActiveModal,
37 public actionLabels: ActionLabelsI18n,
38 public rgwRealmService: RgwRealmService,
39 public notificationService: NotificationService,
40 public docService: DocService
42 this.action = this.editing
43 ? this.actionLabels.EDIT + this.resource
44 : this.actionLabels.CREATE + this.resource;
49 this.multisiteRealmForm = new CdFormGroup({
50 realmName: new FormControl(null, {
53 CdValidators.custom('uniqueName', (realmName: string) => {
55 this.action === 'create' &&
57 this.realmNames.indexOf(realmName) !== -1
62 default_realm: new FormControl(false)
68 this.multisiteInfo[0] !== undefined && this.multisiteInfo[0].hasOwnProperty('realms')
69 ? this.multisiteInfo[0]['realms']
71 this.realmNames = this.realmList.map((realm) => {
74 if (this.action === 'edit') {
76 this.multisiteInfo[1] !== undefined && this.multisiteInfo[1].hasOwnProperty('zonegroups')
77 ? this.multisiteInfo[1]['zonegroups']
79 this.multisiteRealmForm.get('realmName').setValue(this.info.data.name);
80 this.multisiteRealmForm.get('default_realm').setValue(this.info.data.is_default);
81 if (this.info.data.is_default) {
82 this.multisiteRealmForm.get('default_realm').disable();
85 this.zonegroupList.forEach((zgp: any) => {
86 if (zgp.is_master === true && zgp.realm_id === this.info.data.id) {
90 if (this.defaultsInfo && this.defaultsInfo['defaultRealmName'] !== null) {
91 this.multisiteRealmForm.get('default_realm').disable();
92 this.defaultRealmDisabled = true;
94 this.docUrl = this.docService.urlGenerator('rgw-multisite');
98 const values = this.multisiteRealmForm.getRawValue();
99 this.realm = new RgwRealm();
100 if (this.action === 'create') {
101 this.realm.name = values['realmName'];
102 this.rgwRealmService.create(this.realm, values['default_realm']).subscribe(
104 this.notificationService.show(
105 NotificationType.success,
106 $localize`Realm: '${values['realmName']}' created successfully`
108 this.activeModal.close();
111 this.multisiteRealmForm.setErrors({ cdSubmitButton: true });
114 } else if (this.action === 'edit') {
115 this.realm.name = this.info.data.name;
116 this.newRealmName = values['realmName'];
117 this.rgwRealmService.update(this.realm, values['default_realm'], this.newRealmName).subscribe(
119 this.notificationService.show(
120 NotificationType.success,
121 $localize`Realm: '${values['realmName']}' updated successfully`
123 this.activeModal.close();
126 this.multisiteRealmForm.setErrors({ cdSubmitButton: true });