Skip to content

Commit

Permalink
Add getAzExtResourceType function
Browse files Browse the repository at this point in the history
  • Loading branch information
alexweininger committed Aug 24, 2023
1 parent 0d78b7b commit 8a8a8ae
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 6 deletions.
4 changes: 4 additions & 0 deletions api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).

## Unreleased

## [2.2.0] - 2023-08-24

* Add `getAzExtResourceType` function

## [2.1.0] - 2023-06-13

* Extend `ViewPropertiesModel` to allow async loading of properties
Expand Down
11 changes: 11 additions & 0 deletions api/docs/vscode-azureresources-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,17 @@ export declare interface GetApiOptions {
readonly extensionId?: string;
}

/**
* Gets a normalized type for an Azure resource, accounting for the fact that some
* Azure resources share values for type and/or kind
* @param resource The resource to check the {@link AzExtResourceType} for
* @returns The normalized Azure resource type
*/
export declare function getAzExtResourceType(resource: {
type: string;
kind?: string;
}): AzExtResourceType | undefined;

export declare function getAzureResourcesExtensionApi(extensionContext: vscode.ExtensionContext, apiVersionRange: '2.0.0', options?: GetApiOptions): Promise<AzureResourcesExtensionApi>;

export declare function isWrapper(maybeWrapper: unknown): maybeWrapper is Wrapper;
Expand Down
4 changes: 2 additions & 2 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft/vscode-azureresources-api",
"version": "2.1.0",
"version": "2.2.0",
"description": "Type declarations and client library for the Azure Resources extension API",
"repository": {
"type": "git",
Expand Down
96 changes: 96 additions & 0 deletions api/src/getAzExtResourceType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { AzExtResourceType } from "./AzExtResourceType";

const FunctionAppKind = 'functionapp';
const LogicAppKind = 'workflowapp';

/**
* Gets a normalized type for an Azure resource, accounting for the fact that some
* Azure resources share values for type and/or kind
* @param resource The resource to check the {@link AzExtResourceType} for
* @returns The normalized Azure resource type
*/
export function getAzExtResourceType(resource: { type: string; kind?: string; }): AzExtResourceType | undefined {
const type = resource.type.toLowerCase();
const kind = resource.kind?.toLowerCase() || '';

switch (type) {
case 'microsoft.web/sites':
// Logic apps, function apps, and app services all have the same type
if (kind.includes(FunctionAppKind) && kind.includes(LogicAppKind)) {
return AzExtResourceType.LogicApp;
} else if (kind.includes(FunctionAppKind)) {
return AzExtResourceType.FunctionApp;
} else {
return AzExtResourceType.AppServices;
}

default:
return azureTypeToAzExtResourceTypeMap[type];
}
}

const azureTypeToAzExtResourceTypeMap: Record<string, AzExtResourceType | undefined> = {
'microsoft.resources/resourcegroups': AzExtResourceType.ResourceGroup,
'microsoft.app/containerapps': AzExtResourceType.ContainerApps,
'microsoft.app/managedenvironments': AzExtResourceType.ContainerAppsEnvironment,
'microsoft.compute/virtualmachines': AzExtResourceType.VirtualMachines,
'microsoft.dbforpostgresql/flexibleservers': AzExtResourceType.PostgresqlServersFlexible,
'microsoft.dbforpostgresql/servers': AzExtResourceType.PostgresqlServersStandard,
'microsoft.documentdb/databaseaccounts': AzExtResourceType.AzureCosmosDb,
'microsoft.storage/storageaccounts': AzExtResourceType.StorageAccounts,
'microsoft.web/staticsites': AzExtResourceType.StaticWebApps,
// The below are not supported by the Azure extensions but have icons in the Resources extension
'microsoft.apimanagement/service': AzExtResourceType.ApiManagementService,
'microsoft.batch/batchaccounts': AzExtResourceType.BatchAccounts,
'microsoft.cache/redis': AzExtResourceType.CacheRedis,
'microsoft.cdn/profiles': AzExtResourceType.FrontDoorAndCdnProfiles,
'microsoft.compute/availabilitysets': AzExtResourceType.AvailabilitySets,
'microsoft.compute/disks': AzExtResourceType.Disks,
'microsoft.compute/images': AzExtResourceType.Images,
'microsoft.compute/virtualmachinescalesets': AzExtResourceType.VirtualMachineScaleSets,
'microsoft.containerregistry/registries': AzExtResourceType.ContainerRegistry,
'microsoft.containerservice/managedclusters': AzExtResourceType.ContainerServiceManagedClusters,
'microsoft.dbformysql/servers': AzExtResourceType.MysqlServers,
'microsoft.devices/iothubs': AzExtResourceType.DeviceIotHubs,
'microsoft.devtestlab/labs': AzExtResourceType.DevTestLabs,
'microsoft.eventgrid/domains': AzExtResourceType.EventGridDomains,
'microsoft.eventgrid/eventsubscriptions': AzExtResourceType.EventGridEventSubscriptions,
'microsoft.eventgrid/topics': AzExtResourceType.EventGridTopics,
'microsoft.eventhub/namespaces': AzExtResourceType.EventHubNamespaces,
'microsoft.extendedlocation/customlocations': AzExtResourceType.CustomLocations,
'microsoft.insights/components': AzExtResourceType.ApplicationInsights,
'microsoft.keyvault/vaults': AzExtResourceType.KeyVaults,
'microsoft.kubernetes/connectedclusters': AzExtResourceType.KubernetesConnectedClusters,
'microsoft.logic/workflows': AzExtResourceType.LogicWorkflows,
'microsoft.managedidentity/userassignedidentities': AzExtResourceType.ManagedIdentityUserAssignedIdentities,
'microsoft.network/applicationgateways': AzExtResourceType.NetworkApplicationGateways,
'microsoft.network/applicationsecuritygroups': AzExtResourceType.NetworkApplicationSecurityGroups,
'microsoft.network/loadbalancers': AzExtResourceType.LoadBalancers,
'microsoft.network/localnetworkgateways': AzExtResourceType.NetworkLocalNetworkGateways,
'microsoft.network/networkinterfaces': AzExtResourceType.NetworkInterfaces,
'microsoft.network/networksecuritygroups': AzExtResourceType.NetworkSecurityGroups,
'microsoft.network/networkwatchers': AzExtResourceType.NetworkWatchers,
'microsoft.network/publicipaddresses': AzExtResourceType.PublicIpAddresses,
'microsoft.network/publicipprefixes': AzExtResourceType.NetworkPublicIpPrefixes,
'microsoft.network/routetables': AzExtResourceType.NetworkRouteTables,
'microsoft.network/virtualnetworkgateways': AzExtResourceType.NetworkVirtualNetworkGateways,
'microsoft.network/virtualnetworks': AzExtResourceType.VirtualNetworks,
'microsoft.notificationhubs/namespaces': AzExtResourceType.NotificationHubNamespaces,
'microsoft.operationalinsights/workspaces': AzExtResourceType.OperationalInsightsWorkspaces,
'microsoft.operationsmanagement/solutions': AzExtResourceType.OperationsManagementSolutions,
'microsoft.servicebus/namespaces': AzExtResourceType.ServiceBusNamespaces,
'microsoft.servicefabric/clusters': AzExtResourceType.ServiceFabricClusters,
'microsoft.servicefabricmesh/applications': AzExtResourceType.ServiceFabricMeshApplications,
'microsoft.signalrservice/signalr': AzExtResourceType.SignalRService,
'microsoft.appplatform/spring': AzExtResourceType.SpringApps,
'microsoft.sql/servers': AzExtResourceType.SqlServers,
'microsoft.sql/servers/databases': AzExtResourceType.SqlDatabases,
'microsoft.web/hostingenvironments': AzExtResourceType.WebHostingEnvironments,
'microsoft.web/kubeenvironments': AzExtResourceType.AppServiceKubernetesEnvironment,
'microsoft.web/serverfarms': AzExtResourceType.AppServicePlans,
};
2 changes: 1 addition & 1 deletion api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

export * from './AzExtResourceType';
export * from './extensionApi';
export * from './getAzExtResourceType';
export * from './resources/azure';
export * from './resources/base';
export * from './resources/resourcesApi';
export * from './resources/workspace';
export * from './utils/apiUtils';
export * from './utils/getApi';
export * from './utils/wrapper';

7 changes: 5 additions & 2 deletions src/api/compatibility/pickAppResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { AzExtTreeItem, ContextValueFilter, getAzExtResourceType, ITreeItemPickerContext, PickTreeItemWithCompatibility } from "@microsoft/vscode-azext-utils";
import { AzExtTreeItem, ContextValueFilter, ITreeItemPickerContext, PickTreeItemWithCompatibility } from "@microsoft/vscode-azext-utils";
import { PickAppResourceOptions } from "@microsoft/vscode-azext-utils/hostapi";
import { AzExtResourceType } from "../../../api/src/index";
import { AzExtResourceType, getAzExtResourceType } from "../../../api/src/index";
import { ext } from "../../extensionVariables";

export function createCompatibilityPickAppResource() {
return async function pickAppResource<T extends AzExtTreeItem>(context: ITreeItemPickerContext, options?: PickAppResourceOptions): Promise<T> {
const result = await PickTreeItemWithCompatibility.resource<T>(context, ext.v2.api.resources.azureResourceTreeDataProvider, {
// Below error happens if you change AzExtResourceType in this repo, but it hasn't been updated in utils.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
resourceTypes: convertAppResourceFilterToAzExtResourceType(options?.filter),
childItemFilter: convertExpectedChildContextValueToContextValueFilter(options?.expectedChildContextValue)
});
Expand Down

0 comments on commit 8a8a8ae

Please sign in to comment.