Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions dotnetv4/DotNetV4Examples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CognitoBasics", "Cognito\Sc
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CognitoActions", "Cognito\Actions\CognitoActions.csproj", "{1AF980DF-DEEA-4E5D-9001-6EC67EB96AD1}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ECS", "ECS", "{EE6D1933-1E38-406A-B691-446326310D1F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ECSTests", "ECS\ECSTests\ECSTests.csproj", "{3F159C49-3DE7-42F5-AF14-E64C03AF19E8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ECSScenario", "ECS\ECSScenario\ECSScenario.csproj", "{D44D50E1-EC65-4A1C-AAA1-C360E4FC563F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ECSActions", "ECS\ECSActions\ECSActions.csproj", "{7485EAED-F81C-4119-BABC-E009A21ACE46}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -315,6 +323,18 @@ Global
{1AF980DF-DEEA-4E5D-9001-6EC67EB96AD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1AF980DF-DEEA-4E5D-9001-6EC67EB96AD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1AF980DF-DEEA-4E5D-9001-6EC67EB96AD1}.Release|Any CPU.Build.0 = Release|Any CPU
{3F159C49-3DE7-42F5-AF14-E64C03AF19E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3F159C49-3DE7-42F5-AF14-E64C03AF19E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3F159C49-3DE7-42F5-AF14-E64C03AF19E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3F159C49-3DE7-42F5-AF14-E64C03AF19E8}.Release|Any CPU.Build.0 = Release|Any CPU
{D44D50E1-EC65-4A1C-AAA1-C360E4FC563F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D44D50E1-EC65-4A1C-AAA1-C360E4FC563F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D44D50E1-EC65-4A1C-AAA1-C360E4FC563F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D44D50E1-EC65-4A1C-AAA1-C360E4FC563F}.Release|Any CPU.Build.0 = Release|Any CPU
{7485EAED-F81C-4119-BABC-E009A21ACE46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7485EAED-F81C-4119-BABC-E009A21ACE46}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7485EAED-F81C-4119-BABC-E009A21ACE46}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7485EAED-F81C-4119-BABC-E009A21ACE46}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -375,6 +395,9 @@ Global
{D38A409C-EE40-4E70-B500-F3D6EF8E82A4} = {F5214562-85F4-4FD8-B56D-C5D8E7914901}
{38C8C3B0-163D-4B7B-86A2-3EFFBC165E99} = {D38A409C-EE40-4E70-B500-F3D6EF8E82A4}
{1AF980DF-DEEA-4E5D-9001-6EC67EB96AD1} = {F5214562-85F4-4FD8-B56D-C5D8E7914901}
{3F159C49-3DE7-42F5-AF14-E64C03AF19E8} = {EE6D1933-1E38-406A-B691-446326310D1F}
{D44D50E1-EC65-4A1C-AAA1-C360E4FC563F} = {EE6D1933-1E38-406A-B691-446326310D1F}
{7485EAED-F81C-4119-BABC-E009A21ACE46} = {EE6D1933-1E38-406A-B691-446326310D1F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {08502818-E8E1-4A91-A51C-4C8C8D4FF9CA}
Expand Down
37 changes: 37 additions & 0 deletions dotnetv4/ECS/ECS.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.810.20
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ECSActions", "ECSActions\ECSActions.csproj", "{775C4572-76B4-492D-A1FD-606C61D1ADC2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ECSScenario", "ECSScenario\ECSScenario.csproj", "{14A438EC-C572-47C8-91E7-344932B28DF8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ECSTests", "ECSTests\ECSTests.csproj", "{D71EADB2-F6C5-4A75-BF11-B702BF931F14}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{775C4572-76B4-492D-A1FD-606C61D1ADC2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{775C4572-76B4-492D-A1FD-606C61D1ADC2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{775C4572-76B4-492D-A1FD-606C61D1ADC2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{775C4572-76B4-492D-A1FD-606C61D1ADC2}.Release|Any CPU.Build.0 = Release|Any CPU
{14A438EC-C572-47C8-91E7-344932B28DF8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{14A438EC-C572-47C8-91E7-344932B28DF8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{14A438EC-C572-47C8-91E7-344932B28DF8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{14A438EC-C572-47C8-91E7-344932B28DF8}.Release|Any CPU.Build.0 = Release|Any CPU
{D71EADB2-F6C5-4A75-BF11-B702BF931F14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D71EADB2-F6C5-4A75-BF11-B702BF931F14}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D71EADB2-F6C5-4A75-BF11-B702BF931F14}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D71EADB2-F6C5-4A75-BF11-B702BF931F14}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2416AA5E-0460-4DA0-AA2C-168F0130B0EB}
EndGlobalSection
EndGlobal
23 changes: 23 additions & 0 deletions dotnetv4/ECS/ECSActions/ECSActions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<LangVersion>10.0</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<LangVersion>10.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AWSSDK.Core" Version="4.0.0-preview.7" />
<PackageReference Include="AWSSDK.ECS" Version="4.0.0-preview.7" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.2" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.2" />
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="4.0.0-preview.7" />
</ItemGroup>
</Project>
137 changes: 137 additions & 0 deletions dotnetv4/ECS/ECSActions/ECSWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// snippet-start:[ECS.dotnetv4.ECSActions.ECSWrapper]
using Amazon.ECS;
using Amazon.ECS.Model;
using Microsoft.Extensions.Logging;

namespace ECSActions;

public class ECSWrapper
{
private readonly IAmazonECS _ecsClient;
private readonly ILogger<ECSWrapper> _logger;

/// <summary>
/// Constructor for the ECS wrapper.
/// </summary>
/// <param name="ecsClient">The injected ECS client.</param>
/// <param name="logger">The injected logger for the wrapper.</param>
public ECSWrapper(IAmazonECS ecsClient, ILogger<ECSWrapper> logger)

{
_logger = logger;
_ecsClient = ecsClient;
}

// snippet-start:[ECS.dotnetv4.ECSActions.ListClusters]
/// <summary>
/// List cluster ARNs available.
/// </summary>
/// <returns>The ARN list of clusters.</returns>
public async Task<List<string>> GetClusterARNSAsync()
{

Console.WriteLine("Getting a list of all the clusters in your AWS account...");
List<string> clusterArnList = new List<string>();
// Get a list of all the clusters in your AWS account.
try
{

var listClustersResponse = _ecsClient.Paginators.ListClusters(new ListClustersRequest
{
});

var clusterArns = listClustersResponse.ClusterArns;

// Print the ARNs of the clusters.
await foreach (var clusterArn in clusterArns)
{
clusterArnList.Add(clusterArn);
}

if (clusterArnList.Count == 0)
{
_logger.LogWarning("No clusters found in your AWS account.");
}
return clusterArnList;
}
catch (Exception e)
{
_logger.LogError($"An error occurred while getting a list of all the clusters in your AWS account. {e.InnerException}");
throw;
}
}
// snippet-end:[ECS.dotnetv4.ECSActions.ListClusters]

// snippet-start:[ECS.dotnetv4.ECSActions.ListServices]
/// <summary>
/// List service ARNs available.
/// </summary>
/// <param name="clusterARN">The arn of the ECS cluster.</param>
/// <returns>The ARN list of services in given cluster.</returns>
public async Task<List<string>> GetServiceARNSAsync(string clusterARN)
{
List<string> serviceArns = new List<string>();

var request = new ListServicesRequest
{
Cluster = clusterARN
};
// Call the ListServices API operation and get the list of service ARNs.
var serviceList = _ecsClient.Paginators.ListServices(request);

await foreach (var serviceARN in serviceList.ServiceArns)
{
if (serviceARN is null)
continue;

serviceArns.Add(serviceARN);
}

if (serviceArns.Count == 0)
{
_logger.LogWarning($"No services found in cluster {clusterARN} .");
}

return serviceArns;
}
// snippet-end:[ECS.dotnetv4.ECSActions.ListServices]

// snippet-start:[ECS.dotnetv4.ECSActions.ListTasks]
/// <summary>
/// List task ARNs available.
/// </summary>
/// <param name="clusterARN">The arn of the ECS cluster.</param>
/// <returns>The ARN list of tasks in given cluster.</returns>
public async Task<List<string>> GetTaskARNsAsync(string clusterARN)
{
// Set up the request to describe the tasks in the service.
var listTasksRequest = new ListTasksRequest
{
Cluster = clusterARN
};
List<string> taskArns = new List<string>();

// Call the ListTasks API operation and get the list of task ARNs.
var tasks = _ecsClient.Paginators.ListTasks(listTasksRequest);

await foreach (var task in tasks.TaskArns)
{
if (task is null)
continue;

taskArns.Add(task);
}

if (taskArns.Count == 0)
{
_logger.LogWarning("No tasks found in cluster: " + clusterARN);
}

return taskArns;
}
// snippet-end:[ECS.dotnetv4.ECSActions.ListTasks]
}
// snippet-end:[ECS.dotnetv4.ECSActions.ECSWrapper]
65 changes: 65 additions & 0 deletions dotnetv4/ECS/ECSActions/HelloECS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// snippet-start:[ECS.dotnetv4.ECSActions.HelloECS]
using Amazon.ECS;
using Amazon.ECS.Model;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Microsoft.Extensions.Logging.Debug;

namespace ECSActions;

/// <summary>
/// A class that introduces the Amazon ECS Client by listing the
/// cluster ARNs for the account.
/// </summary>
public class HelloECS
{
static async System.Threading.Tasks.Task Main(string[] args)
{
// Use the AWS .NET Core Setup package to set up dependency injection for the Amazon ECS client.
// Use your AWS profile name, or leave it blank to use the default profile.
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<IAmazonECS>()
)
.Build();

var amazonECSClient = host.Services.GetRequiredService<IAmazonECS>();

Console.WriteLine($"Hello Amazon ECS! Following are some cluster ARNS available in the your account");
Console.WriteLine();

var clusters = new List<string>();

var clustersPaginator = amazonECSClient.Paginators.ListClusters(new ListClustersRequest());

await foreach (var response in clustersPaginator.Responses)
{
clusters.AddRange(response.ClusterArns);
}

if (clusters.Count > 0)
{
clusters.ForEach(cluster =>
{
Console.WriteLine($"\tARN: {cluster}");
Console.WriteLine($"Cluster Name: {cluster.Split("/").Last()}");
Console.WriteLine();
});
}
else
{
Console.WriteLine("No clusters were found.");
}

}
}
// snippet-end:[ECS.dotnetv4.ECSActions.HelloECS]
Loading
Loading