Skip to content

[tfe_workspace_run] Allow configuration without apply and destroy blocks #1604

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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Unreleased

ENHANCEMENTS:

* `r/tfe_workspace_run`: Allow configuration without `apply` and `destroy` blocks, by @tmatilai ([#1604](https://github.com/hashicorp/terraform-provider-tfe/pull/1604))

BUG FIXES:
* `d/tfe_outputs`: fixes 'error inferring type for key' for output objects that had a key with null value. (#1709), by @uturunku1 [#1699](https://github.com/hashicorp/terraform-provider-tfe/pull/1709)
* `r/tfe_workspace_settings`: fixes `Provider produced inconsistent result after apply` error when setting `remote_state_consumer_ids` to an empty set in config. [#1728](https://github.com/hashicorp/terraform-provider-tfe/pull/1728)
Expand Down
9 changes: 4 additions & 5 deletions internal/provider/resource_tfe_workspace_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,10 @@ func resourceTFEWorkspaceRun() *schema.Resource {
ForceNew: true,
},
"apply": {
Type: schema.TypeList,
Elem: resourceTFEWorkspaceRunSchema(),
Optional: true,
AtLeastOneOf: []string{"apply", "destroy"},
MaxItems: 1,
Type: schema.TypeList,
Elem: resourceTFEWorkspaceRunSchema(),
Optional: true,
MaxItems: 1,
},
"destroy": {
Type: schema.TypeList,
Expand Down
50 changes: 45 additions & 5 deletions internal/provider/resource_tfe_workspace_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestAccTFEWorkspaceRun_withBothApplyAndDestroyBlocks(t *testing.T) {
})
}

func TestAccTFEWorkspaceRun_invalidParams(t *testing.T) {
func TestAccTFEWorkspaceRun_withNoApplyOrDestroyBlock(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()

tfeClient, err := getClientUsingEnv()
Expand All @@ -126,14 +126,32 @@ func TestAccTFEWorkspaceRun_invalidParams(t *testing.T) {
organization, orgCleanup := createBusinessOrganization(t, tfeClient)
t.Cleanup(orgCleanup)

parentWorkspace, _ := setupWorkspacesWithConfig(t, tfeClient, rInt, organization.Name, "test-fixtures/basic-config")

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceRunDestroy(parentWorkspace.ID, 0),
),
Steps: []resource.TestStep{
{
Config: testAccTFEWorkspaceRun_noApplyOrDestroyBlockProvided(organization.Name, rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceRunDoesNotExist("tfe_workspace_run.ws_run_parent"),
),
},
},
})
}

func TestAccTFEWorkspaceRun_invalidParams(t *testing.T) {
invalidCases := []struct {
Config string
ExpectError *regexp.Regexp
}{
{
Config: testAccTFEWorkspaceRun_noApplyOrDestroyBlockProvided(organization.Name, rInt),
ExpectError: regexp.MustCompile("\"apply\": one of `apply,destroy` must be specified"),
},
{
Config: testAccTFEWorkspaceRun_noWorkspaceProvided(),
ExpectError: regexp.MustCompile(`The argument "workspace_id" is required, but no definition was found`),
Expand Down Expand Up @@ -254,6 +272,28 @@ func testAccCheckTFEWorkspaceRunExistWithExpectedStatus(n string, run *tfe.Run,
}
}

func testAccCheckTFEWorkspaceRunDoesNotExist(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No instance ID is set")
}

// A workspace run resource without apply block has a random ID,
// and no run with that ID should exist.
_, err := testAccConfiguredClient.Client.Runs.Read(ctx, rs.Primary.ID)
if err == nil {
return fmt.Errorf("Expected run to not exist")
}

return nil
}
}

func testAccCheckTFEWorkspaceRunDestroy(workspaceID string, expectedDestroyCount int) resource.TestCheckFunc {
return func(s *terraform.State) error {
mustBeNil, err := retryFn(10, 1, func() (any, error) {
Expand Down
Loading