Azure Monitor: Fix resource picker selection for subresources (#56392)

This commit is contained in:
Andres Martinez Gotor 2022-11-22 16:43:57 +01:00 committed by GitHub
parent 3bea8f2462
commit 5b1ff83ee9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 24 deletions

View File

@ -6,16 +6,16 @@ import { findRow, parseResourceURI, setResource } from './utils';
describe('AzureMonitor ResourcePicker utils', () => {
describe('parseResourceURI', () => {
it('should parse subscription URIs', () => {
expect(parseResourceURI('/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572')).toEqual({
subscription: '44693801-6ee6-49de-9b2d-9106972f9572',
expect(parseResourceURI('/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee')).toEqual({
subscription: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
});
});
it('should parse resource group URIs', () => {
expect(
parseResourceURI('/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources')
parseResourceURI('/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources')
).toEqual({
subscription: '44693801-6ee6-49de-9b2d-9106972f9572',
subscription: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
resourceGroup: 'cloud-datasources',
});
});
@ -23,10 +23,10 @@ describe('AzureMonitor ResourcePicker utils', () => {
it('should parse resource URIs', () => {
expect(
parseResourceURI(
'/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources/providers/Microsoft.Compute/virtualMachines/GithubTestDataVM'
'/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/Microsoft.Compute/virtualMachines/GithubTestDataVM'
)
).toEqual({
subscription: '44693801-6ee6-49de-9b2d-9106972f9572',
subscription: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
resourceGroup: 'cloud-datasources',
metricNamespace: 'Microsoft.Compute/virtualMachines',
resourceName: 'GithubTestDataVM',
@ -36,10 +36,10 @@ describe('AzureMonitor ResourcePicker utils', () => {
it('should parse resource URIs with a subresource', () => {
expect(
parseResourceURI(
'/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources/providers/Microsoft.Storage/storageAccounts/csb100320016c43d2d0/fileServices/default'
'/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/Microsoft.Storage/storageAccounts/csb100320016c43d2d0/fileServices/default'
)
).toEqual({
subscription: '44693801-6ee6-49de-9b2d-9106972f9572',
subscription: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
resourceGroup: 'cloud-datasources',
metricNamespace: 'Microsoft.Storage/storageAccounts/fileServices',
resourceName: 'csb100320016c43d2d0/default',
@ -47,30 +47,30 @@ describe('AzureMonitor ResourcePicker utils', () => {
});
it('returns undefined for invalid input', () => {
expect(parseResourceURI('44693801-6ee6-49de-9b2d-9106972f9572')).toEqual({});
expect(parseResourceURI('aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee')).toEqual({});
});
it('returns a valid response with a missing element in the metric namespace and name', () => {
expect(
parseResourceURI(
'/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources/providers/foo'
'/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/foo'
)
).toEqual({
metricNamespace: 'foo',
resourceGroup: 'cloud-datasources',
resourceName: '',
subscription: '44693801-6ee6-49de-9b2d-9106972f9572',
subscription: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
});
expect(
parseResourceURI(
'/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources/providers/foo/bar'
'/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/foo/bar'
)
).toEqual({
metricNamespace: 'foo/bar',
resourceGroup: 'cloud-datasources',
resourceName: '',
subscription: '44693801-6ee6-49de-9b2d-9106972f9572',
subscription: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
});
});
});
@ -87,7 +87,7 @@ describe('AzureMonitor ResourcePicker utils', () => {
const rows: ResourceRowGroup = [
{
id: '',
uri: '/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources/providers/Microsoft.Storage/storageAccounts/csb100320016c43d2d0',
uri: '/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/Microsoft.Storage/storageAccounts/csb100320016c43d2d0',
name: '',
type: ResourceRowType.Resource,
typeLabel: '',
@ -96,7 +96,7 @@ describe('AzureMonitor ResourcePicker utils', () => {
expect(
findRow(
rows,
'/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources/providers/Microsoft.Storage/storageAccounts/csb100320016c43d2d0/fileServices/default'
'/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/Microsoft.Storage/storageAccounts/csb100320016c43d2d0/fileServices/default'
)
).toEqual(rows[0]);
});
@ -105,7 +105,7 @@ describe('AzureMonitor ResourcePicker utils', () => {
const rows: ResourceRowGroup = [
{
id: '',
uri: '/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources/providers/microsoft.storage/storageaccounts/csb100320016c43d2d0',
uri: '/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/microsoft.storage/storageaccounts/csb100320016c43d2d0',
name: '',
type: ResourceRowType.Resource,
typeLabel: '',
@ -114,7 +114,7 @@ describe('AzureMonitor ResourcePicker utils', () => {
expect(
findRow(
rows,
'/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources/providers/Microsoft.Storage/storageAccounts/csb100320016c43d2d0'
'/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/Microsoft.Storage/storageAccounts/csb100320016c43d2d0'
)
).toEqual(rows[0]);
});
@ -123,7 +123,7 @@ describe('AzureMonitor ResourcePicker utils', () => {
const rows: ResourceRowGroup = [
{
id: '',
uri: '/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/CLOUD-DATASOURCES/providers/microsoft.storage/storageaccounts/csb100320016c43d2d0',
uri: '/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/CLOUD-DATASOURCES/providers/microsoft.storage/storageaccounts/csb100320016c43d2d0',
name: '',
type: ResourceRowType.Resource,
typeLabel: '',
@ -132,10 +132,35 @@ describe('AzureMonitor ResourcePicker utils', () => {
expect(
findRow(
rows,
'/subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources/providers/Microsoft.Storage/storageAccounts/csb100320016c43d2d0'
'/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/Microsoft.Storage/storageAccounts/csb100320016c43d2d0'
)
).toEqual(rows[0]);
});
it('should find a row matching the right subresource', () => {
const rows: ResourceRowGroup = [
{
id: '',
uri: '/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/Microsoft.Sql/servers/foo',
name: '',
type: ResourceRowType.Resource,
typeLabel: '',
},
{
id: '',
uri: '/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/Microsoft.Sql/servers/foo/databases/bar',
name: '',
type: ResourceRowType.Resource,
typeLabel: '',
},
];
expect(
findRow(
rows,
'/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/Microsoft.Sql/servers/foo/databases/bar'
)
).toEqual(rows[1]);
});
});
describe('setResource', () => {

View File

@ -62,6 +62,22 @@ export function isGUIDish(input: string) {
return !!input.match(/^[A-Z0-9]+/i);
}
function compareNamespaceAndName(
rowNamespace?: string,
rowName?: string,
resourceNamespace?: string,
resourceName?: string
) {
// StorageAccounts subresources are not listed independently
if (resourceNamespace?.startsWith('microsoft.storage/storageaccounts')) {
resourceNamespace = 'microsoft.storage/storageaccounts';
if (resourceName?.endsWith('/default')) {
resourceName = resourceName.slice(0, -'/default'.length);
}
}
return rowNamespace === resourceNamespace && rowName === resourceName;
}
function matchURI(rowURI: string, resourceURI: string) {
const targetParams = parseResourceDetails(resourceURI);
const rowParams = parseResourceDetails(rowURI);
@ -69,11 +85,12 @@ function matchURI(rowURI: string, resourceURI: string) {
return (
rowParams?.subscription === targetParams?.subscription &&
rowParams?.resourceGroup?.toLowerCase() === targetParams?.resourceGroup?.toLowerCase() &&
// metricNamespace may include a subresource that we don't need to compare
rowParams?.metricNamespace?.toLowerCase().split('/')[0] ===
targetParams?.metricNamespace?.toLowerCase().split('/')[0] &&
// resourceName may include a subresource that we don't need to compare
rowParams?.resourceName?.split('/')[0] === targetParams?.resourceName?.split('/')[0]
compareNamespaceAndName(
rowParams?.metricNamespace?.toLowerCase(),
rowParams?.resourceName,
targetParams?.metricNamespace?.toLowerCase(),
targetParams?.resourceName
)
);
}