]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
a3cd2f1167e43ff775d09fd111e8fc9eb41bbeca
[ceph-ci.git] /
1 import { Component, EventEmitter, Output } from '@angular/core';
2 import { Validators } from '@angular/forms';
3
4 import { I18n } from '@ngx-translate/i18n-polyfill';
5 import * as _ from 'lodash';
6 import { BsModalRef } from 'ngx-bootstrap/modal';
7
8 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
9 import { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
10 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
11 import { CdValidators } from '../../../shared/forms/cd-validators';
12 import { RgwUserS3Key } from '../models/rgw-user-s3-key';
13
14 @Component({
15   selector: 'cd-rgw-user-s3-key-modal',
16   templateUrl: './rgw-user-s3-key-modal.component.html',
17   styleUrls: ['./rgw-user-s3-key-modal.component.scss']
18 })
19 export class RgwUserS3KeyModalComponent {
20   /**
21    * The event that is triggered when the 'Add' button as been pressed.
22    */
23   @Output()
24   submitAction = new EventEmitter();
25
26   formGroup: CdFormGroup;
27   viewing = true;
28   userCandidates: string[] = [];
29   resource: string;
30   action: string;
31
32   constructor(
33     private formBuilder: CdFormBuilder,
34     public bsModalRef: BsModalRef,
35     private i18n: I18n,
36     public actionLabels: ActionLabelsI18n
37   ) {
38     this.resource = this.i18n('S3 Key');
39     this.createForm();
40   }
41
42   createForm() {
43     this.formGroup = this.formBuilder.group({
44       user: [null, [Validators.required]],
45       generate_key: [true],
46       access_key: [null, [CdValidators.requiredIf({ generate_key: false })]],
47       secret_key: [null, [CdValidators.requiredIf({ generate_key: false })]]
48     });
49   }
50
51   /**
52    * Set the 'viewing' flag. If set to TRUE, the modal dialog is in 'View' mode,
53    * otherwise in 'Add' mode. According to the mode the dialog and its controls
54    * behave different.
55    * @param {boolean} viewing
56    */
57   setViewing(viewing: boolean = true) {
58     this.viewing = viewing;
59     this.action = this.viewing ? this.actionLabels.SHOW : this.actionLabels.CREATE;
60   }
61
62   /**
63    * Set the values displayed in the dialog.
64    */
65   setValues(user: string, access_key: string, secret_key: string) {
66     this.formGroup.setValue({
67       user: user,
68       generate_key: _.isEmpty(access_key),
69       access_key: access_key,
70       secret_key: secret_key
71     });
72   }
73
74   /**
75    * Set the user candidates displayed in the 'Username' dropdown box.
76    */
77   setUserCandidates(candidates: string[]) {
78     this.userCandidates = candidates;
79   }
80
81   onSubmit() {
82     const key: RgwUserS3Key = this.formGroup.value;
83     this.submitAction.emit(key);
84     this.bsModalRef.hide();
85   }
86 }