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

Only reject tags for standard EIPs on EC2 Classic #18909

Merged
merged 8 commits into from
Apr 16, 2021
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
3 changes: 3 additions & 0 deletions .changelog/18909.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_eip: Tags are created for EIPs which default to vpc domain
```
12 changes: 6 additions & 6 deletions aws/resource_aws_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ func resourceAwsEipCreate(d *schema.ResourceData, meta interface{}) error {
Domain: aws.String(domainOpt),
}

if domainOpt == ec2.DomainTypeVpc {
if v := d.Get("tags").(map[string]interface{}); len(v) > 0 {
supportedPlatforms := meta.(*AWSClient).supportedplatforms
if domainOpt != ec2.DomainTypeVpc && len(supportedPlatforms) > 0 && hasEc2Classic(supportedPlatforms) {
return fmt.Errorf("tags cannot be set for a standard-domain EIP - must be a VPC-domain EIP")
}
allocOpts.TagSpecifications = ec2TagSpecificationsFromMap(d.Get("tags").(map[string]interface{}), ec2.ResourceTypeElasticIp)
}

Expand Down Expand Up @@ -183,10 +187,6 @@ func resourceAwsEipCreate(d *schema.ResourceData, meta interface{}) error {

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

if v := d.Get("tags").(map[string]interface{}); len(v) > 0 && d.Get("domain").(string) == ec2.DomainTypeStandard {
return fmt.Errorf("tags can not be set for an EIP in EC2 Classic")
}

return resourceAwsEipUpdate(d, meta)
}

Expand Down Expand Up @@ -400,7 +400,7 @@ func resourceAwsEipUpdate(d *schema.ResourceData, meta interface{}) error {

if d.HasChange("tags") && !d.IsNewResource() {
if d.Get("domain").(string) == ec2.DomainTypeStandard {
return fmt.Errorf("tags can not be set for an EIP in EC2 Classic")
return fmt.Errorf("tags cannot be set for a standard-domain EIP - must be a VPC-domain EIP")
}
o, n := d.GetChange("tags")
if err := keyvaluetags.Ec2UpdateTags(ec2conn, d.Id(), o, n); err != nil {
Expand Down
45 changes: 24 additions & 21 deletions aws/resource_aws_eip_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestAccAWSEIPAssociation_instance(t *testing.T) {
{
Config: testAccAWSEIPAssociationConfig_instance(),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEIPExists("aws_eip.test", &a),
testAccCheckAWSEIPExists("aws_eip.test", false, &a),
testAccCheckAWSEIPAssociationExists(resourceName, &a),
),
},
Expand All @@ -50,7 +50,7 @@ func TestAccAWSEIPAssociation_networkInterface(t *testing.T) {
{
Config: testAccAWSEIPAssociationConfig_networkInterface,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEIPExists("aws_eip.test", &a),
testAccCheckAWSEIPExists("aws_eip.test", false, &a),
testAccCheckAWSEIPAssociationExists(resourceName, &a),
),
},
Expand All @@ -66,6 +66,7 @@ func TestAccAWSEIPAssociation_networkInterface(t *testing.T) {
func TestAccAWSEIPAssociation_basic(t *testing.T) {
var a ec2.Address
resourceName := "aws_eip_association.by_allocation_id"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccEC2VPCOnlyPreCheck(t) },
Expand All @@ -74,13 +75,13 @@ func TestAccAWSEIPAssociation_basic(t *testing.T) {
CheckDestroy: testAccCheckAWSEIPAssociationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEIPAssociationConfig(),
Config: testAccAWSEIPAssociationConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEIPExists("aws_eip.test.0", &a),
testAccCheckAWSEIPExists("aws_eip.test.0", false, &a),
testAccCheckAWSEIPAssociationExists("aws_eip_association.by_allocation_id", &a),
testAccCheckAWSEIPExists("aws_eip.test.1", &a),
testAccCheckAWSEIPExists("aws_eip.test.1", false, &a),
testAccCheckAWSEIPAssociationExists("aws_eip_association.by_public_ip", &a),
testAccCheckAWSEIPExists("aws_eip.test.2", &a),
testAccCheckAWSEIPExists("aws_eip.test.2", false, &a),
testAccCheckAWSEIPAssociationExists("aws_eip_association.to_eni", &a),
),
},
Expand All @@ -106,7 +107,7 @@ func TestAccAWSEIPAssociation_ec2Classic(t *testing.T) {
{
Config: testAccAWSEIPAssociationConfig_ec2Classic(),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEIPEc2ClassicExists("aws_eip.test", &a),
testAccCheckAWSEIPExists("aws_eip.test", true, &a),
testAccCheckAWSEIPAssociationEc2ClassicExists(resourceName, &a),
resource.TestCheckResourceAttrSet(resourceName, "public_ip"),
resource.TestCheckResourceAttr(resourceName, "allocation_id", ""),
Expand Down Expand Up @@ -136,7 +137,7 @@ func TestAccAWSEIPAssociation_spotInstance(t *testing.T) {
{
Config: testAccAWSEIPAssociationConfig_spotInstance(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEIPExists("aws_eip.test", &a),
testAccCheckAWSEIPExists("aws_eip.test", false, &a),
testAccCheckAWSEIPAssociationExists(resourceName, &a),
resource.TestCheckResourceAttrSet(resourceName, "allocation_id"),
resource.TestCheckResourceAttrSet(resourceName, "instance_id"),
Expand All @@ -153,8 +154,8 @@ func TestAccAWSEIPAssociation_spotInstance(t *testing.T) {

func TestAccAWSEIPAssociation_disappears(t *testing.T) {
var a ec2.Address

resourceName := "aws_eip_association.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -163,9 +164,9 @@ func TestAccAWSEIPAssociation_disappears(t *testing.T) {
CheckDestroy: testAccCheckAWSEIPAssociationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEIPAssociationConfigDisappears(),
Config: testAccAWSEIPAssociationConfigDisappears(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEIPExists("aws_eip.test", &a),
testAccCheckAWSEIPExists("aws_eip.test", false, &a),
testAccCheckAWSEIPAssociationExists(resourceName, &a),
testAccCheckResourceDisappears(testAccProvider, resourceAwsEipAssociation(), resourceName),
),
Expand Down Expand Up @@ -295,14 +296,15 @@ func testAccCheckAWSEIPAssociationDestroy(s *terraform.State) error {
return nil
}

func testAccAWSEIPAssociationConfig() string {
func testAccAWSEIPAssociationConfig(rName string) string {
return composeConfig(
testAccAvailableAZsNoOptInConfig(),
testAccLatestAmazonLinuxHvmEbsAmiConfig(), `
testAccLatestAmazonLinuxHvmEbsAmiConfig(),
fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "192.168.0.0/24"
tags = {
Name = "terraform-testacc-eip-association"
Name = %[1]q
}
}

Expand All @@ -311,7 +313,7 @@ resource "aws_subnet" "test" {
cidr_block = "192.168.0.0/25"
availability_zone = data.aws_availability_zones.available.names[0]
tags = {
Name = "tf-acc-eip-association"
Name = %[1]q
}
}

Expand Down Expand Up @@ -360,17 +362,18 @@ resource "aws_network_interface" "test" {
device_index = 1
}
}
`)
`, rName))
}

func testAccAWSEIPAssociationConfigDisappears() string {
func testAccAWSEIPAssociationConfigDisappears(rName string) string {
return composeConfig(
testAccAvailableAZsNoOptInConfig(),
testAccLatestAmazonLinuxHvmEbsAmiConfig(), `
testAccLatestAmazonLinuxHvmEbsAmiConfig(),
fmt.Sprintf(`
resource "aws_vpc" "main" {
cidr_block = "192.168.0.0/24"
tags = {
Name = "terraform-testacc-eip-association-disappears"
Name = %[1]q
}
}

Expand All @@ -379,7 +382,7 @@ resource "aws_subnet" "sub" {
cidr_block = "192.168.0.0/25"
availability_zone = data.aws_availability_zones.available.names[0]
tags = {
Name = "tf-acc-eip-association-disappears"
Name = %[1]q
}
}

Expand All @@ -402,7 +405,7 @@ resource "aws_eip_association" "test" {
allocation_id = aws_eip.test.id
instance_id = aws_instance.test.id
}
`)
`, rName))
}

func testAccAWSEIPAssociationConfig_ec2Classic() string {
Expand Down
Loading