Skip to content

Commit

Permalink
azuread_service_principal: support client_id and deprecate `applica…
Browse files Browse the repository at this point in the history
…tion_id`
  • Loading branch information
manicminer committed Oct 18, 2023
1 parent 4ce7ad6 commit a6fa83e
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 117 deletions.
1 change: 1 addition & 0 deletions internal/services/applications/application_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func applicationDataSource() *pluginsdk.Resource {
Computed: true,
ExactlyOneOf: []string{"application_id", "client_id", "display_name", "object_id"},
ValidateDiagFunc: validation.ValidateDiag(validation.IsUUID),
Deprecated: "The `application_id` property has been replaced with the `client_id` property and will be removed in version 3.0 of the AzureAD provider",
},

"client_id": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ func TestAccApplicationDataSource_byObjectId(t *testing.T) {
})
}

func TestAccApplicationDataSource_byApplicationId(t *testing.T) {
func TestAccApplicationDataSource_byApplicationIdDeprecated(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azuread_application", "test")
r := ApplicationDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.applicationId(data),
Config: r.applicationIdDeprecated(data),
Check: r.testCheck(data),
},
})
Expand Down Expand Up @@ -101,7 +101,7 @@ data "azuread_application" "test" {
`, ApplicationResource{}.complete(data))
}

func (ApplicationDataSource) applicationId(data acceptance.TestData) string {
func (ApplicationDataSource) applicationIdDeprecated(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func applicationFederatedIdentityCredentialResource() *pluginsdk.Resource {
Computed: true,
ForceNew: true,
ExactlyOneOf: []string{"application_id", "application_object_id"},
Deprecated: "The `application_object_id` property has been replaced with the `application_id` property and will be removed in version 3.0 of the AzureAD provider",
ValidateFunc: validation.Any(validation.IsUUID, parse.ValidateApplicationID),
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,40 @@ func servicePrincipalData() *pluginsdk.Resource {

Schema: map[string]*pluginsdk.Schema{
"object_id": {
Description: "The object ID of the service principal",
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"application_id", "display_name", "object_id"},
ValidateDiagFunc: validation.ValidateDiag(validation.IsUUID),
Description: "The object ID of the service principal",
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"client_id", "application_id", "display_name", "object_id"},
ValidateFunc: validation.IsUUID,
},

"display_name": {
Description: "The display name of the application associated with this service principal",
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"application_id", "display_name", "object_id"},
ValidateDiagFunc: validation.ValidateDiag(validation.StringIsNotEmpty),
Description: "The display name of the application associated with this service principal",
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"client_id", "application_id", "display_name", "object_id"},
ValidateFunc: validation.StringIsNotEmpty,
},

"client_id": {
Description: "The client ID of the application associated with this service principal",
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"client_id", "application_id", "display_name", "object_id"},
ValidateFunc: validation.IsUUID,
},

"application_id": {
Description: "The application ID (client ID) of the application associated with this service principal",
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"application_id", "display_name", "object_id"},
ValidateDiagFunc: validation.ValidateDiag(validation.IsUUID),
Description: "The application ID (client ID) of the application associated with this service principal",
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"client_id", "application_id", "display_name", "object_id"},
ValidateFunc: validation.IsUUID,
Deprecated: "The `application_id` property has been replaced with the `client_id` property and will be removed in version 3.0 of the AzureAD provider",
},

"account_enabled": {
Expand Down Expand Up @@ -331,9 +341,15 @@ func servicePrincipalDataSourceRead(ctx context.Context, d *pluginsdk.ResourceDa
return tf.ErrorDiagF(nil, "No service principal found matching display name: %q", displayName)
}
} else {
applicationId := d.Get("application_id").(string)
var clientId string
if v := d.Get("client_id").(string); v != "" {
clientId = v
} else {
clientId = d.Get("application_id").(string)
}

query := odata.Query{
Filter: fmt.Sprintf("appId eq '%s'", applicationId),
Filter: fmt.Sprintf("appId eq '%s'", clientId),
}

result, _, err := client.List(ctx, query)
Expand All @@ -349,14 +365,14 @@ func servicePrincipalDataSourceRead(ctx context.Context, d *pluginsdk.ResourceDa
continue
}

if *sp.AppId == applicationId {
if *sp.AppId == clientId {
servicePrincipal = &sp
break
}
}

if servicePrincipal == nil {
return tf.ErrorDiagF(nil, "No service principal found for application ID: %q", applicationId)
return tf.ErrorDiagF(nil, "No service principal found for application ID: %q", clientId)
}
}

Expand All @@ -383,6 +399,7 @@ func servicePrincipalDataSourceRead(ctx context.Context, d *pluginsdk.ResourceDa
tf.Set(d, "app_roles", helpers.ApplicationFlattenAppRoles(servicePrincipal.AppRoles))
tf.Set(d, "application_id", servicePrincipal.AppId)
tf.Set(d, "application_tenant_id", servicePrincipal.AppOwnerOrganizationId)
tf.Set(d, "client_id", servicePrincipal.AppId)
tf.Set(d, "description", servicePrincipal.Description)
tf.Set(d, "display_name", servicePrincipal.DisplayName)
tf.Set(d, "feature_tags", helpers.ApplicationFlattenFeatures(servicePrincipal.Tags, false))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,25 @@ import (

type ServicePrincipalDataSource struct{}

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

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.byApplicationId(data),
Config: r.byClientId(data),
Check: r.testCheckFunc(data),
},
})
}

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

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.byDeprecatedApplicationId(data),
Check: r.testCheckFunc(data),
},
})
Expand Down Expand Up @@ -98,7 +110,17 @@ func (ServicePrincipalDataSource) testCheckFunc(data acceptance.TestData) accept
)
}

func (ServicePrincipalDataSource) byApplicationId(data acceptance.TestData) string {
func (ServicePrincipalDataSource) byClientId(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s
data "azuread_service_principal" "test" {
client_id = azuread_service_principal.test.client_id
}
`, ServicePrincipalResource{}.complete(data))
}

func (ServicePrincipalDataSource) byDeprecatedApplicationId(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s
Expand Down
Loading

0 comments on commit a6fa83e

Please sign in to comment.