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]
0 commit comments