Adding tags support to Internet Gateway resource

This commit is contained in:
Rob Zienert 2014-12-27 22:05:15 -06:00
parent f61b7af815
commit 9fd59f09e2
2 changed files with 69 additions and 5 deletions

View File

@ -22,6 +22,7 @@ func resourceAwsInternetGateway() *schema.Resource {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
}, },
"tags": tagsSchema(),
}, },
} }
} }
@ -66,6 +67,8 @@ func resourceAwsInternetGatewayRead(d *schema.ResourceData, meta interface{}) er
d.Set("vpc_id", ig.Attachments[0].VpcId) d.Set("vpc_id", ig.Attachments[0].VpcId)
} }
d.Set("tags", tagsToMap(ig.Tags))
return nil return nil
} }
@ -82,6 +85,14 @@ func resourceAwsInternetGatewayUpdate(d *schema.ResourceData, meta interface{})
} }
} }
ec2conn := meta.(*AWSClient).ec2conn
if err := setTags(ec2conn, d); err != nil {
return err
}
d.SetPartial("tags")
return nil return nil
} }
@ -179,9 +190,9 @@ func resourceAwsInternetGatewayDetach(d *schema.ResourceData, meta interface{})
ec2conn := meta.(*AWSClient).ec2conn ec2conn := meta.(*AWSClient).ec2conn
// Get the old VPC ID to detach from // Get the old VPC ID to detach from
vpc_id, _ := d.GetChange("vpc_id") vpcID, _ := d.GetChange("vpc_id")
if vpc_id.(string) == "" { if vpcID.(string) == "" {
log.Printf( log.Printf(
"[DEBUG] Not detaching Internet Gateway '%s' as no VPC ID is set", "[DEBUG] Not detaching Internet Gateway '%s' as no VPC ID is set",
d.Id()) d.Id())
@ -191,10 +202,10 @@ func resourceAwsInternetGatewayDetach(d *schema.ResourceData, meta interface{})
log.Printf( log.Printf(
"[INFO] Detaching Internet Gateway '%s' from VPC '%s'", "[INFO] Detaching Internet Gateway '%s' from VPC '%s'",
d.Id(), d.Id(),
vpc_id.(string)) vpcID.(string))
wait := true wait := true
_, err := ec2conn.DetachInternetGateway(d.Id(), vpc_id.(string)) _, err := ec2conn.DetachInternetGateway(d.Id(), vpcID.(string))
if err != nil { if err != nil {
ec2err, ok := err.(*ec2.Error) ec2err, ok := err.(*ec2.Error)
if ok { if ok {

View File

@ -54,6 +54,33 @@ func TestAccAWSInternetGateway(t *testing.T) {
}) })
} }
func testAccInternetGatewayTags(t *testing.T) {
var v ec2.InternetGateway
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInternetGatewayDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckInternetGatewayConfigTags,
Check: resource.ComposeTestCheckFunc(
testAccCheckInternetGatewayExists("aws_internet_gateway.foo", &v),
),
},
resource.TestStep{
Config: testAccCheckInternetGatewayConfigTagsUpdate,
Check: resource.ComposeTestCheckFunc(
testAccCheckInternetGatewayExists("aws_internet_gateway.foo", &v),
testAccCheckTags(&v.Tags, "foo", ""),
testAccCheckTags(&v.Tags, "bar", "baz"),
),
},
},
})
}
func testAccCheckInternetGatewayDestroy(s *terraform.State) error { func testAccCheckInternetGatewayDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn conn := testAccProvider.Meta().(*AWSClient).ec2conn
@ -67,7 +94,7 @@ func testAccCheckInternetGatewayDestroy(s *terraform.State) error {
[]string{rs.Primary.ID}, ec2.NewFilter()) []string{rs.Primary.ID}, ec2.NewFilter())
if err == nil { if err == nil {
if len(resp.InternetGateways) > 0 { if len(resp.InternetGateways) > 0 {
return fmt.Errorf("still exist.") return fmt.Errorf("still exists")
} }
return nil return nil
@ -136,3 +163,29 @@ resource "aws_internet_gateway" "foo" {
vpc_id = "${aws_vpc.bar.id}" vpc_id = "${aws_vpc.bar.id}"
} }
` `
const testAccCheckInternetGatewayConfigTags = `
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
}
resource "aws_internet_gateway" "foo" {
vpc_id = "${aws_vpc.foo.id}"
tags {
foo = "bar"
}
}
`
const testAccCheckInternetGatewayConfigTagsUpdate = `
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
}
resource "aws_internet_gateway" "foo" {
vpc_id = "${aws_vpc.foo.id}"
tags {
bar = "baz"
}
}
`