-
Couldn't load subscription status.
- Fork 463
Implement configuration profile preview feature for the 'func init' action #4675
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1857774
Initial implementation of config profile feature
aishwaryabh 56a1838
Addressed PR feedback
aishwaryabh d9f4bcf
Add tests
aishwaryabh 54f635f
Address PR feedback & cleanup logging
liliankasem 8a1fdbd
Update interface
liliankasem e35059a
Refactor McpCustomHandlerConfigurationProfile
liliankasem 0618a11
Fix unit test
liliankasem b295d35
Fix comment & release notes
liliankasem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/Cli/func/ConfigurationProfiles/IConfigurationProfile.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
|
||
| using Azure.Functions.Cli.Helpers; | ||
|
|
||
| namespace Azure.Functions.Cli.ConfigurationProfiles | ||
| { | ||
| internal interface IConfigurationProfile | ||
| { | ||
| /// <summary> | ||
| /// Gets the name of the profile. | ||
| /// </summary> | ||
| internal string Name { get; } | ||
|
|
||
| /// <summary> | ||
| /// Applies the profile by generating necessary configuration artifacts. | ||
| /// </summary> | ||
| /// <param name="runtime">The worker runtime of the function app.</param> | ||
| /// <param name="force">If true, forces overwriting existing configurations.</param> | ||
| internal Task ApplyAsync(WorkerRuntime runtime, bool force = false); | ||
| } | ||
| } |
157 changes: 157 additions & 0 deletions
157
src/Cli/func/ConfigurationProfiles/McpCustomHandlerConfigurationProfile.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
|
||
| using Azure.Functions.Cli.Common; | ||
| using Azure.Functions.Cli.Helpers; | ||
| using Newtonsoft.Json; | ||
| using Newtonsoft.Json.Linq; | ||
|
|
||
| namespace Azure.Functions.Cli.ConfigurationProfiles | ||
| { | ||
| internal class McpCustomHandlerConfigurationProfile : IConfigurationProfile | ||
| { | ||
| // This feature flag enables MCP (Multi-Container Platform) support for custom handlers | ||
liliankasem marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // This flag is not required locally, but is required when deploying to Azure environments. | ||
| private const string McpFeatureFlag = "EnableMcpCustomHandlerPreview"; | ||
|
|
||
| public string Name { get; } = "mcp-custom-handler"; | ||
|
|
||
| public async Task ApplyAsync(WorkerRuntime workerRuntime, bool force = false) | ||
| { | ||
| await ApplyHostJsonAsync(force); | ||
| await ApplyLocalSettingsAsync(workerRuntime, force); | ||
| } | ||
|
|
||
| internal async Task ApplyHostJsonAsync(bool force) | ||
| { | ||
| string hostJsonPath = Path.Combine(Environment.CurrentDirectory, Constants.HostJsonFileName); | ||
| bool exists = FileSystemHelpers.FileExists(hostJsonPath); | ||
|
|
||
| // Load host json source: existing host.json or the static resource | ||
| string source = exists | ||
| ? await FileSystemHelpers.ReadAllTextFromFileAsync(hostJsonPath) | ||
| : await StaticResources.HostJson; | ||
|
|
||
| var hostJsonObj = string.IsNullOrWhiteSpace(source) ? new JObject() : JObject.Parse(source); | ||
|
|
||
| // 1) Add configuration profile | ||
| bool updatedConfigProfile = UpsertIfMissing(hostJsonObj, "configurationProfile", JToken.FromObject(Name), force); | ||
| if (updatedConfigProfile) | ||
| { | ||
| SetupProgressLogger.Ok(Constants.HostJsonFileName, $"Set configuration profile to '{Name}'"); | ||
| } | ||
|
|
||
| // 2) Add custom handler settings | ||
| var customHandlerJson = JObject.Parse(await StaticResources.CustomHandlerConfig); | ||
| bool updatedCustomHandler = UpsertIfMissing(hostJsonObj, "customHandler", customHandlerJson, force); | ||
| if (updatedCustomHandler) | ||
| { | ||
| SetupProgressLogger.Ok(Constants.HostJsonFileName, "Configured custom handler settings for MCP"); | ||
| } | ||
|
|
||
| if (updatedConfigProfile || updatedCustomHandler) | ||
| { | ||
| string content = JsonConvert.SerializeObject(hostJsonObj, Formatting.Indented); | ||
| await FileSystemHelpers.WriteAllTextToFileAsync(hostJsonPath, content); | ||
|
|
||
| if (!exists) | ||
| { | ||
| SetupProgressLogger.FileCreated(Constants.HostJsonFileName, Path.GetFullPath(hostJsonPath)); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| SetupProgressLogger.Warn(Constants.HostJsonFileName, "Already configured (use --force to overwrite)"); | ||
| } | ||
| } | ||
|
|
||
| internal async Task ApplyLocalSettingsAsync(WorkerRuntime workerRuntime, bool force) | ||
| { | ||
| string localSettingsPath = Path.Combine(Environment.CurrentDirectory, Constants.LocalSettingsJsonFileName); | ||
| bool exists = FileSystemHelpers.FileExists(localSettingsPath); | ||
|
|
||
| // Load source for local.settings.json: existing file or the static resource | ||
| string source = exists | ||
| ? await FileSystemHelpers.ReadAllTextFromFileAsync(localSettingsPath) | ||
| : (await StaticResources.LocalSettingsJson) | ||
| .Replace($"{{{Constants.FunctionsWorkerRuntime}}}", WorkerRuntimeLanguageHelper.GetRuntimeMoniker(workerRuntime)) | ||
| .Replace($"{{{Constants.AzureWebJobsStorage}}}", Constants.StorageEmulatorConnectionString); | ||
|
|
||
| var localSettingsObj = string.IsNullOrWhiteSpace(source) ? new JObject() : JObject.Parse(source); | ||
|
|
||
| var values = localSettingsObj["Values"] as JObject ?? new JObject(); | ||
|
|
||
| // 1) Set worker runtime setting | ||
| bool updatedWorkerRuntime = UpsertIfMissing( | ||
| values, | ||
| Constants.FunctionsWorkerRuntime, | ||
| WorkerRuntimeLanguageHelper.GetRuntimeMoniker(workerRuntime), | ||
| force); | ||
|
|
||
| if (updatedWorkerRuntime) | ||
| { | ||
| SetupProgressLogger.Ok(Constants.LocalSettingsJsonFileName, $"Set {Constants.FunctionsWorkerRuntime} to '{WorkerRuntimeLanguageHelper.GetRuntimeMoniker(workerRuntime)}'"); | ||
| } | ||
|
|
||
| // 2) Set feature flag setting | ||
| bool updatedFeatureFlag = false; | ||
| bool hasFlagsKey = values.TryGetValue(Constants.AzureWebJobsFeatureFlags, StringComparison.OrdinalIgnoreCase, out var flagsToken); | ||
| var flags = (flagsToken?.ToString() ?? string.Empty) | ||
| .Split(',', StringSplitOptions.RemoveEmptyEntries) | ||
| .Select(f => f.Trim()) | ||
| .Where(f => !string.IsNullOrWhiteSpace(f)) | ||
| .ToList(); | ||
|
|
||
| if (!flags.Contains(McpFeatureFlag, StringComparer.OrdinalIgnoreCase)) | ||
| { | ||
| flags.Add(McpFeatureFlag); | ||
| values[Constants.AzureWebJobsFeatureFlags] = string.Join(",", flags); | ||
| updatedFeatureFlag = true; | ||
|
|
||
| if (!hasFlagsKey) | ||
| { | ||
| SetupProgressLogger.Ok(Constants.LocalSettingsJsonFileName, $"Added feature flag '{McpFeatureFlag}'"); | ||
| } | ||
| else | ||
| { | ||
| SetupProgressLogger.Ok(Constants.LocalSettingsJsonFileName, $"Appended feature flag '{McpFeatureFlag}'"); | ||
| } | ||
| } | ||
|
|
||
| if (updatedWorkerRuntime || updatedFeatureFlag) | ||
| { | ||
| localSettingsObj["Values"] = values; | ||
| string content = JsonConvert.SerializeObject(localSettingsObj, Formatting.Indented); | ||
| await FileSystemHelpers.WriteAllTextToFileAsync(localSettingsPath, content); | ||
|
|
||
| if (!exists) | ||
| { | ||
| SetupProgressLogger.FileCreated(Constants.LocalSettingsJsonFileName, localSettingsPath); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| SetupProgressLogger.Warn(Constants.LocalSettingsJsonFileName, "Already configured (use --force to overwrite)"); | ||
| } | ||
| } | ||
|
|
||
| private static bool UpsertIfMissing(JObject obj, string key, object desiredValue, bool forceSet) | ||
| { | ||
| JToken desired = JToken.FromObject(desiredValue); | ||
|
|
||
| if (obj.TryGetValue(key, StringComparison.OrdinalIgnoreCase, out var existing)) | ||
| { | ||
| if (!forceSet) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| obj[key] = desired; | ||
| return true; | ||
| } | ||
|
|
||
| obj[key] = desired; | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.