-
Notifications
You must be signed in to change notification settings - Fork 545
Expand file tree
/
Copy pathProtectedItemGetCommand.cs
More file actions
124 lines (110 loc) · 5 KB
/
Copy pathProtectedItemGetCommand.cs
File metadata and controls
124 lines (110 loc) · 5 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
120
121
122
123
124
// 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.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.ProtectedItem;
/// <summary>
/// Consolidated protected item command: when --protected-item is supplied returns a single
/// item's details; otherwise lists all protected items in the vault.
/// </summary>
[CommandMetadata(
Id = "bc985e4f-8945-447a-9aba-ef13df309001",
Name = "get",
Title = "Get Protected Item",
Description = """
Gets protected item (backup instances) information. When protected-item is specified, gets the specific protected item,
otherwise gets all protected items in the vault.
Returns protection status, datasource details, policy assignment, and last backup time.
Specify container for RSV workload items.
""",
Destructive = false,
Idempotent = true,
OpenWorld = false,
ReadOnly = true,
Secret = false,
LocalRequired = false)]
public sealed class ProtectedItemGetCommand(ILogger<ProtectedItemGetCommand> logger, IAzureBackupService azureBackupService) : BaseAzureBackupCommand<BaseProtectedItemOptions>()
{
private readonly ILogger<ProtectedItemGetCommand> _logger = logger;
private readonly IAzureBackupService _azureBackupService = azureBackupService;
protected override void RegisterOptions(Command command)
{
base.RegisterOptions(command);
command.Options.Add(AzureBackupOptionDefinitions.ProtectedItem.AsOptional());
command.Options.Add(AzureBackupOptionDefinitions.Container);
}
protected override BaseProtectedItemOptions BindOptions(ParseResult parseResult)
{
var options = base.BindOptions(parseResult);
options.ProtectedItem = parseResult.GetValueOrDefault<string>(AzureBackupOptionDefinitions.ProtectedItem.Name);
options.Container = parseResult.GetValueOrDefault<string>(AzureBackupOptionDefinitions.Container.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.ProtectedItem) ? "list" : "single");
try
{
if (!string.IsNullOrEmpty(options.ProtectedItem))
{
var item = await _azureBackupService.GetProtectedItemAsync(
options.Vault!,
options.ResourceGroup!,
options.Subscription!,
options.ProtectedItem,
options.VaultType,
options.Container,
options.Tenant,
options.RetryPolicy,
cancellationToken);
context.Response.Results = ResponseResult.Create(
new([item]),
AzureBackupJsonContext.Default.ProtectedItemGetCommandResult);
}
else
{
var items = await _azureBackupService.ListProtectedItemsAsync(
options.Vault!,
options.ResourceGroup!,
options.Subscription!,
options.VaultType,
options.Tenant,
options.RetryPolicy,
cancellationToken);
context.Response.Results = ResponseResult.Create(
new(items),
AzureBackupJsonContext.Default.ProtectedItemGetCommandResult);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting protected item(s). ProtectedItem: {ProtectedItem}, Vault: {Vault}",
options.ProtectedItem, options.Vault);
HandleException(context, ex);
}
return context.Response;
}
protected override string GetErrorMessage(Exception ex) => ex switch
{
KeyNotFoundException => "Protected item not found. Verify the item name and vault.",
RequestFailedException reqEx when reqEx.Status == (int)HttpStatusCode.NotFound =>
"Protected item not found. Verify the item name and vault.",
RequestFailedException reqEx => reqEx.Message,
_ => base.GetErrorMessage(ex)
};
internal record ProtectedItemGetCommandResult(List<ProtectedItemInfo> ProtectedItems);
}