]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
0d7d4e732324fe109eda173486f8927d8ffa20ba
[ceph-ci.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 { 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
12 @Component({
13   selector: 'cd-rgw-multisite-realm-form',
14   templateUrl: './rgw-multisite-realm-form.component.html',
15   styleUrls: ['./rgw-multisite-realm-form.component.scss']
16 })
17 export class RgwMultisiteRealmFormComponent implements OnInit {
18   action: string;
19   multisiteRealmForm: CdFormGroup;
20   info: any;
21   editing = false;
22   resource: string;
23   multisiteInfo: object[] = [];
24   realm: RgwRealm;
25   realmList: RgwRealm[] = [];
26   realmNames: string[];
27   newRealmName: string;
28
29   constructor(
30     public activeModal: NgbActiveModal,
31     public actionLabels: ActionLabelsI18n,
32     public rgwRealmService: RgwRealmService,
33     public notificationService: NotificationService
34   ) {
35     this.action = this.editing
36       ? this.actionLabels.EDIT + this.resource
37       : this.actionLabels.CREATE + this.resource;
38     this.createForm();
39   }
40
41   createForm() {
42     this.multisiteRealmForm = new CdFormGroup({
43       realmName: new FormControl(null, {
44         validators: [
45           Validators.required,
46           CdValidators.custom('uniqueName', (realmName: string) => {
47             return (
48               this.action === 'create' &&
49               this.realmNames &&
50               this.realmNames.indexOf(realmName) !== -1
51             );
52           })
53         ]
54       }),
55       default_realm: new FormControl(false)
56     });
57   }
58
59   ngOnInit(): void {
60     this.realmList =
61       this.multisiteInfo[0] !== undefined && this.multisiteInfo[0].hasOwnProperty('realms')
62         ? this.multisiteInfo[0]['realms']
63         : [];
64     this.realmNames = this.realmList.map((realm) => {
65       return realm['name'];
66     });
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);
70     }
71   }
72
73   submit() {
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(
79         () => {
80           this.notificationService.show(
81             NotificationType.success,
82             $localize`Realm: '${values['realmName']}' created successfully`
83           );
84           this.activeModal.close();
85         },
86         () => {
87           this.multisiteRealmForm.setErrors({ cdSubmitButton: true });
88         }
89       );
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(
94         () => {
95           this.notificationService.show(
96             NotificationType.success,
97             $localize`Realm: '${values['realmName']}' updated successfully`
98           );
99           this.activeModal.close();
100         },
101         () => {
102           this.multisiteRealmForm.setErrors({ cdSubmitButton: true });
103         }
104       );
105     }
106   }
107 }