-
Notifications
You must be signed in to change notification settings - Fork 545
Expand file tree
/
Copy pathClusterGetCommand.cs
More file actions
88 lines (77 loc) · 3.43 KB
/
Copy pathClusterGetCommand.cs
File metadata and controls
88 lines (77 loc) · 3.43 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Azure.Mcp.Tools.Aks.Options;
using Azure.Mcp.Tools.Aks.Options.Cluster;
using Azure.Mcp.Tools.Aks.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.Aks.Commands.Cluster;
[CommandMetadata(
Id = "34e0d3d3-cbc5-4df8-8244-1439b97f3de5",
Name = "get",
Title = "Get Azure Kubernetes Service (AKS) Cluster Details",
Description = "List/enumerate all AKS (Azure Kubernetes Service) clusters in a subscription. Get/retrieve/show the details of a specific cluster if a cluster name is provided.",
Destructive = false,
Idempotent = true,
OpenWorld = false,
ReadOnly = true,
Secret = false,
LocalRequired = false)]
public sealed class ClusterGetCommand(ILogger<ClusterGetCommand> logger, IAksService aksService) : BaseAksCommand<ClusterGetOptions>
{
private readonly ILogger<ClusterGetCommand> _logger = logger;
private readonly IAksService _aksService = aksService;
protected override void RegisterOptions(Command command)
{
base.RegisterOptions(command);
command.Options.Add(OptionDefinitions.Common.ResourceGroup);
command.Options.Add(AksOptionDefinitions.Cluster);
command.Validators.Add(commandResults =>
{
var clusterName = commandResults.GetValueOrDefault(AksOptionDefinitions.Cluster);
var resourceGroup = commandResults.GetValueOrDefault(OptionDefinitions.Common.ResourceGroup);
if (!string.IsNullOrEmpty(clusterName) && string.IsNullOrEmpty(resourceGroup))
{
commandResults.AddError("When specifying a cluster name, the --resource-group option is required.");
}
});
}
protected override ClusterGetOptions BindOptions(ParseResult parseResult)
{
var options = base.BindOptions(parseResult);
options.ClusterName = parseResult.GetValueOrDefault(AksOptionDefinitions.Cluster);
options.ResourceGroup ??= parseResult.GetValueOrDefault(OptionDefinitions.Common.ResourceGroup);
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);
try
{
var clusters = await _aksService.GetClusters(
options.Subscription!,
options.ClusterName,
options.ResourceGroup,
options.Tenant,
options.RetryPolicy,
cancellationToken);
context.Response.Results = ResponseResult.Create(new(clusters ?? []), AksJsonContext.Default.ClusterGetCommandResult);
}
catch (Exception ex)
{
_logger.LogError(ex,
"Error getting AKS cluster. Subscription: {Subscription}, ResourceGroup: {ResourceGroup}, ClusterName: {ClusterName}.",
options.Subscription, options.ResourceGroup, options.ClusterName);
HandleException(context, ex);
}
return context.Response;
}
internal record ClusterGetCommandResult(List<Models.Cluster> Clusters);
}