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

d/tfe_project: Ignore case when matching project name #958

Merged
merged 3 commits into from
Jul 17, 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
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 = "admin@company.com"
}

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)
}
Loading