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

Get resource_count information for configured tfe_workspace resources #448

Closed
lucymhdavies opened this issue Mar 22, 2022 · 3 comments
Closed

Comments

@lucymhdavies
Copy link
Contributor

Use-cases

When reading a workspace with the tfe_workspace data source, it is possible to identify how many resources that workspace is currently managing.
See https://registry.terraform.io/providers/hashicorp/tfe/latest/docs/data-sources/workspace#resource_count

However, when we create the workspace with the tfe_workspace resource, we do not get that information back in the attributes.

While it is already possible to write a sentinel policy that prevents deletion of workspaces:
hashicorp/terraform-sentinel-policies#3
Not having the resource_count attribute available in the tfe_workspace resource prevents us from writing a more complex sentinel policy, along the lines of "do not allow deletion of a workspace, unless it currently does not manage any resources"

Proposal

As part of dataSourceTFEWorkspaceRead we have:
https://github.com/hashicorp/terraform-provider-tfe/blob/main/tfe/data_source_workspace.go#L179

	d.Set("resource_count", workspace.ResourceCount)

We could add that to the resource schema as Computed: true, and include that same d.Set in resourceTFEWorkspaceRead to make that information available:
https://github.com/hashicorp/terraform-provider-tfe/blob/main/tfe/resource_tfe_workspace.go#L332

Other attributes read by the data source could be added too, if they would be useful, though resource_count is the one I have an immediate use-case for.

If this seems like a sensible approach, I can raise a PR to implement this.

@lucymhdavies
Copy link
Contributor Author

That idea appears to work.

With this change:

diff --git a/tfe/resource_tfe_workspace.go b/tfe/resource_tfe_workspace.go
index d94d79d..0cf297e 100644
--- a/tfe/resource_tfe_workspace.go
+++ b/tfe/resource_tfe_workspace.go
@@ -203,6 +203,11 @@ func resourceTFEWorkspace() *schema.Resource {
                                        },
                                },
                        },
+
+                       "resource_count": {
+                               Type:     schema.TypeInt,
+                               Computed: true,
+                       },
                },
        }
 }
@@ -345,6 +350,8 @@ func resourceTFEWorkspaceRead(d *schema.ResourceData, meta interface{}) error {
        d.Set("working_directory", workspace.WorkingDirectory)
        d.Set("organization", workspace.Organization.Name)

+       d.Set("resource_count", workspace.ResourceCount)
+
        var sshKeyID string
        if workspace.SSHKey != nil {
                sshKeyID = workspace.SSHKey.ID

And this added to my Terraform configuration:

output "workspace_resource_count" {
  value = tfe_workspace.workspace["webserver-aws-test"].resource_count
}

Then...

$ terraform refresh
...
Outputs:

workspace_resource_count = 34

So I expect adding something similar for the other attributes available in the data source would work similarly.

@annawinkler
Copy link
Contributor

Issue addressed via #682

@lucymhdavies
Copy link
Contributor Author

lucymhdavies commented Dec 8, 2022

Awesome stuff!

That'll definitely be useful, though my use case in particular is actually solved by a much more useful feature we recently added: Safe Delete!
hashicorp/terraform#31949 for other folks' reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants