]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/blob
971a0b3369c4b139a153ad8be618334ad917ab9a
[ceph.git] /
1 import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core';
2
3 import { NgbActiveModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
4 import _ from 'lodash';
5
6 import { Permissions } from '~/app/shared/models/permissions';
7
8 import { RgwBucketService } from '~/app/shared/api/rgw-bucket.service';
9 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
10 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
11 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
12 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
13 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
14 import { ListWithDetails } from '~/app/shared/classes/list-with-details.class';
15 import { CdTableAction } from '~/app/shared/models/cd-table-action';
16 import { Icons } from '~/app/shared/enum/icons.enum';
17 import { ModalService } from '~/app/shared/services/modal.service';
18 import { RgwConfigModalComponent } from '../rgw-config-modal/rgw-config-modal.component';
19 import { TableComponent } from '~/app/shared/datatable/table/table.component';
20 import { ENCRYPTION_TYPE } from '../models/rgw-bucket-encryption';
21 import {
22   KmipConfig,
23   VaultConfig,
24   encryptionDatafromAPI
25 } from '~/app/shared/models/rgw-encryption-config-keys';
26
27 @Component({
28   selector: 'cd-rgw-configuration-page',
29   templateUrl: './rgw-configuration-page.component.html',
30   styleUrls: ['./rgw-configuration-page.component.scss']
31 })
32 export class RgwConfigurationPageComponent extends ListWithDetails implements OnInit {
33   readonly vaultAddress = /^((https?:\/\/)|(www.))(?:([a-zA-Z]+)|(\d+\.\d+.\d+.\d+)):\d{4}$/;
34   @ViewChild(TableComponent)
35   table: TableComponent;
36
37   kmsProviders: string[];
38
39   columns: Array<CdTableColumn> = [];
40
41   configForm: CdFormGroup;
42   permissions: Permissions;
43   encryptionConfigValues: any = [];
44   selection: CdTableSelection = new CdTableSelection();
45
46   @Output()
47   submitAction = new EventEmitter();
48   authMethods: string[];
49   secretEngines: string[];
50   tableActions: CdTableAction[];
51   bsModalRef: NgbModalRef;
52   filteredEncryptionConfigValues: {};
53   excludeProps: any[] = [];
54   disableCreate = true;
55   allEncryptionValues: any;
56
57   constructor(
58     public activeModal: NgbActiveModal,
59     public actionLabels: ActionLabelsI18n,
60     private rgwBucketService: RgwBucketService,
61     public authStorageService: AuthStorageService,
62     private modalService: ModalService
63   ) {
64     super();
65     this.permissions = this.authStorageService.getPermissions();
66   }
67
68   ngOnInit() {
69     this.columns = [
70       {
71         name: $localize`Encryption Type`,
72         prop: 'encryption_type',
73         flexGrow: 1
74       },
75       {
76         name: $localize`Key Management Service Provider`,
77         prop: 'backend',
78         flexGrow: 1
79       },
80       {
81         name: $localize`Address`,
82         prop: 'addr',
83         flexGrow: 1
84       }
85     ];
86     this.tableActions = [
87       {
88         permission: 'create',
89         icon: Icons.add,
90         name: this.actionLabels.CREATE,
91         click: () => this.openRgwConfigModal(false),
92         disable: () => this.disableCreate
93       },
94       {
95         permission: 'update',
96         icon: Icons.edit,
97         name: this.actionLabels.EDIT,
98         click: () => this.openRgwConfigModal(true)
99       }
100     ];
101
102     this.excludeProps = this.columns.map((column) => column.prop);
103     this.excludeProps.push('unique_id');
104   }
105
106   getBackend(encryptionData: encryptionDatafromAPI, encryptionType: ENCRYPTION_TYPE) {
107     const backendSet = new Set(Object.keys(encryptionData[encryptionType]));
108     const result =
109       encryptionType === ENCRYPTION_TYPE.SSE_KMS
110         ? backendSet.has('kmip') && backendSet.has('vault')
111         : backendSet.has('vault');
112     return result;
113   }
114
115   openRgwConfigModal(edit: boolean) {
116     if (edit) {
117       const initialState = {
118         action: 'edit',
119         editing: true,
120         selectedEncryptionConfigValues: this.selection.first(),
121         table: this.table
122       };
123       this.bsModalRef = this.modalService.show(RgwConfigModalComponent, initialState, {
124         size: 'lg'
125       });
126     } else {
127       const initialState = {
128         action: 'create',
129         allEncryptionConfigValues: this.allEncryptionValues
130       };
131       this.bsModalRef = this.modalService.show(RgwConfigModalComponent, initialState, {
132         size: 'lg'
133       });
134     }
135   }
136
137   updateSelection(selection: CdTableSelection) {
138     this.selection = selection;
139   }
140
141   setExpandedRow(expandedRow: VaultConfig | KmipConfig) {
142     super.setExpandedRow(expandedRow);
143   }
144
145   flattenData(data: encryptionDatafromAPI) {
146     const combinedArray = [];
147     for (const kmsData of Object.values(data[ENCRYPTION_TYPE.SSE_KMS])) {
148       combinedArray.push(kmsData);
149     }
150     for (const s3Data of Object.values(data[ENCRYPTION_TYPE.SSE_S3])) {
151       combinedArray.push(s3Data);
152     }
153     return combinedArray;
154   }
155
156   fetchData() {
157     this.rgwBucketService.getEncryptionConfig().subscribe((data: encryptionDatafromAPI) => {
158       this.allEncryptionValues = data;
159       const kmsBackends = this.getBackend(data, ENCRYPTION_TYPE.SSE_KMS);
160       const s3Backends = this.getBackend(data, ENCRYPTION_TYPE.SSE_S3);
161       this.disableCreate = kmsBackends && s3Backends;
162       this.encryptionConfigValues = this.flattenData(data);
163     });
164   }
165 }