-
Notifications
You must be signed in to change notification settings - Fork 272
Add azmcp sql server firewall-rule create and delete commands and unit tests
#121
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 11 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a8bc055
Add SQL Server firewall rule create and delete commands and unit tests
ericshape 6294225
fix format issue
ericshape b591b1b
Delete Azure.Mcp.Tools.Sql.UnitTests.sln.DotSettings.user
ericshape 88af647
Move SqlSetup to AOT compatibility exception section.
ericshape 30a1269
fix AOT issue
ericshape b7a55f4
add docs and update firewall-rule-name in the parameter list
ericshape 658f9a0
Merge branch 'microsoft:main' into main
ericshape 093b675
Update CHANGELOG.md
ericshape 2c5d5a6
fix failed live test
ericshape cfb7ae0
add delete unit test
ericshape 5104623
add delete firewall rule unit test
ericshape 76af369
remove blank line
ericshape 8e25827
remove explict reference
ericshape 1504bd8
remove explict reference
ericshape 5a7012c
Revert "remove explict reference"
ericshape b7a8ea5
Revert "remove explict reference"
ericshape 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
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
112 changes: 112 additions & 0 deletions
112
tools/Azure.Mcp.Tools.Sql/src/Commands/FirewallRule/FirewallRuleCreateCommand.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,112 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Azure.Mcp.Core.Commands; | ||
| using Azure.Mcp.Core.Services.Telemetry; | ||
| using Azure.Mcp.Tools.Sql.Models; | ||
| using Azure.Mcp.Tools.Sql.Options; | ||
| using Azure.Mcp.Tools.Sql.Options.FirewallRule; | ||
| using Azure.Mcp.Tools.Sql.Services; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Azure.Mcp.Tools.Sql.Commands.FirewallRule; | ||
|
|
||
| public sealed class FirewallRuleCreateCommand(ILogger<FirewallRuleCreateCommand> logger) | ||
| : BaseSqlCommand<FirewallRuleCreateOptions>(logger) | ||
| { | ||
| private const string CommandTitle = "Create SQL Server Firewall Rule"; | ||
|
|
||
| private readonly Option<string> _firewallRuleNameOption = SqlOptionDefinitions.FirewallRuleNameOption; | ||
| private readonly Option<string> _startIpAddressOption = SqlOptionDefinitions.StartIpAddressOption; | ||
| private readonly Option<string> _endIpAddressOption = SqlOptionDefinitions.EndIpAddressOption; | ||
|
|
||
| public override string Name => "create"; | ||
|
|
||
| public override string Description => | ||
| """ | ||
| Creates a firewall rule for a SQL server. Firewall rules control which IP addresses | ||
| are allowed to connect to the SQL server. You can specify either a single IP address | ||
| (by setting start and end IP to the same value) or a range of IP addresses. Returns | ||
| the created firewall rule with its properties. | ||
| """; | ||
|
|
||
| public override string Title => CommandTitle; | ||
|
|
||
| public override ToolMetadata Metadata => new() { Destructive = false, ReadOnly = false }; | ||
|
|
||
| protected override void RegisterOptions(Command command) | ||
| { | ||
| base.RegisterOptions(command); | ||
| command.AddOption(_firewallRuleNameOption); | ||
| command.AddOption(_startIpAddressOption); | ||
| command.AddOption(_endIpAddressOption); | ||
| } | ||
|
|
||
| protected override FirewallRuleCreateOptions BindOptions(ParseResult parseResult) | ||
| { | ||
| var options = base.BindOptions(parseResult); | ||
| options.FirewallRuleName = parseResult.GetValueForOption(_firewallRuleNameOption); | ||
| options.StartIpAddress = parseResult.GetValueForOption(_startIpAddressOption); | ||
| options.EndIpAddress = parseResult.GetValueForOption(_endIpAddressOption); | ||
| return options; | ||
| } | ||
|
|
||
| public override async Task<CommandResponse> ExecuteAsync(CommandContext context, ParseResult parseResult) | ||
| { | ||
| var options = BindOptions(parseResult); | ||
|
|
||
| try | ||
| { | ||
| if (!Validate(parseResult.CommandResult, context.Response).IsValid) | ||
| { | ||
| return context.Response; | ||
| } | ||
|
|
||
| var sqlService = context.GetService<ISqlService>(); | ||
|
|
||
| var firewallRule = await sqlService.CreateFirewallRuleAsync( | ||
| options.Server!, | ||
| options.ResourceGroup!, | ||
| options.Subscription!, | ||
| options.FirewallRuleName!, | ||
| options.StartIpAddress!, | ||
| options.EndIpAddress!, | ||
| options.RetryPolicy); | ||
|
|
||
| context.Response.Results = ResponseResult.Create( | ||
| new FirewallRuleCreateResult(firewallRule), | ||
| SqlJsonContext.Default.FirewallRuleCreateResult); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _logger.LogError(ex, | ||
| "Error creating SQL server firewall rule. Server: {Server}, ResourceGroup: {ResourceGroup}, Rule: {Rule}, Options: {@Options}", | ||
| options.Server, options.ResourceGroup, options.FirewallRuleName, options); | ||
| HandleException(context, ex); | ||
| } | ||
|
|
||
| return context.Response; | ||
| } | ||
|
|
||
| protected override string GetErrorMessage(Exception ex) => ex switch | ||
| { | ||
| Azure.RequestFailedException reqEx when reqEx.Status == 404 => | ||
| "SQL server not found. Verify the server name, resource group, and that you have access.", | ||
| Azure.RequestFailedException reqEx when reqEx.Status == 403 => | ||
| $"Authorization failed creating the firewall rule. Verify you have appropriate permissions. Details: {reqEx.Message}", | ||
| Azure.RequestFailedException reqEx when reqEx.Status == 409 => | ||
| "A firewall rule with this name already exists. Choose a different name or update the existing rule.", | ||
| Azure.RequestFailedException reqEx => reqEx.Message, | ||
| ArgumentException argEx => $"Invalid IP address format: {argEx.Message}", | ||
| _ => base.GetErrorMessage(ex) | ||
| }; | ||
|
|
||
| protected override int GetStatusCode(Exception ex) => ex switch | ||
| { | ||
| Azure.RequestFailedException reqEx => reqEx.Status, | ||
| ArgumentException => 400, | ||
| _ => base.GetStatusCode(ex) | ||
| }; | ||
|
|
||
| internal record FirewallRuleCreateResult(SqlServerFirewallRule FirewallRule); | ||
| } |
101 changes: 101 additions & 0 deletions
101
tools/Azure.Mcp.Tools.Sql/src/Commands/FirewallRule/FirewallRuleDeleteCommand.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,101 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Azure.Mcp.Core.Commands; | ||
| using Azure.Mcp.Core.Services.Telemetry; | ||
| using Azure.Mcp.Tools.Sql.Options; | ||
| using Azure.Mcp.Tools.Sql.Options.FirewallRule; | ||
| using Azure.Mcp.Tools.Sql.Services; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Azure.Mcp.Tools.Sql.Commands.FirewallRule; | ||
|
|
||
| public sealed class FirewallRuleDeleteCommand(ILogger<FirewallRuleDeleteCommand> logger) | ||
| : BaseSqlCommand<FirewallRuleDeleteOptions>(logger) | ||
| { | ||
| private const string CommandTitle = "Delete SQL Server Firewall Rule"; | ||
|
|
||
| private readonly Option<string> _firewallRuleNameOption = SqlOptionDefinitions.FirewallRuleNameOption; | ||
|
|
||
| public override string Name => "delete"; | ||
|
|
||
| public override string Description => | ||
| """ | ||
| Deletes a firewall rule from a SQL server. This operation removes the specified | ||
| firewall rule, potentially restricting access for the IP addresses that were | ||
| previously allowed by this rule. The operation is idempotent - if the rule | ||
| doesn't exist, no error is returned. | ||
| """; | ||
|
|
||
| public override string Title => CommandTitle; | ||
|
|
||
| public override ToolMetadata Metadata => new() { Destructive = true, ReadOnly = false }; | ||
|
|
||
| protected override void RegisterOptions(Command command) | ||
| { | ||
| base.RegisterOptions(command); | ||
| command.AddOption(_firewallRuleNameOption); | ||
| } | ||
|
|
||
| protected override FirewallRuleDeleteOptions BindOptions(ParseResult parseResult) | ||
| { | ||
| var options = base.BindOptions(parseResult); | ||
| options.FirewallRuleName = parseResult.GetValueForOption(_firewallRuleNameOption); | ||
| return options; | ||
| } | ||
|
|
||
| public override async Task<CommandResponse> ExecuteAsync(CommandContext context, ParseResult parseResult) | ||
| { | ||
| var options = BindOptions(parseResult); | ||
|
|
||
| try | ||
| { | ||
| if (!Validate(parseResult.CommandResult, context.Response).IsValid) | ||
| { | ||
| return context.Response; | ||
| } | ||
|
|
||
| var sqlService = context.GetService<ISqlService>(); | ||
|
|
||
| var deleted = await sqlService.DeleteFirewallRuleAsync( | ||
| options.Server!, | ||
| options.ResourceGroup!, | ||
| options.Subscription!, | ||
| options.FirewallRuleName!, | ||
| options.RetryPolicy); | ||
|
|
||
| context.Response.Results = ResponseResult.Create( | ||
| new FirewallRuleDeleteResult(deleted, options.FirewallRuleName!), | ||
| SqlJsonContext.Default.FirewallRuleDeleteResult); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _logger.LogError(ex, | ||
| "Error deleting SQL server firewall rule. Server: {Server}, ResourceGroup: {ResourceGroup}, Rule: {Rule}, Options: {@Options}", | ||
| options.Server, options.ResourceGroup, options.FirewallRuleName, options); | ||
| HandleException(context, ex); | ||
| } | ||
|
|
||
| return context.Response; | ||
| } | ||
|
|
||
| protected override string GetErrorMessage(Exception ex) => ex switch | ||
| { | ||
| Azure.RequestFailedException reqEx when reqEx.Status == 404 => | ||
| "SQL server or firewall rule not found. Verify the server name, rule name, resource group, and that you have access.", | ||
| Azure.RequestFailedException reqEx when reqEx.Status == 403 => | ||
| $"Authorization failed deleting the firewall rule. Verify you have appropriate permissions. Details: {reqEx.Message}", | ||
| Azure.RequestFailedException reqEx => reqEx.Message, | ||
| ArgumentException argEx => argEx.Message, | ||
| _ => base.GetErrorMessage(ex) | ||
| }; | ||
|
|
||
| protected override int GetStatusCode(Exception ex) => ex switch | ||
| { | ||
| Azure.RequestFailedException reqEx => reqEx.Status, | ||
| ArgumentException => 400, | ||
| _ => base.GetStatusCode(ex) | ||
| }; | ||
|
|
||
| internal record FirewallRuleDeleteResult(bool Deleted, string RuleName); | ||
| } |
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
19 changes: 19 additions & 0 deletions
19
tools/Azure.Mcp.Tools.Sql/src/Options/FirewallRule/FirewallRuleCreateOptions.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,19 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Text.Json.Serialization; | ||
| using Azure.Mcp.Tools.Sql.Options; | ||
|
|
||
| namespace Azure.Mcp.Tools.Sql.Options.FirewallRule; | ||
|
|
||
| public class FirewallRuleCreateOptions : BaseSqlOptions | ||
| { | ||
| [JsonPropertyName(SqlOptionDefinitions.FirewallRuleName)] | ||
| public string? FirewallRuleName { get; set; } | ||
|
|
||
| [JsonPropertyName(SqlOptionDefinitions.StartIpAddress)] | ||
| public string? StartIpAddress { get; set; } | ||
|
|
||
| [JsonPropertyName(SqlOptionDefinitions.EndIpAddress)] | ||
| public string? EndIpAddress { get; set; } | ||
| } |
13 changes: 13 additions & 0 deletions
13
tools/Azure.Mcp.Tools.Sql/src/Options/FirewallRule/FirewallRuleDeleteOptions.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,13 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Text.Json.Serialization; | ||
| using Azure.Mcp.Tools.Sql.Options; | ||
|
|
||
| namespace Azure.Mcp.Tools.Sql.Options.FirewallRule; | ||
|
|
||
| public class FirewallRuleDeleteOptions : BaseSqlOptions | ||
| { | ||
| [JsonPropertyName(SqlOptionDefinitions.FirewallRuleName)] | ||
| public string? FirewallRuleName { get; set; } | ||
| } |
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
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.