Skip to content

Commit

Permalink
fix setting security group
Browse files Browse the repository at this point in the history
  • Loading branch information
kl4w committed Jun 28, 2018
1 parent 88ef715 commit 40d6ecf
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
12 changes: 10 additions & 2 deletions aws/data_source_aws_launch_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,19 @@ func dataSourceAwsLaunchConfigurationRead(d *schema.ResourceData, meta interface
d.Set("enable_monitoring", lc.InstanceMonitoring.Enabled)
}

if err := d.Set("security_groups", lc.SecurityGroups); err != nil {
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)
}

if err := d.Set("vpc_classic_link_security_groups", lc.ClassicLinkVPCSecurityGroups); err != nil {
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)
}

Expand Down
45 changes: 43 additions & 2 deletions aws/data_source_aws_launch_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestAccAWSLaunchConfigurationDataSource_basic(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccLaunchConfigurationDataSourceConfig(rInt),
Config: testAccLaunchConfigurationDataSourceConfig_basic(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(rName, "image_id"),
resource.TestCheckResourceAttrSet(rName, "instance_type"),
Expand All @@ -31,8 +31,25 @@ func TestAccAWSLaunchConfigurationDataSource_basic(t *testing.T) {
},
})
}
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(rInt int) string {
func testAccLaunchConfigurationDataSourceConfig_basic(rInt int) string {
return fmt.Sprintf(`
resource "aws_launch_configuration" "foo" {
name = "terraform-test-%d"
Expand Down Expand Up @@ -66,3 +83,27 @@ data "aws_launch_configuration" "foo" {
}
`, 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)
}

0 comments on commit 40d6ecf

Please sign in to comment.