-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13883 from ewbankkit/f-aws_apigatewayv2_api-data-…
…source New Data Sources: aws_apigatewayv2_api(s)
- Loading branch information
Showing
12 changed files
with
719 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
```release-note:new-data-source | ||
aws_apigatewayv2_api | ||
``` | ||
|
||
```release-note:new-data-source | ||
aws_apigatewayv2_apis | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws/arn" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/apigatewayv2/finder" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource" | ||
) | ||
|
||
func dataSourceAwsApiGatewayV2Api() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsAwsApiGatewayV2ApiRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"api_endpoint": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"api_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"api_key_selection_expression": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"cors_configuration": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"allow_credentials": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"allow_headers": { | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: hashStringCaseInsensitive, | ||
}, | ||
"allow_methods": { | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: hashStringCaseInsensitive, | ||
}, | ||
"allow_origins": { | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: hashStringCaseInsensitive, | ||
}, | ||
"expose_headers": { | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: hashStringCaseInsensitive, | ||
}, | ||
"max_age": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"disable_execute_api_endpoint": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"execution_arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"protocol_type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"route_selection_expression": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"tags": tagsSchemaComputed(), | ||
"version": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsAwsApiGatewayV2ApiRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigatewayv2conn | ||
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig | ||
apiID := d.Get("api_id").(string) | ||
|
||
api, err := finder.ApiByID(conn, apiID) | ||
|
||
if tfresource.NotFound(err) { | ||
return fmt.Errorf("no API Gateway v2 API matched; change the search criteria and try again") | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("error reading API Gateway v2 API (%s): %w", apiID, err) | ||
} | ||
|
||
d.SetId(apiID) | ||
|
||
d.Set("api_endpoint", api.ApiEndpoint) | ||
d.Set("api_key_selection_expression", api.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) | ||
if err := d.Set("cors_configuration", flattenApiGateway2CorsConfiguration(api.CorsConfiguration)); err != nil { | ||
return fmt.Errorf("error setting cors_configuration: %w", err) | ||
} | ||
d.Set("description", api.Description) | ||
d.Set("disable_execute_api_endpoint", api.DisableExecuteApiEndpoint) | ||
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", api.Name) | ||
d.Set("protocol_type", api.ProtocolType) | ||
d.Set("route_selection_expression", api.RouteSelectionExpression) | ||
if err := d.Set("tags", keyvaluetags.Apigatewayv2KeyValueTags(api.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { | ||
return fmt.Errorf("error setting tags: %w", err) | ||
} | ||
d.Set("version", api.Version) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccAWSAPIGatewayV2ApiDataSource_Http(t *testing.T) { | ||
dataSourceName := "data.aws_apigatewayv2_api.test" | ||
resourceName := "aws_apigatewayv2_api.test" | ||
rName := acctest.RandomWithPrefix("tf-acc-test") | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: nil, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSAPIGatewayV2ApiDataSourceConfigHttp(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrPair(dataSourceName, "api_endpoint", resourceName, "api_endpoint"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "api_key_selection_expression", resourceName, "api_key_selection_expression"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "cors_configuration.#", resourceName, "cors_configuration.#"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "cors_configuration.0.allow_credentials", resourceName, "cors_configuration.0.allow_credentials"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "cors_configuration.0.allow_headers.#", resourceName, "cors_configuration.0.allow_headers.#"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "cors_configuration.0.allow_methods.#", resourceName, "cors_configuration.0.allow_methods.#"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "cors_configuration.0.allow_origins.#", resourceName, "cors_configuration.0.allow_origins.#"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "cors_configuration.0.expose_headers.#", resourceName, "cors_configuration.0.expose_headers.#"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "cors_configuration.0.max_age", resourceName, "cors_configuration.0.max_age"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "description", resourceName, "description"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "disable_execute_api_endpoint", resourceName, "disable_execute_api_endpoint"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "execution_arn", resourceName, "execution_arn"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "protocol_type", resourceName, "protocol_type"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "route_selection_expression", resourceName, "route_selection_expression"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "tags.%", resourceName, "tags.%"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "tags.Key1", resourceName, "tags.Key1"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "tags.Key2", resourceName, "tags.Key2"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "version", resourceName, "version"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAWSAPIGatewayV2ApiDataSource_WebSocket(t *testing.T) { | ||
dataSourceName := "data.aws_apigatewayv2_api.test" | ||
resourceName := "aws_apigatewayv2_api.test" | ||
rName := acctest.RandomWithPrefix("tf-acc-test") | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: nil, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSAPIGatewayV2ApiDataSourceConfigWebSocket(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrPair(dataSourceName, "api_endpoint", resourceName, "api_endpoint"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "api_key_selection_expression", resourceName, "api_key_selection_expression"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "cors_configuration.#", resourceName, "cors_configuration.#"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "description", resourceName, "description"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "disable_execute_api_endpoint", resourceName, "disable_execute_api_endpoint"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "execution_arn", resourceName, "execution_arn"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "protocol_type", resourceName, "protocol_type"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "route_selection_expression", resourceName, "route_selection_expression"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "tags.%", resourceName, "tags.%"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "tags.Key1", resourceName, "tags.Key1"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "tags.Key2", resourceName, "tags.Key2"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "version", resourceName, "version"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccAWSAPIGatewayV2ApiDataSourceConfigHttp(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_apigatewayv2_api" "test" { | ||
description = "test description" | ||
name = %[1]q | ||
protocol_type = "HTTP" | ||
version = "v1" | ||
cors_configuration { | ||
allow_headers = ["Authorization"] | ||
allow_methods = ["GET", "put"] | ||
allow_origins = ["https://www.example.com"] | ||
} | ||
tags = { | ||
Key1 = "Value1h" | ||
Key2 = "Value2h" | ||
} | ||
} | ||
data "aws_apigatewayv2_api" "test" { | ||
api_id = aws_apigatewayv2_api.test.id | ||
} | ||
`, rName) | ||
} | ||
|
||
func testAccAWSAPIGatewayV2ApiDataSourceConfigWebSocket(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_apigatewayv2_api" "test" { | ||
api_key_selection_expression = "$context.authorizer.usageIdentifierKey" | ||
description = "test description" | ||
name = %[1]q | ||
protocol_type = "WEBSOCKET" | ||
route_selection_expression = "$request.body.service" | ||
version = "v1" | ||
tags = { | ||
Key1 = "Value1ws" | ||
Key2 = "Value2ws" | ||
} | ||
} | ||
data "aws_apigatewayv2_api" "test" { | ||
api_id = aws_apigatewayv2_api.test.id | ||
} | ||
`, rName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/apigatewayv2" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/apigatewayv2/finder" | ||
) | ||
|
||
func dataSourceAwsApiGatewayV2Apis() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsAwsApiGatewayV2ApisRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"ids": { | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: schema.HashString, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"protocol_type": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"tags": tagsSchema(), | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsAwsApiGatewayV2ApisRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigatewayv2conn | ||
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig | ||
|
||
tagsToMatch := keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().IgnoreConfig(ignoreTagsConfig) | ||
|
||
apis, err := finder.Apis(conn, &apigatewayv2.GetApisInput{}) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error reading API Gateway v2 APIs: %w", err) | ||
} | ||
|
||
var ids []*string | ||
|
||
for _, api := range apis { | ||
if v, ok := d.GetOk("name"); ok && v.(string) != aws.StringValue(api.Name) { | ||
continue | ||
} | ||
|
||
if v, ok := d.GetOk("protocol_type"); ok && v.(string) != aws.StringValue(api.ProtocolType) { | ||
continue | ||
} | ||
|
||
if len(tagsToMatch) > 0 && !keyvaluetags.Apigatewayv2KeyValueTags(api.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).ContainsAll(tagsToMatch) { | ||
continue | ||
} | ||
|
||
ids = append(ids, api.ApiId) | ||
} | ||
|
||
d.SetId(meta.(*AWSClient).region) | ||
|
||
if err := d.Set("ids", flattenStringSet(ids)); err != nil { | ||
return fmt.Errorf("error setting ids: %w", err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.