@@ -7,25 +7,33 @@ import (
7
7
"github.com/aws/aws-sdk-go/aws"
8
8
"github.com/aws/aws-sdk-go/aws/awserr"
9
9
"github.com/aws/aws-sdk-go/service/ec2"
10
+ "github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
10
11
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
11
12
"github.com/hashicorp/terraform-plugin-sdk/terraform"
12
13
)
13
14
14
15
func TestAccAWSVpcEndpointRouteTableAssociation_basic (t * testing.T ) {
15
16
var vpce ec2.VpcEndpoint
17
+ resourceName := "aws_vpc_endpoint_route_table_association.test"
18
+ rName := fmt .Sprintf ("tf-testacc-vpce-%s" , acctest .RandStringFromCharSet (16 , acctest .CharSetAlphaNum ))
16
19
17
20
resource .ParallelTest (t , resource.TestCase {
18
21
PreCheck : func () { testAccPreCheck (t ) },
19
22
Providers : testAccProviders ,
20
23
CheckDestroy : testAccCheckVpcEndpointRouteTableAssociationDestroy ,
21
24
Steps : []resource.TestStep {
22
25
{
23
- Config : testAccVpcEndpointRouteTableAssociationConfig ,
26
+ Config : testAccVpcEndpointRouteTableAssociationConfig ( rName ) ,
24
27
Check : resource .ComposeTestCheckFunc (
25
- testAccCheckVpcEndpointRouteTableAssociationExists (
26
- "aws_vpc_endpoint_route_table_association.a" , & vpce ),
28
+ testAccCheckVpcEndpointRouteTableAssociationExists (resourceName , & vpce ),
27
29
),
28
30
},
31
+ {
32
+ ResourceName : resourceName ,
33
+ ImportState : true ,
34
+ ImportStateIdFunc : testAccAWSVpcEndpointRouteTableAssociationImportStateIdFunc (resourceName ),
35
+ ImportStateVerify : true ,
36
+ },
29
37
},
30
38
})
31
39
}
@@ -72,7 +80,7 @@ func testAccCheckVpcEndpointRouteTableAssociationExists(n string, vpce *ec2.VpcE
72
80
}
73
81
74
82
if rs .Primary .ID == "" {
75
- return fmt .Errorf ("No ID is set" )
83
+ return fmt .Errorf ("No VPC Endpoint Route Table Association ID is set" )
76
84
}
77
85
78
86
conn := testAccProvider .Meta ().(* AWSClient ).ec2conn
@@ -83,48 +91,65 @@ func testAccCheckVpcEndpointRouteTableAssociationExists(n string, vpce *ec2.VpcE
83
91
return err
84
92
}
85
93
if len (resp .VpcEndpoints ) == 0 {
86
- return fmt .Errorf ("VPC endpoint not found" )
94
+ return fmt .Errorf ("VPC Endpoint not found" )
87
95
}
88
96
89
97
* vpce = * resp .VpcEndpoints [0 ]
90
98
91
99
if len (vpce .RouteTableIds ) == 0 {
92
- return fmt .Errorf ("no route table associations " )
100
+ return fmt .Errorf ("No VPC Endpoint Route Table Associations " )
93
101
}
94
102
95
- for _ , id := range vpce .RouteTableIds {
96
- if * id == rs .Primary .Attributes ["route_table_id" ] {
103
+ for _ , rtId := range vpce .RouteTableIds {
104
+ if aws . StringValue ( rtId ) == rs .Primary .Attributes ["route_table_id" ] {
97
105
return nil
98
106
}
99
107
}
100
108
101
- return fmt .Errorf ("route table association not found" )
109
+ return fmt .Errorf ("VPC Endpoint Route Table Association not found" )
110
+ }
111
+ }
112
+
113
+ func testAccAWSVpcEndpointRouteTableAssociationImportStateIdFunc (n string ) resource.ImportStateIdFunc {
114
+ return func (s * terraform.State ) (string , error ) {
115
+ rs , ok := s .RootModule ().Resources [n ]
116
+ if ! ok {
117
+ return "" , fmt .Errorf ("Not found: %s" , n )
118
+ }
119
+
120
+ id := fmt .Sprintf ("%s/%s" , rs .Primary .Attributes ["vpc_endpoint_id" ], rs .Primary .Attributes ["route_table_id" ])
121
+ return id , nil
102
122
}
103
123
}
104
124
105
- const testAccVpcEndpointRouteTableAssociationConfig = `
106
- resource "aws_vpc" "foo" {
107
- cidr_block = "10.0.0.0/16"
125
+ func testAccVpcEndpointRouteTableAssociationConfig (rName string ) string {
126
+ return fmt .Sprintf (`
127
+ resource "aws_vpc" "test" {
128
+ cidr_block = "10.0.0.0/16"
129
+
108
130
tags = {
109
- Name = "terraform-testacc-vpc-endpoint-route-table-association"
110
- }
131
+ Name = %[1]q
132
+ }
111
133
}
112
134
113
- resource "aws_vpc_endpoint" "s3" {
114
- vpc_id = "${aws_vpc.foo.id}"
115
- service_name = "com.amazonaws.us-west-2.s3"
135
+ data "aws_region" "current" {}
136
+
137
+ resource "aws_vpc_endpoint" "test" {
138
+ vpc_id = "${aws_vpc.test.id}"
139
+ service_name = "com.amazonaws.${data.aws_region.current.name}.s3"
116
140
}
117
141
118
- resource "aws_route_table" "rt " {
119
- vpc_id = "${aws_vpc.foo .id}"
142
+ resource "aws_route_table" "test " {
143
+ vpc_id = "${aws_vpc.test .id}"
120
144
121
145
tags = {
122
- Name = "test"
123
- }
146
+ Name = %[1]q
147
+ }
124
148
}
125
149
126
- resource "aws_vpc_endpoint_route_table_association" "a" {
127
- vpc_endpoint_id = "${aws_vpc_endpoint.s3.id}"
128
- route_table_id = "${aws_route_table.rt.id}"
150
+ resource "aws_vpc_endpoint_route_table_association" "test" {
151
+ vpc_endpoint_id = "${aws_vpc_endpoint.test.id}"
152
+ route_table_id = "${aws_route_table.test.id}"
153
+ }
154
+ ` , rName )
129
155
}
130
- `
0 commit comments