1 import { Component, Inject, OnInit, Optional } from '@angular/core';
2 import { UntypedFormControl, 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';
12 import { BaseModal } from 'carbon-components-angular';
15 selector: 'cd-rgw-multisite-realm-form',
16 templateUrl: './rgw-multisite-realm-form.component.html',
17 styleUrls: ['./rgw-multisite-realm-form.component.scss']
19 export class RgwMultisiteRealmFormComponent extends BaseModal implements OnInit {
20 multisiteRealmForm: CdFormGroup;
22 realmList: RgwRealm[] = [];
23 zonegroupList: RgwRealm[] = [];
27 defaultRealmDisabled = false;
31 public activeModal: NgbActiveModal,
32 public actionLabels: ActionLabelsI18n,
33 public rgwRealmService: RgwRealmService,
34 public notificationService: NotificationService,
35 public docService: DocService,
36 @Optional() @Inject('action') public action: string,
37 @Optional() @Inject('resource') public resource: string,
38 @Optional() @Inject('info') public info: any,
39 @Optional() @Inject('multisiteInfo') public multisiteInfo: object[],
40 @Optional() @Inject('defaultsInfo') public defaultsInfo: string[],
41 @Optional() @Inject('editing') public editing: boolean
45 this.action = this.editing ? this.actionLabels.EDIT : this.actionLabels.CREATE;
50 this.multisiteRealmForm = new CdFormGroup({
51 realmName: new UntypedFormControl(null, {
54 CdValidators.custom('uniqueName', (realmName: string) => {
56 this.action === this.actionLabels.CREATE &&
58 this.realmNames.indexOf(realmName) !== -1
63 default_realm: new UntypedFormControl(false)
69 this.multisiteInfo[0] !== undefined && this.multisiteInfo[0].hasOwnProperty('realms')
70 ? this.multisiteInfo[0]['realms']
72 this.realmNames = this.realmList.map((realm) => {
75 if (this.action === this.actionLabels.EDIT) {
77 this.multisiteInfo[1] !== undefined && this.multisiteInfo[1].hasOwnProperty('zonegroups')
78 ? this.multisiteInfo[1]['zonegroups']
80 this.multisiteRealmForm.get('realmName').setValue(this.info.data.name);
81 this.multisiteRealmForm.get('default_realm').setValue(this.info.data.is_default);
82 if (this.info.data.is_default) {
83 this.multisiteRealmForm.get('default_realm').disable();
86 this.zonegroupList.forEach((zgp: any) => {
87 if (zgp.is_master === true && zgp.realm_id === this.info.data.id) {
91 if (this.defaultsInfo && this.defaultsInfo['defaultRealmName'] !== null) {
92 this.multisiteRealmForm.get('default_realm').disable();
93 this.defaultRealmDisabled = true;
95 this.docUrl = this.docService.urlGenerator('rgw-multisite');
99 const values = this.multisiteRealmForm.getRawValue();
100 this.realm = new RgwRealm();
101 if (this.action === this.actionLabels.CREATE) {
102 this.realm.name = values['realmName'];
103 this.rgwRealmService.create(this.realm, values['default_realm']).subscribe(
105 this.notificationService.show(
106 NotificationType.success,
107 $localize`Realm: '${values['realmName']}' created successfully`
112 this.multisiteRealmForm.setErrors({ cdSubmitButton: true });
116 this.realm.name = this.info.data.name;
117 this.newRealmName = values['realmName'];
118 this.rgwRealmService.update(this.realm, values['default_realm'], this.newRealmName).subscribe(
120 this.notificationService.show(
121 NotificationType.success,
122 $localize`Realm: '${values['realmName']}' updated successfully`
127 this.multisiteRealmForm.setErrors({ cdSubmitButton: true });