Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new data source type aws_autoscaling_group #6849

Merged
merged 4 commits into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 184 additions & 0 deletions aws/data_source_aws_autoscaling_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package aws

import (
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceAwsAutoscalingGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsAutoscalingGroupRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"availability_zones": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"default_cooldown": {
Type: schema.TypeInt,
Computed: true,
},
"desired_capacity": {
Type: schema.TypeInt,
Computed: true,
},
"health_check_grace_period": {
Type: schema.TypeInt,
Computed: true,
},
"health_check_type": {
Type: schema.TypeString,
Computed: true,
},
"launch_configuration": {
Type: schema.TypeString,
Computed: true,
},
"load_balancers": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"new_instances_protected_from_scale_in": {
Type: schema.TypeBool,
Computed: true,
},
"max_size": {
Type: schema.TypeInt,
Computed: true,
},
"min_size": {
Type: schema.TypeInt,
Computed: true,
},
"placement_group": {
Type: schema.TypeString,
Computed: true,
},
"service_linked_role_arn": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"target_group_arns": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"termination_policies": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"vpc_zone_identifier": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceAwsAutoscalingGroupRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).autoscalingconn
d.SetId(time.Now().UTC().String())

groupName := d.Get("name").(string)

input := &autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{
aws.String(groupName),
},
}

log.Printf("[DEBUG] Reading Autoscaling Group: %s", input)

result, err := conn.DescribeAutoScalingGroups(input)

log.Printf("[DEBUG] Checking for error: %s", err)

if err != nil {
return fmt.Errorf("error describing AutoScaling Groups: %s", err)
}

log.Printf("[DEBUG] Found Autoscaling Group: %s", result)

if len(result.AutoScalingGroups) < 1 {
return fmt.Errorf("Your query did not return any results. Please try a different search criteria.")
}

if len(result.AutoScalingGroups) > 1 {
return fmt.Errorf("Your query returned more than one result. Please try a more " +
"specific search criteria.")
}

// If execution made it to this point, we have exactly one 1 group returned
// and this is a safe operation
group := result.AutoScalingGroups[0]

log.Printf("[DEBUG] aws_autoscaling_group - Single Auto Scaling Group found: %s", *group.AutoScalingGroupName)

if err := groupDescriptionAttributes(d, group); err != nil {
return err
}

return nil
}

// Populate group attribute fields with the returned group
func groupDescriptionAttributes(d *schema.ResourceData, group *autoscaling.Group) error {
log.Printf("[DEBUG] Setting attributes: %s", group)
d.Set("name", group.AutoScalingGroupName)
d.Set("arn", group.AutoScalingGroupARN)
if err := d.Set("availability_zones", aws.StringValueSlice(group.AvailabilityZones)); err != nil {
return err
}
d.Set("default_cooldown", group.DefaultCooldown)
d.Set("desired_capacity", group.DesiredCapacity)
d.Set("health_check_grace_period", group.HealthCheckGracePeriod)
d.Set("health_check_type", group.HealthCheckType)
d.Set("launch_configuration", group.LaunchConfigurationName)
if err := d.Set("load_balancers", aws.StringValueSlice(group.LoadBalancerNames)); err != nil {
return err
}
d.Set("new_instances_protected_from_scale_in", group.NewInstancesProtectedFromScaleIn)
d.Set("max_size", group.MaxSize)
d.Set("min_size", group.MinSize)
d.Set("placement_group", group.PlacementGroup)
d.Set("service_linked_role_arn", group.ServiceLinkedRoleARN)
d.Set("status", group.Status)
if err := d.Set("target_group_arns", aws.StringValueSlice(group.TargetGroupARNs)); err != nil {
return err
}
if err := d.Set("termination_policies", aws.StringValueSlice(group.TerminationPolicies)); err != nil {
return err
}
d.Set("vpc_zone_identifier", group.VPCZoneIdentifier)

return nil
}
99 changes: 99 additions & 0 deletions aws/data_source_aws_autoscaling_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package aws

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAwsAutoScalingGroupDataSource_basic(t *testing.T) {
datasourceName := "data.aws_autoscaling_group.good_match"
resourceName := "aws_autoscaling_group.foo"
rName := fmt.Sprintf("tf-test-asg-%d", acctest.RandInt())
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAutoScalingGroupDataResourceConfig(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(datasourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(datasourceName, "availability_zones.#", resourceName, "availability_zones.#"),
resource.TestCheckResourceAttrPair(datasourceName, "default_cooldown", resourceName, "default_cooldown"),
resource.TestCheckResourceAttrPair(datasourceName, "desired_capacity", resourceName, "desired_capacity"),
resource.TestCheckResourceAttrPair(datasourceName, "health_check_grace_period", resourceName, "health_check_grace_period"),
resource.TestCheckResourceAttrPair(datasourceName, "health_check_type", resourceName, "health_check_type"),
resource.TestCheckResourceAttrPair(datasourceName, "launch_configuration", resourceName, "launch_configuration"),
resource.TestCheckResourceAttrPair(datasourceName, "load_balancers.#", resourceName, "load_balancers.#"),
resource.TestCheckResourceAttr(datasourceName, "new_instances_protected_from_scale_in", "false"),
resource.TestCheckResourceAttrPair(datasourceName, "max_size", resourceName, "max_size"),
resource.TestCheckResourceAttrPair(datasourceName, "min_size", resourceName, "min_size"),
resource.TestCheckResourceAttrPair(datasourceName, "target_group_arns.#", resourceName, "target_group_arns.#"),
resource.TestCheckResourceAttr(datasourceName, "vpc_zone_identifier", ""),
),
},
},
})
}

// Lookup based on AutoScalingGroupName
func testAccAutoScalingGroupDataResourceConfig(rName string) string {
return fmt.Sprintf(`
data "aws_ami" "ubuntu" {
most_recent = true

filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"]
}

filter {
name = "virtualization-type"
values = ["hvm"]
}

owners = ["099720109477"] # Canonical
}

data "aws_availability_zones" "available" {
state = "available"
}

resource "aws_launch_configuration" "data_source_aws_autoscaling_group_test" {
name = "%[1]s"
image_id = "${data.aws_ami.ubuntu.id}"
instance_type = "t2.micro"
}

resource "aws_autoscaling_group" "foo" {
name = "%[1]s_foo"
max_size = 0
min_size = 0
health_check_grace_period = 300
health_check_type = "ELB"
desired_capacity = 0
force_delete = true
launch_configuration = "${aws_launch_configuration.data_source_aws_autoscaling_group_test.name}"
availability_zones = ["${data.aws_availability_zones.available.names}"]
}

resource "aws_autoscaling_group" "bar" {
name = "%[1]s_bar"
max_size = 0
min_size = 0
health_check_grace_period = 300
health_check_type = "ELB"
desired_capacity = 0
force_delete = true
launch_configuration = "${aws_launch_configuration.data_source_aws_autoscaling_group_test.name}"
availability_zones = ["${data.aws_availability_zones.available.names}"]
}

data "aws_autoscaling_group" "good_match" {
name = "${aws_autoscaling_group.foo.name}"
}
`, rName)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func Provider() terraform.ResourceProvider {
"aws_api_gateway_rest_api": dataSourceAwsApiGatewayRestApi(),
"aws_api_gateway_vpc_link": dataSourceAwsApiGatewayVpcLink(),
"aws_arn": dataSourceAwsArn(),
"aws_autoscaling_group": dataSourceAwsAutoscalingGroup(),
"aws_autoscaling_groups": dataSourceAwsAutoscalingGroups(),
"aws_availability_zone": dataSourceAwsAvailabilityZone(),
"aws_availability_zones": dataSourceAwsAvailabilityZones(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
<li<%= sidebar_current("docs-aws-datasource-arn") %>>
<a href="/docs/providers/aws/d/arn.html">aws_arn</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-autoscaling-group") %>>
<a href="/docs/providers/aws/d/autoscaling_group.html">aws_autoscaling_group</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-autoscaling-groups") %>>
<a href="/docs/providers/aws/d/autoscaling_groups.html">aws_autoscaling_groups</a>
</li>
Expand Down
46 changes: 46 additions & 0 deletions website/docs/d/autoscaling_group.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
layout: "aws"
page_title: "AWS: aws_autoscaling_group"
sidebar_current: "docs-aws-datasource-autoscaling-group"
bflad marked this conversation as resolved.
Show resolved Hide resolved
description: |-
Get information on an Amazon EC2 Autoscaling Group.
---

# Data Source: aws_autoscaling_group

Use this data source to get information on an existing autoscaling group.

## Example Usage

```hcl
data "aws_autoscaling_group" "foo" {
name = "foo"
}
```

## Argument Reference

* `name` - Specify the exact name of the desired autoscaling group.

## Attributes Reference

~> **NOTE:** Some values are not always set and may not be available for
interpolation.

* `arn` - The Amazon Resource Name (ARN) of the Auto Scaling group.
* `name` - The name of the Auto Scaling group.
* `availability_zones` - One or more Availability Zones for the group.
* `default_cool_down` - The amount of time, in seconds, after a scaling activity completes before another scaling activity can start.
* `desired_capacity` - The desired size of the group.
* `health_check_grace_period` - The amount of time, in seconds, that Amazon EC2 Auto Scaling waits before checking the health status of an EC2 instance that has come into service.
* `health_check_type` - The service to use for the health checks. The valid values are EC2 and ELB.
* `launch_configuratione` - The name of the associated launch configuration.
* `load_balancers` - One or more load balancers associated with the group.
* `max_size` - The maximum size of the group.
* `min_size` - The minimum size of the group.
* `placement_group` - The name of the placement group into which to launch your instances, if any. For more information, see Placement Groups (http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/placement-groups.html) in the Amazon Elastic Compute Cloud User Guide.
* `service_linked_role_arn` - The Amazon Resource Name (ARN) of the service-linked role that the Auto Scaling group uses to call other AWS services on your behalf.
* `status` - The current state of the group when DeleteAutoScalingGroup is in progress.
* `target_group_arns` - The Amazon Resource Names (ARN) of the target groups for your load balancer.
* `termination_policies` - The termination policies for the group.
* `vpc_zone_identifier` - VPC ID for the group.