From 7f268e8823257974b6b3fc3865a4004c4f8c4532 Mon Sep 17 00:00:00 2001 From: "grafana-delivery-bot[bot]" <132647405+grafana-delivery-bot[bot]@users.noreply.github.com> Date: Tue, 27 Jun 2023 07:43:09 -0500 Subject: [PATCH] [v10.0.x] Correlations: Account for restricted datasource (#70746) Correlations: Account for restricted datasource (#70717) * Account for restricted datasource * Allow for fetching datasource to return undefined and beef up filtering after to remove invalid datasources * Revert "Account for restricted datasource" This reverts commit 1087159b9367ba072513eaee0d072d09feda72cd. * Empty-Commit (cherry picked from commit 2738a3a6da6e1e91cb8c8b93a3fe72605090aad2) Co-authored-by: Kristina --- .../features/correlations/useCorrelations.ts | 43 +++++++++++++++---- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/public/app/features/correlations/useCorrelations.ts b/public/app/features/correlations/useCorrelations.ts index 55729366cf9..c45e78d64c2 100644 --- a/public/app/features/correlations/useCorrelations.ts +++ b/public/app/features/correlations/useCorrelations.ts @@ -20,13 +20,28 @@ export interface CorrelationData extends Omit ({ - ...correlation, - source: getDataSourceSrv().getInstanceSettings(sourceUID)!, - target: getDataSourceSrv().getInstanceSettings(targetUID)!, -}); +const toEnrichedCorrelationData = ({ + sourceUID, + targetUID, + ...correlation +}: Correlation): CorrelationData | undefined => { + const sourceDatasource = getDataSourceSrv().getInstanceSettings(sourceUID); + if (sourceDatasource) { + return { + ...correlation, + source: sourceDatasource, + target: getDataSourceSrv().getInstanceSettings(targetUID)!, + }; + } else { + return undefined; + } +}; -const toEnrichedCorrelationsData = (correlations: Correlation[]) => correlations.map(toEnrichedCorrelationData); +const validSourceFilter = (correlation: CorrelationData | undefined): correlation is CorrelationData => !!correlation; + +const toEnrichedCorrelationsData = (correlations: Correlation[]): CorrelationData[] => { + return correlations.map(toEnrichedCorrelationData).filter(validSourceFilter); +}; function getData(response: FetchResponse) { return response.data; } @@ -55,7 +70,12 @@ export const useCorrelations = () => { backend .post(`/api/datasources/uid/${sourceUID}/correlations`, correlation) .then((response) => { - return toEnrichedCorrelationData(response.result); + const enrichedCorrelation = toEnrichedCorrelationData(response.result); + if (enrichedCorrelation !== undefined) { + return enrichedCorrelation; + } else { + throw new Error('invalid sourceUID'); + } }), [backend] ); @@ -70,7 +90,14 @@ export const useCorrelations = () => { ({ sourceUID, uid, ...correlation }) => backend .patch(`/api/datasources/uid/${sourceUID}/correlations/${uid}`, correlation) - .then((response) => toEnrichedCorrelationData(response.result)), + .then((response) => { + const enrichedCorrelation = toEnrichedCorrelationData(response.result); + if (enrichedCorrelation !== undefined) { + return enrichedCorrelation; + } else { + throw new Error('invalid sourceUID'); + } + }), [backend] );