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