Skip to content

Commit

Permalink
Server's group name validation (#49)
Browse files Browse the repository at this point in the history
Closes #41:
Added validation for server's groups name that doesn't allow to use spaces in a group name
  • Loading branch information
disc authored Sep 14, 2023
1 parent 27e1fe3 commit 6e2ef14
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ terraform {
required_providers {
pritunl = {
source = "disc/pritunl"
version = "0.1.7"
version = "0.1.13"
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions internal/provider/resource_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,21 @@ func resourceServer() *schema.Resource {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: func(v interface{}, path cty.Path) diag.Diagnostics {
groupName := v.(string)
if strings.Contains(groupName, " ") {
return diag.Diagnostics{
{
Severity: diag.Error,
Summary: "Group name must not contain spaces",
Detail: groupName + " contains spaces",
AttributePath: cty.Path{cty.GetAttrStep{Name: "groups"}},
},
}
}

return nil
},
},
Required: false,
Optional: true,
Expand Down
41 changes: 41 additions & 0 deletions internal/provider/resource_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,38 @@ func TestGetServer_without_hosts(t *testing.T) {
})
}

func TestCreateServer_with_invalid_group(t *testing.T) {
correctGroupName := "GroupHasNoSpaces"
invalidGroupName := "Group Name With Spaces"

resource.Test(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: testGetServerDestroy,
Steps: []resource.TestStep{
{
Config: testGetServerWithGroupsConfig("tfacc-server1", invalidGroupName),
ExpectError: regexp.MustCompile(fmt.Sprintf("%s contains spaces", invalidGroupName)),
},
{
Config: testGetServerWithGroupsConfig("tfacc-server2", correctGroupName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("pritunl_server.test", "name", "tfacc-server2"),

func(s *terraform.State) error {
groupName := s.RootModule().Resources["pritunl_server.test"].Primary.Attributes["groups.0"]
if groupName != correctGroupName {
return fmt.Errorf("group name mismatch")
}

return nil
},
),
},
},
})
}

func testGetServerSimpleConfig(name string) string {
return fmt.Sprintf(`
resource "pritunl_server" "test" {
Expand Down Expand Up @@ -604,6 +636,15 @@ resource "pritunl_server" "test" {
`, name)
}

func testGetServerWithGroupsConfig(name string, groupName string) string {
return fmt.Sprintf(`
resource "pritunl_server" "test" {
name = "%[1]s"
groups = ["%[2]s"]
}
`, name, groupName)
}

func testGetServerDestroy(s *terraform.State) error {
serverId := s.RootModule().Resources["pritunl_server.test"].Primary.Attributes["id"]

Expand Down

0 comments on commit 6e2ef14

Please sign in to comment.