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 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
90c763c
[tfe_workspace_run] Allow configuration without `apply` and `destroy`…
tmatilai Feb 12, 2025
3f863df
Merge branch 'main' into allow-empty-workspace-run
tmatilai Feb 13, 2025
b85584f
Merge branch 'main' into allow-empty-workspace-run
tmatilai Feb 25, 2025
76afc5b
Merge branch 'main' into allow-empty-workspace-run
tmatilai Feb 26, 2025
05bf180
Merge branch 'main' into allow-empty-workspace-run
tmatilai Mar 5, 2025
cd232bf
Merge branch 'main' into allow-empty-workspace-run
tmatilai Mar 12, 2025
d7c431c
Merge branch 'main' into allow-empty-workspace-run
tmatilai Mar 18, 2025
79d2cd1
Merge branch 'main' into allow-empty-workspace-run
tmatilai Mar 21, 2025
60b9dc2
Merge branch 'main' into allow-empty-workspace-run
tmatilai Mar 26, 2025
4792e8a
Merge branch 'main' into allow-empty-workspace-run
tmatilai Mar 27, 2025
f37d69f
Merge branch 'main' into allow-empty-workspace-run
tmatilai Mar 31, 2025
227e8c0
Merge branch 'main' into allow-empty-workspace-run
tmatilai Apr 1, 2025
94d1a82
Merge branch 'main' into allow-empty-workspace-run
tmatilai Apr 2, 2025
85bb71c
Merge branch 'main' into allow-empty-workspace-run
tmatilai Apr 16, 2025
5c934cc
Merge branch 'main' into allow-empty-workspace-run
tmatilai Apr 22, 2025
e562d64
Merge branch 'main' into allow-empty-workspace-run
tmatilai Apr 24, 2025
3be7388
Merge branch 'main' into allow-empty-workspace-run
tmatilai May 22, 2025
4133881
Merge branch 'main' into allow-empty-workspace-run
tmatilai Jun 2, 2025
b3a6569
Merge branch 'main' into allow-empty-workspace-run
tmatilai Jun 24, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

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