Skip to content

Commit d0f15cf

Browse files
authored
Merge pull request #27011 from jochenboesmans/main
resource/aws_appautoscalingpolicy: add computed attribute `alarmARNs`
2 parents 0d33085 + b4276ce commit d0f15cf

File tree

4 files changed

+30
-36
lines changed

4 files changed

+30
-36
lines changed

.changelog/27011.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/aws_appautoscaling_policy: Add `alarm_arns` attribute
3+
```

internal/service/appautoscaling/policy.go

+24-18
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,24 @@ func ResourcePolicy() *schema.Resource {
3434
},
3535

3636
Schema: map[string]*schema.Schema{
37+
"alarm_arns": {
38+
Type: schema.TypeList,
39+
Computed: true,
40+
Elem: &schema.Schema{
41+
Type: schema.TypeString,
42+
},
43+
},
44+
"arn": {
45+
Type: schema.TypeString,
46+
Computed: true,
47+
},
3748
"name": {
3849
Type: schema.TypeString,
3950
Required: true,
4051
ForceNew: true,
4152
// https://github.com/boto/botocore/blob/9f322b1/botocore/data/autoscaling/2011-01-01/service-2.json#L1862-L1873
4253
ValidateFunc: validation.StringLenBetween(0, 255),
4354
},
44-
"arn": {
45-
Type: schema.TypeString,
46-
Computed: true,
47-
},
4855
"policy_type": {
4956
Type: schema.TypeString,
5057
Optional: true,
@@ -148,15 +155,9 @@ func ResourcePolicy() *schema.Resource {
148155
Required: true,
149156
},
150157
"statistic": {
151-
Type: schema.TypeString,
152-
Required: true,
153-
ValidateFunc: validation.StringInSlice([]string{
154-
applicationautoscaling.MetricStatisticAverage,
155-
applicationautoscaling.MetricStatisticMinimum,
156-
applicationautoscaling.MetricStatisticMaximum,
157-
applicationautoscaling.MetricStatisticSampleCount,
158-
applicationautoscaling.MetricStatisticSum,
159-
}, false),
158+
Type: schema.TypeString,
159+
Required: true,
160+
ValidateFunc: validation.StringInSlice(applicationautoscaling.MetricStatistic_Values(), false),
160161
},
161162
"unit": {
162163
Type: schema.TypeString,
@@ -165,6 +166,11 @@ func ResourcePolicy() *schema.Resource {
165166
},
166167
},
167168
},
169+
"disable_scale_in": {
170+
Type: schema.TypeBool,
171+
Default: false,
172+
Optional: true,
173+
},
168174
"predefined_metric_specification": {
169175
Type: schema.TypeList,
170176
MaxItems: 1,
@@ -184,11 +190,6 @@ func ResourcePolicy() *schema.Resource {
184190
},
185191
},
186192
},
187-
"disable_scale_in": {
188-
Type: schema.TypeBool,
189-
Default: false,
190-
Optional: true,
191-
},
192193
"scale_in_cooldown": {
193194
Type: schema.TypeInt,
194195
Optional: true,
@@ -284,6 +285,11 @@ func resourcePolicyRead(d *schema.ResourceData, meta interface{}) error {
284285

285286
log.Printf("[DEBUG] Read ApplicationAutoScaling policy: %s, SP: %s, Obj: %s", d.Get("name"), d.Get("name"), p)
286287

288+
var alarmARNs = make([]string, 0, len(p.Alarms))
289+
for _, alarm := range p.Alarms {
290+
alarmARNs = append(alarmARNs, aws.StringValue(alarm.AlarmARN))
291+
}
292+
d.Set("alarm_arns", alarmARNs)
287293
d.Set("arn", p.PolicyARN)
288294
d.Set("name", p.PolicyName)
289295
d.Set("policy_type", p.PolicyType)

internal/service/appautoscaling/policy_test.go

+2-18
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func TestAccAppAutoScalingPolicy_basic(t *testing.T) {
9696
Config: testAccPolicyConfig_basic(rName),
9797
Check: resource.ComposeTestCheckFunc(
9898
testAccCheckPolicyExists(resourceName, &policy),
99+
resource.TestCheckResourceAttr(resourceName, "alarm_arns.#", "0"),
99100
resource.TestCheckResourceAttr(resourceName, "name", rName),
100101
resource.TestCheckResourceAttr(resourceName, "policy_type", "StepScaling"),
101102
resource.TestCheckResourceAttrPair(resourceName, "resource_id", appAutoscalingTargetResourceName, "resource_id"),
@@ -136,7 +137,7 @@ func TestAccAppAutoScalingPolicy_disappears(t *testing.T) {
136137
Config: testAccPolicyConfig_basic(rName),
137138
Check: resource.ComposeTestCheckFunc(
138139
testAccCheckPolicyExists(resourceName, &policy),
139-
testAccCheckPolicyDisappears(&policy),
140+
acctest.CheckResourceDisappears(acctest.Provider, tfappautoscaling.ResourcePolicy(), resourceName),
140141
),
141142
ExpectNonEmptyPlan: true,
142143
},
@@ -485,23 +486,6 @@ func testAccCheckPolicyDestroy(s *terraform.State) error {
485486
return nil
486487
}
487488

488-
func testAccCheckPolicyDisappears(policy *applicationautoscaling.ScalingPolicy) resource.TestCheckFunc {
489-
return func(s *terraform.State) error {
490-
conn := acctest.Provider.Meta().(*conns.AWSClient).AppAutoScalingConn
491-
492-
input := &applicationautoscaling.DeleteScalingPolicyInput{
493-
PolicyName: policy.PolicyName,
494-
ResourceId: policy.ResourceId,
495-
ScalableDimension: policy.ScalableDimension,
496-
ServiceNamespace: policy.ServiceNamespace,
497-
}
498-
499-
_, err := conn.DeleteScalingPolicy(input)
500-
501-
return err
502-
}
503-
}
504-
505489
func testAccPolicyConfig_basic(rName string) string {
506490
return fmt.Sprintf(`
507491
resource "aws_ecs_cluster" "test" {

website/docs/r/appautoscaling_policy.html.markdown

+1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ The `target_tracking_scaling_policy_configuration` `predefined_metric_specificat
231231

232232
In addition to all arguments above, the following attributes are exported:
233233

234+
* `alarm_arns` - List of CloudWatch alarm ARNs associated with the scaling policy.
234235
* `arn` - ARN assigned by AWS to the scaling policy.
235236
* `name` - Scaling policy's name.
236237
* `policy_type` - Scaling policy's type.

0 commit comments

Comments
 (0)