diff --git a/public/app/plugins/datasource/cloud-monitoring/components/ConfigEditor/ConfigEditor.tsx b/public/app/plugins/datasource/cloud-monitoring/components/ConfigEditor/ConfigEditor.tsx new file mode 100644 index 00000000000..465138c84a2 --- /dev/null +++ b/public/app/plugins/datasource/cloud-monitoring/components/ConfigEditor/ConfigEditor.tsx @@ -0,0 +1,110 @@ +import React, { PureComponent } from 'react'; +import { Select, FieldSet, InlineField, Alert } from '@grafana/ui'; +import { DataSourcePluginOptionsEditorProps, onUpdateDatasourceJsonDataOptionSelect } from '@grafana/data'; +import { AuthType, authTypes, CloudMonitoringOptions, CloudMonitoringSecureJsonData } from '../../types'; +import { JWTConfig } from './JWTConfig'; + +export type Props = DataSourcePluginOptionsEditorProps; + +export class ConfigEditor extends PureComponent { + render() { + const { options, onOptionsChange } = this.props; + const { secureJsonFields, jsonData } = options; + + if (!jsonData.hasOwnProperty('authenticationType')) { + jsonData.authenticationType = AuthType.JWT; + } + + return ( + <> +
+
+

Google Cloud Monitoring Authentication

+

+ There are two ways to authenticate the Google Cloud Monitoring plugin - either by uploading a Service + Account key file or by automatically retrieving credentials from the Google metadata server. The latter + option is only available when running Grafana on a GCE virtual machine. +

+ +
Uploading a Service Account Key File
+

+ There are two ways to authenticate the Google Cloud Monitoring plugin. You can upload a Service Account + key file or automatically retrieve credentials from the Google metadata server. The latter option is only + available when running Grafana on a GCE virtual machine. +

+

+ The Monitoring Viewer role provides all the permissions that Grafana needs. The following + API needs to be enabled on GCP for the data source to work:{' '} + + Monitoring API + +

+ +
GCE Default Service Account
+

+ If Grafana is running on a Google Compute Engine (GCE) virtual machine, it is possible for Grafana to + automatically retrieve the default project id and authentication token from the metadata server. In order + for this to work, you need to make sure that you have a service account that is setup as the default + account for the virtual machine and that the service account has been given read access to the Google + Cloud Monitoring Monitoring API. +

+ +

+ Detailed instructions on how to create a Service Account can be found{' '} + + in the documentation. + +

+
+
+ +
+ + + + ))} + + + + Do not forget to save your changes after uploading a file + + + ); +} + +export const getStyles = (theme: GrafanaTheme) => css` + margin: ${theme.spacing.md} 0 0; +`; diff --git a/public/app/plugins/datasource/cloud-monitoring/config_ctrl.ts b/public/app/plugins/datasource/cloud-monitoring/config_ctrl.ts deleted file mode 100644 index 96fcd16912e..00000000000 --- a/public/app/plugins/datasource/cloud-monitoring/config_ctrl.ts +++ /dev/null @@ -1,101 +0,0 @@ -import DatasourceSrv from 'app/features/plugins/datasource_srv'; -import { AuthType, authTypes } from './types'; - -export interface JWT { - private_key: string; - token_uri: string; - client_email: string; - project_id: string; -} - -export class CloudMonitoringConfigCtrl { - static templateUrl = 'public/app/plugins/datasource/cloud-monitoring/partials/config.html'; - - // Set through angular bindings - declare current: any; - declare meta: any; - - datasourceSrv: DatasourceSrv; - jsonText: string; - validationErrors: string[] = []; - inputDataValid: boolean; - authenticationTypes: Array<{ key: AuthType; value: string }>; - defaultAuthenticationType: string; - name: string; - - /** @ngInject */ - constructor(datasourceSrv: DatasourceSrv) { - this.defaultAuthenticationType = AuthType.JWT; - this.datasourceSrv = datasourceSrv; - this.name = this.meta.name; - this.current.jsonData = this.current.jsonData || {}; - this.current.jsonData.authenticationType = this.current.jsonData.authenticationType - ? this.current.jsonData.authenticationType - : this.defaultAuthenticationType; - this.current.secureJsonData = this.current.secureJsonData || {}; - this.current.secureJsonFields = this.current.secureJsonFields || {}; - this.authenticationTypes = authTypes; - } - - save(jwt: JWT) { - this.current.secureJsonData.privateKey = jwt.private_key; - this.current.jsonData.tokenUri = jwt.token_uri; - this.current.jsonData.clientEmail = jwt.client_email; - this.current.jsonData.defaultProject = jwt.project_id; - } - - validateJwt(jwt: JWT) { - this.resetValidationMessages(); - if (!jwt.private_key || jwt.private_key.length === 0) { - this.validationErrors.push('Private key field missing in JWT file.'); - } - - if (!jwt.token_uri || jwt.token_uri.length === 0) { - this.validationErrors.push('Token URI field missing in JWT file.'); - } - - if (!jwt.client_email || jwt.client_email.length === 0) { - this.validationErrors.push('Client Email field missing in JWT file.'); - } - - if (!jwt.project_id || jwt.project_id.length === 0) { - this.validationErrors.push('Project Id field missing in JWT file.'); - } - - if (this.validationErrors.length === 0) { - this.inputDataValid = true; - return true; - } - - return false; - } - - onUpload(json: JWT) { - this.jsonText = ''; - if (this.validateJwt(json)) { - this.save(json); - } - } - - onPasteJwt(e: any) { - try { - const json = JSON.parse(e.originalEvent.clipboardData.getData('text/plain') || this.jsonText); - if (this.validateJwt(json)) { - this.save(json); - } - } catch (error) { - this.resetValidationMessages(); - this.validationErrors.push(`Invalid json: ${error.message}`); - } - } - - resetValidationMessages() { - this.validationErrors = []; - this.inputDataValid = false; - this.jsonText = ''; - - this.current.jsonData = Object.assign({}, { authenticationType: this.current.jsonData.authenticationType }); - this.current.secureJsonData = {}; - this.current.secureJsonFields = {}; - } -} diff --git a/public/app/plugins/datasource/cloud-monitoring/module.ts b/public/app/plugins/datasource/cloud-monitoring/module.ts index b5bf0156157..e74af478e85 100644 --- a/public/app/plugins/datasource/cloud-monitoring/module.ts +++ b/public/app/plugins/datasource/cloud-monitoring/module.ts @@ -1,13 +1,14 @@ import { DataSourcePlugin } from '@grafana/data'; import CloudMonitoringDatasource from './datasource'; import { QueryEditor } from './components/QueryEditor'; -import { CloudMonitoringConfigCtrl } from './config_ctrl'; +import { ConfigEditor } from './components/ConfigEditor/ConfigEditor'; + import { CloudMonitoringAnnotationsQueryCtrl } from './annotations_query_ctrl'; import { CloudMonitoringVariableQueryEditor } from './components/VariableQueryEditor'; import { CloudMonitoringQuery } from './types'; export const plugin = new DataSourcePlugin(CloudMonitoringDatasource) .setQueryEditor(QueryEditor) - .setConfigCtrl(CloudMonitoringConfigCtrl) + .setConfigEditor(ConfigEditor) .setAnnotationQueryCtrl(CloudMonitoringAnnotationsQueryCtrl) .setVariableQueryEditor(CloudMonitoringVariableQueryEditor); diff --git a/public/app/plugins/datasource/cloud-monitoring/partials/config.html b/public/app/plugins/datasource/cloud-monitoring/partials/config.html deleted file mode 100644 index 84e61d1eda4..00000000000 --- a/public/app/plugins/datasource/cloud-monitoring/partials/config.html +++ /dev/null @@ -1,141 +0,0 @@ -
-
-

Google Cloud Monitoring Authentication

-

- There are two ways to authenticate the Google Cloud Monitoring plugin - either by uploading a Service Account key file or by - automatically retrieving credentials from the Google metadata server. The latter option is only available when - running Grafana on a GCE virtual machine. -

- -
Uploading a Service Account Key File
-

- There are two ways to authenticate the Google Cloud Monitoring plugin. You can upload a Service Account key file or automatically retrieve - credentials from the Google metadata server. The latter option is only available when running Grafana on a GCE virtual machine. -

-

- The Monitoring Viewer role provides all the permissions that Grafana needs. The following API - needs to be enabled on GCP for the data source to work: - Monitoring API -

- -
GCE Default Service Account
-

- If Grafana is running on a Google Compute Engine (GCE) virtual machine, it is possible for Grafana to - automatically retrieve the default project id and authentication token from the metadata server. In order for this - to work, you need to make sure that you have a service account that is setup as the default account for the - virtual machine and that the service account has been given read access to the Google Cloud Monitoring Monitoring API. -

- -

- Detailed instructions on how to create a Service Account can be found - in the documentation. -

-
-
- -
-
-

Authentication

- Upload your Service Account key file or paste in the contents of the file. The file contents will be encrypted - and saved in the Grafana database. -
- -
-
- Authentication Type -
- -
-
-
- -
-
-
-
- -
-
-
-
-
Or paste Service Account key JSON
-
- -
-
- - {{valError}} -
-
-
-
- -
-
Uploaded Key Details
- -
- Project - -
-
- Client Email - -
-
- Token URI - -
-
- Private Key - -
- -
- Reset Service Account Key - - - Reset to clear the uploaded key and upload a new file. - -
-
- -

- Do not forget to save your changes after uploading a file. -

- -
-
{{ctrl.gceError}}
-
- -

- Verify GCE default service account by clicking Save & Test -

diff --git a/public/app/plugins/datasource/cloud-monitoring/types.ts b/public/app/plugins/datasource/cloud-monitoring/types.ts index 102e086567b..e64ac08c34e 100644 --- a/public/app/plugins/datasource/cloud-monitoring/types.ts +++ b/public/app/plugins/datasource/cloud-monitoring/types.ts @@ -5,9 +5,9 @@ export enum AuthType { GCE = 'gce', } -export const authTypes = [ - { value: 'Google JWT File', key: AuthType.JWT }, - { value: 'GCE Default Service Account', key: AuthType.GCE }, +export const authTypes: Array> = [ + { label: 'Google JWT File', value: AuthType.JWT }, + { label: 'GCE Default Service Account', value: AuthType.GCE }, ]; export enum MetricFindQueryTypes { @@ -111,6 +111,12 @@ export interface CloudMonitoringOptions extends DataSourceJsonData { defaultProject?: string; gceDefaultProject?: string; authenticationType?: string; + clientEmail?: string; + tokenUri?: string; +} + +export interface CloudMonitoringSecureJsonData { + privateKey?: string; } export interface AnnotationTarget {