-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #773 from rhughes1/rhughes1-data-source-workspace-…
…tags feat: Data source for workspace tags in an organization
- Loading branch information
Showing
5 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |