From 7843d1a879d4e7d28a7c48ff08130adc04a740a2 Mon Sep 17 00:00:00 2001 From: Tom Bamford Date: Thu, 16 May 2024 14:55:57 +0100 Subject: [PATCH] data.azuread_service_principal: display name comparison should be case-insensitive --- .../service_principal_data_source.go | 13 ++------ .../service_principal_data_source_test.go | 30 +++++++++++++++++++ 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/internal/services/serviceprincipals/service_principal_data_source.go b/internal/services/serviceprincipals/service_principal_data_source.go index db9f0479d1..fba3c3022f 100644 --- a/internal/services/serviceprincipals/service_principal_data_source.go +++ b/internal/services/serviceprincipals/service_principal_data_source.go @@ -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" @@ -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 } @@ -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 } diff --git a/internal/services/serviceprincipals/service_principal_data_source_test.go b/internal/services/serviceprincipals/service_principal_data_source_test.go index b5bc786139..d1ee9e3e55 100644 --- a/internal/services/serviceprincipals/service_principal_data_source_test.go +++ b/internal/services/serviceprincipals/service_principal_data_source_test.go @@ -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( @@ -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"), @@ -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" +} +` +}