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

data.azuread_service_principal: display name comparison should be case-insensitive #1381

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
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"
}
`
}
Loading