Skip to content

Commit

Permalink
Add ValidateFunc for name to r/tfe_policy_set to catch errors at plan…
Browse files Browse the repository at this point in the history
… time (#168)
  • Loading branch information
mathyourlife-fitbit authored Aug 19, 2020
1 parent bca4d41 commit 0798563
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
7 changes: 5 additions & 2 deletions tfe/resource_tfe_policy_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package tfe
import (
"fmt"
"log"
"regexp"

tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)

func resourceTFEPolicySet() *schema.Resource {
Expand All @@ -20,8 +22,9 @@ func resourceTFEPolicySet() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`\A[\w\_\-]+\z`), "can only include letters, numbers, -, and _."),
},

"description": {
Expand Down
34 changes: 34 additions & 0 deletions tfe/resource_tfe_policy_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tfe

import (
"fmt"
"regexp"
"testing"

tfe "github.com/hashicorp/go-tfe"
Expand Down Expand Up @@ -732,6 +733,20 @@ func testAccCheckTFEPolicySetDestroy(s *terraform.State) error {
return nil
}

func TestAccTFEPolicySet_invalidName(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEPolicySetDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEPolicySet_invalidName,
ExpectError: regexp.MustCompile(`can only include letters, numbers, -, and _.`),
},
},
})
}

const testAccTFEPolicySet_basic = `
resource "tfe_organization" "foobar" {
name = "tst-terraform"
Expand Down Expand Up @@ -970,3 +985,22 @@ resource "tfe_policy_set" "foobar" {
GITHUB_POLICY_SET_BRANCH,
GITHUB_POLICY_SET_PATH,
)

const testAccTFEPolicySet_invalidName = `
resource "tfe_organization" "foobar" {
name = "tst-terraform"
email = "[email protected]"
}
resource "tfe_sentinel_policy" "foo" {
name = "policy-foo"
policy = "main = rule { true }"
organization = "${tfe_organization.foobar.id}"
}
resource "tfe_policy_set" "foobar" {
name = "not the right format"
description = "Policy Set"
organization = "${tfe_organization.foobar.id}"
policy_ids = ["${tfe_sentinel_policy.foo.id}"]
}`

0 comments on commit 0798563

Please sign in to comment.