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

r/aws_eip: Support tags #2768

Merged
merged 1 commit into from
Dec 26, 2017
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
17 changes: 17 additions & 0 deletions aws/resource_aws_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func resourceAwsEip() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},

"tags": tagsSchema(),
},
}
}
Expand Down Expand Up @@ -111,6 +113,13 @@ func resourceAwsEipCreate(d *schema.ResourceData, meta interface{}) error {
}

log.Printf("[INFO] EIP ID: %s (domain: %v)", d.Id(), *allocResp.Domain)

if _, ok := d.GetOk("tags"); ok {
if err := setTags(ec2conn, d); err != nil {
return fmt.Errorf("Error creating EIP tags: %s", err)
}
}

return resourceAwsEipUpdate(d, meta)
}

Expand Down Expand Up @@ -206,6 +215,8 @@ func resourceAwsEipRead(d *schema.ResourceData, meta interface{}) error {
d.SetId(*address.AllocationId)
}

d.Set("tags", tagsToMap(address.Tags))

return nil
}

Expand Down Expand Up @@ -270,6 +281,12 @@ func resourceAwsEipUpdate(d *schema.ResourceData, meta interface{}) error {
}
}

if _, ok := d.GetOk("tags"); ok {
if err := setTags(ec2conn, d); err != nil {
return fmt.Errorf("Error updating EIP tags: %s", err)
}
}

return resourceAwsEipRead(d, meta)
}

Expand Down
48 changes: 48 additions & 0 deletions aws/resource_aws_eip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
Expand Down Expand Up @@ -265,6 +266,42 @@ func TestAccAWSEIPAssociate_not_associated(t *testing.T) {
})
}

func TestAccAWSEIP_tags(t *testing.T) {
var conf ec2.Address
resourceName := "aws_eip.bar"
rName1 := fmt.Sprintf("%s-%d", t.Name(), acctest.RandInt())
rName2 := fmt.Sprintf("%s-%d", t.Name(), acctest.RandInt())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_eip.bar",
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEIPDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSEIPConfig_tags(rName1, t.Name()),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEIPExists(resourceName, &conf),
testAccCheckAWSEIPAttributes(&conf),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.RandomName", rName1),
resource.TestCheckResourceAttr(resourceName, "tags.TestName", t.Name()),
),
},
resource.TestStep{
Config: testAccAWSEIPConfig_tags(rName2, t.Name()),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEIPExists(resourceName, &conf),
testAccCheckAWSEIPAttributes(&conf),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.RandomName", rName2),
resource.TestCheckResourceAttr(resourceName, "tags.TestName", t.Name()),
),
},
},
})
}

func testAccCheckAWSEIPDisappears(v *ec2.Address) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn
Expand Down Expand Up @@ -402,6 +439,17 @@ resource "aws_eip" "bar" {
}
`

func testAccAWSEIPConfig_tags(rName, testName string) string {
return fmt.Sprintf(`
resource "aws_eip" "bar" {
tags {
RandomName = "%[1]s"
TestName = "%[2]s"
}
}
`, rName, testName)
}

const testAccAWSEIPInstanceEc2Classic = `
provider "aws" {
region = "us-east-1"
Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/eip.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ The following arguments are supported:
* `associate_with_private_ip` - (Optional) A user specified primary or secondary private IP address to
associate with the Elastic IP address. If no private IP address is specified,
the Elastic IP address is associated with the primary private IP address.
* `tags` - (Optional) A mapping of tags to assign to the resource.

~> **NOTE:** You can specify either the `instance` ID or the `network_interface` ID,
but not both. Including both will **not** return an error from the AWS API, but will
Expand All @@ -100,7 +101,7 @@ more information.

## Attributes Reference

The following attributes are exported:
The following additional attributes are exported:

* `id` - Contains the EIP allocation ID.
* `private_ip` - Contains the private IP address (if in VPC).
Expand All @@ -110,7 +111,6 @@ The following attributes are exported:
* `instance` - Contains the ID of the attached instance.
* `network_interface` - Contains the ID of the attached network interface.


## Import

EIPs in a VPC can be imported using their Allocation ID, e.g.
Expand Down