github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_api_gateway_domain_name_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/apigateway" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSAPIGatewayDomainName_basic(t *testing.T) { 15 var conf apigateway.DomainName 16 17 // Our test cert is for a wildcard on this domain 18 uniqueId := resource.UniqueId() 19 name := fmt.Sprintf("%s.tf-acc.invalid", uniqueId) 20 nameModified := fmt.Sprintf("test-acc.%s.tf-acc.invalid", uniqueId) 21 22 resource.Test(t, resource.TestCase{ 23 PreCheck: func() { testAccPreCheck(t) }, 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckAWSAPIGatewayDomainNameDestroy, 26 Steps: []resource.TestStep{ 27 { 28 Config: testAccAWSAPIGatewayDomainNameConfigCreate(name), 29 Check: resource.ComposeTestCheckFunc( 30 testAccCheckAWSAPIGatewayDomainNameExists("aws_api_gateway_domain_name.test", &conf), 31 resource.TestCheckResourceAttr("aws_api_gateway_domain_name.test", "certificate_body", testAccAWSAPIGatewayCertBody), 32 resource.TestCheckResourceAttr("aws_api_gateway_domain_name.test", "certificate_chain", testAccAWSAPIGatewayCertChain), 33 resource.TestCheckResourceAttr("aws_api_gateway_domain_name.test", "certificate_name", "tf-acc-apigateway-domain-name"), 34 resource.TestCheckResourceAttr("aws_api_gateway_domain_name.test", "certificate_private_key", testAccAWSAPIGatewayCertPrivateKey), 35 resource.TestCheckResourceAttr("aws_api_gateway_domain_name.test", "domain_name", name), 36 resource.TestCheckResourceAttrSet("aws_api_gateway_domain_name.test", "certificate_upload_date"), 37 ), 38 }, 39 { 40 Config: testAccAWSAPIGatewayDomainNameConfigUpdate(name), 41 Check: resource.ComposeTestCheckFunc( 42 testAccCheckAWSAPIGatewayDomainNameExists("aws_api_gateway_domain_name.test", &conf), 43 resource.TestCheckResourceAttr("aws_api_gateway_domain_name.test", "certificate_body", testAccAWSAPIGatewayCertBody), 44 resource.TestCheckResourceAttr("aws_api_gateway_domain_name.test", "certificate_chain", testAccAWSAPIGatewayCertChain), 45 resource.TestCheckResourceAttr("aws_api_gateway_domain_name.test", "certificate_name", "tf-acc-apigateway-domain-name"), 46 resource.TestCheckResourceAttr("aws_api_gateway_domain_name.test", "certificate_private_key", testAccAWSAPIGatewayCertPrivateKey), 47 resource.TestCheckResourceAttr("aws_api_gateway_domain_name.test", "domain_name", nameModified), 48 resource.TestCheckResourceAttrSet("aws_api_gateway_domain_name.test", "certificate_upload_date"), 49 ), 50 }, 51 }, 52 }) 53 } 54 55 func testAccCheckAWSAPIGatewayDomainNameExists(n string, res *apigateway.DomainName) resource.TestCheckFunc { 56 return func(s *terraform.State) error { 57 rs, ok := s.RootModule().Resources[n] 58 if !ok { 59 return fmt.Errorf("Not found: %s", n) 60 } 61 62 if rs.Primary.ID == "" { 63 return fmt.Errorf("No API Gateway DomainName ID is set") 64 } 65 66 conn := testAccProvider.Meta().(*AWSClient).apigateway 67 68 req := &apigateway.GetDomainNameInput{ 69 DomainName: aws.String(rs.Primary.ID), 70 } 71 describe, err := conn.GetDomainName(req) 72 if err != nil { 73 return err 74 } 75 76 if *describe.DomainName != rs.Primary.ID { 77 return fmt.Errorf("APIGateway DomainName not found") 78 } 79 80 *res = *describe 81 82 return nil 83 } 84 } 85 86 func testAccCheckAWSAPIGatewayDomainNameDestroy(s *terraform.State) error { 87 conn := testAccProvider.Meta().(*AWSClient).apigateway 88 89 for _, rs := range s.RootModule().Resources { 90 if rs.Type != "aws_api_gateway_api_key" { 91 continue 92 } 93 94 describe, err := conn.GetDomainNames(&apigateway.GetDomainNamesInput{}) 95 96 if err == nil { 97 if len(describe.Items) != 0 && 98 *describe.Items[0].DomainName == rs.Primary.ID { 99 return fmt.Errorf("API Gateway DomainName still exists") 100 } 101 } 102 103 aws2err, ok := err.(awserr.Error) 104 if !ok { 105 return err 106 } 107 if aws2err.Code() != "NotFoundException" { 108 return err 109 } 110 111 return nil 112 } 113 114 return nil 115 } 116 117 // Expires on August 20, 2026 118 const testAccAWSAPIGatewayCertBody = `-----BEGIN CERTIFICATE----- 119 MIIEfjCCA2agAwIBAgIRAPHiUbRWC/Ff/reiyzqh3wAwDQYJKoZIhvcNAQELBQAw 120 gcsxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJDQTEWMBQGA1UEBxMNUGlyYXRlIEhh 121 cmJvcjEZMBcGA1UECRMQNTg3OSBDb3R0b24gTGluazETMBEGA1UEERMKOTU1NTkt 122 MTIyNzEVMBMGA1UEChMMRXhhbXBsZSwgSW5jMSgwJgYDVQQLEx9EZXBhcnRtZW50 123 IG9mIFRlcnJhZm9ybSBUZXN0aW5nMRowGAYDVQQDExF0Zi1hY2MtY2EuaW52YWxp 124 ZDEKMAgGA1UEBRMBMjAeFw0xNjA4MjIxNjQ1NTZaFw0yNjA4MjAxNjQ1NTZaMIHK 125 MQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDVBpcmF0ZSBIYXJi 126 b3IxGTAXBgNVBAkTEDU4NzkgQ290dG9uIExpbmsxEzARBgNVBBETCjk1NTU5LTEy 127 MjcxFTATBgNVBAoTDEV4YW1wbGUsIEluYzEoMCYGA1UECxMfRGVwYXJ0bWVudCBv 128 ZiBUZXJyYWZvcm0gVGVzdGluZzEZMBcGA1UEAxMQKi50Zi1hY2MuaW52YWxpZDEK 129 MAgGA1UEBRMBMjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAL/xTPoz 130 3tnvkLjthqsMKwWVtE1yi210UnseBYkc2w6iQUXMCsXkw2PZXW6QIW7o1GNjnrE5 131 9jkeEJ2ABEYyor+ooaNZmjJ0dvY1tFerMhsOkQudDBz+eaR13UQvMFc42nFWrS5/ 132 XCInm4HjWblEfw2b/wbGwTPzIN3+zWdt2XTsSzIRUYgjXy/93aLIVhWaYDsP45RM 133 C48qhJKSsYOVH/PSkgw4AfohDXzn2iltTUBFdGZqBufMW4sPp8G8DIOvt01NsXB2 134 QLLVgcMJVegx9xxg50+cf6ILck5Ap/SzkCN7XYk8zWC36/QIkiQ6bMMy0nJtgE0B 135 jwmwnFF+zF/A4oMCAwEAAaNcMFowDgYDVR0PAQH/BAQDAgWgMBkGA1UdJQQSMBAG 136 CCsGAQUFBwMBBgRVHSUAMAwGA1UdEwEB/wQCMAAwHwYDVR0jBBgwFoAU/hWQ+UUh 137 7EiLd0fCsgy1jvSgxkswDQYJKoZIhvcNAQELBQADggEBABnTdSEOqj2+7FTeiSR9 138 iPGQ3swhrmk59j8XUszrlm6MvZX9AeV9gLcE9J0PfFaAcZM0pR+XZBmyZLoBxUgU 139 8Y17yWwAxZo13EwgzTOmebDxm4W+Og4vBxsKS3x/L8D8BKdzBcmVJvggJqTs5Qau 140 ePetAQcD/Tfbq7vY9fNQJ4XQTlF5sN625miijTQcNg/s/H2Fj3FENR3UaFiQDtLQ 141 eatV0KGF/ik8XFHPbFm7XUZfUcDxvoaljglJfXyVttU956j892ZakvAHzhylFRyy 142 /wJnKV7K3zQVCEV0n/dJpXhf7w9PuWHrCp+HZJLMPMtm1IPblVxPNwHhGBG40I+Z 143 FSg= 144 -----END CERTIFICATE----- 145 ` 146 147 // Expires on August 20, 2026 148 const testAccAWSAPIGatewayCertChain = `-----BEGIN CERTIFICATE----- 149 MIIEoDCCA4igAwIBAgIQRR1EBw7bN+mMYtXrrvzLdzANBgkqhkiG9w0BAQsFADCB 150 yzELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1QaXJhdGUgSGFy 151 Ym9yMRkwFwYDVQQJExA1ODc5IENvdHRvbiBMaW5rMRMwEQYDVQQREwo5NTU1OS0x 152 MjI3MRUwEwYDVQQKEwxFeGFtcGxlLCBJbmMxKDAmBgNVBAsTH0RlcGFydG1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CzEfmP 169 gkDeW/2A1/6eSM6C/ym+9zEgZ9YLxPfiQKzN8Ej+EziFiZW3ZBHnWcW9rZt36wFA 170 swF2nIqWa5nkVnAX2aPVfULN1u1BQiv3XZqNNlBk6m9AsgxiZ9jtOY8oQQU3uDYe 171 bs9ttLMKJApbe08sKmPawVlXHqj6qb85Pg4bzzknA+euRee9KIi87eTlWc/Ht+WN 172 Huh16WqokePii7e/PAaiTAe7ubsPav29lzmHq2NooXrs+FljXveQJpBqu6wKKTXe 173 6/5BBsp2474RiZgTbuO2Mbr2nwcoJ9V6iu1M9siCGbbt1nQr 174 -----END CERTIFICATE----- 175 ` 176 177 const testAccAWSAPIGatewayCertPrivateKey = `-----BEGIN RSA PRIVATE KEY----- 178 MIIEpAIBAAKCAQEAv/FM+jPe2e+QuO2GqwwrBZW0TXKLbXRSex4FiRzbDqJBRcwK 179 xeTDY9ldbpAhbujUY2OesTn2OR4QnYAERjKiv6iho1maMnR29jW0V6syGw6RC50M 180 HP55pHXdRC8wVzjacVatLn9cIiebgeNZuUR/DZv/BsbBM/Mg3f7NZ23ZdOxLMhFR 181 iCNfL/3doshWFZpgOw/jlEwLjyqEkpKxg5Uf89KSDDgB+iENfOfaKW1NQEV0ZmoG 182 58xbiw+nwbwMg6+3TU2xcHZAstWBwwlV6DH3HGDnT5x/ogtyTkCn9LOQI3tdiTzN 183 YLfr9AiSJDpswzLScm2ATQGPCbCcUX7MX8DigwIDAQABAoIBAFSRcXQPpJFrDt2b 184 sajtTItCYVV6MVpBVRHvsUqvDwkMjiu9ccWtPDVjENpk4IYoSWOdAc9eFVEnIPTz 185 8W4oYzKEjusU0G6Ih92E3fd+cy4epeNzB2JC8L94OswO6oKThxNGuDjzXlmiD88T 186 p3WMa1pIr/2BVqCX75Q/7qoyaQwtR/anvOes154gRGY70ucsUpEzo3fyfXdCKO/o 187 y1quQ4mmCS50lPCJ3i8JZH2YmGY+xThnXzk7Q9ipjGrHTjinpX8f9tQB2RRwqlA/ 188 uQ+/q6cOKUAehLtriXyr2QAoCzdz1Lfm8L5KQ3EdrxUUe9dhs/a7bKw4N5eHjw/x 189 eBUagsECgYEAyoaIqZ0ur5NQEHuY9Givs6a+ObWLjM18EdfQ7zX7NJRPL3q0jdkm 190 KW+pn2OzoRxhwgaD6WuJJmj3rd3ledkwxoO6Efwo8x2z1u/T43FsknW/X3wJLLga 191 fPU2T9lSHRufBQUAhPYkf4dSqUklHJ4YSMafbCkvSXKGZqGVLQfu1iECgYEA8p9s 192 /5+d0Pcnykau7Hp8a9z9ehHEIF+ce/wb6/6p3fgCP04FLvKqs74p9SrEnyl9uiFx 193 ZC9P2XhmEwNFGA+qKPaRBo4RoGeAYgCKD0BfdaGfmp6WLhO0pSnl4c06bXnvwqbD 194 DhhT6UvMs6m/uSufwtfCGO48yYiY1z6DqBjuHCMCgYBWJyrltHbSu8D4cgucFRiB 195 PPJ5DDCkIhmgYYWA7R7CvEB/OxyppvFj+RtYMYqNg8xWRH1DA7rhOw/5x4ZB8lGc 196 cRbrZbBp033YdkdV3r9IAoz5aoNgoaSq+Yk0KIeU2FYqRXl2FltqYL+aQgJmjR5Z 197 fxz8Xvy9qtlfuWcDM/e24QKBgQDGtH8mk+lCfUkfRuh4UJCaHnGSif5grS2R9ZZA 198 n18rpbThd9qS6reXYgUm/5Hs8KRBzqX5cS4qY4rlw2XRIPMxfU6lWbFh96KToPFx 199 MD1+L5JxpbRFpGnsYvYdCmHxy03r03wojRAcH7JU6o9U7j936hDTLjqmq7LRhid5 200 goFwlQKBgQCGxTWqQu9FJjtmjdM/ARi7CcwWT40NI8ybyebjDtElekILFMUwO+/w 201 M78Me3d5g/oyeS6qCfyK1f7vBb/WhxLBBegaqMGUWbRoKYD0LH5U3zkqZ9ehnDFB 202 vhcEnoxPD9HmdFkRduJscyzVNJ50lMuQ3uk7zwiYuS/Z7shH81xzMQ== 203 -----END RSA PRIVATE KEY----- 204 ` 205 206 func testAccAWSAPIGatewayDomainNameConfigCreate(name string) string { 207 return fmt.Sprintf(` 208 resource "aws_api_gateway_domain_name" "test" { 209 domain_name = "%s" 210 certificate_body = <<EOF 211 %vEOF 212 certificate_chain = <<EOF 213 %vEOF 214 certificate_name = "tf-acc-apigateway-domain-name" 215 certificate_private_key = <<EOF 216 %vEOF 217 } 218 `, name, testAccAWSAPIGatewayCertBody, testAccAWSAPIGatewayCertChain, testAccAWSAPIGatewayCertPrivateKey) 219 } 220 221 func testAccAWSAPIGatewayDomainNameConfigUpdate(name string) string { 222 return fmt.Sprintf(` 223 resource "aws_api_gateway_domain_name" "test" { 224 domain_name = "test-acc.%s" 225 certificate_body = <<EOF 226 %vEOF 227 certificate_chain = <<EOF 228 %vEOF 229 certificate_name = "tf-acc-apigateway-domain-name" 230 certificate_private_key = <<EOF 231 %vEOF 232 } 233 `, name, testAccAWSAPIGatewayCertBody, testAccAWSAPIGatewayCertChain, testAccAWSAPIGatewayCertPrivateKey) 234 }