-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
New Resource: aws_api_gateway_vpc_link #2512
Changes from 6 commits
64134dd
93bf79d
0bbcedc
1b40524
19ab262
70a6355
d18c6bf
c91d313
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/apigateway" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsApiGatewayVpcLink() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsApiGatewayVpcLinkCreate, | ||
Read: resourceAwsApiGatewayVpcLinkRead, | ||
Update: resourceAwsApiGatewayVpcLinkUpdate, | ||
Delete: resourceAwsApiGatewayVpcLinkDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"target_arn": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsApiGatewayVpcLinkCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigateway | ||
|
||
input := &apigateway.CreateVpcLinkInput{ | ||
Name: aws.String(d.Get("name").(string)), | ||
TargetArns: []*string{aws.String(d.Get("target_arn").(string))}, | ||
} | ||
if v, ok := d.GetOk("description"); ok { | ||
input.Description = aws.String(v.(string)) | ||
} | ||
|
||
resp, err := conn.CreateVpcLink(input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{apigateway.VpcLinkStatusPending}, | ||
Target: []string{apigateway.VpcLinkStatusAvailable}, | ||
Refresh: apigatewayVpcLinkRefreshStatusFunc(conn, *resp.Id), | ||
Timeout: 8 * time.Minute, | ||
MinTimeout: 3 * time.Second, | ||
} | ||
|
||
_, err = stateConf.WaitForState() | ||
if err != nil { | ||
return fmt.Errorf("[WARN] Error waiting for APIGateway Vpc Link status to be \"%s\": %s", apigateway.VpcLinkStatusAvailable, err) | ||
} | ||
|
||
d.SetId(*resp.Id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We usually set the ID as soon as we can after receiving it from the API, before making any further requests so that it's recorded in the state in case any future API requests fail for any reason. Do you mind moving it above the |
||
return nil | ||
} | ||
|
||
func resourceAwsApiGatewayVpcLinkRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigateway | ||
|
||
input := &apigateway.GetVpcLinkInput{ | ||
VpcLinkId: aws.String(d.Id()), | ||
} | ||
|
||
resp, err := conn.GetVpcLink(input) | ||
if err != nil { | ||
if isAWSErr(err, apigateway.ErrCodeNotFoundException, "") { | ||
d.SetId("") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: Do you mind adding our usual |
||
return nil | ||
} | ||
return err | ||
} | ||
|
||
d.Set("name", resp.Name) | ||
d.Set("description", resp.Description) | ||
d.Set("target_arn", resp.TargetArns[0]) | ||
return nil | ||
} | ||
|
||
func resourceAwsApiGatewayVpcLinkUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigateway | ||
|
||
operations := make([]*apigateway.PatchOperation, 0) | ||
|
||
if d.HasChange("name") { | ||
operations = append(operations, &apigateway.PatchOperation{ | ||
Op: aws.String("replace"), | ||
Path: aws.String("/name"), | ||
Value: aws.String(d.Get("name").(string)), | ||
}) | ||
} | ||
|
||
if d.HasChange("description") { | ||
operations = append(operations, &apigateway.PatchOperation{ | ||
Op: aws.String("replace"), | ||
Path: aws.String("/description"), | ||
Value: aws.String(d.Get("description").(string)), | ||
}) | ||
} | ||
|
||
input := &apigateway.UpdateVpcLinkInput{ | ||
VpcLinkId: aws.String(d.Id()), | ||
PatchOperations: operations, | ||
} | ||
|
||
_, err := conn.UpdateVpcLink(input) | ||
if err != nil { | ||
if isAWSErr(err, apigateway.ErrCodeNotFoundException, "") { | ||
d.SetId("") | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{apigateway.VpcLinkStatusPending}, | ||
Target: []string{apigateway.VpcLinkStatusAvailable}, | ||
Refresh: apigatewayVpcLinkRefreshStatusFunc(conn, d.Id()), | ||
Timeout: 8 * time.Minute, | ||
MinTimeout: 3 * time.Second, | ||
} | ||
|
||
_, err = stateConf.WaitForState() | ||
if err != nil { | ||
return fmt.Errorf("[WARN] Error waiting for APIGateway Vpc Link status to be \"%s\": %s", apigateway.VpcLinkStatusAvailable, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsApiGatewayVpcLinkDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigateway | ||
|
||
input := &apigateway.DeleteVpcLinkInput{ | ||
VpcLinkId: aws.String(d.Id()), | ||
} | ||
|
||
_, err := conn.DeleteVpcLink(input) | ||
if err != nil { | ||
if isAWSErr(err, apigateway.ErrCodeNotFoundException, "") { | ||
d.SetId("") | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
stateConf := resource.StateChangeConf{ | ||
Pending: []string{apigateway.VpcLinkStatusPending, | ||
apigateway.VpcLinkStatusAvailable, | ||
apigateway.VpcLinkStatusDeleting}, | ||
Target: []string{"deleted"}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using empty string here is equally valid and in fact preferred, over making up our own statuses 😉 |
||
Timeout: 5 * time.Minute, | ||
MinTimeout: 1 * time.Second, | ||
Refresh: func() (interface{}, string, error) { | ||
resp, err := conn.GetVpcLink(&apigateway.GetVpcLinkInput{ | ||
VpcLinkId: aws.String(d.Id()), | ||
}) | ||
if err != nil { | ||
if isAWSErr(err, apigateway.ErrCodeNotFoundException, "") { | ||
return 1, "deleted", nil | ||
} | ||
return nil, "failed", err | ||
} | ||
return resp, *resp.Status, nil | ||
}, | ||
} | ||
|
||
if _, err := stateConf.WaitForState(); err != nil { | ||
return err | ||
} | ||
|
||
d.SetId("") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: I know we still have it in many resources, but it's redundant. The ID is emptied automatically in the core. |
||
return nil | ||
} | ||
|
||
func apigatewayVpcLinkRefreshStatusFunc(conn *apigateway.APIGateway, vl string) resource.StateRefreshFunc { | ||
return func() (interface{}, string, error) { | ||
input := &apigateway.GetVpcLinkInput{ | ||
VpcLinkId: aws.String(vl), | ||
} | ||
resp, err := conn.GetVpcLink(input) | ||
if err != nil { | ||
return nil, "failed", err | ||
} | ||
return resp, *resp.Status, nil | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/apigateway" | ||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAwsAPIGatewayVpcLink_basic(t *testing.T) { | ||
rName := acctest.RandString(5) | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAwsAPIGatewayVpcLinkDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAPIGatewayVpcLinkConfig(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAwsAPIGatewayVpcLinkExists("aws_api_gateway_vpc_link.test"), | ||
resource.TestCheckResourceAttr("aws_api_gateway_vpc_link.test", "name", fmt.Sprintf("tf-apigateway-%s", rName)), | ||
resource.TestCheckResourceAttr("aws_api_gateway_vpc_link.test", "description", "test"), | ||
), | ||
}, | ||
{ | ||
Config: testAccAPIGatewayVpcLinkConfig_Update(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAwsAPIGatewayVpcLinkExists("aws_api_gateway_vpc_link.test"), | ||
resource.TestCheckResourceAttr("aws_api_gateway_vpc_link.test", "name", fmt.Sprintf("tf-apigateway-update-%s", rName)), | ||
resource.TestCheckResourceAttr("aws_api_gateway_vpc_link.test", "description", "test update"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAwsAPIGatewayVpcLinkDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).apigateway | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_api_gateway_vpc_link" { | ||
continue | ||
} | ||
|
||
input := &apigateway.GetVpcLinkInput{ | ||
VpcLinkId: aws.String(rs.Primary.ID), | ||
} | ||
|
||
resp, err := conn.GetVpcLink(input) | ||
if err != nil { | ||
if isAWSErr(err, apigateway.ErrCodeNotFoundException, "") { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
if *resp.Status != apigateway.VpcLinkStatusDeleting { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should treat any positive response here as a failure - the VPC link should be already deleted by the time test finishes and practically have no status (i.e. it shouldn't be in |
||
return fmt.Errorf("APIGateway VPC Link (%s) not deleted", rs.Primary.ID) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccCheckAwsAPIGatewayVpcLinkExists(name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
_, ok := s.RootModule().Resources[name] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", name) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mind checking the API here also? |
||
return nil | ||
} | ||
} | ||
|
||
func testAccAPIGatewayVpcLinkConfig(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_lb" "test_a" { | ||
name = "tf-lb-a-%s" | ||
internal = true | ||
load_balancer_type = "network" | ||
subnets = ["${aws_subnet.test.id}"] | ||
} | ||
|
||
resource "aws_vpc" "test" { | ||
cidr_block = "10.10.0.0/16" | ||
} | ||
|
||
resource "aws_subnet" "test" { | ||
vpc_id = "${aws_vpc.test.id}" | ||
cidr_block = "10.10.0.0/21" | ||
availability_zone = "us-west-2a" | ||
} | ||
|
||
resource "aws_api_gateway_vpc_link" "test" { | ||
name = "tf-apigateway-%s" | ||
description = "test" | ||
target_arn = "${aws_lb.test_a.arn}" | ||
} | ||
`, rName, rName) | ||
} | ||
|
||
func testAccAPIGatewayVpcLinkConfig_Update(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_lb" "test_a" { | ||
name = "tf-lb-a-%s" | ||
internal = true | ||
load_balancer_type = "network" | ||
subnets = ["${aws_subnet.test.id}"] | ||
} | ||
|
||
resource "aws_vpc" "test" { | ||
cidr_block = "10.10.0.0/16" | ||
} | ||
|
||
resource "aws_subnet" "test" { | ||
vpc_id = "${aws_vpc.test.id}" | ||
cidr_block = "10.10.0.0/21" | ||
availability_zone = "us-west-2a" | ||
} | ||
|
||
resource "aws_api_gateway_vpc_link" "test" { | ||
name = "tf-apigateway-update-%s" | ||
description = "test update" | ||
target_arn = "${aws_lb.test_a.arn}" | ||
} | ||
`, rName, rName) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also add a test with an actual API Gateway and a HTTP endpoint, so that we ensure it's all ok. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I couldn't understand what you meant so cloud you describe more details:bow: ? @Ninir There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess what @Ninir means is that we should add a test which tests the full setup including API Gateway integration leveraging the VPC Link. It seems we'd also need to update other API Gateway resources to make that happen, namely to add few more attributes to I'm personally ok with shipping this resource as is and addressing the above in a separate PR to keep diffs small and easier to review - what do you think @Ninir ? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_api_gateway_vpc_link" | ||
sidebar_current: "docs-aws-resource-api-gateway-vpc-link" | ||
description: |- | ||
Provides an API Gateway VPC Link. | ||
--- | ||
|
||
# aws_api_gateway_vpc_link | ||
|
||
Provides an API Gateway VPC Link. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
resource "aws_lb" "example" { | ||
name = "example" | ||
internal = true | ||
load_balancer_type = "network" | ||
|
||
subnet_mapping { | ||
subnet_id = "12345" | ||
} | ||
} | ||
|
||
resource "aws_api_gateway_vpc_link" "example" { | ||
name = "example" | ||
description = "example description" | ||
target_arn = "${aws_lb.example.arn}" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `name` - (Required) The name used to label and identify the VPC link. | ||
* `description` - (Optional) The description of the VPC link. | ||
* `target_arn` - (Required, ForceNew) The ARN of network load balancer of the VPC targeted by the VPC link. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Required, ForceNew) The ARN of a network load balancer in the VPC targeted by the VPC link. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for correct me! |
||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `id` - The identifier of the VpcLink. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any particular reason this should be implemented as 1-1 relationship instead of reflecting the API, i.e. allowing users to assign multiple LBs to one VPC link?