Skip to content

Commit

Permalink
d/tfe_project: Ignore case when matching project name (#958)
Browse files Browse the repository at this point in the history
* Make project name match case-insensitive

* Update CHANGELOG

* Use EqualFold for string comparison
  • Loading branch information
jbonhag committed Jul 17, 2023
1 parent e2f12c0 commit 5695e84
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
FEATURES:
* **New Resource**: `d/tfe_saml_settings` is a new data source to retrieve SAML settings from the Admin API, by @karvounis-form3 [952](https://github.com/hashicorp/terraform-provider-tfe/pull/952)

BUG FIXES:
* `d/tfe_project`: Ignore case when matching project name from Projects List API, by @jbonhag [958](https://github.com/hashicorp/terraform-provider-tfe/pull/958)

## v0.46.0 (July 3, 2023)

FEATURES:
Expand Down
5 changes: 4 additions & 1 deletion tfe/data_source_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package tfe

import (
"context"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

tfe "github.com/hashicorp/go-tfe"
Expand Down Expand Up @@ -60,7 +62,8 @@ func dataSourceTFEProjectRead(ctx context.Context, d *schema.ResourceData, meta
}

for _, proj := range l.Items {
if proj.Name == projName {
// Case-insensitive uniqueness is enforced in TFC
if strings.EqualFold(proj.Name, projName) {
// Only now include workspaces to cut down on request load.
readOptions := &tfe.WorkspaceListOptions{
ProjectID: proj.ID,
Expand Down
44 changes: 44 additions & 0 deletions tfe/data_source_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@ func TestAccTFEProjectDataSource_basic(t *testing.T) {
})
}

func TestAccTFEProjectDataSource_caseInsensitive(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: testAccTFEProjectDataSourceConfigCaseInsensitive(rInt),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(
"data.tfe_project.foobar", "name", fmt.Sprintf("PROJECT-TEST-%d", rInt)),
resource.TestCheckResourceAttr(
"data.tfe_project.foobar", "organization", orgName),
resource.TestCheckResourceAttrSet("data.tfe_project.foobar", "id"),
),
},
},
})
}

func testAccTFEProjectDataSourceConfig(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
Expand Down Expand Up @@ -63,3 +85,25 @@ resource "tfe_workspace" "foobar" {
project_id = tfe_project.foobar.id
}`, rInt, rInt, rInt)
}

func testAccTFEProjectDataSourceConfigCaseInsensitive(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform-%d"
email = "[email protected]"
}
resource "tfe_project" "foobar" {
name = "project-test-%d"
organization = tfe_organization.foobar.id
}
data "tfe_project" "foobar" {
name = "PROJECT-TEST-%d"
organization = tfe_project.foobar.organization
# Read the data source after creating the project
depends_on = [
tfe_project.foobar
]
}`, rInt, rInt, rInt)
}

0 comments on commit 5695e84

Please sign in to comment.