From e33e3b7d748bfc5862102f67ba4bae30f6dd655c Mon Sep 17 00:00:00 2001 From: amlmtl <> Date: Thu, 7 Dec 2023 16:51:58 +0000 Subject: [PATCH] feat: add internal networking option for ESP connectivity in data source configuration --- pkg/plugin/plugin.go | 23 ++++++++++++++++++----- src/components/ConfigEditor.tsx | 7 +++++++ src/types.ts | 1 + 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/pkg/plugin/plugin.go b/pkg/plugin/plugin.go index 9e1c260..a5a1a8c 100644 --- a/pkg/plugin/plugin.go +++ b/pkg/plugin/plugin.go @@ -92,9 +92,9 @@ type SampleDatasource struct { } type datasourceJsonData struct { - IsViya bool `json:"isViya"` - OauthPassThru bool `json:"oauthPassThru"` - TlsSkipVerify bool `json:"tlsSkipVerify"` + UseInternalNetworking bool `json:"useInternalNetworking"` + OauthPassThru bool `json:"oauthPassThru"` + TlsSkipVerify bool `json:"tlsSkipVerify"` } // Dispose here tells plugin SDK that plugin wants to clean up resources when a new instance @@ -130,7 +130,12 @@ func (d *SampleDatasource) QueryData(ctx context.Context, req *backend.QueryData continue } - serverUrl := qdto.ExternalServerUrl + var serverUrl string + if d.jsonData.UseInternalNetworking { + serverUrl = qdto.InternalServerUrl + } else { + serverUrl = qdto.ExternalServerUrl + } var authHeaderToBePassed *string = nil if authorizationHeaderPtr != nil && d.isServerUrlTrusted(serverUrl, true, authorizationHeaderPtr) { @@ -144,7 +149,15 @@ func (d *SampleDatasource) QueryData(ctx context.Context, req *backend.QueryData } func (d *SampleDatasource) query(_ context.Context, datasourceUid string, qdto querydto.QueryDTO, authorizationHeader *string) backend.DataResponse { - s, err := server.FromUrlString(qdto.ExternalServerUrl) + var qServerUrl string + if d.jsonData.UseInternalNetworking { + qServerUrl = qdto.InternalServerUrl + log.DefaultLogger.Debug("Using internal ESP server URL from query", "query", qdto) + } else { + qServerUrl = qdto.ExternalServerUrl + } + + s, err := server.FromUrlString(qServerUrl) if err != nil { return handleQueryError("invalid server URL", err) } diff --git a/src/components/ConfigEditor.tsx b/src/components/ConfigEditor.tsx index 510afe4..5cb6a08 100644 --- a/src/components/ConfigEditor.tsx +++ b/src/components/ConfigEditor.tsx @@ -50,6 +50,10 @@ export function ConfigEditor({options, onOptionsChange}: DataSourcePluginOptions changePropOptionsJsonData({tlsSkipVerify: checked}); } + const handleInternalNetworkingCheckboxChange = (checked: boolean) => { + changePropOptionsJsonData({useInternalNetworking: checked}); + } + const handleViyaCheckboxChange = (checked: boolean) => { const isViya = checked; // Grafana will ignore attempts to reset datasource URLs and will revert to the previously saved value upon a future save, rather than persist a falsy URL. @@ -95,6 +99,9 @@ export function ConfigEditor({options, onOptionsChange}: DataSourcePluginOptions onChange={handleDiscoveryServiceProviderChange} /> + handleInternalNetworkingCheckboxChange(e.currentTarget.checked)} + /> handleTlsSkipVerifyCheckboxChange(e.currentTarget.checked)} /> diff --git a/src/types.ts b/src/types.ts index a6a0284..81ee82a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -99,4 +99,5 @@ export interface EspDataSourceOptions extends DataSourceJsonData { oauthPassThru: boolean; tlsSkipVerify: boolean; isViya: boolean; + useInternalNetworking: boolean; }