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

New resource: aws_apigatewayv2_api #8842

Merged
merged 36 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
72a2956
initial addition of v2 definition
mbonig Jan 6, 2019
f42be24
can create and delete
mbonig Jan 6, 2019
249b3dc
fixing tests
mbonig Jan 7, 2019
913054d
WIP on additional v2 resources
mbonig Jan 8, 2019
284758a
cleaning out some files
mbonig Jan 9, 2019
ea79089
adding some routes
mbonig Jan 10, 2019
4eefa8b
Get tests to compile after rebase.
ewbankkit May 31, 2019
8560c73
Rename files to match CloudHSM v2 file naming.
ewbankkit May 31, 2019
cd4e42a
Rename resource methods to match CloudHSM v2 resource method naming.
ewbankkit May 31, 2019
a194329
Move 'aws_api_gateway_v2_route' to its own PR.
Jun 3, 2019
87f160c
Add 'aws_api_gateway_v2_api' documentation.
Jun 3, 2019
c4f5315
Get 'aws_api_gateway_v2_api' acceptance tests passing.
Jun 3, 2019
72aabfe
More anonynous API ID for import example.
Jun 5, 2019
b20b40b
Update resource name in tests.
Jun 5, 2019
9fb6f56
Better acceptance tests when all attributes are set.
Jun 6, 2019
ccdcce9
API Gateway v2 API tags.
Jul 1, 2019
d7be1a6
Add test sweeper.
ewbankkit Jul 3, 2019
7ca0d45
Test API ARN in acceptance tests.
ewbankkit Jul 5, 2019
fa953ca
Minor enhancement to error message.
ewbankkit Jul 19, 2019
d5cbea2
Add 'execution_arn' attribute to 'aws_api_gateway2_api' resource.
ewbankkit Aug 11, 2019
f1ec286
Terraform Plugin SDK migration.
ewbankkit Oct 5, 2019
ef40c51
Add API Gateway v2 list tags code generation.
ewbankkit Oct 5, 2019
086680d
Use new internal/keyvaluetags functionality.
ewbankkit Oct 5, 2019
c1adaa3
Replace 'testAccMatchResourceAttrAnonymousRegionalARN' with 'testAccM…
Nov 5, 2019
e3bf1e1
Add 'subcategory'.
ewbankkit Nov 22, 2019
e00849f
r/aws_api_gateway2_api: Tag-on-create.
ewbankkit Dec 5, 2019
d8c461d
Fix go.mod/go.sum conflicts.
Feb 19, 2020
69ee27e
Fix website documentation errors.
Feb 19, 2020
d4054d4
Add TestAccAWSAPIGateway2Api_disappears acceptance test.
Feb 19, 2020
fd5581c
Clean up function names - https://github.com/terraform-providers/terr…
ewbankkit Mar 9, 2020
52f6b9a
Correct test check function name.
ewbankkit Mar 9, 2020
3349d52
Rename resource to 'aws_api_gatewayv2'.
ewbankkit Mar 10, 2020
abb1eb3
Continue with sweep after any individual API fails deletion and captu…
ewbankkit Mar 10, 2020
745c087
Basic support for HTTP APIs - No new attributes yet.
ewbankkit Mar 11, 2020
38f5b02
Correct the dropdown section title.
Mar 11, 2020
46a3572
Rename resource to 'aws_apigatewayv2_api'.
ewbankkit Mar 12, 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
3 changes: 3 additions & 0 deletions aws/internal/keyvaluetags/generators/listtags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var serviceNames = []string{
"acm",
"acmpca",
"amplify",
"apigatewayv2",
"appmesh",
"appstream",
"appsync",
Expand Down Expand Up @@ -212,6 +213,8 @@ func ServiceListTagsFunction(serviceName string) string {
return "ListTagsForCertificate"
case "acmpca":
return "ListTags"
case "apigatewayv2":
return "GetTags"
case "backup":
return "ListTags"
case "cloudhsmv2":
Expand Down
18 changes: 18 additions & 0 deletions aws/internal/keyvaluetags/list_tags_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ func Provider() terraform.ResourceProvider {
"aws_api_gateway_usage_plan": resourceAwsApiGatewayUsagePlan(),
"aws_api_gateway_usage_plan_key": resourceAwsApiGatewayUsagePlanKey(),
"aws_api_gateway_vpc_link": resourceAwsApiGatewayVpcLink(),
"aws_apigatewayv2_api": resourceAwsApiGatewayV2Api(),
"aws_app_cookie_stickiness_policy": resourceAwsAppCookieStickinessPolicy(),
"aws_appautoscaling_target": resourceAwsAppautoscalingTarget(),
"aws_appautoscaling_policy": resourceAwsAppautoscalingPolicy(),
Expand Down
214 changes: 214 additions & 0 deletions aws/resource_aws_apigatewayv2_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/apigatewayv2"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsApiGatewayV2Api() *schema.Resource {
return &schema.Resource{
Create: resourceAwsApiGatewayV2ApiCreate,
Read: resourceAwsApiGatewayV2ApiRead,
Update: resourceAwsApiGatewayV2ApiUpdate,
Delete: resourceAwsApiGatewayV2ApiDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"api_endpoint": {
Type: schema.TypeString,
Computed: true,
},
"api_key_selection_expression": {
Type: schema.TypeString,
Optional: true,
Default: "$request.header.x-api-key",
ValidateFunc: validation.StringInSlice([]string{
"$context.authorizer.usageIdentifierKey",
"$request.header.x-api-key",
}, false),
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(0, 1024),
},
"execution_arn": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 128),
},
"protocol_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
apigatewayv2.ProtocolTypeHttp,
apigatewayv2.ProtocolTypeWebsocket,
}, false),
},
"route_selection_expression": {
Type: schema.TypeString,
Optional: true,
Default: "$request.method $request.path",
},
"tags": tagsSchema(),
"version": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 64),
},
},
}
}

func resourceAwsApiGatewayV2ApiCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigatewayv2conn

protocolType := d.Get("protocol_type").(string)
req := &apigatewayv2.CreateApiInput{
Name: aws.String(d.Get("name").(string)),
ProtocolType: aws.String(protocolType),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().Apigatewayv2Tags(),
}
if v, ok := d.GetOk("api_key_selection_expression"); ok {
req.ApiKeySelectionExpression = aws.String(v.(string))
}
if v, ok := d.GetOk("description"); ok {
req.Description = aws.String(v.(string))
}
if v, ok := d.GetOk("route_selection_expression"); ok {
req.RouteSelectionExpression = aws.String(v.(string))
}
if v, ok := d.GetOk("version"); ok {
req.Version = aws.String(v.(string))
}

log.Printf("[DEBUG] Creating API Gateway v2 API: %s", req)
resp, err := conn.CreateApi(req)
if err != nil {
return fmt.Errorf("error creating API Gateway v2 API: %s", err)
}

d.SetId(aws.StringValue(resp.ApiId))

return resourceAwsApiGatewayV2ApiRead(d, meta)
}

func resourceAwsApiGatewayV2ApiRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigatewayv2conn

resp, err := conn.GetApi(&apigatewayv2.GetApiInput{
ApiId: aws.String(d.Id()),
})
if isAWSErr(err, apigatewayv2.ErrCodeNotFoundException, "") {
log.Printf("[WARN] API Gateway v2 API (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("error reading API Gateway v2 API (%s): %s", d.Id(), err)
}

d.Set("api_endpoint", resp.ApiEndpoint)
d.Set("api_key_selection_expression", resp.ApiKeySelectionExpression)
apiArn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "apigateway",
Region: meta.(*AWSClient).region,
Resource: fmt.Sprintf("/apis/%s", d.Id()),
}.String()
d.Set("arn", apiArn)
d.Set("description", resp.Description)
executionArn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "execute-api",
Region: meta.(*AWSClient).region,
AccountID: meta.(*AWSClient).accountid,
Resource: d.Id(),
}.String()
d.Set("execution_arn", executionArn)
d.Set("name", resp.Name)
d.Set("protocol_type", resp.ProtocolType)
d.Set("route_selection_expression", resp.RouteSelectionExpression)
if err := d.Set("tags", keyvaluetags.Apigatewayv2KeyValueTags(resp.Tags).IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}
d.Set("version", resp.Version)

return nil
}

func resourceAwsApiGatewayV2ApiUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigatewayv2conn

if d.HasChanges("api_key_selection_expression", "description", "name", "route_selection_expression", "version") {
req := &apigatewayv2.UpdateApiInput{
ApiId: aws.String(d.Id()),
}

if d.HasChange("api_key_selection_expression") {
req.ApiKeySelectionExpression = aws.String(d.Get("api_key_selection_expression").(string))
}
if d.HasChange("description") {
req.Description = aws.String(d.Get("description").(string))
}
if d.HasChange("name") {
req.Name = aws.String(d.Get("name").(string))
}
if d.HasChange("route_selection_expression") {
req.RouteSelectionExpression = aws.String(d.Get("route_selection_expression").(string))
}
if d.HasChange("version") {
req.Version = aws.String(d.Get("version").(string))
}

log.Printf("[DEBUG] Updating API Gateway v2 API: %s", req)
_, err := conn.UpdateApi(req)
if err != nil {
return fmt.Errorf("error updating API Gateway v2 API (%s): %s", d.Id(), err)
}
}

if d.HasChange("tags") {
o, n := d.GetChange("tags")
if err := keyvaluetags.Apigatewayv2UpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating API Gateway v2 API (%s) tags: %s", d.Id(), err)
}
}

return resourceAwsApiGatewayV2ApiRead(d, meta)
}

func resourceAwsApiGatewayV2ApiDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigatewayv2conn

log.Printf("[DEBUG] Deleting API Gateway v2 API (%s)", d.Id())
_, err := conn.DeleteApi(&apigatewayv2.DeleteApiInput{
ApiId: aws.String(d.Id()),
})
if isAWSErr(err, apigatewayv2.ErrCodeNotFoundException, "") {
return nil
}
if err != nil {
return fmt.Errorf("error deleting API Gateway v2 API (%s): %s", d.Id(), err)
}

return nil
}
Loading