-
Notifications
You must be signed in to change notification settings - Fork 545
Expand file tree
/
Copy pathPolicyGetCommand.cs
More file actions
119 lines (105 loc) · 4.45 KB
/
Copy pathPolicyGetCommand.cs
File metadata and controls
119 lines (105 loc) · 4.45 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Net;
using Azure.Mcp.Tools.AzureBackup.Models;
using Azure.Mcp.Tools.AzureBackup.Options;
using Azure.Mcp.Tools.AzureBackup.Options.Policy;
using Azure.Mcp.Tools.AzureBackup.Services;
using Microsoft.Extensions.Logging;
using Microsoft.Mcp.Core.Commands;
using Microsoft.Mcp.Core.Extensions;
using Microsoft.Mcp.Core.Models.Command;
using Microsoft.Mcp.Core.Models.Option;
namespace Azure.Mcp.Tools.AzureBackup.Commands.Policy;
/// <summary>
/// Consolidated policy command: when --policy is supplied returns a single policy's details;
/// otherwise lists all policies in the vault.
/// </summary>
[CommandMetadata(
Id = "5f7ef3ae-72f3-4fe8-bd1e-ea56e4db86df",
Name = "get",
Title = "Get Backup Policy",
Description = """
Gets backup policy information. When policy is specified, gets the specific policy, otherwise gets all backup
policies configured in the vault.
Returns datasource types and protected items count.
""",
Destructive = false,
Idempotent = true,
OpenWorld = false,
ReadOnly = true,
Secret = false,
LocalRequired = false)]
public sealed class PolicyGetCommand(ILogger<PolicyGetCommand> logger, IAzureBackupService azureBackupService) : BaseAzureBackupCommand<PolicyGetOptions>()
{
private readonly ILogger<PolicyGetCommand> _logger = logger;
private readonly IAzureBackupService _azureBackupService = azureBackupService;
protected override void RegisterOptions(Command command)
{
base.RegisterOptions(command);
command.Options.Add(AzureBackupOptionDefinitions.Policy.AsOptional());
}
protected override PolicyGetOptions BindOptions(ParseResult parseResult)
{
var options = base.BindOptions(parseResult);
options.Policy = parseResult.GetValueOrDefault<string>(AzureBackupOptionDefinitions.Policy.Name);
return options;
}
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);
AzureBackupTelemetryTags.AddSubscriptionTag(context.Activity, options.Subscription);
AzureBackupTelemetryTags.AddVaultTags(context.Activity, options.VaultType);
context.Activity?.AddTag(AzureBackupTelemetryTags.OperationScope, string.IsNullOrEmpty(options.Policy) ? "list" : "single");
try
{
if (!string.IsNullOrEmpty(options.Policy))
{
var policy = await _azureBackupService.GetPolicyAsync(
options.Vault!,
options.ResourceGroup!,
options.Subscription!,
options.Policy,
options.VaultType,
options.Tenant,
options.RetryPolicy,
cancellationToken);
context.Response.Results = ResponseResult.Create(
new([policy]),
AzureBackupJsonContext.Default.PolicyGetCommandResult);
}
else
{
var policies = await _azureBackupService.ListPoliciesAsync(
options.Vault!,
options.ResourceGroup!,
options.Subscription!,
options.VaultType,
options.Tenant,
options.RetryPolicy,
cancellationToken);
context.Response.Results = ResponseResult.Create(
new(policies),
AzureBackupJsonContext.Default.PolicyGetCommandResult);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting policy/policies. Policy: {Policy}, Vault: {Vault}", options.Policy, options.Vault);
HandleException(context, ex);
}
return context.Response;
}
protected override string GetErrorMessage(Exception ex) => ex switch
{
RequestFailedException reqEx when reqEx.Status == (int)HttpStatusCode.NotFound =>
"Policy not found. Verify the policy name and vault.",
RequestFailedException reqEx => reqEx.Message,
_ => base.GetErrorMessage(ex)
};
internal record PolicyGetCommandResult(List<BackupPolicyInfo> Policies);
}