From 5695e84426bd59164074085a2d9e7384b05e1ff3 Mon Sep 17 00:00:00 2001 From: Jeff Bonhag Date: Mon, 17 Jul 2023 11:40:22 -0400 Subject: [PATCH] `d/tfe_project`: Ignore case when matching project name (#958) * Make project name match case-insensitive * Update CHANGELOG * Use EqualFold for string comparison --- CHANGELOG.md | 3 +++ tfe/data_source_project.go | 5 +++- tfe/data_source_project_test.go | 44 +++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43068fee3..f0b4564bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: diff --git a/tfe/data_source_project.go b/tfe/data_source_project.go index 4fcdd2118..96aac25c2 100644 --- a/tfe/data_source_project.go +++ b/tfe/data_source_project.go @@ -8,6 +8,8 @@ package tfe import ( "context" + "strings" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" tfe "github.com/hashicorp/go-tfe" @@ -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, diff --git a/tfe/data_source_project_test.go b/tfe/data_source_project_test.go index 650603157..e7e6cca27 100644 --- a/tfe/data_source_project_test.go +++ b/tfe/data_source_project_test.go @@ -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" { @@ -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) +}