-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathHelloControlTower.cs
More file actions
75 lines (62 loc) · 2.54 KB
/
HelloControlTower.cs
File metadata and controls
75 lines (62 loc) · 2.54 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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[ControlTower.dotnetv4.HelloControlTower]
using Amazon.ControlTower;
using Amazon.ControlTower.Model;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Microsoft.Extensions.Logging.Debug;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
namespace ControlTowerActions;
/// <summary>
/// A class that introduces the AWS Control Tower by listing the
/// available baselines for the account.
/// </summary>
public class HelloControlTower
{
private static ILogger logger = null!;
static async Task Main(string[] args)
{
// Set up dependency injection for AWS Control Tower.
using var host = Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
logging.AddFilter("System", LogLevel.Debug)
.AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Information)
.AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace))
.ConfigureServices((_, services) =>
services.AddAWSService<IAmazonControlTower>()
)
.Build();
logger = LoggerFactory.Create(builder => { builder.AddConsole(); })
.CreateLogger<HelloControlTower>();
var amazonClient = host.Services.GetRequiredService<IAmazonControlTower>();
Console.Clear();
Console.WriteLine("Hello, AWS Control Tower! Let's list available baselines:");
Console.WriteLine();
var baselines = new List<BaselineSummary>();
try
{
var baselinesPaginator = amazonClient.Paginators.ListBaselines(new ListBaselinesRequest());
await foreach (var response in baselinesPaginator.Responses)
{
baselines.AddRange(response.Baselines);
}
Console.WriteLine($"{baselines.Count} baseline(s) retrieved.");
foreach (var baseline in baselines)
{
Console.WriteLine($"\t{baseline.Name}");
}
}
catch (Amazon.ControlTower.Model.AccessDeniedException)
{
Console.WriteLine("Access denied. Please ensure you have the necessary permissions.");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
// snippet-end:[ControlTower.dotnetv4.HelloControlTower]