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

add ValidateFunc to resourceTFEPolicySet to catch syntax errors at tf plan time #168

Merged
merged 5 commits into from
Aug 19, 2020
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
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 @@ -445,6 +446,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 @@ -591,3 +606,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}"]
}`