/
table_aws_ecs_container_instance.go
249 lines (225 loc) · 7.59 KB
/
table_aws_ecs_container_instance.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
package aws
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
ecsv1 "github.com/aws/aws-sdk-go/service/ecs"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)
func tableAwsEcsContainerInstance(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "aws_ecs_container_instance",
Description: "AWS ECS Container Instance",
List: &plugin.ListConfig{
ParentHydrate: listEcsClusters,
Hydrate: listEcsContainerInstances,
Tags: map[string]string{"service": "ecs", "action": "ListContainerInstances"},
},
GetMatrixItemFunc: SupportedRegionMatrix(ecsv1.EndpointsID),
Columns: awsGlobalRegionColumns([]*plugin.Column{
{
Name: "arn",
Description: "The namespace Amazon Resource Name (ARN) of the container instance.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ContainerInstanceArn"),
},
{
Name: "ec2_instance_id",
Description: "The EC2 instance ID of the container instance.",
Type: proto.ColumnType_STRING,
},
{
Name: "cluster_arn",
Description: "The ARN of the cluster.",
Type: proto.ColumnType_STRING,
},
{
Name: "agent_connected",
Description: "True if the agent is connected to Amazon ECS.",
Type: proto.ColumnType_BOOL,
},
{
Name: "agent_update_status",
Description: "The status of the most recent agent update.",
Type: proto.ColumnType_STRING,
},
{
Name: "attachments",
Description: "The resources attached to a container instance, such as elastic network interfaces.",
Type: proto.ColumnType_JSON,
},
{
Name: "attributes",
Description: "The attributes set for the container instance.",
Type: proto.ColumnType_JSON,
},
{
Name: "capacity_provider_name",
Description: "The capacity provider associated with the container instance.",
Type: proto.ColumnType_STRING,
},
{
Name: "pending_tasks_count",
Description: "The number of tasks on the container instance that are in the PENDING status.",
Type: proto.ColumnType_INT,
},
{
Name: "registered_at",
Description: "The Unix timestamp for when the container instance was registered.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "registered_resources",
Description: "CPU and memory that can be allocated on this container instance to tasks.",
Type: proto.ColumnType_JSON,
},
{
Name: "remaining_resources",
Description: "CPU and memory that is available for new tasks.",
Type: proto.ColumnType_JSON,
},
{
Name: "running_tasks_count",
Description: "CPU and memory that is available for new tasks.",
Type: proto.ColumnType_INT,
},
{
Name: "status",
Description: "The status of the container instance.",
Type: proto.ColumnType_STRING,
},
{
Name: "status_reason",
Description: "The reason that the container instance reached its current status.",
Type: proto.ColumnType_STRING,
},
{
Name: "version",
Description: "The reason that the container instance reached its current status.",
Type: proto.ColumnType_INT,
},
{
Name: "version_info",
Description: "Version information for the Amazon ECS container agent and Docker daemon running on the container instance.",
Type: proto.ColumnType_JSON,
},
{
Name: "tags",
Description: resourceInterfaceDescription("tags"),
Type: proto.ColumnType_JSON,
Transform: transform.From(containerInstanceTurbotTags),
},
{
Name: "title",
Description: resourceInterfaceDescription("title"),
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ContainerInstanceArn"),
},
{
Name: "akas",
Description: resourceInterfaceDescription("akas"),
Type: proto.ColumnType_JSON,
Transform: transform.FromField("ContainerInstanceArn").Transform(arnToAkas),
},
}),
}
}
type containerInstanceData = struct {
types.ContainerInstance
ClusterArn string
}
//// LIST FUNCTION
// listEcsContainerInstances handles both listing and describing of the instances.
//
// The reason for this is the DescribeContainerInstance call can accept up to 100 ARNs. If we moved it out to another
// hydrate functions we may save a request or two if we only wanted to retrieve the ARNs but the tradeoff is we need
// to get any other info an API call per container instance would need to be made. So in the case where we need to get
// all info for less then 100 instances including the Describe request here, and batching requests means only making
// two API calls as opposed to 101.
func listEcsContainerInstances(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
// Create Session
svc, err := ECSClient(ctx, d)
if err != nil {
return nil, err
}
// EcsContainerInstance is a sub resource of an EcsCluster, we need the cluster ARN to list these.
var clusterArn string
if h.Item != nil {
clusterArn = *h.Item.(types.Cluster).ClusterArn
} else {
clusterArn = d.EqualsQuals["cluster_arn"].GetStringValue()
}
// DescribeContainerInstances can accept up to 100 ARNs at a time, so make sure
// ListContainerInstances returns the same and append to this in chunks not more then 100.
var containerInstanceArns [][]string
// Limiting the results
maxLimit := int32(100)
if d.QueryContext.Limit != nil {
limit := int32(*d.QueryContext.Limit)
if limit < maxLimit {
if limit < 1 {
maxLimit = 1
} else {
maxLimit = limit
}
}
}
input := &ecs.ListContainerInstancesInput{
Cluster: aws.String(clusterArn),
MaxResults: aws.Int32(maxLimit),
}
paginator := ecs.NewListContainerInstancesPaginator(svc, input, func(o *ecs.ListContainerInstancesPaginatorOptions) {
o.Limit = maxLimit
o.StopOnDuplicateToken = true
})
// List call
for paginator.HasMorePages() {
// apply rate limiting
d.WaitForListRateLimit(ctx)
output, err := paginator.NextPage(ctx)
if err != nil {
plugin.Logger(ctx).Error("aws_ecs_cluster.listEcsClusters", "api_error", err)
return nil, err
}
if len(output.ContainerInstanceArns) != 0 {
containerInstanceArns = append(containerInstanceArns, output.ContainerInstanceArns)
}
}
for _, arns := range containerInstanceArns {
input := &ecs.DescribeContainerInstancesInput{
Cluster: aws.String(clusterArn),
ContainerInstances: arns,
Include: []types.ContainerInstanceField{
"TAGS",
},
}
result, err := svc.DescribeContainerInstances(ctx, input)
if err != nil {
return nil, err
}
for _, inst := range result.ContainerInstances {
d.StreamListItem(ctx, containerInstanceData{inst, clusterArn})
// Context may get cancelled due to manual cancellation or if the limit has been reached
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
}
return nil, nil
}
//// TRANSFORM FUNCTIONS
func containerInstanceTurbotTags(ctx context.Context, d *transform.TransformData) (interface{}, error) {
tags := d.HydrateItem.(containerInstanceData).Tags
// Mapping the resource tags inside turbotTags
var turbotTagsMap map[string]string
if tags != nil {
turbotTagsMap = map[string]string{}
for _, i := range tags {
turbotTagsMap[*i.Key] = *i.Value
}
}
return turbotTagsMap, nil
}