Skip to content

Commit f58bf22

Browse files
authored
Allow to change display_name for databricks_service_principal (#3523)
* Allow to change `display_name` for `databricks_service_principal` We now allow to change display name of service principals, so we can remove `force_new` for it. Fixes #3519 * Address review comments and fix few tests
1 parent fb762de commit f58bf22

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

docs/resources/service_principal.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ resource "databricks_service_principal" "sp" {
9898
The following arguments are available:
9999

100100
- `application_id` This is the Azure Application ID of the given Azure service principal and will be their form of access and identity. For Databricks-managed service principals this value is auto-generated.
101-
- `display_name` - (Required) This is an alias for the service principal and can be the full name of the service principal.
101+
- `display_name` - (Required for Databricks-managed service principals) This is an alias for the service principal and can be the full name of the service principal.
102102
- `external_id` - (Optional) ID of the service principal in an external identity provider.
103103
- `allow_cluster_create` - (Optional) Allow the service principal to have [cluster](cluster.md) create privileges. Defaults to false. More fine grained permissions could be assigned with [databricks_permissions](permissions.md#Cluster-usage) and `cluster_id` argument. Everyone without `allow_cluster_create` argument set, but with [permission to use](permissions.md#Cluster-Policy-usage) Cluster Policy would be able to create clusters, but within the boundaries of that specific policy.
104104
- `allow_instance_pool_create` - (Optional) Allow the service principal to have [instance pool](instance_pool.md) create privileges. Defaults to false. More fine grained permissions could be assigned with [databricks_permissions](permissions.md#Instance-Pool-usage) and [instance_pool_id](permissions.md#instance_pool_id) argument.

internal/acceptance/service_principal_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package acceptance
33
import (
44
"context"
55
"fmt"
6+
"strings"
67
"testing"
78

89
"github.com/databricks/databricks-sdk-go"
@@ -91,28 +92,40 @@ func TestAccServicePrinicpalHomeDeleteNotDeleted(t *testing.T) {
9192

9293
func TestMwsAccServicePrincipalResourceOnAzure(t *testing.T) {
9394
GetEnvOrSkipTest(t, "ARM_CLIENT_ID")
95+
azureSpnRenamed := strings.ReplaceAll(azureSpn, `"SPN `, `"SPN Renamed `)
9496
accountLevel(t, step{
9597
Template: azureSpn,
98+
}, step{
99+
Template: azureSpnRenamed,
96100
})
97101
}
98102

99103
func TestAccServicePrincipalResourceOnAzure(t *testing.T) {
100104
GetEnvOrSkipTest(t, "ARM_CLIENT_ID")
105+
azureSpnRenamed := strings.ReplaceAll(azureSpn, `"SPN `, `"SPN Renamed `)
101106
workspaceLevel(t, step{
102107
Template: azureSpn,
108+
}, step{
109+
Template: azureSpnRenamed,
103110
})
104111
}
105112

106113
func TestMwsAccServicePrincipalResourceOnAws(t *testing.T) {
107114
GetEnvOrSkipTest(t, "TEST_ROOT_BUCKET")
115+
awsSpnRenamed := strings.ReplaceAll(awsSpn, `"SPN `, `"SPN Renamed `)
108116
accountLevel(t, step{
109117
Template: awsSpn,
118+
}, step{
119+
Template: awsSpnRenamed,
110120
})
111121
}
112122

113123
func TestAccServicePrincipalResourceOnAws(t *testing.T) {
114124
GetEnvOrSkipTest(t, "TEST_EC2_INSTANCE_PROFILE")
125+
awsSpnRenamed := strings.ReplaceAll(awsSpn, `"SPN `, `"SPN Renamed `)
115126
workspaceLevel(t, step{
116127
Template: awsSpn,
128+
}, step{
129+
Template: awsSpnRenamed,
117130
})
118131
}

scim/resource_service_principal.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (a ServicePrincipalsAPI) Delete(servicePrincipalID string) error {
9999
func ResourceServicePrincipal() common.Resource {
100100
type entity struct {
101101
ApplicationID string `json:"application_id,omitempty" tf:"computed,force_new"`
102-
DisplayName string `json:"display_name,omitempty" tf:"computed,force_new"`
102+
DisplayName string `json:"display_name,omitempty" tf:"computed"`
103103
Active bool `json:"active,omitempty"`
104104
ExternalID string `json:"external_id,omitempty" tf:"suppress_diff"`
105105
}
@@ -138,6 +138,8 @@ func ResourceServicePrincipal() common.Resource {
138138
Optional: true,
139139
Computed: true,
140140
}
141+
m["application_id"].AtLeastOneOf = []string{"application_id", "display_name"}
142+
m["display_name"].AtLeastOneOf = []string{"application_id", "display_name"}
141143
return m
142144
})
143145
spFromData := func(d *schema.ResourceData) User {

scim/resource_service_principal_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ func TestResourceServicePrincipalCreate(t *testing.T) {
141141
}.ApplyNoError(t)
142142
}
143143

144+
func TestResourceServicePrincipalCreate_ErrorNoDisplayNameOrAppId(t *testing.T) {
145+
qa.ResourceFixture{
146+
Resource: ResourceServicePrincipal(),
147+
Create: true,
148+
HCL: `
149+
allow_cluster_create = true
150+
`,
151+
}.ExpectError(t, "invalid config supplied. [application_id] Missing required argument. [display_name] Missing required argument")
152+
}
153+
144154
func TestResourceServicePrincipalCreate_Error(t *testing.T) {
145155
qa.ResourceFixture{
146156
Fixtures: qa.HTTPFailures,
@@ -541,7 +551,7 @@ func TestResourceServicePrincipalDelete_NonExistingDir(t *testing.T) {
541551
}
542552

543553
func TestCreateForceOverridesManuallyAddedServicePrincipalErrorNotMatched(t *testing.T) {
544-
d := ResourceUser().ToResource().TestResourceData()
554+
d := ResourceServicePrincipal().ToResource().TestResourceData()
545555
d.Set("force", true)
546556
rerr := createForceOverridesManuallyAddedServicePrincipal(
547557
fmt.Errorf("nonsense"), d,
@@ -561,7 +571,7 @@ func TestCreateForceOverwriteCannotListServicePrincipals(t *testing.T) {
561571
},
562572
},
563573
}, func(ctx context.Context, client *common.DatabricksClient) {
564-
d := ResourceUser().ToResource().TestResourceData()
574+
d := ResourceServicePrincipal().ToResource().TestResourceData()
565575
d.Set("force", true)
566576
err := createForceOverridesManuallyAddedServicePrincipal(
567577
fmt.Errorf("Service principal with application ID %s already exists.", appID),
@@ -624,7 +634,7 @@ func TestCreateForceOverwriteFindsAndSetsServicePrincipalID(t *testing.T) {
624634
},
625635
},
626636
}, func(ctx context.Context, client *common.DatabricksClient) {
627-
d := ResourceUser().ToResource().TestResourceData()
637+
d := ResourceServicePrincipal().ToResource().TestResourceData()
628638
d.Set("force", true)
629639
d.Set("application_id", appID)
630640
err := createForceOverridesManuallyAddedServicePrincipal(

0 commit comments

Comments
 (0)