@@ -34,19 +34,31 @@ 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+ // Pass the full domain to the certificate generation
46+ cert , key = makeCertAndKey (testutil .OrganizationId , fullDomainNameHttp )
4047)
4148
4249var (
4350 //go:embed testdata/resource-bucket.tf
4451 resourceBucket string
4552
46- //go:embed testdata/resource-http.tf
47- resourceHttp string
53+ //go:embed testdata/resource-http-base.tf
54+ resourceHttpBase string
55+
56+ //go:embed testdata/resource-http-custom-domain.tf
57+ resourceHttpCustomDomain string
4858)
4959
60+ var resourceHttpFull = resourceHttpBase + "\n " + resourceHttpCustomDomain
61+
5062var testConfigVarsBucket = config.Variables {
5163 "project_id" : config .StringVariable (testutil .ProjectId ),
5264 "bucket_name" : config .StringVariable (bucketName ),
@@ -90,7 +102,7 @@ func configVarsHttpUpdated() config.Variables {
90102 return updatedConfig
91103}
92104
93- func makeCertAndKey (organization string ) (cert , key []byte ) {
105+ func makeCertAndKey (organization string , domain string ) (cert , key []byte ) {
94106 privateKey , err := rsa .GenerateKey (cryptoRand .Reader , 2048 )
95107 if err != nil {
96108 fmt .Printf ("failed to generate key: %s" , err .Error ())
@@ -100,10 +112,11 @@ func makeCertAndKey(organization string) (cert, key []byte) {
100112 Issuer : pkix.Name {CommonName : organization },
101113 Subject : pkix.Name {
102114 Organization : []string {organization },
115+ CommonName : domain , // Required by most modern TLS validations
103116 },
104- NotBefore : time . Now (),
105- NotAfter : time .Now (). Add ( time . Hour ),
106-
117+ DNSNames : [] string { domain }, // Subject Alternative Name (SAN) is strictly required now
118+ NotBefore : time .Now (),
119+ NotAfter : time . Now (). Add ( time . Hour ),
107120 KeyUsage : x509 .KeyUsageDigitalSignature | x509 .KeyUsageCertSign ,
108121 ExtKeyUsage : []x509.ExtKeyUsage {x509 .ExtKeyUsageServerAuth },
109122 BasicConstraintsValid : true ,
@@ -129,14 +142,13 @@ func makeCertAndKey(organization string) (cert, key []byte) {
129142}
130143
131144func TestAccCDNDistributionHttp (t * testing.T ) {
132- fullDomainName := fmt .Sprintf ("%s.%s" , testutil .ConvertConfigVariable (testConfigVarsHttp ["dns_record_name" ]), testutil .ConvertConfigVariable (testConfigVarsHttp ["dns_name" ]))
133145 resource .Test (t , resource.TestCase {
134146 ProtoV6ProviderFactories : testutil .TestAccProtoV6ProviderFactories ,
135147 CheckDestroy : testAccCheckCDNDistributionDestroy ,
136148 Steps : []resource.TestStep {
137- // Distribution Create
149+ // Distribution Create (Only Base config)
138150 {
139- Config : testutil .CdnProviderConfig () + "\n " + resourceHttp ,
151+ Config : testutil .CdnProviderConfig () + "\n " + resourceHttpBase ,
140152 ConfigVariables : testConfigVarsHttp ,
141153 Check : resource .ComposeAggregateTestCheckFunc (
142154 resource .TestCheckResourceAttrSet ("stackit_cdn_distribution.distribution" , "distribution_id" ),
@@ -166,22 +178,22 @@ func TestAccCDNDistributionHttp(t *testing.T) {
166178 resource .TestCheckResourceAttr ("stackit_cdn_distribution.distribution" , "status" , "ACTIVE" ),
167179 ),
168180 },
169- // Wait step, that confirms the CNAME record has "propagated"
181+ // Wait step, confirms the CNAME record has "propagated" before trying to add the custom domain
170182 {
171- Config : testutil .CdnProviderConfig () + "\n " + resourceHttp ,
183+ Config : testutil .CdnProviderConfig () + "\n " + resourceHttpBase ,
172184 ConfigVariables : testConfigVarsHttp ,
173185 Check : func (_ * terraform.State ) error {
174- _ , err := blockUntilDomainResolves (fullDomainName )
186+ _ , err := blockUntilDomainResolves (fullDomainNameHttp )
175187 return err
176188 },
177189 },
178- // Custom Domain Create
190+ // Custom Domain Create (Now using Full config)
179191 {
180- Config : testutil .CdnProviderConfig () + "\n " + resourceHttp ,
192+ Config : testutil .CdnProviderConfig () + "\n " + resourceHttpFull ,
181193 ConfigVariables : testConfigVarsHttp ,
182194 Check : resource .ComposeAggregateTestCheckFunc (
183195 resource .TestCheckResourceAttr ("stackit_cdn_custom_domain.custom_domain" , "status" , "ACTIVE" ),
184- resource .TestCheckResourceAttr ("stackit_cdn_custom_domain.custom_domain" , "name" , fullDomainName ),
196+ resource .TestCheckResourceAttr ("stackit_cdn_custom_domain.custom_domain" , "name" , fullDomainNameHttp ),
185197 resource .TestCheckResourceAttr ("stackit_cdn_custom_domain.custom_domain" , "certificate.version" , "1" ),
186198 resource .TestCheckResourceAttrPair ("stackit_cdn_distribution.distribution" , "distribution_id" , "stackit_cdn_custom_domain.custom_domain" , "distribution_id" ),
187199 resource .TestCheckResourceAttrPair ("stackit_cdn_distribution.distribution" , "project_id" , "stackit_cdn_custom_domain.custom_domain" , "project_id" ),
@@ -235,15 +247,15 @@ func TestAccCDNDistributionHttp(t *testing.T) {
235247 },
236248 // Data Source
237249 {
238- Config : testutil .CdnProviderConfig () + "\n " + resourceHttp ,
250+ Config : testutil .CdnProviderConfig () + "\n " + resourceHttpFull ,
239251 ConfigVariables : testConfigVarsHttp ,
240252 Check : resource .ComposeAggregateTestCheckFunc (
241253 resource .TestCheckResourceAttrSet ("data.stackit_cdn_distribution.distribution" , "distribution_id" ),
242254 resource .TestCheckResourceAttrSet ("data.stackit_cdn_distribution.distribution" , "created_at" ),
243255 resource .TestCheckResourceAttrSet ("data.stackit_cdn_distribution.distribution" , "updated_at" ),
244256 resource .TestCheckResourceAttr ("data.stackit_cdn_distribution.distribution" , "domains.#" , "2" ),
245257 resource .TestCheckResourceAttrSet ("data.stackit_cdn_distribution.distribution" , "domains.0.name" ),
246- resource .TestCheckResourceAttr ("data.stackit_cdn_distribution.distribution" , "domains.1.name" , fullDomainName ),
258+ resource .TestCheckResourceAttr ("data.stackit_cdn_distribution.distribution" , "domains.1.name" , fullDomainNameHttp ),
247259 resource .TestCheckResourceAttr ("data.stackit_cdn_distribution.distribution" , "domains.0.status" , "ACTIVE" ),
248260 resource .TestCheckResourceAttr ("data.stackit_cdn_distribution.distribution" , "domains.1.status" , "ACTIVE" ),
249261 resource .TestCheckResourceAttr ("data.stackit_cdn_distribution.distribution" , "domains.0.type" , "managed" ),
@@ -268,22 +280,22 @@ func TestAccCDNDistributionHttp(t *testing.T) {
268280 resource .TestCheckResourceAttr ("data.stackit_cdn_distribution.distribution" , "status" , "ACTIVE" ),
269281
270282 resource .TestCheckResourceAttr ("data.stackit_cdn_custom_domain.custom_domain" , "status" , "ACTIVE" ),
271- resource .TestCheckResourceAttr ("data.stackit_cdn_custom_domain.custom_domain" , "name" , fullDomainName ),
283+ resource .TestCheckResourceAttr ("data.stackit_cdn_custom_domain.custom_domain" , "name" , fullDomainNameHttp ),
272284 resource .TestCheckResourceAttr ("data.stackit_cdn_custom_domain.custom_domain" , "certificate.version" , "1" ),
273285 resource .TestCheckResourceAttrPair ("stackit_cdn_distribution.distribution" , "distribution_id" , "stackit_cdn_custom_domain.custom_domain" , "distribution_id" ),
274286 ),
275287 },
276288 // Update
277289 {
278- Config : testutil .CdnProviderConfig () + "\n " + resourceHttp ,
290+ Config : testutil .CdnProviderConfig () + "\n " + resourceHttpFull ,
279291 ConfigVariables : configVarsHttpUpdated (),
280292 Check : resource .ComposeAggregateTestCheckFunc (
281293 resource .TestCheckResourceAttrSet ("stackit_cdn_distribution.distribution" , "distribution_id" ),
282294 resource .TestCheckResourceAttrSet ("stackit_cdn_distribution.distribution" , "created_at" ),
283295 resource .TestCheckResourceAttrSet ("stackit_cdn_distribution.distribution" , "updated_at" ),
284296 resource .TestCheckResourceAttr ("stackit_cdn_distribution.distribution" , "domains.#" , "2" ),
285297 resource .TestCheckResourceAttrSet ("stackit_cdn_distribution.distribution" , "domains.0.name" ),
286- resource .TestCheckResourceAttr ("stackit_cdn_distribution.distribution" , "domains.1.name" , fullDomainName ),
298+ resource .TestCheckResourceAttr ("stackit_cdn_distribution.distribution" , "domains.1.name" , fullDomainNameHttp ),
287299 resource .TestCheckResourceAttr ("stackit_cdn_distribution.distribution" , "domains.0.status" , "ACTIVE" ),
288300 resource .TestCheckResourceAttr ("stackit_cdn_distribution.distribution" , "domains.1.status" , "ACTIVE" ),
289301 resource .TestCheckResourceAttr ("stackit_cdn_distribution.distribution" , "domains.0.type" , "managed" ),
@@ -309,7 +321,7 @@ func TestAccCDNDistributionHttp(t *testing.T) {
309321 ),
310322
311323 resource .TestCheckResourceAttr ("stackit_cdn_custom_domain.custom_domain" , "status" , "ACTIVE" ),
312- resource .TestCheckResourceAttr ("stackit_cdn_custom_domain.custom_domain" , "name" , fullDomainName ),
324+ resource .TestCheckResourceAttr ("stackit_cdn_custom_domain.custom_domain" , "name" , fullDomainNameHttp ),
313325 resource .TestCheckResourceAttr ("stackit_cdn_custom_domain.custom_domain" , "certificate.version" , "1" ),
314326 resource .TestCheckResourceAttrPair ("stackit_cdn_distribution.distribution" , "distribution_id" , "stackit_cdn_custom_domain.custom_domain" , "distribution_id" ),
315327 resource .TestCheckResourceAttrPair ("stackit_cdn_distribution.distribution" , "project_id" , "stackit_cdn_custom_domain.custom_domain" , "project_id" ),
@@ -490,10 +502,10 @@ func testAccCheckCDNDistributionDestroy(s *terraform.State) error {
490502
491503const (
492504 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)
505+ recordCheckAttempts = 100 // wait up to 5 minutes for record to become available (normally takes less than 2 minutes)
494506)
495507
496- func blockUntilDomainResolves (domain string ) (string , error ) {
508+ func blockUntilDomainResolves (domain string ) (net. IP , error ) {
497509 // Create a custom resolver that bypasses the local system DNS settings/cache
498510 // and queries Google DNS (8.8.8.8) directly.
499511 r := & net.Resolver {
@@ -506,17 +518,23 @@ func blockUntilDomainResolves(domain string) (string, error) {
506518 return d .DialContext (ctx , network , "8.8.8.8:53" )
507519 },
508520 }
521+
509522 // wait until it becomes ready
510- isReady := func () (string , error ) {
523+ isReady := func () (net. IP , error ) {
511524 // Use a context for the individual query timeout
512525 ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
513526 defer cancel ()
514527
515- cname , err := r .LookupCNAME (ctx , domain )
528+ ips , err := r .LookupIP (ctx , "ip" , domain )
516529 if err != nil {
517- return "" , fmt .Errorf ("error looking up CNAME for domain %s: %w" , domain , err )
530+ return nil , fmt .Errorf ("error looking up IP for domain %s: %w" , domain , err )
531+ }
532+ for _ , ip := range ips {
533+ if ip .String () != "<nil>" {
534+ return ip , nil
535+ }
518536 }
519- return cname , nil
537+ return nil , fmt . Errorf ( "no IP for domain: %v" , domain )
520538 }
521539
522540 return retry (recordCheckAttempts , recordCheckInterval , isReady )
@@ -525,7 +543,7 @@ func blockUntilDomainResolves(domain string) (string, error) {
525543func retry [T any ](attempts int , sleep time.Duration , f func () (T , error )) (T , error ) {
526544 var zero T
527545 var errOuter error
528- for range attempts {
546+ for i := 0 ; i < attempts ; i ++ {
529547 dist , err := f ()
530548 if err == nil {
531549 return dist , nil
0 commit comments