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

d/launch_configuration: new data source #3624

Merged
merged 4 commits into from
Jun 28, 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
246 changes: 246 additions & 0 deletions aws/data_source_aws_launch_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
package aws

import (
"errors"
"fmt"
"log"

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

func dataSourceAwsLaunchConfiguration() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsLaunchConfigurationRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"image_id": {
Type: schema.TypeString,
Computed: true,
},

"instance_type": {
Type: schema.TypeString,
Computed: true,
},

"iam_instance_profile": {
Type: schema.TypeString,
Computed: true,
},

"key_name": {
Type: schema.TypeString,
Computed: true,
},

"user_data": {
Type: schema.TypeString,
Computed: true,
},

"security_groups": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"vpc_classic_link_id": {
Type: schema.TypeString,
Computed: true,
},

"vpc_classic_link_security_groups": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"associate_public_ip_address": {
Type: schema.TypeBool,
Computed: true,
},

"spot_price": {
Type: schema.TypeString,
Computed: true,
},

"ebs_optimized": {
Type: schema.TypeBool,
Computed: true,
},

"placement_tenancy": {
Type: schema.TypeString,
Computed: true,
},

"enable_monitoring": {
Type: schema.TypeBool,
Computed: true,
},

"ebs_block_device": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"delete_on_termination": {
Type: schema.TypeBool,
Computed: true,
},

"device_name": {
Type: schema.TypeString,
Computed: true,
},

"iops": {
Type: schema.TypeInt,
Computed: true,
},

"snapshot_id": {
Type: schema.TypeString,
Computed: true,
},

"volume_size": {
Type: schema.TypeInt,
Computed: true,
},

"volume_type": {
Type: schema.TypeString,
Computed: true,
},

"encrypted": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},

"ephemeral_block_device": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"device_name": {
Type: schema.TypeString,
Computed: true,
},

"virtual_name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"root_block_device": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"delete_on_termination": {
Type: schema.TypeBool,
Computed: true,
},

"iops": {
Type: schema.TypeInt,
Computed: true,
},

"volume_size": {
Type: schema.TypeInt,
Computed: true,
},

"volume_type": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceAwsLaunchConfigurationRead(d *schema.ResourceData, meta interface{}) error {
autoscalingconn := meta.(*AWSClient).autoscalingconn
ec2conn := meta.(*AWSClient).ec2conn

if v, ok := d.GetOk("name"); ok {
d.SetId(v.(string))
}

describeOpts := autoscaling.DescribeLaunchConfigurationsInput{
LaunchConfigurationNames: []*string{aws.String(d.Id())},
}

log.Printf("[DEBUG] launch configuration describe configuration: %s", describeOpts)
describConfs, err := autoscalingconn.DescribeLaunchConfigurations(&describeOpts)
if err != nil {
return fmt.Errorf("Error retrieving launch configuration: %s", err)
}

if describConfs == nil || len(describConfs.LaunchConfigurations) == 0 {
return errors.New("No matching Launch Configuration found")
}

if len(describConfs.LaunchConfigurations) > 1 {
return errors.New("Multiple matching Launch Configurations found")
}

lc := describConfs.LaunchConfigurations[0]

d.Set("key_name", lc.KeyName)
d.Set("image_id", lc.ImageId)
d.Set("instance_type", lc.InstanceType)
d.Set("name", lc.LaunchConfigurationName)
d.Set("user_data", lc.UserData)
d.Set("iam_instance_profile", lc.IamInstanceProfile)
d.Set("ebs_optimized", lc.EbsOptimized)
d.Set("spot_price", lc.SpotPrice)
d.Set("associate_public_ip_address", lc.AssociatePublicIpAddress)
d.Set("vpc_classic_link_id", lc.ClassicLinkVPCId)
d.Set("enable_monitoring", false)

if lc.InstanceMonitoring != nil {
d.Set("enable_monitoring", lc.InstanceMonitoring.Enabled)
}

vpcSGs := make([]string, 0, len(lc.SecurityGroups))
for _, sg := range lc.SecurityGroups {
vpcSGs = append(vpcSGs, *sg)
}
if err := d.Set("security_groups", vpcSGs); err != nil {
return fmt.Errorf("error setting security_groups: %s", err)
}

classicSGs := make([]string, 0, len(lc.ClassicLinkVPCSecurityGroups))
for _, sg := range lc.ClassicLinkVPCSecurityGroups {
classicSGs = append(classicSGs, *sg)
}
if err := d.Set("vpc_classic_link_security_groups", classicSGs); err != nil {
return fmt.Errorf("error setting vpc_classic_link_security_groups: %s", err)
}

if err := readLCBlockDevices(d, lc, ec2conn); err != nil {
return err
}

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

import (
"fmt"
"testing"

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

func TestAccAWSLaunchConfigurationDataSource_basic(t *testing.T) {
rInt := acctest.RandInt()
rName := "data.aws_launch_configuration.foo"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccLaunchConfigurationDataSourceConfig_basic(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(rName, "image_id"),
resource.TestCheckResourceAttrSet(rName, "instance_type"),
resource.TestCheckResourceAttrSet(rName, "associate_public_ip_address"),
resource.TestCheckResourceAttrSet(rName, "user_data"),
resource.TestCheckResourceAttr(rName, "root_block_device.#", "1"),
resource.TestCheckResourceAttr(rName, "ebs_block_device.#", "1"),
resource.TestCheckResourceAttr(rName, "ephemeral_block_device.#", "1"),
),
},
},
})
}
func TestAccAWSLaunchConfigurationDataSource_securityGroups(t *testing.T) {
rInt := acctest.RandInt()
rName := "data.aws_launch_configuration.foo"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccLaunchConfigurationDataSourceConfig_securityGroups(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(rName, "security_groups.#", "1"),
),
},
},
})
}

func testAccLaunchConfigurationDataSourceConfig_basic(rInt int) string {
return fmt.Sprintf(`
resource "aws_launch_configuration" "foo" {
name = "terraform-test-%d"
image_id = "ami-21f78e11"
instance_type = "m1.small"
associate_public_ip_address = true
user_data = "foobar-user-data"

root_block_device {
volume_type = "gp2"
volume_size = 11
}
ebs_block_device {
device_name = "/dev/sdb"
volume_size = 9
}
ebs_block_device {
device_name = "/dev/sdc"
volume_size = 10
volume_type = "io1"
iops = 100
}
ephemeral_block_device {
device_name = "/dev/sde"
virtual_name = "ephemeral0"
}
}

data "aws_launch_configuration" "foo" {
name = "${aws_launch_configuration.foo.name}"
}
`, rInt)
}

func testAccLaunchConfigurationDataSourceConfig_securityGroups(rInt int) string {
return fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "10.1.0.0/16"
}

resource "aws_security_group" "test" {
name = "terraform-test_%d"
vpc_id = "${aws_vpc.test.id}"
}

resource "aws_launch_configuration" "test" {
name = "terraform-test-%d"
image_id = "ami-21f78e11"
instance_type = "m1.small"
security_groups = ["${aws_security_group.test.id}"]
}

data "aws_launch_configuration" "foo" {
name = "${aws_launch_configuration.test.name}"
}
`, rInt, rInt)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ func Provider() terraform.ResourceProvider {
"aws_kms_secret": dataSourceAwsKmsSecret(),
"aws_lambda_function": dataSourceAwsLambdaFunction(),
"aws_lambda_invocation": dataSourceAwsLambdaInvocation(),
"aws_launch_configuration": dataSourceAwsLaunchConfiguration(),
"aws_mq_broker": dataSourceAwsMqBroker(),
"aws_nat_gateway": dataSourceAwsNatGateway(),
"aws_network_acls": dataSourceAwsNetworkAcls(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@
<li<%= sidebar_current("docs-aws-datasource-lambda-function") %>>
<a href="/docs/providers/aws/d/lambda_function.html">aws_lambda_function</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-launch-configuration") %>>
<a href="/docs/providers/aws/d/launch_configuration.html">aws_launch_configuration</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-lb-x") %>>
<a href="/docs/providers/aws/d/lb.html">aws_lb</a>
</li>
Expand Down
Loading