Skip to content

Commit

Permalink
Fix: warn message after plan (#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
vmanilo authored Oct 31, 2024
1 parent fb32b7d commit 5c0965a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 34 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/Twingate/terraform-provider-twingate/v3

go 1.22
go 1.22.0

toolchain go1.22.5

require (
Expand Down
4 changes: 2 additions & 2 deletions twingate/internal/client/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func (client *Client) CreateResource(ctx context.Context, input *model.Resource)
resource.IsBrowserShortcutEnabled = nil
}

if input.SecurityPolicyID == nil {
resource.SecurityPolicyID = nil
if input.SecurityPolicyID != nil && *input.SecurityPolicyID == "" {
resource.SecurityPolicyID = input.SecurityPolicyID
}

return resource, nil
Expand Down
28 changes: 3 additions & 25 deletions twingate/internal/provider/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,9 +773,8 @@ func (r *twingateResource) Read(ctx context.Context, req resource.ReadRequest, r
if resource != nil {
resource.IsAuthoritative = convertAuthoritativeFlag(state.IsAuthoritative)

if state.SecurityPolicyID.ValueString() == "" {
emptyPolicy := ""
resource.SecurityPolicyID = &emptyPolicy
if state.SecurityPolicyID.IsNull() {
resource.SecurityPolicyID = nil
}
}

Expand Down Expand Up @@ -1323,31 +1322,10 @@ func (m useDefaultPolicyForUnknownModifier) MarkdownDescription(_ context.Contex
// PlanModifyString implements the plan modification logic.
func (m useDefaultPolicyForUnknownModifier) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) {
if req.StateValue.IsNull() && req.ConfigValue.IsNull() {
resp.PlanValue = types.StringPointerValue(nil)

return
}

// Do nothing if there is no state value.
if req.StateValue.IsNull() {
return
}

// Do nothing if there is an unknown configuration value, otherwise interpolation gets messed up.
if req.ConfigValue.IsUnknown() {
return
}
resp.PlanValue = types.StringNull()

// Do nothing if there is a known planned value.
if req.ConfigValue.ValueString() != "" {
return
}

if req.StateValue.ValueString() == "" && req.PlanValue.ValueString() == DefaultSecurityPolicyID {
resp.PlanValue = types.StringValue("")
} else if req.StateValue.ValueString() == DefaultSecurityPolicyID && req.PlanValue.ValueString() == "" {
resp.PlanValue = types.StringValue(DefaultSecurityPolicyID)
}
}

func accessGroupAttributeTypes() map[string]tfattr.Type {
Expand Down
17 changes: 11 additions & 6 deletions twingate/internal/test/acctests/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3030,10 +3030,10 @@ func TestAccTwingateResourceUpdateSecurityPolicy(t *testing.T) {
CheckDestroy: acctests.CheckTwingateResourceDestroy,
Steps: []sdk.TestStep{
{
Config: createResourceWithSecurityPolicy(remoteNetworkName, resourceName, defaultPolicy),
Config: createResourceWithoutSecurityPolicy(remoteNetworkName, resourceName),
Check: acctests.ComposeTestCheckFunc(
acctests.CheckTwingateResourceExists(theResource),
sdk.TestCheckResourceAttr(theResource, attr.SecurityPolicyID, defaultPolicy),
sdk.TestCheckNoResourceAttr(theResource, attr.SecurityPolicyID),
),
},
{
Expand All @@ -3052,8 +3052,11 @@ func TestAccTwingateResourceUpdateSecurityPolicy(t *testing.T) {
},
{
Config: createResourceWithSecurityPolicy(remoteNetworkName, resourceName, ""),
// no changes
PlanOnly: true,
Check: acctests.ComposeTestCheckFunc(
acctests.CheckTwingateResourceExists(theResource),
sdk.TestCheckResourceAttr(theResource, attr.SecurityPolicyID, ""),
),
ExpectNonEmptyPlan: true,
},
},
})
Expand Down Expand Up @@ -3118,11 +3121,13 @@ func TestAccTwingateResourceSetDefaultSecurityPolicyByDefault(t *testing.T) {
Check: acctests.ComposeTestCheckFunc(
acctests.CheckResourceSecurityPolicy(theResource, defaultPolicy),
),
ExpectNonEmptyPlan: true,
},
{
Config: createResourceWithoutSecurityPolicy(remoteNetworkName, resourceName),
// no changes
PlanOnly: true,
Check: acctests.ComposeTestCheckFunc(
acctests.CheckResourceSecurityPolicy(theResource, defaultPolicy),
),
},
},
})
Expand Down

0 comments on commit 5c0965a

Please sign in to comment.