Skip to content

Commit

Permalink
Merge pull request #1381 from hashicorp/bugfix/service-principal-data…
Browse files Browse the repository at this point in the history
…-source-display-name-case-insensitivity

data.azuread_service_principal: display name comparison should be case-insensitive
  • Loading branch information
manicminer authored May 16, 2024
2 parents 071fcf7 + 7843d1a commit 2ac1624
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
"github.com/hashicorp/terraform-provider-azuread/internal/clients"
"github.com/hashicorp/terraform-provider-azuread/internal/helpers"
Expand Down Expand Up @@ -327,11 +328,7 @@ func servicePrincipalDataSourceRead(ctx context.Context, d *pluginsdk.ResourceDa
}

for _, sp := range *result {
if sp.DisplayName == nil {
continue
}

if *sp.DisplayName == displayName {
if strings.EqualFold(pointer.From(sp.DisplayName), displayName) {
servicePrincipal = &sp
break
}
Expand Down Expand Up @@ -361,11 +358,7 @@ func servicePrincipalDataSourceRead(ctx context.Context, d *pluginsdk.ResourceDa
}

for _, sp := range *result {
if sp.AppId == nil {
continue
}

if *sp.AppId == clientId {
if strings.EqualFold(pointer.From(sp.AppId), clientId) {
servicePrincipal = &sp
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ func TestAccServicePrincipalDataSource_byObjectId(t *testing.T) {
})
}

func TestAccServicePrincipalDataSource_builtInByDisplayName(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azuread_service_principal", "test")
r := ServicePrincipalDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.builtInByDisplayName(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("app_role_ids.%").MatchesRegex(regexp.MustCompile("[0-9]+")),
check.That(data.ResourceName).Key("app_roles.#").MatchesRegex(regexp.MustCompile("[0-9]+")),
check.That(data.ResourceName).Key("client_id").IsUuid(),
check.That(data.ResourceName).Key("display_name").Exists(),
check.That(data.ResourceName).Key("oauth2_permission_scope_ids.%").MatchesRegex(regexp.MustCompile("[0-9]+")),
check.That(data.ResourceName).Key("oauth2_permission_scopes.#").MatchesRegex(regexp.MustCompile("[0-9]+")),
),
},
})
}

func (ServicePrincipalDataSource) testCheckFunc(data acceptance.TestData) acceptance.TestCheckFunc {
tenantId := os.Getenv("ARM_TENANT_ID")
return acceptance.ComposeTestCheckFunc(
Expand All @@ -85,6 +104,7 @@ func (ServicePrincipalDataSource) testCheckFunc(data acceptance.TestData) accept
check.That(data.ResourceName).Key("app_roles.#").HasValue("2"),
check.That(data.ResourceName).Key("application_id").IsUuid(),
check.That(data.ResourceName).Key("application_tenant_id").HasValue(tenantId),
check.That(data.ResourceName).Key("client_id").IsUuid(),
check.That(data.ResourceName).Key("description").HasValue("An internal app for testing"),
check.That(data.ResourceName).Key("display_name").Exists(),
check.That(data.ResourceName).Key("feature_tags.#").HasValue("1"),
Expand Down Expand Up @@ -173,3 +193,13 @@ data "azuread_service_principal" "test" {
}
`, ServicePrincipalResource{}.complete(data))
}

func (ServicePrincipalDataSource) builtInByDisplayName(data acceptance.TestData) string {
return `
provider "azuread" {}
data "azuread_service_principal" "test" {
display_name = "MiCrOsOfT GrApH"
}
`
}

0 comments on commit 2ac1624

Please sign in to comment.