Skip to content

Commit 64512eb

Browse files
authored
Merge pull request #5896 from ajholland/aholland/fix_org_import
fix zero_trust_organization import
2 parents 81f1c03 + 5ddb392 commit 64512eb

File tree

4 files changed

+98
-2
lines changed

4 files changed

+98
-2
lines changed

internal/services/zero_trust_organization/normalizations.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package zero_trust_organization
22

33
import (
44
"context"
5+
56
"github.com/hashicorp/terraform-plugin-framework/diag"
7+
"github.com/hashicorp/terraform-plugin-framework/types"
68
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
79
)
810

@@ -34,3 +36,17 @@ func normalizeReadZeroTrustOrganizationAPIData(_ context.Context, data, sourceDa
3436

3537
return diags
3638
}
39+
40+
func normalizeImportZeroTrustOrganizationAPIData(_ context.Context, data *ZeroTrustOrganizationModel) diag.Diagnostics {
41+
diags := make(diag.Diagnostics, 0)
42+
43+
if data.AutoRedirectToIdentity.IsNull() {
44+
data.AutoRedirectToIdentity = types.BoolValue(false)
45+
}
46+
47+
if data.LoginDesign != nil && *data.LoginDesign == (ZeroTrustOrganizationLoginDesignModel{}) {
48+
data.LoginDesign = nil
49+
}
50+
51+
return diags
52+
}

internal/services/zero_trust_organization/resource.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ package zero_trust_organization
55
import (
66
"context"
77
"fmt"
8-
"github.com/cloudflare/terraform-provider-cloudflare/internal/importpath"
9-
"github.com/hashicorp/terraform-plugin-framework/types"
108
"io"
119
"net/http"
1210

11+
"github.com/cloudflare/terraform-provider-cloudflare/internal/importpath"
12+
"github.com/hashicorp/terraform-plugin-framework/types"
13+
1314
"github.com/cloudflare/cloudflare-go/v5"
1415
"github.com/cloudflare/cloudflare-go/v5/option"
1516
"github.com/cloudflare/cloudflare-go/v5/zero_trust"
@@ -257,6 +258,7 @@ func (r *ZeroTrustOrganizationResource) ImportState(ctx context.Context, req res
257258
}
258259
data = &env.Result
259260

261+
resp.Diagnostics.Append(normalizeImportZeroTrustOrganizationAPIData(ctx, data)...)
260262
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
261263
}
262264

internal/services/zero_trust_organization/resource_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,25 @@ func TestAccCloudflareAccessOrganization(t *testing.T) {
8181
ImportStateId: accountID,
8282
ImportStateCheck: accessOrgImportStateCheck,
8383
},
84+
{
85+
Config: testAccCloudflareAccessOrganizationConfigEmpty(rnd, accountID, testAuthDomain()),
86+
Check: resource.ComposeTestCheckFunc(
87+
resource.TestCheckResourceAttr(name, consts.AccountIDSchemaKey, accountID),
88+
resource.TestCheckResourceAttr(name, "name", testAuthDomain()),
89+
resource.TestCheckResourceAttr(name, "auth_domain", rnd+"-"+testAuthDomain()),
90+
resource.TestCheckResourceAttr(name, "session_duration", "12h"),
91+
// Verify that login_design is not present in the state
92+
resource.TestCheckNoResourceAttr(name, "login_design.background_color"),
93+
resource.TestCheckNoResourceAttr(name, "login_design.text_color"),
94+
resource.TestCheckNoResourceAttr(name, "login_design.logo_path"),
95+
resource.TestCheckNoResourceAttr(name, "login_design.header_text"),
96+
resource.TestCheckNoResourceAttr(name, "login_design.footer_text"),
97+
),
98+
ResourceName: name,
99+
ImportState: true,
100+
ImportStateId: accountID,
101+
ImportStateCheck: accessOrgImportStateCheckEmpty,
102+
},
84103
},
85104
})
86105
}
@@ -122,3 +141,56 @@ func accessOrgImportStateCheck(instanceStates []*terraform.InstanceState) error
122141
func testAccCloudflareAccessOrganizationConfigBasic(rnd, accountID, headerText, authDomain string) string {
123142
return acctest.LoadTestCase("accessorganizationconfigbasic.tf", rnd, accountID, headerText, authDomain)
124143
}
144+
145+
func testAccCloudflareAccessOrganizationConfigEmpty(rnd, accountID, authDomain string) string {
146+
return acctest.LoadTestCase("accessorganizationconfigempty.tf", rnd, accountID, authDomain)
147+
}
148+
149+
func accessOrgImportStateCheckEmpty(instanceStates []*terraform.InstanceState) error {
150+
state := instanceStates[0]
151+
attrs := state.Attributes
152+
wantAuthDomain := testAuthDomain()
153+
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
154+
155+
if stateName := attrs["name"]; !strings.HasSuffix(stateName, wantAuthDomain) {
156+
return fmt.Errorf("name has value %q and does not match expected suffix %q", stateName, wantAuthDomain)
157+
}
158+
159+
if stateAuthdomain := attrs["auth_domain"]; !strings.HasSuffix(stateAuthdomain, wantAuthDomain) {
160+
return fmt.Errorf("auth_domain has value %q and does not match expected suffix %q", stateAuthdomain, wantAuthDomain)
161+
}
162+
163+
stateChecks := []struct {
164+
field string
165+
stateValue string
166+
expectedValue string
167+
}{
168+
{field: consts.AccountIDSchemaKey, stateValue: attrs[consts.AccountIDSchemaKey], expectedValue: accountID},
169+
{field: "is_ui_read_only", stateValue: attrs["is_ui_read_only"], expectedValue: "false"},
170+
{field: "auto_redirect_to_identity", stateValue: attrs["auto_redirect_to_identity"], expectedValue: "false"},
171+
{field: "user_seat_expiration_inactive_time", stateValue: attrs["user_seat_expiration_inactive_time"], expectedValue: "1460h"},
172+
}
173+
174+
for _, check := range stateChecks {
175+
if check.stateValue != check.expectedValue {
176+
return fmt.Errorf("%s has value %q and does not match expected value %q", check.field, check.stateValue, check.expectedValue)
177+
}
178+
}
179+
180+
loginDesignAttrs := []string{
181+
"login_design.background_color",
182+
"login_design.text_color",
183+
"login_design.logo_path",
184+
"login_design.header_text",
185+
"login_design.footer_text",
186+
}
187+
188+
// Verify login_design attributes are not present
189+
for _, attr := range loginDesignAttrs {
190+
if _, exists := attrs[attr]; exists {
191+
return fmt.Errorf("%s exists in state but should not be present", attr)
192+
}
193+
}
194+
195+
return nil
196+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
resource "cloudflare_zero_trust_organization" "%[1]s" {
2+
account_id = "%[2]s"
3+
name = "%[3]s"
4+
auth_domain = "%[1]s-%[3]s"
5+
session_duration = "12h"
6+
}

0 commit comments

Comments
 (0)