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';
13 selector: 'cd-rgw-multisite-realm-form',
14 templateUrl: './rgw-multisite-realm-form.component.html',
15 styleUrls: ['./rgw-multisite-realm-form.component.scss']
17 export class RgwMultisiteRealmFormComponent implements OnInit {
19 multisiteRealmForm: CdFormGroup;
23 multisiteInfo: object[] = [];
25 realmList: RgwRealm[] = [];
30 public activeModal: NgbActiveModal,
31 public actionLabels: ActionLabelsI18n,
32 public rgwRealmService: RgwRealmService,
33 public notificationService: NotificationService
35 this.action = this.editing
36 ? this.actionLabels.EDIT + this.resource
37 : this.actionLabels.CREATE + this.resource;
42 this.multisiteRealmForm = new CdFormGroup({
43 realmName: new FormControl(null, {
46 CdValidators.custom('uniqueName', (realmName: string) => {
48 this.action === 'create' &&
50 this.realmNames.indexOf(realmName) !== -1
55 default_realm: new FormControl(false)
61 this.multisiteInfo[0] !== undefined && this.multisiteInfo[0].hasOwnProperty('realms')
62 ? this.multisiteInfo[0]['realms']
64 this.realmNames = this.realmList.map((realm) => {
67 if (this.action === 'edit') {
68 this.multisiteRealmForm.get('realmName').setValue(this.info.data.name);
69 this.multisiteRealmForm.get('default_realm').setValue(this.info.data.is_default);
74 const values = this.multisiteRealmForm.value;
75 this.realm = new RgwRealm();
76 if (this.action === 'create') {
77 this.realm.name = values['realmName'];
78 this.rgwRealmService.create(this.realm, values['default_realm']).subscribe(
80 this.notificationService.show(
81 NotificationType.success,
82 $localize`Realm: '${values['realmName']}' created successfully`
84 this.activeModal.close();
87 this.multisiteRealmForm.setErrors({ cdSubmitButton: true });
90 } else if (this.action === 'edit') {
91 this.realm.name = this.info.data.name;
92 this.newRealmName = values['realmName'];
93 this.rgwRealmService.update(this.realm, values['default_realm'], this.newRealmName).subscribe(
95 this.notificationService.show(
96 NotificationType.success,
97 $localize`Realm: '${values['realmName']}' updated successfully`
99 this.activeModal.close();
102 this.multisiteRealmForm.setErrors({ cdSubmitButton: true });