]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
18c52a56caa9b851e374d8389b9fded782bfc8f7
[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   editing = false;
21   resource: string;
22   multisiteInfo: object[] = [];
23   realm: RgwRealm;
24   realmList: RgwRealm[] = [];
25   realmNames: string[];
26
27   constructor(
28     public activeModal: NgbActiveModal,
29     public actionLabels: ActionLabelsI18n,
30     public rgwRealmService: RgwRealmService,
31     public notificationService: NotificationService
32   ) {
33     this.action = this.editing
34       ? this.actionLabels.EDIT + this.resource
35       : this.actionLabels.CREATE + this.resource;
36     this.createForm();
37   }
38
39   createForm() {
40     this.multisiteRealmForm = new CdFormGroup({
41       realmName: new FormControl(null, {
42         validators: [
43           Validators.required,
44           CdValidators.custom('uniqueName', (realmName: string) => {
45             return this.realmNames && this.realmNames.indexOf(realmName) !== -1;
46           })
47         ]
48       }),
49       default_realm: new FormControl(false)
50     });
51   }
52
53   ngOnInit(): void {
54     this.realmList =
55       this.multisiteInfo[0] !== undefined && this.multisiteInfo[0].hasOwnProperty('realms')
56         ? this.multisiteInfo[0]['realms']
57         : [];
58     this.realmNames = this.realmList.map((realm) => {
59       return realm['name'];
60     });
61   }
62
63   submit() {
64     const values = this.multisiteRealmForm.value;
65     this.realm = new RgwRealm();
66     this.realm.name = values['realmName'];
67     this.rgwRealmService.create(this.realm, values['default_realm']).subscribe(
68       () => {
69         this.notificationService.show(
70           NotificationType.success,
71           $localize`Realm: '${values['realmName']}' created successfully`
72         );
73         this.activeModal.close();
74       },
75       () => {
76         this.multisiteRealmForm.setErrors({ cdSubmitButton: true });
77       }
78     );
79   }
80 }