Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Porting Changes to client-side azure resource filtering (#18274) #18278

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 86 additions & 3 deletions src/connectionconfig/azureHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@

import * as vscode from "vscode";
import { l10n } from "vscode";
import { ConnectionDialogWebviewState } from "../sharedInterfaces/connectionDialog";
import { getErrorMessage } from "../utils/utils";
import { VSCodeAzureSubscriptionProvider } from "@microsoft/vscode-azext-azureauth";
import {
AzureSqlServerInfo,
ConnectionDialogWebviewState,
} from "../sharedInterfaces/connectionDialog";
import { getErrorMessage, listAllIterator } from "../utils/utils";
import {
AzureSubscription,
VSCodeAzureSubscriptionProvider,
} from "@microsoft/vscode-azext-azureauth";
import {
GenericResourceExpanded,
ResourceManagementClient,
} from "@azure/arm-resources";

export const azureSubscriptionFilterConfigKey =
"azureResourceGroups.selectedSubscriptions";
Expand Down Expand Up @@ -97,3 +107,76 @@ export async function getQuickPickItems(

return quickPickItems;
}

const serverResourceType = "Microsoft.Sql/servers";
const databaseResourceType = "Microsoft.Sql/servers/databases";
const elasticPoolsResourceType = "Microsoft.Sql/servers/elasticpools";

export async function fetchServersFromAzure(
sub: AzureSubscription,
): Promise<AzureSqlServerInfo[]> {
const result: AzureSqlServerInfo[] = [];
const client = new ResourceManagementClient(
sub.credential,
sub.subscriptionId,
);

// for some subscriptions, supplying a `resourceType eq 'Microsoft.Sql/servers/databases'` filter to list() causes an error:
// > invalid filter in query string 'resourceType eq "Microsoft.Sql/servers/databases'"
// no idea why, so we're fetching all resources and filtering them ourselves

const resources = await listAllIterator<GenericResourceExpanded>(
client.resources.list(),
);

const servers = resources.filter((r) => r.type === serverResourceType);
const databases = resources.filter(
(r) =>
r.type === databaseResourceType ||
r.type === elasticPoolsResourceType,
);

for (const server of servers) {
result.push({
server: server.name,
databases: [],
location: server.location,
resourceGroup: extractFromResourceId(server.id, "resourceGroups"),
subscription: `${sub.name} (${sub.subscriptionId})`,
});
}

for (const database of databases) {
const serverName = extractFromResourceId(database.id, "servers");
const server = result.find((s) => s.server === serverName);
if (server) {
server.databases.push(
database.name.substring(serverName.length + 1),
); // database.name is in the form 'serverName/databaseName', so we need to remove the server name and slash
}
}

return result;
}

function extractFromResourceId(
resourceId: string,
property: string,
): string | undefined {
if (!property.endsWith("/")) {
property += "/";
}

let startIndex = resourceId.indexOf(property);

if (startIndex === -1) {
return undefined;
} else {
startIndex += property.length;
}

return resourceId.substring(
startIndex,
resourceId.indexOf("/", startIndex),
);
}
80 changes: 3 additions & 77 deletions src/connectionconfig/connectionDialogWebviewController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import * as vscode from "vscode";

import {
AuthenticationType,
AzureSqlServerInfo,
AzureSubscriptionInfo,
ConnectionDialogFormItemSpec,
ConnectionDialogReducers,
Expand All @@ -25,20 +24,17 @@ import {
FormItemSpec,
FormItemType,
} from "../reactviews/common/forms/form";
import {
GenericResourceExpanded,
ResourceManagementClient,
} from "@azure/arm-resources";
import {
ConnectionDialog as Loc,
refreshTokenLabel,
} from "../constants/locConstants";
import {
azureSubscriptionFilterConfigKey,
confirmVscodeAzureSignin,
fetchServersFromAzure,
promptForAzureSubscriptionFilter,
} from "./azureHelper";
import { getErrorMessage, listAllIterator } from "../utils/utils";
import { getErrorMessage } from "../utils/utils";

import { ApiStatus } from "../sharedInterfaces/webview";
import { AzureController } from "../azure/azureController";
Expand Down Expand Up @@ -1116,7 +1112,7 @@ export class ConnectionDialogWebviewController extends ReactWebviewPanelControll
);

try {
const servers = await this.fetchServersFromAzure(azSub);
const servers = await fetchServersFromAzure(azSub);
state.azureServers.push(...servers);
stateSub.loaded = true;
this.updateState();
Expand Down Expand Up @@ -1144,74 +1140,4 @@ export class ConnectionDialogWebviewController extends ReactWebviewPanelControll
return rv;
}, new Map<K, V[]>());
}

private async fetchServersFromAzure(
sub: AzureSubscription,
): Promise<AzureSqlServerInfo[]> {
const result: AzureSqlServerInfo[] = [];
const client = new ResourceManagementClient(
sub.credential,
sub.subscriptionId,
);
const servers = await listAllIterator<GenericResourceExpanded>(
client.resources.list({
filter: "resourceType eq 'Microsoft.Sql/servers'",
}),
);
const databasesPromise = listAllIterator<GenericResourceExpanded>(
client.resources.list({
filter: "resourceType eq 'Microsoft.Sql/servers/databases'",
}),
);

for (const server of servers) {
result.push({
server: server.name,
databases: [],
location: server.location,
resourceGroup: this.extractFromResourceId(
server.id,
"resourceGroups",
),
subscription: `${sub.name} (${sub.subscriptionId})`,
});
}

for (const database of await databasesPromise) {
const serverName = this.extractFromResourceId(
database.id,
"servers",
);
const server = result.find((s) => s.server === serverName);
if (server) {
server.databases.push(
database.name.substring(serverName.length + 1),
); // database.name is in the form 'serverName/databaseName', so we need to remove the server name and slash
}
}

return result;
}

private extractFromResourceId(
resourceId: string,
property: string,
): string | undefined {
if (!property.endsWith("/")) {
property += "/";
}

let startIndex = resourceId.indexOf(property);

if (startIndex === -1) {
return undefined;
} else {
startIndex += property.length;
}

return resourceId.substring(
startIndex,
resourceId.indexOf("/", startIndex),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const AzureBrowsePage = () => {
setSubscriptions(subs.sort());

if (!selectedSubscription && subs.length === 1) {
setSubscriptionValue(subs[0]);
setSelectedSubscription(subs[0]);
}
}, [context.state.azureSubscriptions]);
Expand Down