-
Notifications
You must be signed in to change notification settings - Fork 545
Expand file tree
/
Copy pathKeyValueDeleteCommand.cs
More file actions
68 lines (59 loc) · 2.65 KB
/
Copy pathKeyValueDeleteCommand.cs
File metadata and controls
68 lines (59 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Azure.Mcp.Tools.AppConfig.Options.KeyValue;
using Azure.Mcp.Tools.AppConfig.Services;
using Microsoft.Extensions.Logging;
using Microsoft.Mcp.Core.Commands;
using Microsoft.Mcp.Core.Models.Command;
namespace Azure.Mcp.Tools.AppConfig.Commands.KeyValue;
[CommandMetadata(
Id = "f885a499-82ec-4897-a788-fb6b4615ab06",
Name = "delete",
Title = "Delete App Configuration Key-Value Setting",
Description = """
Delete a key-value pair from an App Configuration store. If a label is specified, only the labeled version is deleted.
If no label is specified, the key-value with the matching key and the default label will be deleted.
""",
Destructive = true,
Idempotent = true,
OpenWorld = false,
ReadOnly = false,
Secret = false,
LocalRequired = false)]
public sealed class KeyValueDeleteCommand(ILogger<KeyValueDeleteCommand> logger, IAppConfigService appConfigService)
: BaseKeyValueCommand<KeyValueDeleteOptions>()
{
private readonly ILogger<KeyValueDeleteCommand> _logger = logger;
private readonly IAppConfigService _appConfigService = appConfigService;
public override async Task<CommandResponse> ExecuteAsync(CommandContext context, ParseResult parseResult, CancellationToken cancellationToken)
{
if (!Validate(parseResult.CommandResult, context.Response).IsValid)
{
return context.Response;
}
var options = BindOptions(parseResult);
try
{
var existed = await _appConfigService.DeleteKeyValue(
options.Account!,
options.Key!,
options.Subscription!,
options.Tenant,
options.RetryPolicy,
options.Label,
cancellationToken);
var labelSuffix = options.Label is null ? string.Empty : $" with label '{options.Label}'";
var message = existed
? $"Key '{options.Key}'{labelSuffix} deleted successfully."
: $"Key '{options.Key}'{labelSuffix} did not exist in store '{options.Account}'.";
context.Response.Results = ResponseResult.Create(new(options.Key, options.Label, existed, message), AppConfigJsonContext.Default.KeyValueDeleteCommandResult);
}
catch (Exception ex)
{
_logger.LogError(ex, "An exception occurred deleting value. Key: {Key}.", options.Key);
HandleException(context, ex);
}
return context.Response;
}
internal record KeyValueDeleteCommandResult(string? Key, string? Label, bool Existed, string Message);
}