Skip to content

Commit ba9ee21

Browse files
authored
.NET v4 Preview: Adding ECS Folder (#7261)
Adding ECS Folder
1 parent d1d1f40 commit ba9ee21

File tree

11 files changed

+683
-0
lines changed

11 files changed

+683
-0
lines changed

dotnetv4/DotNetV4Examples.sln

+23
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CognitoBasics", "Cognito\Sc
129129
EndProject
130130
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CognitoActions", "Cognito\Actions\CognitoActions.csproj", "{1AF980DF-DEEA-4E5D-9001-6EC67EB96AD1}"
131131
EndProject
132+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ECS", "ECS", "{EE6D1933-1E38-406A-B691-446326310D1F}"
133+
EndProject
134+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ECSTests", "ECS\ECSTests\ECSTests.csproj", "{3F159C49-3DE7-42F5-AF14-E64C03AF19E8}"
135+
EndProject
136+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ECSScenario", "ECS\ECSScenario\ECSScenario.csproj", "{D44D50E1-EC65-4A1C-AAA1-C360E4FC563F}"
137+
EndProject
138+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ECSActions", "ECS\ECSActions\ECSActions.csproj", "{7485EAED-F81C-4119-BABC-E009A21ACE46}"
139+
EndProject
132140
Global
133141
GlobalSection(SolutionConfigurationPlatforms) = preSolution
134142
Debug|Any CPU = Debug|Any CPU
@@ -315,6 +323,18 @@ Global
315323
{1AF980DF-DEEA-4E5D-9001-6EC67EB96AD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
316324
{1AF980DF-DEEA-4E5D-9001-6EC67EB96AD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
317325
{1AF980DF-DEEA-4E5D-9001-6EC67EB96AD1}.Release|Any CPU.Build.0 = Release|Any CPU
326+
{3F159C49-3DE7-42F5-AF14-E64C03AF19E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
327+
{3F159C49-3DE7-42F5-AF14-E64C03AF19E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
328+
{3F159C49-3DE7-42F5-AF14-E64C03AF19E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
329+
{3F159C49-3DE7-42F5-AF14-E64C03AF19E8}.Release|Any CPU.Build.0 = Release|Any CPU
330+
{D44D50E1-EC65-4A1C-AAA1-C360E4FC563F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
331+
{D44D50E1-EC65-4A1C-AAA1-C360E4FC563F}.Debug|Any CPU.Build.0 = Debug|Any CPU
332+
{D44D50E1-EC65-4A1C-AAA1-C360E4FC563F}.Release|Any CPU.ActiveCfg = Release|Any CPU
333+
{D44D50E1-EC65-4A1C-AAA1-C360E4FC563F}.Release|Any CPU.Build.0 = Release|Any CPU
334+
{7485EAED-F81C-4119-BABC-E009A21ACE46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
335+
{7485EAED-F81C-4119-BABC-E009A21ACE46}.Debug|Any CPU.Build.0 = Debug|Any CPU
336+
{7485EAED-F81C-4119-BABC-E009A21ACE46}.Release|Any CPU.ActiveCfg = Release|Any CPU
337+
{7485EAED-F81C-4119-BABC-E009A21ACE46}.Release|Any CPU.Build.0 = Release|Any CPU
318338
EndGlobalSection
319339
GlobalSection(SolutionProperties) = preSolution
320340
HideSolutionNode = FALSE
@@ -375,6 +395,9 @@ Global
375395
{D38A409C-EE40-4E70-B500-F3D6EF8E82A4} = {F5214562-85F4-4FD8-B56D-C5D8E7914901}
376396
{38C8C3B0-163D-4B7B-86A2-3EFFBC165E99} = {D38A409C-EE40-4E70-B500-F3D6EF8E82A4}
377397
{1AF980DF-DEEA-4E5D-9001-6EC67EB96AD1} = {F5214562-85F4-4FD8-B56D-C5D8E7914901}
398+
{3F159C49-3DE7-42F5-AF14-E64C03AF19E8} = {EE6D1933-1E38-406A-B691-446326310D1F}
399+
{D44D50E1-EC65-4A1C-AAA1-C360E4FC563F} = {EE6D1933-1E38-406A-B691-446326310D1F}
400+
{7485EAED-F81C-4119-BABC-E009A21ACE46} = {EE6D1933-1E38-406A-B691-446326310D1F}
378401
EndGlobalSection
379402
GlobalSection(ExtensibilityGlobals) = postSolution
380403
SolutionGuid = {08502818-E8E1-4A91-A51C-4C8C8D4FF9CA}

dotnetv4/ECS/ECS.sln

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.810.20
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ECSActions", "ECSActions\ECSActions.csproj", "{775C4572-76B4-492D-A1FD-606C61D1ADC2}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ECSScenario", "ECSScenario\ECSScenario.csproj", "{14A438EC-C572-47C8-91E7-344932B28DF8}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ECSTests", "ECSTests\ECSTests.csproj", "{D71EADB2-F6C5-4A75-BF11-B702BF931F14}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
Release|Any CPU = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{775C4572-76B4-492D-A1FD-606C61D1ADC2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{775C4572-76B4-492D-A1FD-606C61D1ADC2}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{775C4572-76B4-492D-A1FD-606C61D1ADC2}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{775C4572-76B4-492D-A1FD-606C61D1ADC2}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{14A438EC-C572-47C8-91E7-344932B28DF8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{14A438EC-C572-47C8-91E7-344932B28DF8}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{14A438EC-C572-47C8-91E7-344932B28DF8}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{14A438EC-C572-47C8-91E7-344932B28DF8}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{D71EADB2-F6C5-4A75-BF11-B702BF931F14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{D71EADB2-F6C5-4A75-BF11-B702BF931F14}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{D71EADB2-F6C5-4A75-BF11-B702BF931F14}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{D71EADB2-F6C5-4A75-BF11-B702BF931F14}.Release|Any CPU.Build.0 = Release|Any CPU
30+
EndGlobalSection
31+
GlobalSection(SolutionProperties) = preSolution
32+
HideSolutionNode = FALSE
33+
EndGlobalSection
34+
GlobalSection(ExtensibilityGlobals) = postSolution
35+
SolutionGuid = {2416AA5E-0460-4DA0-AA2C-168F0130B0EB}
36+
EndGlobalSection
37+
EndGlobal
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<OutputType>Exe</OutputType>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
11+
<LangVersion>10.0</LangVersion>
12+
</PropertyGroup>
13+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
14+
<LangVersion>10.0</LangVersion>
15+
</PropertyGroup>
16+
<ItemGroup>
17+
<PackageReference Include="AWSSDK.Core" Version="4.0.0-preview.7" />
18+
<PackageReference Include="AWSSDK.ECS" Version="4.0.0-preview.7" />
19+
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.2" />
20+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.2" />
21+
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="4.0.0-preview.7" />
22+
</ItemGroup>
23+
</Project>

dotnetv4/ECS/ECSActions/ECSWrapper.cs

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// snippet-start:[ECS.dotnetv4.ECSActions.ECSWrapper]
5+
using Amazon.ECS;
6+
using Amazon.ECS.Model;
7+
using Microsoft.Extensions.Logging;
8+
9+
namespace ECSActions;
10+
11+
public class ECSWrapper
12+
{
13+
private readonly IAmazonECS _ecsClient;
14+
private readonly ILogger<ECSWrapper> _logger;
15+
16+
/// <summary>
17+
/// Constructor for the ECS wrapper.
18+
/// </summary>
19+
/// <param name="ecsClient">The injected ECS client.</param>
20+
/// <param name="logger">The injected logger for the wrapper.</param>
21+
public ECSWrapper(IAmazonECS ecsClient, ILogger<ECSWrapper> logger)
22+
23+
{
24+
_logger = logger;
25+
_ecsClient = ecsClient;
26+
}
27+
28+
// snippet-start:[ECS.dotnetv4.ECSActions.ListClusters]
29+
/// <summary>
30+
/// List cluster ARNs available.
31+
/// </summary>
32+
/// <returns>The ARN list of clusters.</returns>
33+
public async Task<List<string>> GetClusterARNSAsync()
34+
{
35+
36+
Console.WriteLine("Getting a list of all the clusters in your AWS account...");
37+
List<string> clusterArnList = new List<string>();
38+
// Get a list of all the clusters in your AWS account.
39+
try
40+
{
41+
42+
var listClustersResponse = _ecsClient.Paginators.ListClusters(new ListClustersRequest
43+
{
44+
});
45+
46+
var clusterArns = listClustersResponse.ClusterArns;
47+
48+
// Print the ARNs of the clusters.
49+
await foreach (var clusterArn in clusterArns)
50+
{
51+
clusterArnList.Add(clusterArn);
52+
}
53+
54+
if (clusterArnList.Count == 0)
55+
{
56+
_logger.LogWarning("No clusters found in your AWS account.");
57+
}
58+
return clusterArnList;
59+
}
60+
catch (Exception e)
61+
{
62+
_logger.LogError($"An error occurred while getting a list of all the clusters in your AWS account. {e.InnerException}");
63+
throw;
64+
}
65+
}
66+
// snippet-end:[ECS.dotnetv4.ECSActions.ListClusters]
67+
68+
// snippet-start:[ECS.dotnetv4.ECSActions.ListServices]
69+
/// <summary>
70+
/// List service ARNs available.
71+
/// </summary>
72+
/// <param name="clusterARN">The arn of the ECS cluster.</param>
73+
/// <returns>The ARN list of services in given cluster.</returns>
74+
public async Task<List<string>> GetServiceARNSAsync(string clusterARN)
75+
{
76+
List<string> serviceArns = new List<string>();
77+
78+
var request = new ListServicesRequest
79+
{
80+
Cluster = clusterARN
81+
};
82+
// Call the ListServices API operation and get the list of service ARNs.
83+
var serviceList = _ecsClient.Paginators.ListServices(request);
84+
85+
await foreach (var serviceARN in serviceList.ServiceArns)
86+
{
87+
if (serviceARN is null)
88+
continue;
89+
90+
serviceArns.Add(serviceARN);
91+
}
92+
93+
if (serviceArns.Count == 0)
94+
{
95+
_logger.LogWarning($"No services found in cluster {clusterARN} .");
96+
}
97+
98+
return serviceArns;
99+
}
100+
// snippet-end:[ECS.dotnetv4.ECSActions.ListServices]
101+
102+
// snippet-start:[ECS.dotnetv4.ECSActions.ListTasks]
103+
/// <summary>
104+
/// List task ARNs available.
105+
/// </summary>
106+
/// <param name="clusterARN">The arn of the ECS cluster.</param>
107+
/// <returns>The ARN list of tasks in given cluster.</returns>
108+
public async Task<List<string>> GetTaskARNsAsync(string clusterARN)
109+
{
110+
// Set up the request to describe the tasks in the service.
111+
var listTasksRequest = new ListTasksRequest
112+
{
113+
Cluster = clusterARN
114+
};
115+
List<string> taskArns = new List<string>();
116+
117+
// Call the ListTasks API operation and get the list of task ARNs.
118+
var tasks = _ecsClient.Paginators.ListTasks(listTasksRequest);
119+
120+
await foreach (var task in tasks.TaskArns)
121+
{
122+
if (task is null)
123+
continue;
124+
125+
taskArns.Add(task);
126+
}
127+
128+
if (taskArns.Count == 0)
129+
{
130+
_logger.LogWarning("No tasks found in cluster: " + clusterARN);
131+
}
132+
133+
return taskArns;
134+
}
135+
// snippet-end:[ECS.dotnetv4.ECSActions.ListTasks]
136+
}
137+
// snippet-end:[ECS.dotnetv4.ECSActions.ECSWrapper]

dotnetv4/ECS/ECSActions/HelloECS.cs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// snippet-start:[ECS.dotnetv4.ECSActions.HelloECS]
5+
using Amazon.ECS;
6+
using Amazon.ECS.Model;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Hosting;
9+
using Microsoft.Extensions.Logging;
10+
using Microsoft.Extensions.Logging.Console;
11+
using Microsoft.Extensions.Logging.Debug;
12+
13+
namespace ECSActions;
14+
15+
/// <summary>
16+
/// A class that introduces the Amazon ECS Client by listing the
17+
/// cluster ARNs for the account.
18+
/// </summary>
19+
public class HelloECS
20+
{
21+
static async System.Threading.Tasks.Task Main(string[] args)
22+
{
23+
// Use the AWS .NET Core Setup package to set up dependency injection for the Amazon ECS client.
24+
// Use your AWS profile name, or leave it blank to use the default profile.
25+
using var host = Host.CreateDefaultBuilder(args)
26+
.ConfigureLogging(logging =>
27+
logging.AddFilter("System", LogLevel.Debug)
28+
.AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Information)
29+
.AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace))
30+
.ConfigureServices((_, services) =>
31+
services.AddAWSService<IAmazonECS>()
32+
)
33+
.Build();
34+
35+
var amazonECSClient = host.Services.GetRequiredService<IAmazonECS>();
36+
37+
Console.WriteLine($"Hello Amazon ECS! Following are some cluster ARNS available in the your account");
38+
Console.WriteLine();
39+
40+
var clusters = new List<string>();
41+
42+
var clustersPaginator = amazonECSClient.Paginators.ListClusters(new ListClustersRequest());
43+
44+
await foreach (var response in clustersPaginator.Responses)
45+
{
46+
clusters.AddRange(response.ClusterArns);
47+
}
48+
49+
if (clusters.Count > 0)
50+
{
51+
clusters.ForEach(cluster =>
52+
{
53+
Console.WriteLine($"\tARN: {cluster}");
54+
Console.WriteLine($"Cluster Name: {cluster.Split("/").Last()}");
55+
Console.WriteLine();
56+
});
57+
}
58+
else
59+
{
60+
Console.WriteLine("No clusters were found.");
61+
}
62+
63+
}
64+
}
65+
// snippet-end:[ECS.dotnetv4.ECSActions.HelloECS]

0 commit comments

Comments
 (0)