-
Notifications
You must be signed in to change notification settings - Fork 545
Expand file tree
/
Copy pathWebappGetCommand.cs
More file actions
103 lines (91 loc) · 4.15 KB
/
Copy pathWebappGetCommand.cs
File metadata and controls
103 lines (91 loc) · 4.15 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Azure.Mcp.Tools.AppService.Models;
using Azure.Mcp.Tools.AppService.Options;
using Azure.Mcp.Tools.AppService.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.AppService.Commands.Webapp;
[CommandMetadata(
Id = "4412f1af-16e7-46db-8305-33e3d7ae06de",
Name = "get",
Title = "Gets Azure App Service Web App Details",
Description = """
Retrieves detailed information about Azure App Service web apps, including app name, resource group, location,
state, hostnames, etc. If a specific app name is not provided, the command will return details for all web apps
in a subscription or resource group. You can specify the app name, resource group name, and subscription to get
details for a specific web app.
""",
Destructive = false,
Idempotent = true,
OpenWorld = false,
ReadOnly = true,
Secret = false,
LocalRequired = false)]
public sealed class WebappGetCommand(ILogger<WebappGetCommand> logger, IAppServiceService appServiceService)
: BaseAppServiceCommand<BaseAppServiceOptions>(resourceGroupRequired: false)
{
private readonly ILogger<WebappGetCommand> _logger = logger;
private readonly IAppServiceService _appServiceService = appServiceService;
protected override void RegisterOptions(Command command)
{
base.RegisterOptions(command);
command.Validators.Add(commandResult =>
{
var appName = commandResult.GetValueOrDefault<string>(AppServiceOptionDefinitions.AppServiceName.Name);
var resourceGroup = commandResult.GetValueOrDefault<string>(OptionDefinitions.Common.ResourceGroup.Name);
if (!string.IsNullOrWhiteSpace(appName) && string.IsNullOrWhiteSpace(resourceGroup))
{
commandResult.AddError($"When specifying '{AppServiceOptionDefinitions.AppServiceName.Name}', you must also specify '{OptionDefinitions.Common.ResourceGroup.Name}'.");
}
});
}
protected override BaseAppServiceOptions BindOptions(ParseResult parseResult) => base.BindOptions(parseResult);
public override async Task<CommandResponse> ExecuteAsync(CommandContext context, ParseResult parseResult, CancellationToken cancellationToken)
{
// Validate first, then bind
if (!Validate(parseResult.CommandResult, context.Response).IsValid)
{
return context.Response;
}
var options = BindOptions(parseResult);
try
{
context.Activity?.AddTag("subscription", options.Subscription);
var webapps = await _appServiceService.GetWebAppsAsync(
options.Subscription!,
options.ResourceGroup,
options.AppName,
options.Tenant,
options.RetryPolicy,
cancellationToken);
context.Response.Results = ResponseResult.Create(new(webapps), AppServiceJsonContext.Default.WebappGetResult);
}
catch (Exception ex)
{
if (options.AppName == null)
{
if (options.ResourceGroup == null)
{
_logger.LogError(ex, "Failed to list Web Apps in subscription {Subscription}", options.Subscription);
}
else
{
_logger.LogError(ex, "Failed to list Web Apps in resource group {ResourceGroup} and subscription {Subscription}",
options.ResourceGroup, options.Subscription);
}
}
else
{
_logger.LogError(ex, "Failed to get Web App details for '{AppName}' in subscription {Subscription} and resource group {ResourceGroup}",
options.AppName, options.Subscription, options.ResourceGroup);
}
HandleException(context, ex);
}
return context.Response;
}
public record WebappGetResult(List<WebappDetails> Webapps);
}