Skip to content

Commit

Permalink
service/ec2: Support additional tag on create resources (#14501)
Browse files Browse the repository at this point in the history
* vpc resource tags on create + align test with contrib guide

* customer gateway resource tags on create

* vpn connection resource tags on create

* vpn gateway resource tags on create

* spot instance request resource tags on create + test

* dhcp options resource tags on create

* route table resource tags on create + tags test

* security group resource tags on create + test

* vpc basic check for empty tags

* network acl resource tags on create + test

* route table empty tag test

* IGW resource tags on create + test

* Egress Only IGW resource tags on create + test

* vpc peering connection resource tags on create + test

* spot instance resource tags on create + test

* ENI resource tags on create + test

* subent tag on create + test

* subent tag on create + test

* use `testAccAvailableAZsNoOptInConfig()``

* docs for spot instance tags

* Update aws/resource_aws_subnet_test.go

Co-authored-by: Brian Flad <bflad417@gmail.com>

* Update website/docs/r/spot_instance_request.html.markdown

Co-authored-by: Brian Flad <bflad417@gmail.com>

Co-authored-by: Brian Flad <bflad417@gmail.com>
  • Loading branch information
DrFaust92 and bflad authored Aug 19, 2020
1 parent dc6e207 commit e67040b
Show file tree
Hide file tree
Showing 26 changed files with 649 additions and 241 deletions.
13 changes: 4 additions & 9 deletions aws/resource_aws_customer_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ func resourceAwsCustomerGatewayCreate(d *schema.ResourceData, meta interface{})
}

createOpts := &ec2.CreateCustomerGatewayInput{
BgpAsn: aws.Int64(i64BgpAsn),
PublicIp: aws.String(ipAddress),
Type: aws.String(vpnType),
BgpAsn: aws.Int64(i64BgpAsn),
PublicIp: aws.String(ipAddress),
Type: aws.String(vpnType),
TagSpecifications: ec2TagSpecificationsFromMap(d.Get("tags").(map[string]interface{}), ec2.ResourceTypeCustomerGateway),
}

// Create the Customer Gateway.
Expand Down Expand Up @@ -117,12 +118,6 @@ func resourceAwsCustomerGatewayCreate(d *schema.ResourceData, meta interface{})
"Error waiting for customer gateway (%s) to become ready: %s", cgId, err)
}

if v := d.Get("tags").(map[string]interface{}); len(v) > 0 {
if err := keyvaluetags.Ec2CreateTags(conn, d.Id(), v); err != nil {
return fmt.Errorf("error adding EC2 Customer Gateway (%s) tags: %s", d.Id(), err)
}
}

return resourceAwsCustomerGatewayRead(d, meta)
}

Expand Down
9 changes: 2 additions & 7 deletions aws/resource_aws_egress_only_internet_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,15 @@ func resourceAwsEgressOnlyInternetGatewayCreate(d *schema.ResourceData, meta int
conn := meta.(*AWSClient).ec2conn

resp, err := conn.CreateEgressOnlyInternetGateway(&ec2.CreateEgressOnlyInternetGatewayInput{
VpcId: aws.String(d.Get("vpc_id").(string)),
VpcId: aws.String(d.Get("vpc_id").(string)),
TagSpecifications: ec2TagSpecificationsFromMap(d.Get("tags").(map[string]interface{}), "egress-only-internet-gateway"),
})
if err != nil {
return fmt.Errorf("Error creating egress internet gateway: %s", err)
}

d.SetId(aws.StringValue(resp.EgressOnlyInternetGateway.EgressOnlyInternetGatewayId))

if v := d.Get("tags").(map[string]interface{}); len(v) > 0 {
if err := keyvaluetags.Ec2CreateTags(conn, d.Id(), v); err != nil {
return fmt.Errorf("error adding tags: %s", err)
}
}

return resourceAwsEgressOnlyInternetGatewayRead(d, meta)
}

Expand Down
1 change: 1 addition & 0 deletions aws/resource_aws_egress_only_internet_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func TestAccAWSEgressOnlyInternetGateway_basic(t *testing.T) {
Config: testAccAWSEgressOnlyInternetGatewayConfig_basic,
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSEgressOnlyInternetGatewayExists(resourceName, &igw),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
Expand Down
2 changes: 1 addition & 1 deletion aws/resource_aws_eip_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ resource "aws_eip" "test" {
resource "aws_eip_association" "test" {
allocation_id = aws_eip.test.id
instance_id = aws_spot_instance_request.foo.spot_instance_id
instance_id = aws_spot_instance_request.test.spot_instance_id
}
`, testAccAWSSpotInstanceRequestConfig(rInt))
}
Expand Down
11 changes: 4 additions & 7 deletions aws/resource_aws_internet_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ func resourceAwsInternetGatewayCreate(d *schema.ResourceData, meta interface{})
// Create the gateway
log.Printf("[DEBUG] Creating internet gateway")
var err error
resp, err := conn.CreateInternetGateway(nil)
input := &ec2.CreateInternetGatewayInput{
TagSpecifications: ec2TagSpecificationsFromMap(d.Get("tags").(map[string]interface{}), ec2.ResourceTypeInternetGateway),
}
resp, err := conn.CreateInternetGateway(input)
if err != nil {
return fmt.Errorf("Error creating internet gateway: %s", err)
}
Expand Down Expand Up @@ -79,12 +82,6 @@ func resourceAwsInternetGatewayCreate(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error refreshing internet gateway state: %s", err)
}

if v := d.Get("tags").(map[string]interface{}); len(v) > 0 {
if err := keyvaluetags.Ec2CreateTags(conn, d.Id(), v); err != nil {
return fmt.Errorf("error adding EC2 Internet Gateway (%s) tags: %s", d.Id(), err)
}
}

// Attach the new gateway to the correct vpc
err = resourceAwsInternetGatewayAttach(d, meta)
if err != nil {
Expand Down
48 changes: 29 additions & 19 deletions aws/resource_aws_internet_gateway_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/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
Expand Down Expand Up @@ -207,6 +208,7 @@ func TestAccAWSInternetGateway_delete(t *testing.T) {
func TestAccAWSInternetGateway_tags(t *testing.T) {
var v ec2.InternetGateway
resourceName := "aws_internet_gateway.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -215,13 +217,11 @@ func TestAccAWSInternetGateway_tags(t *testing.T) {
CheckDestroy: testAccCheckInternetGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckInternetGatewayConfigTags,
Config: testAccAWSInternetGatewayConfigTags1(rName, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckInternetGatewayExists(resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.Name", "terraform-testacc-internet-gateway-tags"),
resource.TestCheckResourceAttr(resourceName, "tags.test", "bar"),
testAccCheckResourceAttrAccountID(resourceName, "owner_id"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
Expand All @@ -230,13 +230,20 @@ func TestAccAWSInternetGateway_tags(t *testing.T) {
ImportStateVerify: true,
},
{
Config: testAccCheckInternetGatewayConfigTagsUpdate,
Config: testAccAWSInternetGatewayConfigTags2(rName, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckInternetGatewayExists(resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.Name", "terraform-testacc-internet-gateway-tags"),
resource.TestCheckResourceAttr(resourceName, "tags.bar", "baz"),
testAccCheckResourceAttrAccountID(resourceName, "owner_id"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccAWSInternetGatewayConfigTags1(rName, "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckInternetGatewayExists(resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
},
Expand Down Expand Up @@ -371,40 +378,43 @@ resource "aws_internet_gateway" "test" {
}
`

const testAccCheckInternetGatewayConfigTags = `
func testAccAWSInternetGatewayConfigTags1(rName, tagKey1, tagValue1 string) string {
return fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "10.1.0.0/16"
tags = {
Name = "terraform-testacc-internet-gateway-tags"
Name = %[1]q
}
}
resource "aws_internet_gateway" "test" {
vpc_id = aws_vpc.test.id
tags = {
Name = "terraform-testacc-internet-gateway-tags"
test = "bar"
%[2]q = %[3]q
}
}
`
`, rName, tagKey1, tagValue1)
}

const testAccCheckInternetGatewayConfigTagsUpdate = `
func testAccAWSInternetGatewayConfigTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "10.1.0.0/16"
tags = {
Name = "terraform-testacc-internet-gateway-tags"
Name = %[1]q
}
}
resource "aws_internet_gateway" "test" {
vpc_id = aws_vpc.test.id
tags = {
Name = "terraform-testacc-internet-gateway-tags"
bar = "baz"
%[2]q = %[3]q
%[4]q = %[5]q
}
}
`
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}
9 changes: 2 additions & 7 deletions aws/resource_aws_network_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ func resourceAwsNetworkAclCreate(d *schema.ResourceData, meta interface{}) error

// Create the Network Acl
createOpts := &ec2.CreateNetworkAclInput{
VpcId: aws.String(d.Get("vpc_id").(string)),
VpcId: aws.String(d.Get("vpc_id").(string)),
TagSpecifications: ec2TagSpecificationsFromMap(d.Get("tags").(map[string]interface{}), ec2.ResourceTypeNetworkAcl),
}

log.Printf("[DEBUG] Network Acl create config: %#v", createOpts)
Expand All @@ -203,12 +204,6 @@ func resourceAwsNetworkAclCreate(d *schema.ResourceData, meta interface{}) error
networkAcl := resp.NetworkAcl
d.SetId(aws.StringValue(networkAcl.NetworkAclId))

if v := d.Get("tags").(map[string]interface{}); len(v) > 0 {
if err := keyvaluetags.Ec2CreateTags(conn, d.Id(), v); err != nil {
return fmt.Errorf("error adding EC2 VPN Gateway (%s) tags: %s", d.Id(), err)
}
}

// Update rules and subnet association once acl is created
return resourceAwsNetworkAclUpdate(d, meta)
}
Expand Down
92 changes: 88 additions & 4 deletions aws/resource_aws_network_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func TestAccAWSNetworkAcl_basic(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSNetworkAclExists(resourceName, &networkAcl),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "ec2", regexp.MustCompile(`network-acl/acl-.+`)),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
Expand All @@ -129,6 +130,50 @@ func TestAccAWSNetworkAcl_basic(t *testing.T) {
})
}

func TestAccAWSNetworkAcl_tags(t *testing.T) {
resourceName := "aws_network_acl.test"
rName := acctest.RandomWithPrefix("tf-acc-test")
var networkAcl ec2.NetworkAcl

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSNetworkAclDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSNetworkAclConfigTags1(rName, "key1", "value1"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSNetworkAclExists(resourceName, &networkAcl),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSNetworkAclConfigTags2(rName, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSNetworkAclExists(resourceName, &networkAcl),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccAWSNetworkAclConfigTags1(rName, "key2", "value2"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSNetworkAclExists(resourceName, &networkAcl),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
},
})
}

func TestAccAWSNetworkAcl_disappears(t *testing.T) {
var networkAcl ec2.NetworkAcl
resourceName := "aws_network_acl.test"
Expand Down Expand Up @@ -1152,10 +1197,6 @@ resource "aws_network_acl" "test" {
from_port = 80
to_port = 80
}
tags = {
Name = "tf-acc-acl-egress-and-ingress"
}
}
`

Expand Down Expand Up @@ -1556,3 +1597,46 @@ resource "aws_network_acl" "test" {
}
`
}

func testAccAWSNetworkAclConfigTags1(rName, tagKey1, tagValue1 string) string {
return fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "10.1.0.0/16"
tags = {
Name = %[1]q
}
}
resource "aws_network_acl" "test" {
vpc_id = aws_vpc.test.id
ingress = []
tags = {
%[2]q = %[3]q
}
}
`, rName, tagKey1, tagValue1)
}

func testAccAWSNetworkAclConfigTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "10.1.0.0/16"
tags = {
Name = %[1]q
}
}
resource "aws_network_acl" "test" {
vpc_id = aws_vpc.test.id
ingress = []
tags = {
%[2]q = %[3]q
%[4]q = %[5]q
}
}
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}
9 changes: 2 additions & 7 deletions aws/resource_aws_network_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ func resourceAwsNetworkInterfaceCreate(d *schema.ResourceData, meta interface{})
conn := meta.(*AWSClient).ec2conn

request := &ec2.CreateNetworkInterfaceInput{
SubnetId: aws.String(d.Get("subnet_id").(string)),
SubnetId: aws.String(d.Get("subnet_id").(string)),
TagSpecifications: ec2TagSpecificationsFromMap(d.Get("tags").(map[string]interface{}), ec2.ResourceTypeNetworkInterface),
}

security_groups := d.Get("security_groups").(*schema.Set).List()
Expand Down Expand Up @@ -151,12 +152,6 @@ func resourceAwsNetworkInterfaceCreate(d *schema.ResourceData, meta interface{})

d.SetId(*resp.NetworkInterface.NetworkInterfaceId)

if v := d.Get("tags").(map[string]interface{}); len(v) > 0 {
if err := keyvaluetags.Ec2CreateTags(conn, d.Id(), v); err != nil {
return fmt.Errorf("error adding tags: %s", err)
}
}

return resourceAwsNetworkInterfaceUpdate(d, meta)
}

Expand Down
Loading

0 comments on commit e67040b

Please sign in to comment.