diff --git a/CHANGELOG.md b/CHANGELOG.md index de860fad8..b947b7173 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ENHANCEMENTS: * `r/tfe_workspace` update `tags` to be a computed attribute, by @Maed223 [#1767](https://github.com/hashicorp/terraform-provider-tfe/pull/1767) +* `r/tfe_workspace_run`: Allow configuration without `apply` and `destroy` blocks, by @tmatilai [#1604](https://github.com/hashicorp/terraform-provider-tfe/pull/1604) ## v0.67.0 diff --git a/internal/provider/resource_tfe_workspace_run.go b/internal/provider/resource_tfe_workspace_run.go index a0bd1c6af..ef294a183 100644 --- a/internal/provider/resource_tfe_workspace_run.go +++ b/internal/provider/resource_tfe_workspace_run.go @@ -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, diff --git a/internal/provider/resource_tfe_workspace_run_test.go b/internal/provider/resource_tfe_workspace_run_test.go index 2b3b5fd62..13aa88fbd 100644 --- a/internal/provider/resource_tfe_workspace_run_test.go +++ b/internal/provider/resource_tfe_workspace_run_test.go @@ -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() @@ -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`), @@ -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) {