/
table_aws_ecs_task.go
324 lines (298 loc) · 8.84 KB
/
table_aws_ecs_task.go
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package aws
import (
"context"
"strings"
"sync"
"github.com/turbot/steampipe-plugin-sdk/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/plugin/transform"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/turbot/steampipe-plugin-sdk/plugin"
)
//// TABLE DEFINITION
func tableAwsEcsTask(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "aws_ecs_task",
Description: "AWS ECS Task",
List: &plugin.ListConfig{
Hydrate: listEcsTasks,
ParentHydrate: listEcsClusters,
ShouldIgnoreError: isNotFoundError([]string{"ClusterNotFoundException"}),
},
GetMatrixItem: BuildRegionList,
Columns: awsRegionalColumns([]*plugin.Column{
{
Name: "task_arn",
Description: "The Amazon Resource Name (ARN) that identifies the task.",
Type: proto.ColumnType_STRING,
},
{
Name: "task_definition_arn",
Description: "The ARN of the task definition from which the task was created.",
Type: proto.ColumnType_STRING,
},
{
Name: "cpu",
Description: "The number of cpu units used by the task.",
Type: proto.ColumnType_INT,
},
{
Name: "desired_status",
Description: "The desired status of the task.",
Type: proto.ColumnType_STRING,
},
{
Name: "last_status",
Description: "The last known status of the task.",
Type: proto.ColumnType_STRING,
},
{
Name: "health_status",
Description: "The health status of the task.",
Type: proto.ColumnType_STRING,
},
{
Name: "launch_type",
Description: "The infrastructure on which your task is running.",
Type: proto.ColumnType_STRING,
},
{
Name: "availability_zone",
Description: "The availability zone of the task.",
Type: proto.ColumnType_STRING,
},
{
Name: "cluster_arn",
Description: "The ARN of the cluster that hosts the task.",
Type: proto.ColumnType_STRING,
},
{
Name: "memory",
Description: "The amount (in MiB) of memory used by the task.",
Type: proto.ColumnType_INT,
},
{
Name: "connectivity",
Description: "The connectivity status of a task.",
Type: proto.ColumnType_STRING,
},
{
Name: "container_instance_arn",
Description: "The ARN of the container instances that host the task.",
Type: proto.ColumnType_STRING,
},
{
Name: "enable_execute_command",
Description: "Whether or not execute command functionality is enabled for this task.",
Type: proto.ColumnType_BOOL,
},
{
Name: "execution_stopped_at",
Description: "The Unix timestamp for when the task execution stopped.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "group",
Description: "The name of the task group associated with the task.",
Type: proto.ColumnType_STRING,
},
{
Name: "created_at",
Description: "The Unix timestamp for when the task was created (the task entered the PENDING state).",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "platform_version",
Description: "The platform version on which your task is running.",
Type: proto.ColumnType_STRING,
},
{
Name: "pull_started_at",
Description: "The Unix timestamp for when the container image pull began.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "pull_stopped_at",
Description: "The Unix timestamp for when the container image pull completed.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "started_at",
Description: "The Unix timestamp for when the task started (the task transitioned from the PENDING state to the RUNNING state).",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "started_by",
Description: "The tag specified when a task is started.",
Type: proto.ColumnType_STRING,
},
{
Name: "stop_code",
Description: "The stop code indicating why a task was stopped.",
Type: proto.ColumnType_STRING,
},
{
Name: "stopped_at",
Description: "The Unix timestamp for when the task was stopped (the task transitioned from the RUNNING state to the STOPPED state).",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "stopped_reason",
Description: "The reason that the task was stopped.",
Type: proto.ColumnType_STRING,
},
{
Name: "stopping_at",
Description: "The Unix timestamp for when the task stops (transitions from the RUNNING state to STOPPED).",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "version",
Description: "The version counter for the task.",
Type: proto.ColumnType_INT,
},
{
Name: "containers",
Description: "A list of containers that make up your task.",
Type: proto.ColumnType_JSON,
},
{
Name: "inference_accelerators",
Description: "The Elastic Inference accelerator associated with the task.",
Type: proto.ColumnType_JSON,
},
{
Name: "overrides",
Description: "One or more container overrides for the task.",
Type: proto.ColumnType_JSON,
},
{
Name: "attributes",
Description: "The attributes of the task.",
Type: proto.ColumnType_JSON,
},
{
Name: "ephemeral_storage",
Description: "The ephemeral storage settings for the task.",
Type: proto.ColumnType_JSON,
},
{
Name: "tags_src",
Description: "A list of tags associated with task.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("Tags"),
},
// Standard columns
{
Name: "title",
Description: resourceInterfaceDescription("title"),
Type: proto.ColumnType_STRING,
Transform: transform.FromP(getAwsEcsTaskTurbotData, "Title"),
},
{
Name: "tags",
Description: resourceInterfaceDescription("tags"),
Type: proto.ColumnType_JSON,
Transform: transform.FromP(getAwsEcsTaskTurbotData, "Tags"),
},
{
Name: "akas",
Description: resourceInterfaceDescription("akas"),
Type: proto.ColumnType_JSON,
Transform: transform.FromField("TaskArn").Transform(transform.EnsureStringArray),
},
}),
}
}
//// LIST FUNCTION
func listEcsTasks(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
// Create Session
svc, err := EcsService(ctx, d)
if err != nil {
return nil, err
}
// Get cluster details
cluster := h.Item.(*ecs.Cluster)
// List all available ECS tasks
var taskNames [][]*string
err = svc.ListTasksPages(
&ecs.ListTasksInput{
Cluster: cluster.ClusterArn,
},
func(page *ecs.ListTasksOutput, isLast bool) bool {
if len(page.TaskArns) != 0 {
// Create a chunk of array of size 10
taskNames = append(taskNames, page.TaskArns)
}
return !isLast
},
)
if err != nil {
return nil, err
}
var wg sync.WaitGroup
taskCh := make(chan *ecs.DescribeTasksOutput, len(taskNames))
errorCh := make(chan error, len(taskNames))
for _, taskData := range taskNames {
wg.Add(1)
go getTaskDataAsync(taskData, cluster.ClusterArn, svc, &wg, taskCh, errorCh)
}
// wait for all services to be processed
wg.Wait()
// NOTE: close channel before ranging over results
close(taskCh)
close(errorCh)
for err := range errorCh {
// return the first error
return nil, err
}
for result := range taskCh {
for _, service := range result.Tasks {
d.StreamListItem(ctx, service)
}
}
return nil, nil
}
//// HYDRATE FUNCTIONS
func getTaskDataAsync(taskData []*string, clusterARN *string, svc *ecs.ECS, wg *sync.WaitGroup, taskCh chan *ecs.DescribeTasksOutput, errorCh chan error) {
defer wg.Done()
rowData, err := getEcsTask(taskData, clusterARN, svc)
if err != nil {
errorCh <- err
} else if rowData != nil {
taskCh <- rowData
}
}
func getEcsTask(taskData []*string, clusterARN *string, svc *ecs.ECS) (*ecs.DescribeTasksOutput, error) {
params := &ecs.DescribeTasksInput{
Tasks: taskData,
Cluster: clusterARN,
}
response, err := svc.DescribeTasks(params)
if err != nil {
return nil, err
}
return response, nil
}
//// TRANSFORM FUNCTIONS
func getAwsEcsTaskTurbotData(_ context.Context, d *transform.TransformData) (interface{}, error) {
param := d.Param.(string)
ecsTask := d.HydrateItem.(*ecs.Task)
// Get resource title
arn := ecsTask.TaskArn
splitArn := strings.Split(*arn, "/")
title := splitArn[len(splitArn)-1]
if param == "Tags" {
if ecsTask.Tags == nil {
return nil, nil
}
// Get the resource tags
if ecsTask.Tags != nil {
turbotTagsMap := map[string]string{}
for _, i := range ecsTask.Tags {
turbotTagsMap[*i.Key] = *i.Value
}
return turbotTagsMap, nil
}
}
return title, nil
}