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 bug preventing removal of vcs_repo on workspaces #173

Merged
merged 4 commits into from
May 12, 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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## 0.17.0 (Unreleased)
## 0.16.2 (Unreleased)

BUG FIXES:
* r/tfe_workspace: Allow VCS repo to be removed from a workspace when it has been removed from the configuration. ([#173](https://github.com/terraform-providers/terraform-provider-tfe/pull/173))

## 0.16.1 (April 28, 2020)

BUG FIXES:
Expand All @@ -13,6 +17,7 @@ FEATURES:
- **New Resource**: `tfe_team_organization_member` ([#154](https://github.com/terraform-providers/terraform-provider-tfe/pull/154))

## 0.15.1 (March 25, 2020)

ENHANCEMENTS:
* r/tfe_workspace: Migrate ID from <organization>/<workspace> to opaque external_id ([#106](https://github.com/terraform-providers/terraform-provider-tfe/pull/106))
* r/tfe_variable: Migrate workspace_id from <organization>/<workspace> to opaque external_id ([#106](https://github.com/terraform-providers/terraform-provider-tfe/pull/106))
Expand All @@ -21,6 +26,7 @@ ENHANCEMENTS:
## 0.15.0 (March 25, 2020)

## 0.14.1 (March 04, 2020)

BUG FIXES:

* t/tfe_workspace: Issues with updating `working_directory` ([[#137](https://github.com/terraform-providers/terraform-provider-tfe/pull/137)])
Expand Down
42 changes: 24 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,22 @@ $ $GOPATH/bin/terraform-provider-tfe
...
```

## Referencing a local version of `go-tfe`

You may want to create configs or run tests against a local version of `go-tfe`. Add the following line to `go.mod` above the require statement, using your local path to `go-tfe`.

```
replace github.com/hashicorp/go-tfe => /path-to-local-repo/go-tfe
```

## Testing

### 1. (Optional) Create a policy sets repo
### 1. (Optional) Create repositories for policy sets and workspaces

If you are planning to run the full suite of tests or work on policy sets, you'll need to set up a policy set repository in GitHub.
If you are planning to run the full suite of tests or work on workspaces or policy sets, you'll need to set up a workspace repository and a policy set repository in GitHub.

Your workspace repository will need the following:
1. A branch other than master

Your policy set repository will need the following:
1. A policy set stored in a subdirectory
Expand All @@ -105,6 +116,8 @@ these values with the environment variables specified below:
1. `GITHUB_POLICY_SET_IDENTIFIER` - GitHub policy set repository identifier in the format `username/repository`. Required for running policy set tests.
1. `GITHUB_POLICY_SET_BRANCH`: A GitHub branch for the repository specified by `GITHUB_POLICY_SET_IDENTIFIER`. Required for running policy set tests.
1. `GITHUB_POLICY_SET_PATH`: A GitHub subdirectory for the repository specified by `GITHUB_POLICY_SET_IDENTIFIER`. Required for running policy set tests.
1. `GITHUB_WORKSPACE_IDENTIFIER` - GitHub workspace repository identifier in the format `username/repository`. Required for running workspace tests.
1. `GITHUB_WORKSPACE_BRANCH`: A GitHub branch for the repository specified by `GITHUB_WORKSPACE_IDENTIFIER`. Required for running workspace tests.

You can set your environment variables up however you prefer. The following are instructions for setting up environment variables using [envchain](https://github.com/sorah/envchain).
1. Make sure you have envchain installed. [Instructions for this can be found in the envchain README](https://github.com/sorah/envchain#installation).
Expand All @@ -117,9 +130,9 @@ You can set your environment variables up however you prefer. The following are

Set all of the environment variables at once with the following command:
```sh
envchain --set YOUR_NAMESPACE_HERE TFE_HOSTNAME TFE_TOKEN TFE_USER1 TFE_USER2 GITHUB_TOKEN GITHUB_POLICY_SET_IDENTIFIER GITHUB_POLICY_SET_BRANCH GITHUB_POLICY_SET_PATH
envchain --set YOUR_NAMESPACE_HERE TFE_HOSTNAME TFE_TOKEN TFE_USER1 TFE_USER2 GITHUB_TOKEN GITHUB_POLICY_SET_IDENTIFIER GITHUB_POLICY_SET_BRANCH GITHUB_POLICY_SET_PATH GITHUB_WORKSPACE_IDENTIFIER GITHUB_WORKSPACE_BRANCH
```

### 3. Run the tests

#### Running the provider tests
Expand Down Expand Up @@ -160,18 +173,11 @@ $ TESTARGS="-run TestAccTFENotificationConfiguration" envchain YOUR_NAMESPACE_HE
$ TESTARGS="-run TestAccTFENotificationConfiguration" make testacc
```

### 4. Referencing a local version of `go-tfe`

You may want to run tests against a local version of `go-tfe`. Add the following line to `go.mod` above the require statement, using your local path to `go-tfe`.

```
replace github.com/hashicorp/go-tfe => /path-to-local-repo/go-tfe
```

## Updating the Changelog

Only update the `Unreleased` section. Please use the template below when updating the changelog:
Only update the `Unreleased` section. Make sure you change the unreleased tag to an appropriate version, using [Semantic Versioning](https://semver.org/) as a guideline.

Please use the template below when updating the changelog:
```
<change category>:
* **New Resource:** `name_of_new_resource` ([#123](link-to-PR))
Expand All @@ -180,8 +186,8 @@ Only update the `Unreleased` section. Please use the template below when updatin

### Change categories

BREAKING CHANGES: Use this for any changes that aren't backwards compatible. Include details on how to handle these changes.
FEATURES: Use this for any larger new features added
ENHANCEMENTS: Use this for smaller new features added
BUG FIXES: Use this for any bugs that were fixed
NOTES: Use this section if you need to include any additional notes on things like upgrading, upcoming deprecations, or any other information you might want to highlight.
- BREAKING CHANGES: Use this for any changes that aren't backwards compatible. Include details on how to handle these changes.
- FEATURES: Use this for any large new features added.
- ENHANCEMENTS: Use this for smaller new features added.
- BUG FIXES: Use this for any bugs that were fixed.
- NOTES: Use this section if you need to include any additional notes on things like upgrading, upcoming deprecations, or any other information you might want to highlight.
4 changes: 3 additions & 1 deletion tfe/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ func testAccPreCheck(t *testing.T) {
}

var GITHUB_TOKEN = os.Getenv("GITHUB_TOKEN")
var GITHUB_WORKSPACE_IDENTIFIER = os.Getenv("GITHUB_WORKSPACE_IDENTIFIER")
var GITHUB_WORKSPACE_BRANCH = os.Getenv("GITHUB_WORKSPACE_BRANCH")
var GITHUB_POLICY_SET_IDENTIFIER = os.Getenv("GITHUB_POLICY_SET_IDENTIFIER")
var GITHUB_POLICY_SET_BRANCH = os.Getenv("GITHUB_POLICY_SET_BRANCH")
var GITHUB_POLICY_SET_PATH = os.Getenv("GITHUB_POLICY_SET_PATH")
var TFE_USER1 = os.Getenv("TFE_USER1")
var TFE_USER2 = os.Getenv("TFE_USER2")
var GITHUB_POLICY_SET_IDENTIFIER = os.Getenv("GITHUB_POLICY_SET_IDENTIFIER")
13 changes: 13 additions & 0 deletions tfe/resource_tfe_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,19 @@ func resourceTFEWorkspaceUpdate(d *schema.ResourceData, meta interface{}) error
}
}

// Remove vcs_repo from the workspace
// if the value of vcs_repo has been changed
// by removing it from the config
if d.HasChange("vcs_repo") {
_, ok := d.GetOk("vcs_repo")
if !ok {
_, err := tfeClient.Workspaces.RemoveVCSConnectionByID(ctx, id)
if err != nil {
return fmt.Errorf("Error removing VCS repo from workspace %s: %v", id, err)
}
}
}

if d.HasChange("ssh_key_id") {
sshKeyID := d.Get("ssh_key_id").(string)
externalID, _ := d.GetChange("external_id")
Expand Down
221 changes: 221 additions & 0 deletions tfe/resource_tfe_workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,87 @@ func TestAccTFEWorkspace_updateTriggerPrefixes(t *testing.T) {
})
}

func TestAccTFEWorkspace_updateVCSRepo(t *testing.T) {
workspace := &tfe.Workspace{}

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
if GITHUB_TOKEN == "" {
t.Skip("Please set GITHUB_TOKEN to run this test")
}
if GITHUB_WORKSPACE_IDENTIFIER == "" {
t.Skip("Please set GITHUB_WORKSPACE_IDENTIFIER to run this test")
}
if GITHUB_WORKSPACE_BRANCH == "" {
t.Skip("Please set GITHUB_WORKSPACE_BRANCH to run this test")
}
},
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEWorkspaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEWorkspace_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceExists(
"tfe_workspace.foobar", workspace),
testAccCheckTFEWorkspaceAttributes(workspace),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "name", "workspace-test"),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "auto_apply", "true"),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "operations", "true"),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "queue_all_runs", "true"),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "working_directory", ""),
),
},
{
Config: testAccTFEWorkspace_updateAddVCSRepo,
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceExists("tfe_workspace.foobar", workspace),
testAccCheckTFEWorkspaceUpdatedAddVCSRepoAttributes(workspace),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "name", "workspace-test-add-vcs-repo"),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "vcs_repo.0.identifier", GITHUB_WORKSPACE_IDENTIFIER),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "vcs_repo.0.branch", ""),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "vcs_repo.0.ingress_submodules", "false"),
),
},
{
Config: testAccTFEWorkspace_updateUpdateVCSRepoBranch,
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceExists("tfe_workspace.foobar", workspace),
testAccCheckTFEWorkspaceUpdatedUpdateVCSRepoBranchAttributes(workspace),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "name", "workspace-test-update-vcs-repo-branch"),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "vcs_repo.0.identifier", GITHUB_WORKSPACE_IDENTIFIER),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "vcs_repo.0.branch", GITHUB_WORKSPACE_BRANCH),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "vcs_repo.0.ingress_submodules", "false"),
),
},
{
Config: testAccTFEWorkspace_updateRemoveVCSRepo,
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceExists("tfe_workspace.foobar", workspace),
testAccCheckTFEWorkspaceUpdatedRemoveVCSRepoAttributes(workspace),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "name", "workspace-test-remove-vcs-repo"),
resource.TestCheckNoResourceAttr("tfe_workspace.foobar", "vcs_repo"),
),
},
},
})
}

func TestAccTFEWorkspace_sshKey(t *testing.T) {
workspace := &tfe.Workspace{}

Expand Down Expand Up @@ -605,6 +686,75 @@ func testAccCheckTFEWorkspaceAttributesSSHKey(
}
}

func testAccCheckTFEWorkspaceUpdatedAddVCSRepoAttributes(
workspace *tfe.Workspace) resource.TestCheckFunc {
return func(s *terraform.State) error {
if workspace.Name != "workspace-test-add-vcs-repo" {
return fmt.Errorf("Bad name: %s", workspace.Name)
}

if workspace.VCSRepo == nil {
return fmt.Errorf("Bad VCS repo: %v", workspace.VCSRepo)
}

if workspace.VCSRepo.Branch != "" {
return fmt.Errorf("Bad VCS repo branch: %v", workspace.VCSRepo.Branch)
}

if workspace.VCSRepo.Identifier != GITHUB_WORKSPACE_IDENTIFIER {
return fmt.Errorf("Bad VCS repo identifier: %v", workspace.VCSRepo.Identifier)
}

if workspace.VCSRepo.IngressSubmodules != false {
return fmt.Errorf("Bad VCS repo ingress submodules: %v", workspace.VCSRepo.IngressSubmodules)
}

return nil
}
}

func testAccCheckTFEWorkspaceUpdatedUpdateVCSRepoBranchAttributes(
workspace *tfe.Workspace) resource.TestCheckFunc {
return func(s *terraform.State) error {
if workspace.Name != "workspace-test-update-vcs-repo-branch" {
return fmt.Errorf("Bad name: %s", workspace.Name)
}

if workspace.VCSRepo == nil {
return fmt.Errorf("Bad VCS repo: %v", workspace.VCSRepo)
}

if workspace.VCSRepo.Branch != GITHUB_WORKSPACE_BRANCH {
return fmt.Errorf("Bad VCS repo branch: %v", workspace.VCSRepo.Branch)
}

if workspace.VCSRepo.Identifier != GITHUB_WORKSPACE_IDENTIFIER {
return fmt.Errorf("Bad VCS repo identifier: %v", workspace.VCSRepo.Identifier)
}

if workspace.VCSRepo.IngressSubmodules != false {
return fmt.Errorf("Bad VCS repo ingress submodules: %v", workspace.VCSRepo.IngressSubmodules)
}

return nil
}
}

func testAccCheckTFEWorkspaceUpdatedRemoveVCSRepoAttributes(
workspace *tfe.Workspace) resource.TestCheckFunc {
return func(s *terraform.State) error {
if workspace.Name != "workspace-test-remove-vcs-repo" {
return fmt.Errorf("Bad name: %s", workspace.Name)
}

if workspace.VCSRepo != nil {
return fmt.Errorf("Bad VCS repo: %v", workspace.VCSRepo)
}

return nil
}
}

func testAccCheckTFEWorkspaceDestroy(s *terraform.State) error {
tfeClient := testAccProvider.Meta().(*tfe.Client)

Expand Down Expand Up @@ -779,3 +929,74 @@ resource "tfe_workspace" "foobar" {
organization = "${tfe_organization.foobar.id}"
auto_apply = true
}`

var testAccTFEWorkspace_updateAddVCSRepo = fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform"
email = "[email protected]"
}

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_workspace" "foobar" {
name = "workspace-test-add-vcs-repo"
organization = "${tfe_organization.foobar.id}"
auto_apply = true
vcs_repo {
identifier = "%s"
oauth_token_id = "${tfe_oauth_client.test.oauth_token_id}"
}
}
`,
GITHUB_TOKEN,
GITHUB_WORKSPACE_IDENTIFIER,
)

var testAccTFEWorkspace_updateUpdateVCSRepoBranch = fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform"
email = "[email protected]"
}

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_workspace" "foobar" {
name = "workspace-test-update-vcs-repo-branch"
organization = "${tfe_organization.foobar.id}"
auto_apply = true
vcs_repo {
identifier = "%s"
oauth_token_id = "${tfe_oauth_client.test.oauth_token_id}"
branch = "%s"
}
}
`,
GITHUB_TOKEN,
GITHUB_WORKSPACE_IDENTIFIER,
GITHUB_WORKSPACE_BRANCH,
)

const testAccTFEWorkspace_updateRemoveVCSRepo = `
resource "tfe_organization" "foobar" {
name = "tst-terraform"
email = "[email protected]"
}

resource "tfe_workspace" "foobar" {
name = "workspace-test-remove-vcs-repo"
organization = "${tfe_organization.foobar.id}"
auto_apply = true
}
`