The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/app/plugins/datasource/grafana-azure-monitor-datas.../components/ResourcePicker/utils.ts

57 lines
1.8 KiB

import produce from 'immer';
import { ResourceRow, ResourceRowGroup } from './types';
// This regex matches URIs representing:
// - subscriptions: /subscriptions/44693801-6ee6-49de-9b2d-9106972f9572
// - resource groups: /subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources
// - resources: /subscriptions/44693801-6ee6-49de-9b2d-9106972f9572/resourceGroups/cloud-datasources/providers/Microsoft.Compute/virtualMachines/GithubTestDataVM
const RESOURCE_URI_REGEX = /\/subscriptions\/(?<subscriptionID>[^/]+)(?:\/resourceGroups\/(?<resourceGroup>[^/]+)(?:\/providers.+\/(?<resource>[^/]+))?)?/;
type RegexGroups = Record<string, string | undefined>;
export function parseResourceURI(resourceURI: string) {
const matches = RESOURCE_URI_REGEX.exec(resourceURI);
const groups: RegexGroups = matches?.groups ?? {};
const { subscriptionID, resourceGroup, resource } = groups;
if (!subscriptionID) {
return undefined;
}
return { subscriptionID, resourceGroup, resource };
}
export function isGUIDish(input: string) {
return !!input.match(/^[A-Z0-9]+/i);
}
export function findRow(rows: ResourceRowGroup, id: string): ResourceRow | undefined {
for (const row of rows) {
if (row.id === id) {
return row;
}
if (row.children) {
const result = findRow(row.children, id);
if (result) {
return result;
}
}
}
return undefined;
}
export function addResources(rows: ResourceRowGroup, targetResourceGroupID: string, newResources: ResourceRowGroup) {
return produce(rows, (draftState) => {
const draftRow = findRow(draftState, targetResourceGroupID);
if (!draftRow) {
// This case shouldn't happen often because we're usually coming here from a resource we already have
throw new Error('Unable to find resource');
}
draftRow.children = newResources;
});
}