-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
tests/resource/aws_api_gateway_domain_name: Remove hardcoded environment variable handling, create public ACM certificate, improve state value checks #16139
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,118 @@ | ||
package aws | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws/arn" | ||
"github.com/aws/aws-sdk-go/aws/endpoints" | ||
"github.com/aws/aws-sdk-go/service/apigateway" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
// API Gateway Edge-Optimized Domain Name can only be created with ACM Certificates in specific regions. | ||
|
||
// testAccApigatewayEdgeDomainNameRegion is the chosen API Gateway Domain Name testing region | ||
// | ||
// Cached to prevent issues should multiple regions become available. | ||
var testAccApigatewayEdgeDomainNameRegion string | ||
|
||
// testAccProviderApigatewayEdgeDomainName is the API Gateway Domain Name provider instance | ||
// | ||
// This Provider can be used in testing code for API calls without requiring | ||
// the use of saving and referencing specific ProviderFactories instances. | ||
// | ||
// testAccPreCheckApigatewayEdgeDomainName(t) must be called before using this provider instance. | ||
var testAccProviderApigatewayEdgeDomainName *schema.Provider | ||
|
||
// testAccProviderApigatewayEdgeDomainNameConfigure ensures the provider is only configured once | ||
var testAccProviderApigatewayEdgeDomainNameConfigure sync.Once | ||
|
||
// testAccPreCheckApigatewayEdgeDomainName verifies AWS credentials and that API Gateway Domain Name is supported | ||
func testAccPreCheckApigatewayEdgeDomainName(t *testing.T) { | ||
testAccPartitionHasServicePreCheck(apigateway.EndpointsID, t) | ||
|
||
// Since we are outside the scope of the Terraform configuration we must | ||
// call Configure() to properly initialize the provider configuration. | ||
testAccProviderApigatewayEdgeDomainNameConfigure.Do(func() { | ||
testAccProviderApigatewayEdgeDomainName = Provider() | ||
|
||
region := testAccGetApigatewayEdgeDomainNameRegion() | ||
|
||
if region == "" { | ||
t.Skip("API Gateway Domain Name not available in this AWS Partition") | ||
} | ||
|
||
config := map[string]interface{}{ | ||
"region": region, | ||
} | ||
|
||
diags := testAccProviderApigatewayEdgeDomainName.Configure(context.Background(), terraform.NewResourceConfigRaw(config)) | ||
|
||
if diags != nil && diags.HasError() { | ||
for _, d := range diags { | ||
if d.Severity == diag.Error { | ||
t.Fatalf("error configuring API Gateway Domain Name provider: %s", d.Summary) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
|
||
// testAccApigatewayEdgeDomainNameRegionProviderConfig is the Terraform provider configuration for API Gateway Domain Name region testing | ||
// | ||
// Testing API Gateway Domain Name assumes no other provider configurations | ||
// are necessary and overwrites the "aws" provider configuration. | ||
func testAccApigatewayEdgeDomainNameRegionProviderConfig() string { | ||
return testAccRegionalProviderConfig(testAccGetApigatewayEdgeDomainNameRegion()) | ||
} | ||
|
||
// testAccGetApigatewayEdgeDomainNameRegion returns the API Gateway Domain Name region for testing | ||
func testAccGetApigatewayEdgeDomainNameRegion() string { | ||
if testAccApigatewayEdgeDomainNameRegion != "" { | ||
return testAccApigatewayEdgeDomainNameRegion | ||
} | ||
|
||
// AWS Commercial: https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-custom-domains.html | ||
// AWS GovCloud (US) - edge custom domain names not supported: https://docs.aws.amazon.com/govcloud-us/latest/UserGuide/govcloud-abp.html | ||
// AWS China - edge custom domain names not supported: https://docs.amazonaws.cn/en_us/aws/latest/userguide/api-gateway.html | ||
switch testAccGetPartition() { | ||
case endpoints.AwsPartitionID: | ||
testAccApigatewayEdgeDomainNameRegion = endpoints.UsEast1RegionID | ||
} | ||
|
||
return testAccApigatewayEdgeDomainNameRegion | ||
} | ||
|
||
// testAccCheckResourceAttrRegionalARNApigatewayEdgeDomainName ensures the Terraform state exactly matches the expected API Gateway Edge Domain Name format | ||
func testAccCheckResourceAttrRegionalARNApigatewayEdgeDomainName(resourceName, attributeName, arnService string, domain string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
attributeValue := arn.ARN{ | ||
Partition: testAccGetPartition(), | ||
Region: testAccGetApigatewayEdgeDomainNameRegion(), | ||
Resource: fmt.Sprintf("/domainnames/%s", domain), | ||
Service: arnService, | ||
}.String() | ||
|
||
return resource.TestCheckResourceAttr(resourceName, attributeName, attributeValue)(s) | ||
} | ||
} | ||
|
||
// testAccCheckResourceAttrRegionalARNApigatewayRegionalDomainName ensures the Terraform state exactly matches the expected API Gateway Regional Domain Name format | ||
func testAccCheckResourceAttrRegionalARNApigatewayRegionalDomainName(resourceName, attributeName, arnService string, domain string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
attributeValue := arn.ARN{ | ||
Partition: testAccGetPartition(), | ||
Region: testAccGetRegion(), | ||
Resource: fmt.Sprintf("/domainnames/%s", domain), | ||
Service: arnService, | ||
}.String() | ||
|
||
return resource.TestCheckResourceAttr(resourceName, attributeName, attributeValue)(s) | ||
} | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
FYI, in #15258 I have pulled this into a separate function as I will use it in a couple of places: 4a7197b#diff-de07b61dfd6679fbda71d2c219efc2356d9a0757a6366304a5361ee09f86803bR395-R441.
If this PR is merged first I'll refactor
testAccAWSAPIGatewayDomainNameConfig_CertificateArn
to use this new function.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.
Okay cool -- feel free to add it to the contributing guide and create a followup technical debt issue. 👍
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.
Done: #16171.