Skip to content

Commit

Permalink
Merge branch 'main' into uk1288-add-public-modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Uk1288 committed Jul 19, 2022
2 parents 990ac91 + 2935495 commit dcdb8d8
Show file tree
Hide file tree
Showing 19 changed files with 548 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @hashicorp/tf-practitioner
* @hashicorp/tf-cli
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
## 0.33.0 (Unreleased)
## Unreleased

ENHANCEMENTS:
* d/agent_pool: Improve efficiency of reading agent pool data when the target organization has more than 20 agent pools ([#508](https://github.com/hashicorp/terraform-provider-tfe/pull/508))
* Added warning logs for 404 error responses ([#538](https://github.com/hashicorp/terraform-provider-tfe/pull/538))

## 0.33.0 (July 8th, 2022)

FEATURES:
* **New Resource**: `tfe_workspace_variable_set` ([#537](https://github.com/hashicorp/terraform-provider-tfe/pull/537)) adds the ability to assign a variable set to a workspace in a single, flexible resource.
* r/tfe_registry_module: Add ability to create both public and private `registry_modules` without VCS. ([#546](https://github.com/hashicorp/terraform-provider-tfe/pull/546))
* r/tfe_workspace, d/tfe_workspace: `trigger-patterns` ([#502](https://github.com/hashicorp/terraform-provider-tfe/pull/502)) attribute is introduced to support specifying a set of [glob patterns](https://www.terraform.io/cloud-docs/workspaces/settings/vcs#glob-patterns-for-automatic-run-triggering) for automatic VCS run triggering.

DEPRECATION NOTICE:
* The `workspace_ids` argument on `tfe_variable_set` has been labelled as deprecated and should not be used in conjunction with `tfe_workspace_variable_set`.
Expand Down Expand Up @@ -39,6 +46,7 @@ FEATURES:
* **New Data Source**: d/tfe_organization_run_task ([#488](https://github.com/hashicorp/terraform-provider-tfe/pull/488))
* **New Data Source**: d/tfe_workspace_run_task ([#488](https://github.com/hashicorp/terraform-provider-tfe/pull/488))
* r/tfe_notification_configuration: Add Microsoft Teams notification type ([#484](https://github.com/hashicorp/terraform-provider-tfe/pull/484))
* d/workspace_ids: Add `exclude_tags` to `tfe_workspace_ids` attributes ([#523](https://github.com/hashicorp/terraform-provider-tfe/pull/523))

## 0.31.0 (April 21, 2022)

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Declare the provider in your configuration and `terraform init` will automatical
terraform {
required_providers {
tfe = {
version = "~> 0.30.2"
version = "~> 0.33.0"
}
}
}
Expand All @@ -46,7 +46,7 @@ The above snippet using `required_providers` is for Terraform 0.13+; if you are

```hcl
provider "tfe" {
version = "~> 0.30.2"
version = "~> 0.33.0"
...
}
```
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-retryablehttp v0.7.1 // indirect
github.com/hashicorp/go-slug v0.9.1
github.com/hashicorp/go-tfe v1.4.0
github.com/hashicorp/go-tfe v1.5.0
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce
github.com/hashicorp/hcl/v2 v2.10.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ github.com/hashicorp/go-slug v0.9.1 h1:gYNVJ3t0jAWx8AT2eYZci3Xd7NBHyjayW9AR1DU4k
github.com/hashicorp/go-slug v0.9.1/go.mod h1:Ib+IWBYfEfJGI1ZyXMGNbu2BU+aa3Dzu41RKLH301v4=
github.com/hashicorp/go-tfe v1.4.0 h1:rMoQ2r1QppaglYsBdmdgFphipNTCkjTaO1oOLVj04Ec=
github.com/hashicorp/go-tfe v1.4.0/go.mod h1:E8a90lC4kjU5Lc2c0D+SnWhUuyuoCIVm4Ewzv3jCD3A=
github.com/hashicorp/go-tfe v1.5.0 h1:MtABkqH2s6lRFl8HaGt0qESLGAyrmMAFfecsEm+13K8=
github.com/hashicorp/go-tfe v1.5.0/go.mod h1:E8a90lC4kjU5Lc2c0D+SnWhUuyuoCIVm4Ewzv3jCD3A=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
Expand Down
6 changes: 5 additions & 1 deletion tfe/data_source_agent_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ func dataSourceTFEAgentPoolRead(d *schema.ResourceData, meta interface{}) error
organization := d.Get("organization").(string)

// Create an options struct.
options := tfe.AgentPoolListOptions{}
// to reduce the number of pages returned, search based on the name. TFE instances which
// do not support agent pool search will just ignore the query parameter
options := tfe.AgentPoolListOptions{
Query: name,
}

for {
l, err := tfeClient.AgentPools.List(ctx, organization, &options)
Expand Down
7 changes: 7 additions & 0 deletions tfe/data_source_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ func dataSourceTFEWorkspace() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
},

"trigger_patterns": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"working_directory": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -183,6 +189,7 @@ func dataSourceTFEWorkspaceRead(d *schema.ResourceData, meta interface{}) error
d.Set("structured_run_output_enabled", workspace.StructuredRunOutputEnabled)
d.Set("terraform_version", workspace.TerraformVersion)
d.Set("trigger_prefixes", workspace.TriggerPrefixes)
d.Set("trigger_patterns", workspace.TriggerPatterns)
d.Set("working_directory", workspace.WorkingDirectory)

// Set remote_state_consumer_ids if global_remote_state is false
Expand Down
36 changes: 33 additions & 3 deletions tfe/data_source_workspace_ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ func dataSourceTFEWorkspaceIDs() *schema.Resource {
Optional: true,
},

"exclude_tags": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
},

"organization": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -70,11 +76,27 @@ func dataSourceTFEWorkspaceIDsRead(d *schema.ResourceData, meta interface{}) err

options := &tfe.WorkspaceListOptions{}

excludeTagLookupMap := make(map[string]bool)
var excludeTagBuf strings.Builder
for _, excludedTag := range d.Get("exclude_tags").(*schema.Set).List() {
if exTag, ok := excludedTag.(string); ok && len(strings.TrimSpace(exTag)) != 0 {
excludeTagLookupMap[exTag] = true

if excludeTagBuf.Len() > 0 {
excludeTagBuf.WriteByte(',')
}
excludeTagBuf.WriteString(exTag)
}
}

if excludeTagBuf.Len() > 0 {
options.ExcludeTags = excludeTagBuf.String()
}

// Create a search string with all the tag names we are looking for.
var tagSearchParts []string
for _, tagName := range d.Get("tag_names").([]interface{}) {
name := tagName.(string)
if len(strings.TrimSpace(name)) != 0 {
if name, ok := tagName.(string); ok && len(strings.TrimSpace(name)) != 0 {
id += name // add to the state id
tagSearchParts = append(tagSearchParts, name)
}
Expand All @@ -94,7 +116,15 @@ func dataSourceTFEWorkspaceIDsRead(d *schema.ResourceData, meta interface{}) err

for _, w := range wl.Items {
nameIncluded := isWildcard || names[w.Name]
if hasOnlyTags || nameIncluded {
// fallback for tfe instances that don't yet support exclude-tags
hasExcludedTag := false
for _, tag := range w.TagNames {
if _, ok := excludeTagLookupMap[tag]; ok {
hasExcludedTag = true
break
}
}
if (hasOnlyTags || nameIncluded) && !hasExcludedTag {
fullNames[w.Name] = organization + "/" + w.Name
ids[w.Name] = w.ID
}
Expand Down
139 changes: 139 additions & 0 deletions tfe/data_source_workspace_ids_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,72 @@ func TestAccTFEWorkspaceIDsDataSource_namesEmpty(t *testing.T) {
})
}

func TestAccTFEWorkspaceIDsDataSource_excludeTags(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,
CheckDestroy: testAccCheckTFEWorkspaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEWorkspaceIDsDataSourceConfig_excludeTags(rInt),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(
"data.tfe_workspace_ids.good", "organization", orgName),

// full_names attribute
resource.TestCheckResourceAttr(
"data.tfe_workspace_ids.good", "full_names.%", "1"),
resource.TestCheckResourceAttr(
"data.tfe_workspace_ids.good",
fmt.Sprintf("full_names.workspace-bar-%d", rInt),
fmt.Sprintf("tst-terraform-%d/workspace-bar-%d", rInt, rInt),
),

// ids attribute
resource.TestCheckResourceAttr(
"data.tfe_workspace_ids.good", "ids.%", "1"),
resource.TestCheckResourceAttrSet(
"data.tfe_workspace_ids.good", fmt.Sprintf("ids.workspace-bar-%d", rInt)),

// id attribute
resource.TestCheckResourceAttrSet("data.tfe_workspace_ids.good", "id"),
),
},
},
})
}

func TestAccTFEWorkspaceIDsDataSource_sameTagInTagNamesAndExcludeTags(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,
CheckDestroy: testAccCheckTFEWorkspaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEWorkspaceIDsDataSourceConfig_sameTagInTagNamesAndExcludeTags(rInt),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(
"data.tfe_workspace_ids.good", "organization", orgName),

// full_names attribute should be empty
resource.TestCheckResourceAttr(
"data.tfe_workspace_ids.good", "full_names.%", "0"),

// ids attribute should be empty
resource.TestCheckResourceAttr(
"data.tfe_workspace_ids.good", "ids.%", "0"),
),
},
},
})
}

func testAccTFEWorkspaceIDsDataSourceConfig_basic(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
Expand Down Expand Up @@ -427,3 +493,76 @@ data "tfe_workspace_ids" "good" {
organization = tfe_workspace.foo.organization
}`, rInt, rInt, rInt, rInt)
}

func testAccTFEWorkspaceIDsDataSourceConfig_excludeTags(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform-%d"
email = "admin@company.com"
}
resource "tfe_workspace" "foo" {
name = "workspace-foo-%d"
organization = tfe_organization.foobar.id
tag_names = ["good", "happy"]
}
resource "tfe_workspace" "bar" {
name = "workspace-bar-%d"
organization = tfe_organization.foobar.id
tag_names = ["good"]
}
resource "tfe_workspace" "dummy" {
name = "workspace-dummy-%d"
organization = tfe_organization.foobar.id
}
data "tfe_workspace_ids" "good" {
tag_names = ["good"]
exclude_tags = ["happy"]
organization = tfe_workspace.foo.organization
depends_on = [
tfe_workspace.foo,
tfe_workspace.bar,
tfe_workspace.dummy
]
}`, rInt, rInt, rInt, rInt)
}

func testAccTFEWorkspaceIDsDataSourceConfig_sameTagInTagNamesAndExcludeTags(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform-%d"
email = "admin@company.com"
}
resource "tfe_workspace" "foo" {
name = "workspace-foo-%d"
organization = tfe_organization.foobar.id
tag_names = ["good", "happy"]
}
resource "tfe_workspace" "bar" {
name = "workspace-bar-%d"
organization = tfe_organization.foobar.id
tag_names = ["happy", "play"]
}
resource "tfe_workspace" "dummy" {
name = "workspace-dummy-%d"
organization = tfe_organization.foobar.id
tag_names = ["good", "play", "happy"]
}
data "tfe_workspace_ids" "good" {
tag_names = ["good", "happy"]
exclude_tags = ["happy"]
organization = tfe_workspace.foo.organization
depends_on = [
tfe_workspace.foo,
tfe_workspace.bar,
tfe_workspace.dummy
]
}`, rInt, rInt, rInt, rInt)
}
Loading

0 comments on commit dcdb8d8

Please sign in to comment.