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

feat: Data source for workspace tags in an organization #773

Merged
merged 16 commits into from
Feb 9, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased

FEATURES:
* **New Data Source**: d/tfe_organization_tags is a new data source to allow reading all workspace tags within an organization, by @rhughes1 ([#773](https://github.com/hashicorp/terraform-provider-tfe/pull/773))
* r/workspace, d/workspace: Add `source_name` and `source_url` to workspaces ([#527](https://github.com/hashicorp/terraform-provider-tfe/pull/527))

ENHANCEMENTS:
Expand Down
84 changes: 84 additions & 0 deletions tfe/data_source_organization_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package tfe

import (
"fmt"

tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceTFEOrganizationTags() *schema.Resource {
return &schema.Resource{
Read: dataSourceTFEOrganizationTagsRead,

Schema: map[string]*schema.Schema{
"organization": {
Type: schema.TypeString,
Optional: true,
},
"tags": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},

"id": {
Type: schema.TypeString,
Computed: true,
},

"workspace_count": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
}
}

func dataSourceTFEOrganizationTagsRead(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(ConfiguredClient)

organizationName, err := tfeClient.schemaOrDefaultOrganization(d)
if err != nil {
return err
}

var tags []map[string]interface{}

options := tfe.OrganizationTagsListOptions{}
for {
organizationTagsList, err := tfeClient.Client.OrganizationTags.List(ctx, organizationName, &options)
if err != nil {
return fmt.Errorf("Error retrieving organization tags: %w", err)
}

for _, orgTag := range organizationTagsList.Items {
tag := map[string]interface{}{
"id": orgTag.ID,
"name": orgTag.Name,
"workspace_count": orgTag.InstanceCount,
}
tags = append(tags, tag)
}

// Exit the loop when we've seen all pages.
if organizationTagsList.CurrentPage >= organizationTagsList.TotalPages {
break
}

// Update the page number to get the next page.
options.PageNumber = organizationTagsList.NextPage
}

d.Set("tags", tags)
d.SetId(organizationName)

return nil
}
63 changes: 63 additions & 0 deletions tfe/data_source_organization_tags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package tfe

import (
"fmt"
"math/rand"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccTFEOrganizationTagsDataSource_basic(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
orgName := fmt.Sprintf("tst-terraform-%d", rInt)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccTFEOrganizationTagsDataSourceConfig(rInt),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(
"data.tfe_organization_tags.foobar", "organization", orgName),
resource.TestCheckResourceAttr(
"data.tfe_organization_tags.foobar", "tags.0.name", "modules"),
resource.TestCheckResourceAttr(
"data.tfe_organization_tags.foobar", "tags.0.workspace_count", "1"),
),
},
},
})
}

func testAccTFEOrganizationTagsDataSourceConfig(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform-%d"
email = "admin@company.com"
}

resource "tfe_workspace" "foobar" {
name = "workspace-test-%d"
organization = tfe_organization.foobar.id
description = "provider-testing"
allow_destroy_plan = false
auto_apply = true
file_triggers_enabled = true
queue_all_runs = false
speculative_enabled = true
assessments_enabled = false
tag_names = ["modules"]
terraform_version = "0.11.1"
trigger_prefixes = ["/modules", "/shared"]
working_directory = "terraform/test"
global_remote_state = true
}

data "tfe_organization_tags" "foobar" {
organization = tfe_workspace.foobar.organization
depends_on = [tfe_workspace.foobar]
}`, rInt, rInt)
}
1 change: 1 addition & 0 deletions tfe/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func Provider() *schema.Provider {
"tfe_oauth_client": dataSourceTFEOAuthClient(),
"tfe_organization_membership": dataSourceTFEOrganizationMembership(),
"tfe_organization_run_task": dataSourceTFEOrganizationRunTask(),
"tfe_organization_tags": dataSourceTFEOrganizationTags(),
"tfe_slug": dataSourceTFESlug(),
"tfe_ssh_key": dataSourceTFESSHKey(),
"tfe_team": dataSourceTFETeam(),
Expand Down
36 changes: 36 additions & 0 deletions website/docs/d/organization_tags.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
layout: "tfe"
page_title: "Terraform Enterprise: tfe_organization_tags"
description: |-
Get information on an organization's workspace tags.
---

# Data Source: tfe_organization_tags

Use this data source to get information about the workspace tags for a given organization.

## Example Usage

```hcl
data "tfe_organization_tags" "example" {
organization = "my-org-name"
}
```

## Argument Reference

The following arguments are supported:

* `organization` - (Required) Name of the organization.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `tags` - A list of workspace tags within the organization

The `tag` block contains:

* `name` - The name of the workspace tag
* `id` - The ID of the workspace tag
* `workspace_count` - The number of workspaces the tag is associate with