-
Notifications
You must be signed in to change notification settings - Fork 437
Add azmcp sql server create/delete/show commands and unit tests
#312
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 all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8872a38
add sql server create/delete/show command impl
ericshape 75509c4
fix spell issue
ericshape d59c2b5
add docs to the md files
ericshape 0c22b93
Merge branch 'microsoft:main' into sql_server_crud
ericshape 60a13ee
Add live tests for SQL server create, show, and delete
ericshape 9a01010
fix live test
ericshape 5102c99
Merge branch 'main' into sql_server_crud
ericshape 0234c22
Merge branch 'microsoft:main' into sql_server_crud
ericshape 448979d
address commends and rebase
ericshape 3353eb2
fix server crud live test
ericshape 9f58d02
delete live test due to test sub id cannot get resource
ericshape e62a3b6
Update CHANGELOG.md
ericshape f838f6e
Update docs/azmcp-commands.md
ericshape dcd3175
Update tools/Azure.Mcp.Tools.Sql/src/Services/SqlService.cs
ericshape 04a5dbf
address comment
ericshape 96c5348
fix the build error
ericshape 6ad32db
update tool metadata info
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
129 changes: 129 additions & 0 deletions
129
tools/Azure.Mcp.Tools.Sql/src/Commands/Server/ServerCreateCommand.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,129 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Azure.Mcp.Core.Commands; | ||
| using Azure.Mcp.Core.Extensions; | ||
| using Azure.Mcp.Core.Services.Telemetry; | ||
| using Azure.Mcp.Tools.Sql.Models; | ||
| using Azure.Mcp.Tools.Sql.Options; | ||
| using Azure.Mcp.Tools.Sql.Options.Server; | ||
| using Azure.Mcp.Tools.Sql.Services; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Azure.Mcp.Tools.Sql.Commands.Server; | ||
|
|
||
| public sealed class ServerCreateCommand(ILogger<ServerCreateCommand> logger) | ||
| : BaseSqlCommand<ServerCreateOptions>(logger) | ||
| { | ||
| private const string CommandTitle = "Create SQL Server"; | ||
|
|
||
| private readonly Option<string> _administratorLoginOption = SqlOptionDefinitions.AdministratorLoginOption; | ||
| private readonly Option<string> _administratorPasswordOption = SqlOptionDefinitions.AdministratorPasswordOption; | ||
| private readonly Option<string> _locationOption = SqlOptionDefinitions.LocationOption; | ||
| private readonly Option<string> _versionOption = SqlOptionDefinitions.VersionOption; | ||
| private readonly Option<string> _publicNetworkAccessOption = SqlOptionDefinitions.PublicNetworkAccessOption; | ||
|
|
||
| public override string Name => "create"; | ||
|
|
||
| public override string Description => | ||
| """ | ||
| Creates a new Azure SQL server in the specified resource group and location. | ||
| The server will be created with the specified administrator credentials and | ||
| optional configuration settings. Returns the created server with its properties | ||
| including the fully qualified domain name. | ||
| """; | ||
|
|
||
| public override string Title => CommandTitle; | ||
|
|
||
| public override ToolMetadata Metadata => new() | ||
| { | ||
| Destructive = false, | ||
| Idempotent = false, | ||
| OpenWorld = true, | ||
| ReadOnly = false, | ||
| LocalRequired = false, | ||
| Secret = false | ||
| }; | ||
|
|
||
| protected override void RegisterOptions(Command command) | ||
| { | ||
| base.RegisterOptions(command); | ||
| command.Options.Add(_administratorLoginOption); | ||
| command.Options.Add(_administratorPasswordOption); | ||
| command.Options.Add(_locationOption); | ||
| command.Options.Add(_versionOption); | ||
| command.Options.Add(_publicNetworkAccessOption); | ||
| } | ||
|
|
||
| protected override ServerCreateOptions BindOptions(ParseResult parseResult) | ||
| { | ||
| var options = base.BindOptions(parseResult); | ||
| options.AdministratorLogin = parseResult.GetValueOrDefault(_administratorLoginOption); | ||
| options.AdministratorPassword = parseResult.GetValueOrDefault(_administratorPasswordOption); | ||
| options.Location = parseResult.GetValueOrDefault(_locationOption); | ||
| options.Version = parseResult.GetValueOrDefault(_versionOption); | ||
| options.PublicNetworkAccess = parseResult.GetValueOrDefault(_publicNetworkAccessOption); | ||
| return options; | ||
| } | ||
|
|
||
| public override async Task<CommandResponse> ExecuteAsync(CommandContext context, ParseResult parseResult) | ||
| { | ||
| if (!Validate(parseResult.CommandResult, context.Response).IsValid) | ||
| { | ||
| return context.Response; | ||
| } | ||
|
|
||
| var options = BindOptions(parseResult); | ||
|
|
||
| try | ||
| { | ||
| var sqlService = context.GetService<ISqlService>(); | ||
|
|
||
| var server = await sqlService.CreateServerAsync( | ||
| options.Server!, | ||
| options.ResourceGroup!, | ||
| options.Subscription!, | ||
| options.Location!, | ||
| options.AdministratorLogin!, | ||
| options.AdministratorPassword!, | ||
| options.Version, | ||
| options.PublicNetworkAccess, | ||
| options.RetryPolicy); | ||
|
|
||
| context.Response.Results = ResponseResult.Create( | ||
| new ServerCreateResult(server), | ||
| SqlJsonContext.Default.ServerCreateResult); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _logger.LogError(ex, | ||
| "Error creating SQL server. Server: {Server}, ResourceGroup: {ResourceGroup}, Location: {Location}, Options: {@Options}", | ||
| options.Server, options.ResourceGroup, options.Location, options); | ||
| HandleException(context, ex); | ||
| } | ||
|
|
||
| return context.Response; | ||
| } | ||
|
|
||
| protected override string GetErrorMessage(Exception ex) => ex switch | ||
| { | ||
| Azure.RequestFailedException reqEx when reqEx.Status == 409 => | ||
| "A SQL server with this name already exists. Choose a different server name.", | ||
| Azure.RequestFailedException reqEx when reqEx.Status == 403 => | ||
| $"Authorization failed creating the SQL server. Verify you have appropriate permissions. Details: {reqEx.Message}", | ||
| Azure.RequestFailedException reqEx when reqEx.Status == 400 => | ||
| $"Invalid request parameters for SQL server creation: {reqEx.Message}", | ||
| Azure.RequestFailedException reqEx => reqEx.Message, | ||
| ArgumentException argEx => $"Invalid parameter: {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 ServerCreateResult(SqlServer Server); | ||
| } |
129 changes: 129 additions & 0 deletions
129
tools/Azure.Mcp.Tools.Sql/src/Commands/Server/ServerDeleteCommand.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,129 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Azure.Mcp.Core.Commands; | ||
| using Azure.Mcp.Core.Extensions; | ||
| using Azure.Mcp.Core.Services.Telemetry; | ||
| using Azure.Mcp.Tools.Sql.Options; | ||
| using Azure.Mcp.Tools.Sql.Options.Server; | ||
| using Azure.Mcp.Tools.Sql.Services; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Azure.Mcp.Tools.Sql.Commands.Server; | ||
|
|
||
| public sealed class ServerDeleteCommand(ILogger<ServerDeleteCommand> logger) | ||
| : BaseSqlCommand<ServerDeleteOptions>(logger) | ||
| { | ||
| private const string CommandTitle = "Delete SQL Server"; | ||
|
|
||
| private readonly Option<bool> _forceOption = SqlOptionDefinitions.ForceOption; | ||
|
|
||
| public override string Name => "delete"; | ||
|
|
||
| public override string Description => | ||
| """ | ||
| Deletes an Azure SQL server and all of its databases from the specified resource group. | ||
| This operation is irreversible and will permanently remove the server and all its data. | ||
| Use the --force flag to skip confirmation prompts. | ||
| """; | ||
|
|
||
| public override string Title => CommandTitle; | ||
|
|
||
| public override ToolMetadata Metadata => new() | ||
| { | ||
| Destructive = true, | ||
| Idempotent = true, | ||
| OpenWorld = true, | ||
| ReadOnly = false, | ||
| LocalRequired = false, | ||
| Secret = false | ||
| }; | ||
|
|
||
| protected override void RegisterOptions(Command command) | ||
| { | ||
| base.RegisterOptions(command); | ||
| command.Options.Add(_forceOption); | ||
| } | ||
|
|
||
| protected override ServerDeleteOptions BindOptions(ParseResult parseResult) | ||
| { | ||
| var options = base.BindOptions(parseResult); | ||
| options.Force = parseResult.GetValueOrDefault(_forceOption); | ||
| return options; | ||
| } | ||
|
|
||
| public override async Task<CommandResponse> ExecuteAsync(CommandContext context, ParseResult parseResult) | ||
| { | ||
| if (!Validate(parseResult.CommandResult, context.Response).IsValid) | ||
| { | ||
| return context.Response; | ||
| } | ||
|
|
||
| var options = BindOptions(parseResult); | ||
|
|
||
| try | ||
| { | ||
| // Show warning about destructive operation unless force is specified | ||
| if (!options.Force) | ||
| { | ||
| context.Response.Status = 200; | ||
| context.Response.Message = | ||
| $"WARNING: This operation will permanently delete the SQL server '{options.Server}' " + | ||
| $"and ALL its databases in resource group '{options.ResourceGroup}'. " + | ||
| $"This action cannot be undone. Use --force to confirm deletion."; | ||
| return context.Response; | ||
| } | ||
|
|
||
| var sqlService = context.GetService<ISqlService>(); | ||
|
|
||
| var deleted = await sqlService.DeleteServerAsync( | ||
| options.Server!, | ||
| options.ResourceGroup!, | ||
| options.Subscription!, | ||
| options.RetryPolicy); | ||
|
|
||
| if (deleted) | ||
| { | ||
| context.Response.Results = ResponseResult.Create( | ||
| new ServerDeleteResult($"SQL server '{options.Server}' was successfully deleted.", true), | ||
| SqlJsonContext.Default.ServerDeleteResult); | ||
| } | ||
| else | ||
| { | ||
| context.Response.Status = 404; | ||
| context.Response.Message = $"SQL server '{options.Server}' not found in resource group '{options.ResourceGroup}'."; | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _logger.LogError(ex, | ||
| "Error deleting SQL server. Server: {Server}, ResourceGroup: {ResourceGroup}, Options: {@Options}", | ||
| options.Server, options.ResourceGroup, options); | ||
| HandleException(context, ex); | ||
| } | ||
|
|
||
| return context.Response; | ||
| } | ||
|
|
||
| protected override string GetErrorMessage(Exception ex) => ex switch | ||
| { | ||
| Azure.RequestFailedException reqEx when reqEx.Status == 404 => | ||
| $"The given SQL server not found. It may have already been deleted.", | ||
| Azure.RequestFailedException reqEx when reqEx.Status == 403 => | ||
| $"Authorization failed deleting the SQL server. Verify you have appropriate permissions. Details: {reqEx.Message}", | ||
| Azure.RequestFailedException reqEx when reqEx.Status == 409 => | ||
| $"Cannot delete SQL server due to a conflict. It may be in use or have dependent resources. Details: {reqEx.Message}", | ||
| Azure.RequestFailedException reqEx => reqEx.Message, | ||
| ArgumentException argEx => $"Invalid parameter: {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 ServerDeleteResult(string Message, bool Success); | ||
| } |
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.