]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
1e18598b0dbb18a9e4634034b5a7b6dbfd0d162d
[ceph.git] /
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';
13
14 @Component({
15   selector: 'cd-rgw-multisite-realm-form',
16   templateUrl: './rgw-multisite-realm-form.component.html',
17   styleUrls: ['./rgw-multisite-realm-form.component.scss']
18 })
19 export class RgwMultisiteRealmFormComponent extends BaseModal implements OnInit {
20   multisiteRealmForm: CdFormGroup;
21   realm: RgwRealm;
22   realmList: RgwRealm[] = [];
23   zonegroupList: RgwRealm[] = [];
24   realmNames: string[];
25   newRealmName: string;
26   isMaster: boolean;
27   defaultRealmDisabled = false;
28   docUrl: string;
29
30   constructor(
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
42   ) {
43     super();
44
45     this.action = this.editing ? this.actionLabels.EDIT : this.actionLabels.CREATE;
46     this.createForm();
47   }
48
49   createForm() {
50     this.multisiteRealmForm = new CdFormGroup({
51       realmName: new UntypedFormControl(null, {
52         validators: [
53           Validators.required,
54           CdValidators.custom('uniqueName', (realmName: string) => {
55             return (
56               this.action === this.actionLabels.CREATE &&
57               this.realmNames &&
58               this.realmNames.indexOf(realmName) !== -1
59             );
60           })
61         ]
62       }),
63       default_realm: new UntypedFormControl(false)
64     });
65   }
66
67   ngOnInit(): void {
68     this.realmList =
69       this.multisiteInfo[0] !== undefined && this.multisiteInfo[0].hasOwnProperty('realms')
70         ? this.multisiteInfo[0]['realms']
71         : [];
72     this.realmNames = this.realmList.map((realm) => {
73       return realm['name'];
74     });
75     if (this.action === this.actionLabels.EDIT) {
76       this.zonegroupList =
77         this.multisiteInfo[1] !== undefined && this.multisiteInfo[1].hasOwnProperty('zonegroups')
78           ? this.multisiteInfo[1]['zonegroups']
79           : [];
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();
84       }
85     }
86     this.zonegroupList.forEach((zgp: any) => {
87       if (zgp.is_master === true && zgp.realm_id === this.info.data.id) {
88         this.isMaster = true;
89       }
90     });
91     if (this.defaultsInfo && this.defaultsInfo['defaultRealmName'] !== null) {
92       this.multisiteRealmForm.get('default_realm').disable();
93       this.defaultRealmDisabled = true;
94     }
95     this.docUrl = this.docService.urlGenerator('rgw-multisite');
96   }
97
98   submit() {
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(
104         () => {
105           this.notificationService.show(
106             NotificationType.success,
107             $localize`Realm: '${values['realmName']}' created successfully`
108           );
109           this.closeModal();
110         },
111         () => {
112           this.multisiteRealmForm.setErrors({ cdSubmitButton: true });
113         }
114       );
115     } else {
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(
119         () => {
120           this.notificationService.show(
121             NotificationType.success,
122             $localize`Realm: '${values['realmName']}' updated successfully`
123           );
124           this.closeModal();
125         },
126         () => {
127           this.multisiteRealmForm.setErrors({ cdSubmitButton: true });
128         }
129       );
130     }
131   }
132 }