-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scaffold the AWS Autoscaling Group Metrics Collection
- Loading branch information
Showing
8 changed files
with
437 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
builtin/providers/aws/resource_aws_autoscaling_metrics_collection.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/autoscaling" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsAutoscalingMetric() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsAutoscalingMetricCreate, | ||
Read: resourceAwsAutoscalingMetricRead, | ||
Update: resourceAwsAutoscalingMetricUpdate, | ||
Delete: resourceAwsAutoscalingMetricDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"autoscaling_group_name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"metrics": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Required: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: schema.HashString, | ||
}, | ||
|
||
"granularity": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsAutoscalingMetricCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).autoscalingconn | ||
|
||
asgName := d.Get("autoscaling_group_name").(string) | ||
props := &autoscaling.EnableMetricsCollectionInput{ | ||
AutoScalingGroupName: aws.String(asgName), | ||
Granularity: aws.String(d.Get("granularity").(string)), | ||
Metrics: expandStringList(d.Get("metrics").(*schema.Set).List()), | ||
} | ||
|
||
log.Printf("[INFO] Enabling metrics collection for the ASG: %s", asgName) | ||
_, err := conn.EnableMetricsCollection(props) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(d.Get("autoscaling_group_name").(string)) | ||
|
||
return resourceAwsAutoscalingMetricRead(d, meta) | ||
} | ||
|
||
func resourceAwsAutoscalingMetricRead(d *schema.ResourceData, meta interface{}) error { | ||
g, err := getAwsAutoscalingGroup(d, meta) | ||
if err != nil { | ||
return err | ||
} | ||
if g == nil { | ||
return nil | ||
} | ||
|
||
if g.EnabledMetrics != nil && len(g.EnabledMetrics) > 0 { | ||
if err := d.Set("metrics", flattenAsgEnabledMetrics(g.EnabledMetrics)); err != nil { | ||
log.Printf("[WARN] Error setting metrics for (%s): %s", d.Id(), err) | ||
} | ||
d.Set("granularity", g.EnabledMetrics[0].Granularity) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsAutoscalingMetricUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).autoscalingconn | ||
|
||
if d.HasChange("metrics") { | ||
o, n := d.GetChange("metrics") | ||
if o == nil { | ||
o = new(schema.Set) | ||
} | ||
if n == nil { | ||
n = new(schema.Set) | ||
} | ||
|
||
os := o.(*schema.Set) | ||
ns := n.(*schema.Set) | ||
|
||
disableMetrics := os.Difference(ns) | ||
if disableMetrics.Len() != 0 { | ||
props := &autoscaling.DisableMetricsCollectionInput{ | ||
AutoScalingGroupName: aws.String(d.Id()), | ||
Metrics: expandStringList(disableMetrics.List()), | ||
} | ||
|
||
_, err := conn.DisableMetricsCollection(props) | ||
if err != nil { | ||
return fmt.Errorf("Failure to Disable metrics collection types for ASG %s: %s", d.Id(), err) | ||
} | ||
} | ||
|
||
enabledMetrics := ns.Difference(os) | ||
if enabledMetrics.Len() != 0 { | ||
props := &autoscaling.EnableMetricsCollectionInput{ | ||
AutoScalingGroupName: aws.String(d.Id()), | ||
Metrics: expandStringList(enabledMetrics.List()), | ||
} | ||
|
||
_, err := conn.EnableMetricsCollection(props) | ||
if err != nil { | ||
return fmt.Errorf("Failure to Enable metrics collection types for ASG %s: %s", d.Id(), err) | ||
} | ||
} | ||
} | ||
|
||
return resourceAwsAutoscalingMetricRead(d, meta) | ||
} | ||
|
||
func resourceAwsAutoscalingMetricDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).autoscalingconn | ||
|
||
props := &autoscaling.DisableMetricsCollectionInput{ | ||
AutoScalingGroupName: aws.String(d.Id()), | ||
} | ||
|
||
log.Printf("[INFO] Disabling ALL metrics collection for the ASG: %s", d.Id()) | ||
_, err := conn.DisableMetricsCollection(props) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
189 changes: 189 additions & 0 deletions
189
builtin/providers/aws/resource_aws_autoscaling_metrics_collection_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/autoscaling" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAWSAutoscalingMetricsCollection_basic(t *testing.T) { | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSAutoscalingMetricsCollectionDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccAWSAutoscalingMetricsCollectionConfig_allMetricsCollected, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSAutoscalingMetricsCollectionExists("aws_autoscaling_metrics_collection.test"), | ||
resource.TestCheckResourceAttr( | ||
"aws_autoscaling_metrics_collection.test", "metrics.#", "7"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAWSAutoscalingMetricsCollection_update(t *testing.T) { | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSAutoscalingMetricsCollectionDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccAWSAutoscalingMetricsCollectionConfig_allMetricsCollected, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSAutoscalingMetricsCollectionExists("aws_autoscaling_metrics_collection.test"), | ||
resource.TestCheckResourceAttr( | ||
"aws_autoscaling_metrics_collection.test", "metrics.#", "7"), | ||
), | ||
}, | ||
|
||
resource.TestStep{ | ||
Config: testAccAWSAutoscalingMetricsCollectionConfig_updatingMetricsCollected, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSAutoscalingMetricsCollectionExists("aws_autoscaling_metrics_collection.test"), | ||
resource.TestCheckResourceAttr( | ||
"aws_autoscaling_metrics_collection.test", "metrics.#", "5"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
func testAccCheckAWSAutoscalingMetricsCollectionExists(n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No AutoScaling Group ID is set") | ||
} | ||
|
||
conn := testAccProvider.Meta().(*AWSClient).autoscalingconn | ||
|
||
describeGroups, err := conn.DescribeAutoScalingGroups( | ||
&autoscaling.DescribeAutoScalingGroupsInput{ | ||
AutoScalingGroupNames: []*string{aws.String(rs.Primary.ID)}, | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(describeGroups.AutoScalingGroups) != 1 || | ||
*describeGroups.AutoScalingGroups[0].AutoScalingGroupName != rs.Primary.ID { | ||
return fmt.Errorf("AutoScaling Group not found") | ||
} | ||
|
||
if describeGroups.AutoScalingGroups[0].EnabledMetrics == nil { | ||
return fmt.Errorf("AutoScaling Groups Metrics Collection not found") | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckAWSAutoscalingMetricsCollectionDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).autoscalingconn | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
// Try to find the Group | ||
describeGroups, err := conn.DescribeAutoScalingGroups( | ||
&autoscaling.DescribeAutoScalingGroupsInput{ | ||
AutoScalingGroupNames: []*string{aws.String(rs.Primary.ID)}, | ||
}) | ||
|
||
if err == nil { | ||
if len(describeGroups.AutoScalingGroups) != 0 && | ||
*describeGroups.AutoScalingGroups[0].AutoScalingGroupName == rs.Primary.ID { | ||
return fmt.Errorf("AutoScalingGroup still exists") | ||
} | ||
} | ||
|
||
// Verify the error | ||
ec2err, ok := err.(awserr.Error) | ||
if !ok { | ||
return err | ||
} | ||
if ec2err.Code() != "InvalidGroup.NotFound" { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
const testAccAWSAutoscalingMetricsCollectionConfig_allMetricsCollected = ` | ||
resource "aws_launch_configuration" "foobar" { | ||
image_id = "ami-21f78e11" | ||
instance_type = "t1.micro" | ||
} | ||
resource "aws_autoscaling_group" "bar" { | ||
availability_zones = ["us-west-2a"] | ||
name = "foobar3-terraform-test" | ||
max_size = 1 | ||
min_size = 0 | ||
health_check_grace_period = 300 | ||
health_check_type = "EC2" | ||
desired_capacity = 0 | ||
force_delete = true | ||
termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] | ||
launch_configuration = "${aws_launch_configuration.foobar.name}" | ||
} | ||
resource "aws_autoscaling_metrics_collection" "test" { | ||
autoscaling_group_name = "${aws_autoscaling_group.bar.name}" | ||
granularity = "1Minute" | ||
metrics = ["GroupTotalInstances", | ||
"GroupPendingInstances", | ||
"GroupTerminatingInstances", | ||
"GroupDesiredCapacity", | ||
"GroupInServiceInstances", | ||
"GroupMinSize", | ||
"GroupMaxSize" | ||
] | ||
} | ||
` | ||
|
||
const testAccAWSAutoscalingMetricsCollectionConfig_updatingMetricsCollected = ` | ||
resource "aws_launch_configuration" "foobar" { | ||
image_id = "ami-21f78e11" | ||
instance_type = "t1.micro" | ||
} | ||
resource "aws_autoscaling_group" "bar" { | ||
availability_zones = ["us-west-2a"] | ||
name = "foobar3-terraform-test" | ||
max_size = 1 | ||
min_size = 0 | ||
health_check_grace_period = 300 | ||
health_check_type = "EC2" | ||
desired_capacity = 0 | ||
force_delete = true | ||
termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] | ||
launch_configuration = "${aws_launch_configuration.foobar.name}" | ||
} | ||
resource "aws_autoscaling_metrics_collection" "test" { | ||
autoscaling_group_name = "${aws_autoscaling_group.bar.name}" | ||
granularity = "1Minute" | ||
metrics = ["GroupTotalInstances", | ||
"GroupPendingInstances", | ||
"GroupTerminatingInstances", | ||
"GroupDesiredCapacity", | ||
"GroupMaxSize" | ||
] | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.