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

Updated the resourceTFEPolicySetUpdate function #185

Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## 0.19.0 (Unreleased)
BUG FIXES:
* r/tfe_policy_set: Fixes issue when updating Policy Set branch attribute ([#185](https://github.com/terraform-providers/terraform-provider-tfe/pull/185))
## 0.18.1 (June 10, 2020)

ENHANCEMENTS:
Expand Down
13 changes: 12 additions & 1 deletion tfe/resource_tfe_policy_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func resourceTFEPolicySetUpdate(d *schema.ResourceData, meta interface{}) error
}

// Don't bother updating the policy set's attributes if they haven't changed
if d.HasChange("name") || d.HasChange("description") || d.HasChange("global") {
if d.HasChange("name") || d.HasChange("description") || d.HasChange("global") || d.HasChange("vcs_repo") {
// Create a new options struct.
options := tfe.PolicySetUpdateOptions{
Name: tfe.String(name),
Expand All @@ -294,6 +294,17 @@ func resourceTFEPolicySetUpdate(d *schema.ResourceData, meta interface{}) error
options.Description = tfe.String(desc.(string))
}

if v, ok := d.GetOk("vcs_repo"); ok {
vcsRepo := v.([]interface{})[0].(map[string]interface{})

options.VCSRepo = &tfe.VCSRepoOptions{
Identifier: tfe.String(vcsRepo["identifier"].(string)),
Branch: tfe.String(vcsRepo["branch"].(string)),
IngressSubmodules: tfe.Bool(vcsRepo["ingress_submodules"].(bool)),
OAuthTokenID: tfe.String(vcsRepo["oauth_token_id"].(string)),
}
}

log.Printf("[DEBUG] Update configuration for policy set: %s", d.Id())
_, err := tfeClient.PolicySets.Update(ctx, d.Id(), options)
if err != nil {
Expand Down
101 changes: 101 additions & 0 deletions tfe/resource_tfe_policy_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,74 @@ func TestAccTFEPolicySet_vcs(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccTFEPolicySet_vcs,
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEPolicySetExists("tfe_policy_set.foobar", policySet),
testAccCheckTFEPolicySetAttributes(policySet),
resource.TestCheckResourceAttr(
"tfe_policy_set.foobar", "name", "tst-terraform"),
resource.TestCheckResourceAttr(
"tfe_policy_set.foobar", "description", "Policy Set"),
resource.TestCheckResourceAttr(
"tfe_policy_set.foobar", "global", "false"),
resource.TestCheckResourceAttr(
"tfe_policy_set.foobar", "vcs_repo.0.identifier", GITHUB_POLICY_SET_IDENTIFIER),
resource.TestCheckResourceAttr(
"tfe_policy_set.foobar", "vcs_repo.0.branch", "master"),
resource.TestCheckResourceAttr(
"tfe_policy_set.foobar", "vcs_repo.0.ingress_submodules", "true"),
resource.TestCheckResourceAttr(
"tfe_policy_set.foobar", "policies_path", GITHUB_POLICY_SET_PATH),
),
},
},
})
}

func TestAccTFEPolicySet_updateVCSBranch(t *testing.T) {
policySet := &tfe.PolicySet{}

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
if GITHUB_TOKEN == "" {
t.Skip("Please set GITHUB_TOKEN to run this test")
}
if GITHUB_POLICY_SET_IDENTIFIER == "" {
t.Skip("Please set GITHUB_POLICY_SET_IDENTIFIER to run this test")
}
if GITHUB_POLICY_SET_BRANCH == "" {
t.Skip("Please set GITHUB_POLICY_SET_BRANCH to run this test")
}
if GITHUB_POLICY_SET_PATH == "" {
t.Skip("Please set GITHUB_POLICY_SET_PATH to run this test")
}
},
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEPolicySetDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEPolicySet_vcs,
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEPolicySetExists("tfe_policy_set.foobar", policySet),
testAccCheckTFEPolicySetAttributes(policySet),
resource.TestCheckResourceAttr(
"tfe_policy_set.foobar", "name", "tst-terraform"),
resource.TestCheckResourceAttr(
"tfe_policy_set.foobar", "description", "Policy Set"),
resource.TestCheckResourceAttr(
"tfe_policy_set.foobar", "global", "false"),
resource.TestCheckResourceAttr(
"tfe_policy_set.foobar", "vcs_repo.0.identifier", GITHUB_POLICY_SET_IDENTIFIER),
resource.TestCheckResourceAttr(
"tfe_policy_set.foobar", "vcs_repo.0.branch", "master"),
resource.TestCheckResourceAttr(
"tfe_policy_set.foobar", "vcs_repo.0.ingress_submodules", "true"),
resource.TestCheckResourceAttr(
"tfe_policy_set.foobar", "policies_path", GITHUB_POLICY_SET_PATH),
),
},
{
Config: testAccTFEPolicySet_updateVCSBranch,
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEPolicySetExists("tfe_policy_set.foobar", policySet),
testAccCheckTFEPolicySetAttributes(policySet),
Expand Down Expand Up @@ -850,6 +918,39 @@ resource "tfe_oauth_client" "test" {
service_provider = "github"
}

resource "tfe_policy_set" "foobar" {
name = "tst-terraform"
description = "Policy Set"
organization = "${tfe_organization.foobar.id}"
vcs_repo {
identifier = "%s"
branch = "master"
ingress_submodules = true
oauth_token_id = "${tfe_oauth_client.test.oauth_token_id}"
}

policies_path = "%s"
}
`,
GITHUB_TOKEN,
GITHUB_POLICY_SET_IDENTIFIER,
GITHUB_POLICY_SET_PATH,
)

var testAccTFEPolicySet_updateVCSBranch = fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform"
email = "admin@company.com"
}

resource "tfe_oauth_client" "test" {
organization = "${tfe_organization.foobar.id}"
api_url = "https://api.github.com"
http_url = "https://github.com"
oauth_token = "%s"
service_provider = "github"
}

resource "tfe_policy_set" "foobar" {
name = "tst-terraform"
description = "Policy Set"
Expand Down