From 48aea187e3f31f29dd1bf262130d818d0adf148d Mon Sep 17 00:00:00 2001 From: Eduardo Gomes Date: Tue, 16 Jul 2024 08:49:35 +0200 Subject: [PATCH] Fix updating access apps self_hosted_domains returning errors --- .changelog/3468.txt | 3 ++ .../resource_cloudflare_access_application.go | 8 ++++- ...urce_cloudflare_access_application_test.go | 35 ++++++++++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 .changelog/3468.txt diff --git a/.changelog/3468.txt b/.changelog/3468.txt new file mode 100644 index 0000000000..07c55a31ab --- /dev/null +++ b/.changelog/3468.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/cloudflare-access-application: fixes bug when updating self_hosted_domains +``` diff --git a/internal/sdkv2provider/resource_cloudflare_access_application.go b/internal/sdkv2provider/resource_cloudflare_access_application.go index 6f60b231ad..9a6a2f3a9f 100644 --- a/internal/sdkv2provider/resource_cloudflare_access_application.go +++ b/internal/sdkv2provider/resource_cloudflare_access_application.go @@ -171,7 +171,13 @@ func resourceCloudflareAccessApplicationRead(ctx context.Context, d *schema.Reso d.Set("name", accessApplication.Name) d.Set("aud", accessApplication.AUD) d.Set("session_duration", accessApplication.SessionDuration) - d.Set("domain", accessApplication.Domain) + if _, domainWasSet := d.GetOk("domain"); domainWasSet { + // Only set the domain if it was set in the configuration, as apps can be created without a domain + // if they define a non-empty self_hosted_domains array + d.Set("domain", accessApplication.Domain) + } else { + d.Set("domain", nil) + } d.Set("type", accessApplication.Type) d.Set("auto_redirect_to_identity", accessApplication.AutoRedirectToIdentity) d.Set("enable_binding_cookie", accessApplication.EnableBindingCookie) diff --git a/internal/sdkv2provider/resource_cloudflare_access_application_test.go b/internal/sdkv2provider/resource_cloudflare_access_application_test.go index 14673f3007..339b3033a0 100644 --- a/internal/sdkv2provider/resource_cloudflare_access_application_test.go +++ b/internal/sdkv2provider/resource_cloudflare_access_application_test.go @@ -936,8 +936,25 @@ func TestAccCloudflareAccessApplication_WithSelfHostedDomains(t *testing.T) { Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(name, consts.AccountIDSchemaKey, accountID), resource.TestCheckResourceAttr(name, "name", rnd), - resource.TestCheckResourceAttrSet(name, "domain"), resource.TestCheckResourceAttr(name, "self_hosted_domains.#", "2"), + resource.TestCheckResourceAttr(name, "self_hosted_domains.0", fmt.Sprintf("d1.%s.%s", rnd, domain)), + resource.TestCheckResourceAttr(name, "self_hosted_domains.1", fmt.Sprintf("d2.%s.%s", rnd, domain)), + resource.TestCheckResourceAttr(name, "name", rnd), + resource.TestCheckResourceAttr(name, "type", "self_hosted"), + resource.TestCheckResourceAttr(name, "session_duration", "24h"), + resource.TestCheckResourceAttr(name, "cors_headers.#", "0"), + resource.TestCheckResourceAttr(name, "sass_app.#", "0"), + resource.TestCheckResourceAttr(name, "auto_redirect_to_identity", "false"), + ), + }, + { + Config: testAccCloudflareAccessApplicationWithSelfHostedDomains2(rnd, domain, cloudflare.AccountIdentifier(accountID)), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(name, consts.AccountIDSchemaKey, accountID), + resource.TestCheckResourceAttr(name, "name", rnd), + resource.TestCheckResourceAttr(name, "self_hosted_domains.#", "2"), + resource.TestCheckResourceAttr(name, "self_hosted_domains.0", fmt.Sprintf("d3.%s.%s", rnd, domain)), + resource.TestCheckResourceAttr(name, "self_hosted_domains.1", fmt.Sprintf("d4.%s.%s", rnd, domain)), resource.TestCheckResourceAttr(name, "type", "self_hosted"), resource.TestCheckResourceAttr(name, "session_duration", "24h"), resource.TestCheckResourceAttr(name, "cors_headers.#", "0"), @@ -1394,6 +1411,22 @@ resource "cloudflare_access_application" "%[1]s" { `, rnd, domain, identifier.Type, identifier.Identifier) } +func testAccCloudflareAccessApplicationWithSelfHostedDomains2(rnd string, domain string, identifier *cloudflare.ResourceContainer) string { + return fmt.Sprintf(` +resource "cloudflare_access_application" "%[1]s" { + %[3]s_id = "%[4]s" + name = "%[1]s" + type = "self_hosted" + session_duration = "24h" + auto_redirect_to_identity = false + self_hosted_domains = [ + "d3.%[1]s.%[2]s", + "d4.%[1]s.%[2]s" + ] +} +`, rnd, domain, identifier.Type, identifier.Identifier) +} + func testAccCheckCloudflareAccessApplicationDestroy(s *terraform.State) error { client := testAccProvider.Meta().(*cloudflare.API)