Skip to content

Commit

Permalink
[v9.3.x] AzureMonitor: Add error indicating potential configuration i…
Browse files Browse the repository at this point in the history
…ssue on Resource Picker (grafana#60090)

AzureMonitor: Add error indicating potential configuration issue on Resource Picker (grafana#60041)

* Add error indicating potential configuration issue

- Update test

* Update error

- Lint update

(cherry picked from commit 4730be8)

Co-authored-by: Andreas Christou <andreas.christou@grafana.com>
  • Loading branch information
2 people authored and GuaYounesPW committed Feb 8, 2023
1 parent 425ede8 commit 2068d29
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { act, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { omit } from 'lodash';
import React from 'react';

import createMockDatasource from '../../__mocks__/datasource';
Expand All @@ -23,23 +24,26 @@ const singleResourceSelectionURI =
'/subscriptions/def-456/resourceGroups/dev-3/providers/Microsoft.Compute/virtualMachines/db-server';

const noop: any = () => {};
function createMockResourcePickerData() {
function createMockResourcePickerData(preserveImplementation?: string[]) {
const mockDatasource = createMockDatasource();
const mockResourcePicker = new ResourcePickerData(
createMockInstanceSetttings(),
mockDatasource.azureMonitorDatasource
);

mockResourcePicker.getSubscriptions = jest.fn().mockResolvedValue(createMockSubscriptions());
mockResourcePicker.getResourceGroupsBySubscriptionId = jest
.fn()
.mockResolvedValue(createMockResourceGroupsBySubscription());
mockResourcePicker.getResourcesForResourceGroup = jest.fn().mockResolvedValue(mockResourcesByResourceGroup());
mockResourcePicker.getResourceURIFromWorkspace = jest.fn().mockReturnValue('');
mockResourcePicker.getResourceURIDisplayProperties = jest.fn().mockResolvedValue({});
mockResourcePicker.search = jest.fn().mockResolvedValue(mockSearchResults());
const mockFunctions = omit(
{
getSubscriptions: jest.fn().mockResolvedValue(createMockSubscriptions()),
getResourceGroupsBySubscriptionId: jest.fn().mockResolvedValue(createMockResourceGroupsBySubscription()),
getResourcesForResourceGroup: jest.fn().mockResolvedValue(mockResourcesByResourceGroup()),
getResourceURIFromWorkspace: jest.fn().mockReturnValue(''),
getResourceURIDisplayProperties: jest.fn().mockResolvedValue({}),
search: jest.fn().mockResolvedValue(mockSearchResults()),
},
preserveImplementation || []
);

return mockResourcePicker;
return Object.assign(mockResourcePicker, mockFunctions);
}

const queryType: ResourcePickerQueryType = 'logs';
Expand Down Expand Up @@ -284,6 +288,25 @@ describe('AzureMonitor ResourcePicker', () => {
expect(subscriptionCheckboxAfterClear).toBeInTheDocument();
});

it('should throw an error if no namespaces are found', async () => {
const resourcePickerData = createMockResourcePickerData(['getResourceGroupsBySubscriptionId']);
render(
<ResourcePicker
{...defaultProps}
queryType={'metrics'}
resourcePickerData={resourcePickerData}
resource={noResourceURI}
/>
);
const subscriptionExpand = await screen.findByLabelText('Expand Primary Subscription');
await subscriptionExpand.click();
const error = await screen.findByRole('alert');
expect(error).toHaveTextContent('An error occurred while requesting resources from Azure Monitor');
expect(error).toHaveTextContent(
'Unable to resolve a list of valid metric namespaces. Validate the datasource configuration is correct and required permissions have been granted for all subscriptions. Grafana requires at least the Reader role to be assigned.'
);
});

describe('when rendering resource picker without any selectable entry types', () => {
it('renders no checkboxes', async () => {
await act(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,12 @@ export default class ResourcePickerData extends DataSourceWithBackend<AzureMonit
supportedMetricNamespaces = supportedMetricNamespaces.concat(namespaceVals);
}
}

if (supportedMetricNamespaces.length === 0) {
throw new Error(
'Unable to resolve a list of valid metric namespaces. Validate the datasource configuration is correct and required permissions have been granted for all subscriptions. Grafana requires at least the Reader role to be assigned.'
);
}
this.supportedMetricNamespaces = uniq(supportedMetricNamespaces).join(',');
}

Expand Down

0 comments on commit 2068d29

Please sign in to comment.