Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: crash on invalid alias value #553

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions twingate/internal/provider/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,10 +827,10 @@ func (r *twingateResource) Update(ctx context.Context, req resource.UpdateReques

if resource != nil {
resource.IsAuthoritative = input.IsAuthoritative
}

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

r.helper(ctx, resource, &state, &plan, &resp.State, &resp.Diagnostics, err, operationUpdate)
Expand Down
46 changes: 45 additions & 1 deletion twingate/internal/test/acctests/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package resource

import (
"fmt"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"regexp"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"

"github.com/Twingate/terraform-provider-twingate/v3/twingate/internal/attr"
"github.com/Twingate/terraform-provider-twingate/v3/twingate/internal/model"
"github.com/Twingate/terraform-provider-twingate/v3/twingate/internal/provider/resource"
Expand Down Expand Up @@ -2047,6 +2048,49 @@ func TestAccTwingateResourceCreateWithAlias(t *testing.T) {
})
}

func TestAccTwingateResourceUpdateWithInvalidAlias(t *testing.T) {
const terraformResourceName = "test29_update_invalid"
theResource := acctests.TerraformResource(terraformResourceName)
remoteNetworkName := test.RandomName()
resourceName := test.RandomResourceName()

sdk.Test(t, sdk.TestCase{
ProtoV6ProviderFactories: acctests.ProviderFactories,
PreCheck: func() { acctests.PreCheck(t) },
CheckDestroy: acctests.CheckTwingateResourceDestroy,
Steps: []sdk.TestStep{
{
Config: createResource29WithoutAlias(terraformResourceName, remoteNetworkName, resourceName),
Check: acctests.ComposeTestCheckFunc(
sdk.TestCheckNoResourceAttr(theResource, attr.Alias),
),
},
{
Config: createResource29(terraformResourceName, remoteNetworkName, resourceName, "test-com"),
ExpectError: regexp.MustCompile("Alias must be a[\\n\\s]+valid DNS name"),
},
},
})
}

func TestAccTwingateResourceCreateWithInvalidAlias(t *testing.T) {
const terraformResourceName = "test29_create_invalid"
remoteNetworkName := test.RandomName()
resourceName := test.RandomResourceName()

sdk.Test(t, sdk.TestCase{
ProtoV6ProviderFactories: acctests.ProviderFactories,
PreCheck: func() { acctests.PreCheck(t) },
CheckDestroy: acctests.CheckTwingateResourceDestroy,
Steps: []sdk.TestStep{
{
Config: createResource29(terraformResourceName, remoteNetworkName, resourceName, "test-com"),
ExpectError: regexp.MustCompile("Alias must be a[\\n\\s]+valid DNS name"),
},
},
})
}

func createResource29(terraformResourceName, networkName, resourceName, aliasName string) string {
return fmt.Sprintf(`
resource "twingate_remote_network" "%s" {
Expand Down