@@ -34,19 +34,30 @@ var (
3434 credentialsName = "acc-c" + acctest .RandStringFromCharSet (3 , acctest .CharSetAlpha )
3535 credentialsNameUpdated = "acc-c-updated" + acctest .RandStringFromCharSet (3 , acctest .CharSetAlpha )
3636 httpTestName = "acc-h" + acctest .RandStringFromCharSet (3 , acctest .CharSetAlpha )
37- dnsNameHttp = fmt .Sprintf ("tf-%s.runs.onstackit.cloud" , httpTestName )
38- dnsRecordNameHttp = uuid .NewString ()
39- cert , key = makeCertAndKey (testutil .OrganizationId )
37+
38+ // FIX: Reverted to stackit.gg as used in the working old code to avoid reserved domain rejection
39+ dnsNameHttp = fmt .Sprintf ("tf-acc-%s.stackit.gg" , strings .Split (uuid .NewString (), "-" )[0 ])
40+ dnsRecordNameHttp = uuid .NewString ()
41+
42+ // Build the full domain name here so we can use it to sign the certificate
43+ fullDomainNameHttp = fmt .Sprintf ("%s.%s" , dnsRecordNameHttp , dnsNameHttp )
44+
45+ cert , key = makeCertAndKey (testutil .OrganizationId , fullDomainNameHttp )
4046)
4147
4248var (
4349 //go:embed testdata/resource-bucket.tf
4450 resourceBucket string
4551
46- //go:embed testdata/resource-http.tf
47- resourceHttp string
52+ //go:embed testdata/resource-http-base.tf
53+ resourceHttpBase string
54+
55+ //go:embed testdata/resource-http-custom-domain.tf
56+ resourceHttpCustomDomain string
4857)
4958
59+ var resourceHttpFull = resourceHttpBase + "\n " + resourceHttpCustomDomain
60+
5061var testConfigVarsBucket = config.Variables {
5162 "project_id" : config .StringVariable (testutil .ProjectId ),
5263 "bucket_name" : config .StringVariable (bucketName ),
@@ -90,7 +101,7 @@ func configVarsHttpUpdated() config.Variables {
90101 return updatedConfig
91102}
92103
93- func makeCertAndKey (organization string ) (cert , key []byte ) {
104+ func makeCertAndKey (organization , domain string ) (cert , key []byte ) {
94105 privateKey , err := rsa .GenerateKey (cryptoRand .Reader , 2048 )
95106 if err != nil {
96107 fmt .Printf ("failed to generate key: %s" , err .Error ())
@@ -100,10 +111,11 @@ func makeCertAndKey(organization string) (cert, key []byte) {
100111 Issuer : pkix.Name {CommonName : organization },
101112 Subject : pkix.Name {
102113 Organization : []string {organization },
114+ CommonName : domain , // Required by most modern TLS validations
103115 },
104- NotBefore : time . Now () ,
105- NotAfter : time .Now (). Add ( time . Hour ),
106-
116+ DNSNames : [] string { domain } ,
117+ NotBefore : time .Now (),
118+ NotAfter : time . Now (). Add ( time . Hour ),
107119 KeyUsage : x509 .KeyUsageDigitalSignature | x509 .KeyUsageCertSign ,
108120 ExtKeyUsage : []x509.ExtKeyUsage {x509 .ExtKeyUsageServerAuth },
109121 BasicConstraintsValid : true ,
@@ -129,14 +141,13 @@ func makeCertAndKey(organization string) (cert, key []byte) {
129141}
130142
131143func TestAccCDNDistributionHttp (t * testing.T ) {
132- fullDomainName := fmt .Sprintf ("%s.%s" , testutil .ConvertConfigVariable (testConfigVarsHttp ["dns_record_name" ]), testutil .ConvertConfigVariable (testConfigVarsHttp ["dns_name" ]))
133144 resource .Test (t , resource.TestCase {
134145 ProtoV6ProviderFactories : testutil .TestAccProtoV6ProviderFactories ,
135146 CheckDestroy : testAccCheckCDNDistributionDestroy ,
136147 Steps : []resource.TestStep {
137- // Distribution Create
148+ // Distribution Create (Only Base config)
138149 {
139- Config : testutil .CdnProviderConfig () + "\n " + resourceHttp ,
150+ Config : testutil .CdnProviderConfig () + "\n " + resourceHttpBase ,
140151 ConfigVariables : testConfigVarsHttp ,
141152 Check : resource .ComposeAggregateTestCheckFunc (
142153 resource .TestCheckResourceAttrSet ("stackit_cdn_distribution.distribution" , "distribution_id" ),
@@ -166,22 +177,22 @@ func TestAccCDNDistributionHttp(t *testing.T) {
166177 resource .TestCheckResourceAttr ("stackit_cdn_distribution.distribution" , "status" , "ACTIVE" ),
167178 ),
168179 },
169- // Wait step, that confirms the CNAME record has "propagated"
180+ // Wait step, confirms the CNAME record has "propagated" before trying to add the custom domain
170181 {
171- Config : testutil .CdnProviderConfig () + "\n " + resourceHttp ,
182+ Config : testutil .CdnProviderConfig () + "\n " + resourceHttpBase ,
172183 ConfigVariables : testConfigVarsHttp ,
173184 Check : func (_ * terraform.State ) error {
174- _ , err := blockUntilDomainResolves (fullDomainName )
185+ _ , err := blockUntilDomainResolves (fullDomainNameHttp )
175186 return err
176187 },
177188 },
178- // Custom Domain Create
189+ // Custom Domain Create (Now using Full config)
179190 {
180- Config : testutil .CdnProviderConfig () + "\n " + resourceHttp ,
191+ Config : testutil .CdnProviderConfig () + "\n " + resourceHttpFull ,
181192 ConfigVariables : testConfigVarsHttp ,
182193 Check : resource .ComposeAggregateTestCheckFunc (
183194 resource .TestCheckResourceAttr ("stackit_cdn_custom_domain.custom_domain" , "status" , "ACTIVE" ),
184- resource .TestCheckResourceAttr ("stackit_cdn_custom_domain.custom_domain" , "name" , fullDomainName ),
195+ resource .TestCheckResourceAttr ("stackit_cdn_custom_domain.custom_domain" , "name" , fullDomainNameHttp ),
185196 resource .TestCheckResourceAttr ("stackit_cdn_custom_domain.custom_domain" , "certificate.version" , "1" ),
186197 resource .TestCheckResourceAttrPair ("stackit_cdn_distribution.distribution" , "distribution_id" , "stackit_cdn_custom_domain.custom_domain" , "distribution_id" ),
187198 resource .TestCheckResourceAttrPair ("stackit_cdn_distribution.distribution" , "project_id" , "stackit_cdn_custom_domain.custom_domain" , "project_id" ),
@@ -235,15 +246,15 @@ func TestAccCDNDistributionHttp(t *testing.T) {
235246 },
236247 // Data Source
237248 {
238- Config : testutil .CdnProviderConfig () + "\n " + resourceHttp ,
249+ Config : testutil .CdnProviderConfig () + "\n " + resourceHttpFull ,
239250 ConfigVariables : testConfigVarsHttp ,
240251 Check : resource .ComposeAggregateTestCheckFunc (
241252 resource .TestCheckResourceAttrSet ("data.stackit_cdn_distribution.distribution" , "distribution_id" ),
242253 resource .TestCheckResourceAttrSet ("data.stackit_cdn_distribution.distribution" , "created_at" ),
243254 resource .TestCheckResourceAttrSet ("data.stackit_cdn_distribution.distribution" , "updated_at" ),
244255 resource .TestCheckResourceAttr ("data.stackit_cdn_distribution.distribution" , "domains.#" , "2" ),
245256 resource .TestCheckResourceAttrSet ("data.stackit_cdn_distribution.distribution" , "domains.0.name" ),
246- resource .TestCheckResourceAttr ("data.stackit_cdn_distribution.distribution" , "domains.1.name" , fullDomainName ),
257+ resource .TestCheckResourceAttr ("data.stackit_cdn_distribution.distribution" , "domains.1.name" , fullDomainNameHttp ),
247258 resource .TestCheckResourceAttr ("data.stackit_cdn_distribution.distribution" , "domains.0.status" , "ACTIVE" ),
248259 resource .TestCheckResourceAttr ("data.stackit_cdn_distribution.distribution" , "domains.1.status" , "ACTIVE" ),
249260 resource .TestCheckResourceAttr ("data.stackit_cdn_distribution.distribution" , "domains.0.type" , "managed" ),
@@ -268,22 +279,22 @@ func TestAccCDNDistributionHttp(t *testing.T) {
268279 resource .TestCheckResourceAttr ("data.stackit_cdn_distribution.distribution" , "status" , "ACTIVE" ),
269280
270281 resource .TestCheckResourceAttr ("data.stackit_cdn_custom_domain.custom_domain" , "status" , "ACTIVE" ),
271- resource .TestCheckResourceAttr ("data.stackit_cdn_custom_domain.custom_domain" , "name" , fullDomainName ),
282+ resource .TestCheckResourceAttr ("data.stackit_cdn_custom_domain.custom_domain" , "name" , fullDomainNameHttp ),
272283 resource .TestCheckResourceAttr ("data.stackit_cdn_custom_domain.custom_domain" , "certificate.version" , "1" ),
273284 resource .TestCheckResourceAttrPair ("stackit_cdn_distribution.distribution" , "distribution_id" , "stackit_cdn_custom_domain.custom_domain" , "distribution_id" ),
274285 ),
275286 },
276287 // Update
277288 {
278- Config : testutil .CdnProviderConfig () + "\n " + resourceHttp ,
289+ Config : testutil .CdnProviderConfig () + "\n " + resourceHttpFull ,
279290 ConfigVariables : configVarsHttpUpdated (),
280291 Check : resource .ComposeAggregateTestCheckFunc (
281292 resource .TestCheckResourceAttrSet ("stackit_cdn_distribution.distribution" , "distribution_id" ),
282293 resource .TestCheckResourceAttrSet ("stackit_cdn_distribution.distribution" , "created_at" ),
283294 resource .TestCheckResourceAttrSet ("stackit_cdn_distribution.distribution" , "updated_at" ),
284295 resource .TestCheckResourceAttr ("stackit_cdn_distribution.distribution" , "domains.#" , "2" ),
285296 resource .TestCheckResourceAttrSet ("stackit_cdn_distribution.distribution" , "domains.0.name" ),
286- resource .TestCheckResourceAttr ("stackit_cdn_distribution.distribution" , "domains.1.name" , fullDomainName ),
297+ resource .TestCheckResourceAttr ("stackit_cdn_distribution.distribution" , "domains.1.name" , fullDomainNameHttp ),
287298 resource .TestCheckResourceAttr ("stackit_cdn_distribution.distribution" , "domains.0.status" , "ACTIVE" ),
288299 resource .TestCheckResourceAttr ("stackit_cdn_distribution.distribution" , "domains.1.status" , "ACTIVE" ),
289300 resource .TestCheckResourceAttr ("stackit_cdn_distribution.distribution" , "domains.0.type" , "managed" ),
@@ -309,7 +320,7 @@ func TestAccCDNDistributionHttp(t *testing.T) {
309320 ),
310321
311322 resource .TestCheckResourceAttr ("stackit_cdn_custom_domain.custom_domain" , "status" , "ACTIVE" ),
312- resource .TestCheckResourceAttr ("stackit_cdn_custom_domain.custom_domain" , "name" , fullDomainName ),
323+ resource .TestCheckResourceAttr ("stackit_cdn_custom_domain.custom_domain" , "name" , fullDomainNameHttp ),
313324 resource .TestCheckResourceAttr ("stackit_cdn_custom_domain.custom_domain" , "certificate.version" , "1" ),
314325 resource .TestCheckResourceAttrPair ("stackit_cdn_distribution.distribution" , "distribution_id" , "stackit_cdn_custom_domain.custom_domain" , "distribution_id" ),
315326 resource .TestCheckResourceAttrPair ("stackit_cdn_distribution.distribution" , "project_id" , "stackit_cdn_custom_domain.custom_domain" , "project_id" ),
@@ -490,10 +501,10 @@ func testAccCheckCDNDistributionDestroy(s *terraform.State) error {
490501
491502const (
492503 recordCheckInterval time.Duration = 3 * time .Second
493- recordCheckAttempts = 100 // wait up to 5 minutes for record to be come available (normally takes less than 2 minutes)
504+ recordCheckAttempts = 100 // wait up to 5 minutes for record to become available (normally takes less than 2 minutes)
494505)
495506
496- func blockUntilDomainResolves (domain string ) (string , error ) {
507+ func blockUntilDomainResolves (domain string ) (net. IP , error ) {
497508 // Create a custom resolver that bypasses the local system DNS settings/cache
498509 // and queries Google DNS (8.8.8.8) directly.
499510 r := & net.Resolver {
@@ -506,17 +517,23 @@ func blockUntilDomainResolves(domain string) (string, error) {
506517 return d .DialContext (ctx , network , "8.8.8.8:53" )
507518 },
508519 }
520+
509521 // wait until it becomes ready
510- isReady := func () (string , error ) {
522+ isReady := func () (net. IP , error ) {
511523 // Use a context for the individual query timeout
512524 ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
513525 defer cancel ()
514526
515- cname , err := r .LookupCNAME (ctx , domain )
527+ ips , err := r .LookupIP (ctx , "ip" , domain )
516528 if err != nil {
517- return "" , fmt .Errorf ("error looking up CNAME for domain %s: %w" , domain , err )
529+ return nil , fmt .Errorf ("error looking up IP for domain %s: %w" , domain , err )
530+ }
531+ for _ , ip := range ips {
532+ if ip .String () != "<nil>" {
533+ return ip , nil
534+ }
518535 }
519- return cname , nil
536+ return nil , fmt . Errorf ( "no IP for domain: %v" , domain )
520537 }
521538
522539 return retry (recordCheckAttempts , recordCheckInterval , isReady )
@@ -525,7 +542,7 @@ func blockUntilDomainResolves(domain string) (string, error) {
525542func retry [T any ](attempts int , sleep time.Duration , f func () (T , error )) (T , error ) {
526543 var zero T
527544 var errOuter error
528- for range attempts {
545+ for i := 0 ; i < attempts ; i ++ {
529546 dist , err := f ()
530547 if err == nil {
531548 return dist , nil
0 commit comments