-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathrevealResource.ts
More file actions
60 lines (52 loc) · 2.82 KB
/
Copy pathrevealResource.ts
File metadata and controls
60 lines (52 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ParsedAzureResourceId } from '@microsoft/vscode-azext-azureutils';
import { AzExtTreeItem, IActionContext, parseError } from '@microsoft/vscode-azext-utils';
import { VSCodeRevealOptions } from '../../api/src/index';
import { ext } from '../extensionVariables';
import { ResourceGroupsItem } from '../tree/ResourceGroupsItem';
import { ResourceTreeDataProviderBase } from '../tree/ResourceTreeDataProviderBase';
import { localize } from '../utils/localize';
export async function revealResource(context: IActionContext, resourceId: string, options?: VSCodeRevealOptions): Promise<void> {
setTelemetryPropertiesForId(context, resourceId);
try {
const item: ResourceGroupsItem | undefined = await (ext.v2.api.resources.azureResourceTreeDataProvider as ResourceTreeDataProviderBase).findItemById(resourceId);
if (item) {
await ext.appResourceTreeView.reveal(item as unknown as AzExtTreeItem, options ?? { expand: false, focus: true, select: true });
} else {
void context.ui.showWarningMessage(localize('resourceNotFound', 'Resource with id "${0}" not found.', resourceId));
}
} catch (error) {
context.telemetry.properties.revealError = parseError(error).message;
}
}
function setTelemetryPropertiesForId(context: IActionContext, resourceId: string): void {
const parsedAzureResourceId = parsePartialAzureResourceId(resourceId);
const resourceKind = getResourceKindFromId(parsedAzureResourceId);
context.telemetry.properties.resourceKind = resourceKind;
if (resourceKind === 'resource') {
context.telemetry.properties.resourceType = parsedAzureResourceId.provider?.replace(/\//g, '|');
}
}
function parsePartialAzureResourceId(id: string): Partial<ParsedAzureResourceId> & Pick<ParsedAzureResourceId, 'rawId'> {
const matches = id.match(/^\/subscriptions\/([^\/]*)(\/resourceGroups\/([^\/]*)(\/providers\/([^\/]*\/[^\/]*)\/([^\/]*))?)?$/i);
return {
rawId: id,
subscriptionId: matches?.[1],
resourceGroup: matches?.[3],
provider: matches?.[5],
resourceName: matches?.[6]
}
}
function getResourceKindFromId(parsedId: Partial<ParsedAzureResourceId>): 'subscription' | 'resourceGroup' | 'resource' {
if (parsedId.resourceName) {
return 'resource';
} else if (parsedId.resourceGroup) {
return 'resourceGroup';
} else if (parsedId.subscriptionId) {
return 'subscription';
}
throw new Error('Invalid Azure Resource Id');
}