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

ec2 resources - tag on create #14501

Merged
merged 22 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
8e95317
vpc resource tags on create + align test with contrib guide
DrFaust92 Aug 7, 2020
9e216bf
customer gateway resource tags on create
DrFaust92 Aug 7, 2020
dfed7c4
vpn connection resource tags on create
DrFaust92 Aug 7, 2020
2fde0f4
vpn gateway resource tags on create
DrFaust92 Aug 7, 2020
b0e4a15
spot instance request resource tags on create + test
DrFaust92 Aug 7, 2020
0fabeb9
dhcp options resource tags on create
DrFaust92 Aug 7, 2020
80d528e
route table resource tags on create + tags test
DrFaust92 Aug 7, 2020
cb79caa
security group resource tags on create + test
DrFaust92 Aug 7, 2020
2a851c4
vpc basic check for empty tags
DrFaust92 Aug 7, 2020
c7c275b
network acl resource tags on create + test
DrFaust92 Aug 7, 2020
b856d84
route table empty tag test
DrFaust92 Aug 7, 2020
781d1f7
IGW resource tags on create + test
DrFaust92 Aug 7, 2020
5244b49
Egress Only IGW resource tags on create + test
DrFaust92 Aug 7, 2020
7a3ecd3
vpc peering connection resource tags on create + test
DrFaust92 Aug 7, 2020
f01539c
spot instance resource tags on create + test
DrFaust92 Aug 7, 2020
a10e1bb
ENI resource tags on create + test
DrFaust92 Aug 7, 2020
6908252
subent tag on create + test
DrFaust92 Aug 7, 2020
84e24ed
subent tag on create + test
DrFaust92 Aug 7, 2020
301d8f8
use `testAccAvailableAZsNoOptInConfig()``
DrFaust92 Aug 13, 2020
2183a5b
docs for spot instance tags
DrFaust92 Aug 18, 2020
621bcfa
Update aws/resource_aws_subnet_test.go
DrFaust92 Aug 19, 2020
6ca9222
Update website/docs/r/spot_instance_request.html.markdown
DrFaust92 Aug 19, 2020
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
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