/
table_aws_appautoscaling_policy.go
175 lines (155 loc) · 5.46 KB
/
table_aws_appautoscaling_policy.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
package aws
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/applicationautoscaling"
"github.com/aws/aws-sdk-go-v2/service/applicationautoscaling/types"
applicationautoscalingv1 "github.com/aws/aws-sdk-go/service/applicationautoscaling"
"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"
)
//// TABLE DEFINITION
func tableAwsAppAutoScalingPolicy(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "aws_appautoscaling_policy",
Description: "AWS Application Auto Scaling Policy",
List: &plugin.ListConfig{
Hydrate: listAwsApplicationAutoScalingPolicies,
Tags: map[string]string{"service": "application-autoscaling", "action": "DescribeScalingPolicies"},
KeyColumns: []*plugin.KeyColumn{
{
Name: "service_namespace",
Require: plugin.Required,
},
{
Name: "resource_id",
Require: plugin.Optional,
},
{
Name: "policy_name",
Require: plugin.Optional,
},
},
},
GetMatrixItemFunc: SupportedRegionMatrix(applicationautoscalingv1.EndpointsID),
Columns: awsRegionalColumns([]*plugin.Column{
{
Name: "policy_arn",
Description: "The Amazon Resource Name (ARN) of the appautoscaling policy.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("PolicyARN"),
},
{
Name: "policy_name",
Description: "The name of the scaling policy.",
Type: proto.ColumnType_STRING,
},
{
Name: "service_namespace",
Description: "The namespace of the AWS service that provides the resource, or a custom-resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "resource_id",
Description: "The identifier of the resource associated with the scaling policy.",
Type: proto.ColumnType_STRING,
},
{
Name: "scalable_dimension",
Description: "The scalable dimension associated with the scaling policy. This string consists of the service namespace, resource type, and scaling property.",
Type: proto.ColumnType_STRING,
},
{
Name: "policy_type",
Description: "The policy type. Currently supported values are TargetTrackingScaling and StepScaling",
Type: proto.ColumnType_STRING,
},
{
Name: "target_tracking_scaling_policy_configuration",
Description: "The target tracking scaling policy configuration (if policy type is TargetTrackingScaling).",
Type: proto.ColumnType_JSON,
},
{
Name: "step_scaling_policy_configuration",
Description: "The step tracking scaling policy configuration (if policy type is StepScaling).",
Type: proto.ColumnType_JSON,
},
{
Name: "alarms",
Description: "The CloudWatch alarms associated with the scaling policy.",
Type: proto.ColumnType_JSON,
},
{
Name: "creation_time",
Description: "The Unix timestamp for when the scaling policy was created.",
Type: proto.ColumnType_TIMESTAMP,
},
// Standard columns for all tables
{
Name: "title",
Description: resourceInterfaceDescription("title"),
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ResourceId"),
},
}),
}
}
//// LIST FUNCTION
func listAwsApplicationAutoScalingPolicies(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
name := d.EqualsQuals["service_namespace"].GetStringValue()
// Create Session
svc, err := ApplicationAutoScalingClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_appautoscaling_policy.listAwsApplicationAutoScalingPolicies", "get_client_error", err)
return nil, err
}
// The maximum number for MaxResults parameter is not defined by the API
// We have set the MaxResults to 1000 based on our test
input := &applicationautoscaling.DescribeScalingPoliciesInput{
MaxResults: aws.Int32(1000),
}
// Additonal Filter
equalQuals := d.EqualsQuals
input.ServiceNamespace = types.ServiceNamespace(name)
if equalQuals["policy_name"] != nil {
input.PolicyNames = []string{equalQuals["policy_name"].GetStringValue()}
}
if equalQuals["resource_id"] != nil {
input.ResourceId = aws.String(equalQuals["resource_id"].GetStringValue())
}
if equalQuals["scalable_dimension"] != nil {
input.ScalableDimension = types.ScalableDimension(equalQuals["scalable_dimension"].GetStringValue())
}
if d.QueryContext.Limit != nil {
limit := int32(*d.QueryContext.Limit)
if limit < *input.MaxResults {
if limit < 1 {
input.MaxResults = aws.Int32(1)
} else {
input.MaxResults = aws.Int32(limit)
}
}
}
paginator := applicationautoscaling.NewDescribeScalingPoliciesPaginator(svc, input, func(o *applicationautoscaling.DescribeScalingPoliciesPaginatorOptions) {
o.Limit = *input.MaxResults
o.StopOnDuplicateToken = true
})
for paginator.HasMorePages() {
// apply rate limiting
d.WaitForListRateLimit(ctx)
output, err := paginator.NextPage(ctx)
if err != nil {
plugin.Logger(ctx).Error("aws_appautoscaling_policy.listAwsApplicationAutoScalingPolicies", "api_error", err)
return nil, err
}
for _, scalingPolicy := range output.ScalingPolicies {
d.StreamListItem(ctx, scalingPolicy)
// Context can be cancelled due to manual cancellation or the limit has been hit
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
}
return nil, err
}