-
Notifications
You must be signed in to change notification settings - Fork 149
Add Local and Remote Settings conversion #4387
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
Changes from 20 commits
b8f19e5
6979c6e
f8f10e3
562fad5
b75809a
5e423da
df7712c
453f0af
4387565
2d13d56
4373847
64cee3d
3c126a7
2e432f7
1d9d860
7164e58
af3704b
842b462
099a0f4
b5250a9
a7520fd
098b932
b6433f8
28e99cf
87ed16d
cdfc374
e82e6f6
5a969bc
87d2988
8c63d4f
b70e19f
f43f490
0dd20d5
e1dafec
e4819a6
4e63165
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.md in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { type IResourceGroupWizardContext, type Role } from "@microsoft/vscode-azext-azureutils"; | ||
| import { type ExecuteActivityContext, type ISubscriptionActionContext } from "@microsoft/vscode-azext-utils"; | ||
| import { type SlotTreeItem } from "../../tree/SlotTreeItem"; | ||
| import { type Connection } from "./ConnectionsListStep"; | ||
|
|
||
| export interface AddMIConnectionsContext extends ExecuteActivityContext, IResourceGroupWizardContext, ISubscriptionActionContext { | ||
| functionapp?: SlotTreeItem; | ||
| connections?: Connection[]; | ||
| connectionsToAdd?: Connection[]; | ||
| roles?: Role[]; | ||
| localSettingsPath?: string; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.md in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { type StringDictionary } from "@azure/arm-appservice"; | ||
| import { isSettingConvertible } from "@microsoft/vscode-azext-azureappsettings"; | ||
| import { AzExtFsExtra, AzureWizardPromptStep, nonNullValue, type IAzureQuickPickItem } from "@microsoft/vscode-azext-utils"; | ||
| import * as vscode from 'vscode'; | ||
| import { type ILocalSettingsJson } from "../../funcConfig/local.settings"; | ||
| import { localize } from "../../localize"; | ||
| import { decryptLocalSettings } from "../appSettings/localSettings/decryptLocalSettings"; | ||
| import { encryptLocalSettings } from "../appSettings/localSettings/encryptLocalSettings"; | ||
| import { getLocalSettingsFile } from "../appSettings/localSettings/getLocalSettingsFile"; | ||
| import { type AddMIConnectionsContext } from "./AddMIConnectionsContext"; | ||
|
|
||
| export interface Connection { | ||
| name: string; | ||
| value: string; | ||
| } | ||
|
|
||
| export class ConnectionsListStep extends AzureWizardPromptStep<AddMIConnectionsContext> { | ||
| public async prompt(context: AddMIConnectionsContext): Promise<void> { | ||
| const picks = await this.getPicks(context); | ||
|
|
||
| if (picks.length === 0) { | ||
| const noItemFoundMessage: string = localize('noConnectionsFound', 'No connections found in local settings'); | ||
| (await context.ui.showQuickPick(picks, { | ||
| placeHolder: localize('selectConnections', 'Select the connections you want to add managed identity support for'), | ||
| suppressPersistence: true, | ||
| noPicksMessage: noItemFoundMessage | ||
| })); | ||
| } else { | ||
| context.connections = (await context.ui.showQuickPick(picks, { | ||
| placeHolder: localize('selectConnections', 'Select the connections you want to add managed identity support for'), | ||
| suppressPersistence: true, | ||
| canPickMany: true, | ||
| })).map(item => item.data); | ||
| } | ||
| } | ||
|
|
||
| public shouldPrompt(context: AddMIConnectionsContext): boolean { | ||
| return !context.connections || context.connections.length === 0; | ||
| } | ||
|
|
||
| private async getPicks(context: AddMIConnectionsContext): Promise<IAzureQuickPickItem<Connection>[]> { | ||
| if (context.functionapp) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
| return this.getRemoteQuickPicks(context); | ||
| } else { | ||
| return this.getLocalQuickPicks(context); | ||
| } | ||
| } | ||
|
|
||
| private async getLocalQuickPicks(context: AddMIConnectionsContext, workspaceFolder?: vscode.WorkspaceFolder): Promise<IAzureQuickPickItem<Connection>[]> { | ||
| const picks: IAzureQuickPickItem<Connection>[] = []; | ||
| const message: string = localize('selectLocalSettings', 'Select the local settings to add identity settings for.'); | ||
| const localSettingsPath: string = await getLocalSettingsFile(context, message, workspaceFolder); | ||
| context.localSettingsPath = localSettingsPath; | ||
|
|
||
| if (await AzExtFsExtra.pathExists(localSettingsPath)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that you're using this function, I think you can get rid of the if statement above. |
||
| const localSettings = await getLocalSettingsJson(context, localSettingsPath); | ||
| if (localSettings.Values) { | ||
| for (const [key, value] of Object.entries(localSettings.Values)) { | ||
| if (!isSettingConvertible(key, value)) { | ||
| continue; | ||
| } | ||
|
|
||
| picks.push({ | ||
| label: key, | ||
| data: { | ||
| name: key, | ||
| value: value | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return picks; | ||
| } | ||
|
|
||
| private async getRemoteQuickPicks(context: AddMIConnectionsContext): Promise<IAzureQuickPickItem<Connection>[]> { | ||
| const picks: IAzureQuickPickItem<Connection>[] = []; | ||
|
|
||
| const client = await nonNullValue(context.functionapp?.site.createClient(context)); | ||
| const appSettings: StringDictionary = await client.listApplicationSettings(); | ||
| if (appSettings.properties) { | ||
| for (const [key, value] of Object.entries(appSettings.properties)) { | ||
| if (!isSettingConvertible(key, value)) { | ||
| continue; | ||
| } | ||
|
|
||
| picks.push({ | ||
| label: key, | ||
| data: { | ||
| name: key, | ||
| value: value | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return picks; | ||
| } | ||
| } | ||
|
|
||
| export async function getLocalSettingsJson(context: AddMIConnectionsContext, localSettingsPath: string): Promise<ILocalSettingsJson> { | ||
| let localSettings: ILocalSettingsJson = <ILocalSettingsJson>await AzExtFsExtra.readJSON(localSettingsPath); | ||
| const localSettingsUri: vscode.Uri = vscode.Uri.file(localSettingsPath); | ||
| if (localSettings.IsEncrypted) { | ||
| await decryptLocalSettings(context, localSettingsUri); | ||
| try { | ||
| localSettings = await AzExtFsExtra.readJSON<ILocalSettingsJson>(localSettingsPath); | ||
| } finally { | ||
| await encryptLocalSettings(context, localSettingsUri); | ||
| } | ||
| } | ||
|
|
||
| return localSettings; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.md in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { activitySuccessContext, activitySuccessIcon, AzExtFsExtra, AzureWizardExecuteStep, createUniversallyUniqueContextValue, GenericTreeItem, nonNullProp } from "@microsoft/vscode-azext-utils"; | ||
| import { ext } from "../../extensionVariables"; | ||
| import { localize } from "../../localize"; | ||
| import { getLocalSettingsFile } from "../appSettings/localSettings/getLocalSettingsFile"; | ||
| import { type AddMIConnectionsContext } from "./AddMIConnectionsContext"; | ||
| import { getLocalSettingsJson } from "./ConnectionsListStep"; | ||
|
|
||
| export class LocalSettingsAddStep extends AzureWizardExecuteStep<AddMIConnectionsContext> { | ||
| public priority: number = 125; | ||
|
|
||
| public async execute(context: AddMIConnectionsContext): Promise<void> { | ||
| // If right clicking on a connection we will have the connections to convert but not the local settings path | ||
| if (!context.localSettingsPath) { | ||
| const message: string = localize('selectLocalSettings', 'Select the local settings file to add connections to.'); | ||
| context.localSettingsPath = await getLocalSettingsFile(context, message); | ||
| } | ||
|
|
||
| const localSettings = await getLocalSettingsJson(context, context.localSettingsPath); | ||
| if (localSettings.Values) { | ||
| for (const connection of nonNullProp(context, 'connectionsToAdd')) { | ||
| localSettings.Values[connection.name] = connection.value; | ||
| context.activityChildren?.push( | ||
| new GenericTreeItem(undefined, { | ||
| contextValue: createUniversallyUniqueContextValue(['useExistingResourceGroupInfoItem', activitySuccessContext]), | ||
| label: localize('addedLocalSetting', 'Add Local setting "{0}"', connection.name), | ||
| iconPath: activitySuccessIcon | ||
| }) | ||
| ); | ||
| } | ||
| await AzExtFsExtra.writeJSON(nonNullProp(context, 'localSettingsPath'), localSettings); | ||
| await ext.rgApi.workspaceResourceTree.refresh(context); | ||
| } | ||
| } | ||
| public shouldExecute(context: AddMIConnectionsContext): boolean { | ||
| return !context.functionapp && !!context.connections | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.md in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { activitySuccessContext, activitySuccessIcon, AzureWizardExecuteStep, createUniversallyUniqueContextValue, GenericTreeItem, nonNullProp } from "@microsoft/vscode-azext-utils"; | ||
| import { ext } from "../../extensionVariables"; | ||
| import { localize } from "../../localize"; | ||
| import { type AddMIConnectionsContext } from "./AddMIConnectionsContext"; | ||
|
|
||
| export class RemoteSettingsAddStep extends AzureWizardExecuteStep<AddMIConnectionsContext> { | ||
| public priority: number = 110; | ||
|
|
||
| public async execute(context: AddMIConnectionsContext): Promise<void> { | ||
| const client = await nonNullProp(context, 'functionapp').site.createClient(context); | ||
| const remoteSettings = await client.listApplicationSettings(); | ||
| const properties = remoteSettings.properties || {}; | ||
| for (const connection of nonNullProp(context, 'connectionsToAdd')) { | ||
| properties[connection.name] = connection.value; | ||
| } | ||
|
|
||
| await client.updateApplicationSettings({ properties }); | ||
| for (const connection of nonNullProp(context, 'connectionsToAdd')) { | ||
|
nturinski marked this conversation as resolved.
|
||
| context.activityChildren?.push( | ||
| new GenericTreeItem(undefined, { | ||
|
nturinski marked this conversation as resolved.
|
||
| contextValue: createUniversallyUniqueContextValue(['useExistingResourceGroupInfoItem', activitySuccessContext]), | ||
| label: localize('addedAppSetting', 'Add app setting "{0}"', connection.name), | ||
| iconPath: activitySuccessIcon | ||
| }) | ||
| ); | ||
| } | ||
| await ext.rgApi.tree.refresh(context); | ||
| } | ||
|
|
||
| public shouldExecute(context: AddMIConnectionsContext): boolean { | ||
| return !!context.functionapp && !!context.connections; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.