provider/aws: aws_api_gateway_domain_name resource implementation

API Gateway allows users to "claim" a domain name for use as a custom
hostname for deployed API endpoints. The domain name resource just claims
the domain name; a user would then use a "base path mapping" resource
(to be implemented in a later commit) to map a particular API to a
particular path prefix on that domain.

The acceptance tests contain some TLS certificates that expire in 2026;
we'll need to generate some more certificates before we get there.
This commit is contained in:
Jarrod Jackson 2016-08-21 09:19:48 -07:00 committed by Martin Atkins
parent e37dbefd90
commit 7010973c5a
3 changed files with 394 additions and 0 deletions

View File

@ -165,6 +165,7 @@ func Provider() terraform.ResourceProvider {
"aws_api_gateway_api_key": resourceAwsApiGatewayApiKey(),
"aws_api_gateway_authorizer": resourceAwsApiGatewayAuthorizer(),
"aws_api_gateway_deployment": resourceAwsApiGatewayDeployment(),
"aws_api_gateway_domain_name": resourceAwsApiGatewayDomainName(),
"aws_api_gateway_integration": resourceAwsApiGatewayIntegration(),
"aws_api_gateway_integration_response": resourceAwsApiGatewayIntegrationResponse(),
"aws_api_gateway_method": resourceAwsApiGatewayMethod(),

View File

@ -0,0 +1,182 @@
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsApiGatewayDomainName() *schema.Resource {
return &schema.Resource{
Create: resourceAwsApiGatewayDomainNameCreate,
Read: resourceAwsApiGatewayDomainNameRead,
Update: resourceAwsApiGatewayDomainNameUpdate,
Delete: resourceAwsApiGatewayDomainNameDelete,
Schema: map[string]*schema.Schema{
"certificate_body": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"certificate_chain": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"certificate_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"certificate_private_key": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"domain_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"cloudfront_domain_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"cloudfront_zone_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsApiGatewayDomainNameCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Creating API Gateway Domain Name")
domainName, err := conn.CreateDomainName(&apigateway.CreateDomainNameInput{
CertificateBody: aws.String(d.Get("certificate_body").(string)),
CertificateChain: aws.String(d.Get("certificate_chain").(string)),
CertificateName: aws.String(d.Get("certificate_name").(string)),
CertificatePrivateKey: aws.String(d.Get("certificate_private_key").(string)),
DomainName: aws.String(d.Get("domain_name").(string)),
})
if err != nil {
return fmt.Errorf("Error creating API Gateway Domain Name: %s", err)
}
d.SetId(*domainName.DomainName)
d.Set("cloudfront_domain_name", *domainName.DistributionDomainName)
d.Set("cloudfront_zone_id", cloudFrontRoute53ZoneID)
return resourceAwsApiGatewayDomainNameRead(d, meta)
}
func resourceAwsApiGatewayDomainNameRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Reading API Gateway Domain Name %s", d.Id())
domainName, err := conn.GetDomainName(&apigateway.GetDomainNameInput{
DomainName: aws.String(d.Id()),
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
log.Printf("[WARN] API gateway domain name %s has vanished\n", d.Id())
d.SetId("")
return nil
}
return err
}
d.Set("certificate_name", domainName.CertificateName)
d.Set("certificate_upload_date", domainName.CertificateUploadDate)
d.Set("cloudfront_domain_name", domainName.DistributionDomainName)
d.Set("domain_name", domainName.DomainName)
return nil
}
func resourceAwsApiGatewayDomainNameUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation {
operations := make([]*apigateway.PatchOperation, 0)
if d.HasChange("certificate_body") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Path: aws.String("/certificate_body"),
Value: aws.String(d.Get("certificate_body").(string)),
})
}
if d.HasChange("certificate_chain") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Path: aws.String("/certificate_chain"),
Value: aws.String(d.Get("certificate_chain").(string)),
})
}
if d.HasChange("certificate_name") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Path: aws.String("/certificate_name"),
Value: aws.String(d.Get("certificate_name").(string)),
})
}
if d.HasChange("certificate_private_key") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Path: aws.String("/certificate_private_key"),
Value: aws.String(d.Get("certificate_private_key").(string)),
})
}
return operations
}
func resourceAwsApiGatewayDomainNameUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Updating API Gateway Domain Name %s", d.Id())
_, err := conn.UpdateDomainName(&apigateway.UpdateDomainNameInput{
DomainName: aws.String(d.Id()),
PatchOperations: resourceAwsApiGatewayDomainNameUpdateOperations(d),
})
if err != nil {
return err
}
return resourceAwsApiGatewayDomainNameRead(d, meta)
}
func resourceAwsApiGatewayDomainNameDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Deleting API Gateway Domain Name: %s", d.Id())
return resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err := conn.DeleteDomainName(&apigateway.DeleteDomainNameInput{
DomainName: aws.String(d.Id()),
})
if err == nil {
return nil
}
if apigatewayErr, ok := err.(awserr.Error); ok && apigatewayErr.Code() == "NotFoundException" {
return nil
}
return resource.NonRetryableError(err)
})
}

View File

@ -0,0 +1,211 @@
package aws
import (
"fmt"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccAWSAPIGatewayDomainName_basic(t *testing.T) {
var conf apigateway.DomainName
// Our test cert is for a wildcard on this domain
name := fmt.Sprintf("%s.tf-acc.invalid", resource.UniqueId())
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAPIGatewayDomainNameDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSAPIGatewayDomainNameConfigCreate(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayDomainNameExists("aws_api_gateway_domain_name.test", &conf),
resource.TestCheckResourceAttr(
"aws_api_gateway_domain_name.test", "certificate_body", testAccAWSAPIGatewayCertBody,
),
resource.TestCheckResourceAttr(
"aws_api_gateway_domain_name.test", "certificate_chain", testAccAWSAPIGatewayCertChain,
),
resource.TestCheckResourceAttr(
"aws_api_gateway_domain_name.test", "certificate_name", "tf-acc-apigateway-domain-name",
),
resource.TestCheckResourceAttr(
"aws_api_gateway_domain_name.test", "certificate_private_key", testAccAWSAPIGatewayCertPrivateKey,
),
resource.TestCheckResourceAttr(
"aws_api_gateway_domain_name.test", "domain_name", name,
),
),
},
},
})
}
func testAccCheckAWSAPIGatewayDomainNameExists(n string, res *apigateway.DomainName) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No API Gateway DomainName ID is set")
}
conn := testAccProvider.Meta().(*AWSClient).apigateway
req := &apigateway.GetDomainNameInput{
DomainName: aws.String(rs.Primary.ID),
}
describe, err := conn.GetDomainName(req)
if err != nil {
return err
}
if *describe.DomainName != rs.Primary.ID {
return fmt.Errorf("APIGateway DomainName not found")
}
*res = *describe
return nil
}
}
func testAccCheckAWSAPIGatewayDomainNameDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).apigateway
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_api_gateway_api_key" {
continue
}
describe, err := conn.GetDomainNames(&apigateway.GetDomainNamesInput{})
if err == nil {
if len(describe.Items) != 0 &&
*describe.Items[0].DomainName == rs.Primary.ID {
return fmt.Errorf("API Gateway DomainName still exists")
}
}
aws2err, ok := err.(awserr.Error)
if !ok {
return err
}
if aws2err.Code() != "NotFoundException" {
return err
}
return nil
}
return nil
}
// Expires on August 20, 2026
const testAccAWSAPIGatewayCertBody = `-----BEGIN CERTIFICATE-----
MIIEfjCCA2agAwIBAgIRAPHiUbRWC/Ff/reiyzqh3wAwDQYJKoZIhvcNAQELBQAw
gcsxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJDQTEWMBQGA1UEBxMNUGlyYXRlIEhh
cmJvcjEZMBcGA1UECRMQNTg3OSBDb3R0b24gTGluazETMBEGA1UEERMKOTU1NTkt
MTIyNzEVMBMGA1UEChMMRXhhbXBsZSwgSW5jMSgwJgYDVQQLEx9EZXBhcnRtZW50
IG9mIFRlcnJhZm9ybSBUZXN0aW5nMRowGAYDVQQDExF0Zi1hY2MtY2EuaW52YWxp
ZDEKMAgGA1UEBRMBMjAeFw0xNjA4MjIxNjQ1NTZaFw0yNjA4MjAxNjQ1NTZaMIHK
MQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDVBpcmF0ZSBIYXJi
b3IxGTAXBgNVBAkTEDU4NzkgQ290dG9uIExpbmsxEzARBgNVBBETCjk1NTU5LTEy
MjcxFTATBgNVBAoTDEV4YW1wbGUsIEluYzEoMCYGA1UECxMfRGVwYXJ0bWVudCBv
ZiBUZXJyYWZvcm0gVGVzdGluZzEZMBcGA1UEAxMQKi50Zi1hY2MuaW52YWxpZDEK
MAgGA1UEBRMBMjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAL/xTPoz
3tnvkLjthqsMKwWVtE1yi210UnseBYkc2w6iQUXMCsXkw2PZXW6QIW7o1GNjnrE5
9jkeEJ2ABEYyor+ooaNZmjJ0dvY1tFerMhsOkQudDBz+eaR13UQvMFc42nFWrS5/
XCInm4HjWblEfw2b/wbGwTPzIN3+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m
KW+pn2OzoRxhwgaD6WuJJmj3rd3ledkwxoO6Efwo8x2z1u/T43FsknW/X3wJLLga
fPU2T9lSHRufBQUAhPYkf4dSqUklHJ4YSMafbCkvSXKGZqGVLQfu1iECgYEA8p9s
/5+d0Pcnykau7Hp8a9z9ehHEIF+ce/wb6/6p3fgCP04FLvKqs74p9SrEnyl9uiFx
ZC9P2XhmEwNFGA+qKPaRBo4RoGeAYgCKD0BfdaGfmp6WLhO0pSnl4c06bXnvwqbD
DhhT6UvMs6m/uSufwtfCGO48yYiY1z6DqBjuHCMCgYBWJyrltHbSu8D4cgucFRiB
PPJ5DDCkIhmgYYWA7R7CvEB/OxyppvFj+RtYMYqNg8xWRH1DA7rhOw/5x4ZB8lGc
cRbrZbBp033YdkdV3r9IAoz5aoNgoaSq+Yk0KIeU2FYqRXl2FltqYL+aQgJmjR5Z
fxz8Xvy9qtlfuWcDM/e24QKBgQDGtH8mk+lCfUkfRuh4UJCaHnGSif5grS2R9ZZA
n18rpbThd9qS6reXYgUm/5Hs8KRBzqX5cS4qY4rlw2XRIPMxfU6lWbFh96KToPFx
MD1+L5JxpbRFpGnsYvYdCmHxy03r03wojRAcH7JU6o9U7j936hDTLjqmq7LRhid5
goFwlQKBgQCGxTWqQu9FJjtmjdM/ARi7CcwWT40NI8ybyebjDtElekILFMUwO+/w
M78Me3d5g/oyeS6qCfyK1f7vBb/WhxLBBegaqMGUWbRoKYD0LH5U3zkqZ9ehnDFB
vhcEnoxPD9HmdFkRduJscyzVNJ50lMuQ3uk7zwiYuS/Z7shH81xzMQ==
-----END RSA PRIVATE KEY-----
`
func testAccAWSAPIGatewayDomainNameConfigCreate(name string) string {
return fmt.Sprintf(`
resource "aws_api_gateway_domain_name" "test" {
domain_name = "%s"
certificate_body = "%v"
certificate_chain = "%v"
certificate_name = "tf-acc-apigateway-domain-name"
certificate_private_key = "%v"
}
`, name, testAccAWSAPIGatewayCertBody, testAccAWSAPIGatewayCertChain, testAccAWSAPIGatewayCertPrivateKey)
}