mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Azure Monitor: Add variable function to list regions (#62297)
This commit is contained in:
parent
88119ad6c3
commit
317ef1de86
@ -41,6 +41,7 @@ export default function createMockQuery(overrides?: Partial<AzureMonitorQuery>):
|
||||
alias: '',
|
||||
// timeGrains: [],
|
||||
top: '10',
|
||||
region: '',
|
||||
...overrides?.azureMonitor,
|
||||
},
|
||||
};
|
||||
|
@ -159,6 +159,30 @@ describe('AzureMonitorDatasource', () => {
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('expand template variables for a region', () => {
|
||||
const region = '$reg';
|
||||
templateSrv.init([
|
||||
{
|
||||
id: 'reg',
|
||||
name: 'reg',
|
||||
current: {
|
||||
value: `eastus`,
|
||||
},
|
||||
},
|
||||
]);
|
||||
const query = createMockQuery({
|
||||
azureMonitor: {
|
||||
region,
|
||||
},
|
||||
});
|
||||
const templatedQuery = ctx.ds.azureMonitorDatasource.applyTemplateVariables(query, {});
|
||||
expect(templatedQuery).toMatchObject({
|
||||
azureMonitor: {
|
||||
region: 'eastus',
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('When performing getMetricNamespaces', () => {
|
||||
|
@ -125,6 +125,7 @@ export default class AzureMonitorDatasource extends DataSourceWithBackend<AzureM
|
||||
timeGrain,
|
||||
allowedTimeGrainsMs: item.allowedTimeGrainsMs,
|
||||
metricName: templateSrv.replace(item.metricName, scopedVars),
|
||||
region: templateSrv.replace(item.region, scopedVars),
|
||||
aggregation: aggregation,
|
||||
dimensionFilters,
|
||||
top: top || '10',
|
||||
|
@ -323,5 +323,21 @@ describe('VariableEditor:', () => {
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('should run the query if requesting regions', async () => {
|
||||
const onChange = jest.fn();
|
||||
const { rerender } = render(<VariableEditor {...defaultProps} onChange={onChange} />);
|
||||
// wait for initial load
|
||||
await waitFor(() => expect(screen.getByText('Logs')).toBeInTheDocument());
|
||||
await selectAndRerender('select query type', 'Regions', onChange, rerender);
|
||||
await selectAndRerender('select subscription', 'Primary Subscription', onChange, rerender);
|
||||
expect(onChange).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
queryType: AzureQueryType.LocationsQuery,
|
||||
subscription: 'sub',
|
||||
refId: 'A',
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -30,6 +30,7 @@ const VariableEditor = (props: Props) => {
|
||||
{ label: 'Subscriptions', value: AzureQueryType.SubscriptionsQuery },
|
||||
{ label: 'Resource Groups', value: AzureQueryType.ResourceGroupsQuery },
|
||||
{ label: 'Namespaces', value: AzureQueryType.NamespacesQuery },
|
||||
{ label: 'Regions', value: AzureQueryType.LocationsQuery },
|
||||
{ label: 'Resource Names', value: AzureQueryType.ResourceNamesQuery },
|
||||
{ label: 'Metric Names', value: AzureQueryType.MetricNamesQuery },
|
||||
{ label: 'Workspaces', value: AzureQueryType.WorkspacesQuery },
|
||||
@ -93,6 +94,9 @@ const VariableEditor = (props: Props) => {
|
||||
setRequireNamespace(true);
|
||||
setRequireResource(true);
|
||||
break;
|
||||
case AzureQueryType.LocationsQuery:
|
||||
setRequireSubscription(true);
|
||||
break;
|
||||
}
|
||||
}, [queryType]);
|
||||
|
||||
|
@ -12,6 +12,7 @@ export enum AzureQueryType {
|
||||
ResourceNamesQuery = 'Azure Resource Names',
|
||||
MetricNamesQuery = 'Azure Metric Names',
|
||||
WorkspacesQuery = 'Azure Workspaces',
|
||||
LocationsQuery = 'Azure Locations',
|
||||
/** Deprecated */
|
||||
GrafanaTemplateVariableFn = 'Grafana Template Variable Function',
|
||||
}
|
||||
|
@ -108,6 +108,17 @@ export class VariableSupport extends CustomVariableSupport<DataSource, AzureMoni
|
||||
};
|
||||
}
|
||||
return { data: [] };
|
||||
case AzureQueryType.LocationsQuery:
|
||||
if (queryObj.subscription && this.hasValue(queryObj.subscription)) {
|
||||
const locationMap = await this.datasource.azureMonitorDatasource.getLocations([queryObj.subscription]);
|
||||
const res: Array<{ text: string; value: string }> = [];
|
||||
locationMap.forEach((loc) => {
|
||||
res.push({ text: loc.displayName, value: loc.name });
|
||||
});
|
||||
return {
|
||||
data: res?.length ? [toDataFrame(res)] : [],
|
||||
};
|
||||
}
|
||||
default:
|
||||
request.targets[0] = queryObj;
|
||||
const queryResp = await lastValueFrom(this.datasource.query(request));
|
||||
|
Loading…
Reference in New Issue
Block a user