Skip to content

Commit ea58f99

Browse files
committed
Fixed #656 - Make application_id optional for non-Azure deployments
1 parent 16c1422 commit ea58f99

File tree

5 files changed

+51
-74
lines changed

5 files changed

+51
-74
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Added the `databricks_user` data source ([#648](https://github.com/databrickslabs/terraform-provider-databricks/pull/648))
88
* Fixed support for `spot_instance_policy` in SQLA Endpoints ([#665](https://github.com/databrickslabs/terraform-provider-databricks/issues/665))
99
* Added documentation for `databricks_pipeline` resource ([#673](https://github.com/databrickslabs/terraform-provider-databricks/pull/673))
10+
* Fixed mapping for `databricks_service_principal` on AWS ([#656](https://github.com/databrickslabs/terraform-provider-databricks/issues/656))
1011
* Made preview environment tests to run on a release basis
1112

1213
Updated dependency versions:

docs/resources/service_principal.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ subcategory: "Security"
33
---
44
# databricks_service_principal Resource
55

6-
Directly creates service principal, that could be added to [databricks_group](group.md) within workspace.
6+
Directly creates a service principal that could be added to [databricks_group](group.md) within workspace.
77

88
## Example Usage
99

@@ -19,7 +19,7 @@ Creating service principal with administrative permissions - referencing special
1919

2020
```hcl
2121
data "databricks_group" "admins" {
22-
display_name = "admins"
22+
display_name = "admins"
2323
}
2424
2525
resource "databricks_service_principal" "sp" {
@@ -44,12 +44,14 @@ resource "databricks_service_principal" "sp" {
4444

4545
## Argument Reference
4646

47+
-> `application_id` is required on Azure Databricks and is not allowed on other clouds. `display_name` is required on all clouds except Azure.
48+
4749
The following arguments are available:
4850

49-
* `application_id` - (Required) This is the application id of the given service principal and will be their form of access and identity.
50-
* `display_name` - (Optional) This is an alias for the service principal can be the full name of the service principal.
51-
* `allow_cluster_create` - (Optional) Allow the service principal to have [cluster](cluster.md) create priviliges. 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` arugment set, but with [permission to use](permissions.md#Cluster-Policy-usage) Cluster Policy would be able to create clusters, but within boundaries of that specific policy.
52-
* `allow_instance_pool_create` - (Optional) Allow the service principal to have [instance pool](instance_pool.md) create priviliges. 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.
51+
* `application_id` - This is the application id of the given service principal and will be their form of access and identity. On other clouds than Azure this value is auto-generated.
52+
* `display_name` - (Required) This is an alias for the service principal and can be the full name of the service principal.
53+
* `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.
54+
* `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.
5355
* `active` - (Optional) Either service principal is active or not. True by default, but can be set to false in case of service principal deactivation with preserving service principal assets.
5456

5557
## Attribute Reference
Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,28 @@
11
package acceptance
22

33
import (
4-
"os"
54
"testing"
65

76
"github.com/databrickslabs/terraform-provider-databricks/internal/acceptance"
8-
"github.com/databrickslabs/terraform-provider-databricks/qa"
9-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
107
)
118

129
func TestAzureAccServicePrincipalResource(t *testing.T) {
13-
if _, ok := os.LookupEnv("CLOUD_ENV"); !ok {
14-
t.Skip("Acceptance tests skipped unless env 'CLOUD_ENV' is set")
15-
}
16-
config := qa.EnvironmentTemplate(t, `
17-
data "databricks_group" "admins" {
18-
display_name = "admins"
19-
}
20-
resource "databricks_service_principal" "sp_first" {
21-
application_id = "00000000-1234-5678-0000-000000000001"
22-
display_name = "Eerste {var.RANDOM}"
23-
}
24-
resource "databricks_service_principal" "sp_second" {
25-
application_id = "00000000-1234-5678-0000-000000000002"
26-
display_name = "Tweede {var.RANDOM}"
27-
allow_cluster_create = true
28-
}
29-
resource "databricks_service_principal" "sp_third" {
30-
application_id = "00000000-1234-5678-0000-000000000003"
31-
allow_instance_pool_create = true
32-
}`)
33-
acceptance.AccTest(t, resource.TestCase{
34-
Steps: []resource.TestStep{
35-
{
36-
Config: config,
37-
Check: resource.ComposeTestCheckFunc(
38-
resource.TestCheckResourceAttr("databricks_service_principal.sp_first", "allow_cluster_create", "false"),
39-
resource.TestCheckResourceAttr("databricks_service_principal.sp_first", "allow_instance_pool_create", "false"),
40-
resource.TestCheckResourceAttr("databricks_service_principal.sp_second", "allow_cluster_create", "true"),
41-
resource.TestCheckResourceAttr("databricks_service_principal.sp_second", "allow_instance_pool_create", "false"),
42-
resource.TestCheckResourceAttr("databricks_service_principal.sp_third", "allow_cluster_create", "false"),
43-
resource.TestCheckResourceAttr("databricks_service_principal.sp_third", "allow_instance_pool_create", "true"),
44-
),
45-
Destroy: false,
46-
},
10+
acceptance.Test(t, []acceptance.Step{
11+
{
12+
Template: `resource "databricks_service_principal" "this" {
13+
application_id = "00000000-1234-5678-0000-000000000001"
14+
display_name = "SPN {var.RANDOM}"
15+
}`,
16+
},
17+
})
18+
}
19+
20+
func TestAwsAccServicePrincipalResource(t *testing.T) {
21+
acceptance.Test(t, []acceptance.Step{
22+
{
23+
Template: `resource "databricks_service_principal" "this" {
24+
display_name = "SPN {var.RANDOM}"
25+
}`,
4726
},
4827
})
4928
}

identity/resource_service_principal.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type ServicePrincipalsAPI struct {
2222

2323
// ServicePrincipalEntity entity from which resource schema is made
2424
type ServicePrincipalEntity struct {
25-
ApplicationID string `json:"application_id"`
25+
ApplicationID string `json:"application_id,omitempty" tf:"computed"`
2626
DisplayName string `json:"display_name,omitempty" tf:"computed"`
2727
Active bool `json:"active,omitempty"`
2828
AllowClusterCreate bool `json:"allow_cluster_create,omitempty"`
@@ -116,6 +116,15 @@ func ResourceServicePrincipal() *schema.Resource {
116116
if err := common.DataToStructPointer(d, servicePrincipalSchema, &sp); err != nil {
117117
return err
118118
}
119+
if c.IsAzure() && sp.ApplicationID == "" {
120+
return fmt.Errorf("application_id is required for service principals in Azure Databricks")
121+
}
122+
if c.IsAws() && sp.ApplicationID != "" {
123+
return fmt.Errorf("application_id is not allowed for service principals in Databricks on AWS")
124+
}
125+
if c.IsAws() && sp.DisplayName == "" {
126+
return fmt.Errorf("display_name is required for service principals in Databricks on AWS")
127+
}
119128
servicePrincipal, err := NewServicePrincipalsAPI(ctx, c).CreateR(sp)
120129
if err != nil {
121130
return err

identity/resource_service_principal_test.go

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
func TestAzureAccSP(t *testing.T) {
16-
if _, ok := os.LookupEnv("CLOUD_ENV"); !ok {
16+
if cloud, ok := os.LookupEnv("CLOUD_ENV"); !ok || cloud != "azure" {
1717
t.Skip("Acceptance tests skipped unless env 'CLOUD_ENV' is set")
1818
}
1919

@@ -51,9 +51,8 @@ func TestResourceServicePrincipalRead(t *testing.T) {
5151
Method: "GET",
5252
Resource: "/api/2.0/preview/scim/v2/ServicePrincipals/abc",
5353
Response: ScimUser{
54-
ID: "abc",
55-
DisplayName: "Example Service Principal",
56-
ApplicationID: "00000000-0000-0000-0000-000000000000",
54+
ID: "abc",
55+
DisplayName: "Example Service Principal",
5756
Groups: []GroupsListItem{
5857
{
5958
Display: "admins",
@@ -74,7 +73,6 @@ func TestResourceServicePrincipalRead(t *testing.T) {
7473
}.Apply(t)
7574
require.NoError(t, err, err)
7675
assert.Equal(t, "abc", d.Id(), "Id should not be empty")
77-
assert.Equal(t, "00000000-0000-0000-0000-000000000000", d.Get("application_id"))
7876
assert.Equal(t, "Example Service Principal", d.Get("display_name"))
7977
assert.Equal(t, false, d.Get("allow_cluster_create"))
8078
}
@@ -132,8 +130,7 @@ func TestResourceServicePrincipalCreate(t *testing.T) {
132130
Value: "allow-cluster-create",
133131
},
134132
},
135-
ApplicationID: "00000000-0000-0000-0000-000000000000",
136-
Schemas: []URN{ServicePrincipalSchema},
133+
Schemas: []URN{ServicePrincipalSchema},
137134
},
138135
Response: ScimUser{
139136
ID: "abc",
@@ -143,10 +140,9 @@ func TestResourceServicePrincipalCreate(t *testing.T) {
143140
Method: "GET",
144141
Resource: "/api/2.0/preview/scim/v2/ServicePrincipals/abc",
145142
Response: ScimUser{
146-
DisplayName: "Example Service Principal",
147-
Active: true,
148-
ApplicationID: "00000000-0000-0000-0000-000000000000",
149-
ID: "abc",
143+
DisplayName: "Example Service Principal",
144+
Active: true,
145+
ID: "abc",
150146
Entitlements: []entitlementsListItem{
151147
{
152148
Value: AllowClusterCreateEntitlement,
@@ -168,14 +164,12 @@ func TestResourceServicePrincipalCreate(t *testing.T) {
168164
Resource: ResourceServicePrincipal(),
169165
Create: true,
170166
HCL: `
171-
application_id = "00000000-0000-0000-0000-000000000000"
172167
display_name = "Example Service Principal"
173168
allow_cluster_create = true
174169
`,
175170
}.Apply(t)
176171
require.NoError(t, err, err)
177172
assert.Equal(t, "abc", d.Id(), "Id should not be empty")
178-
assert.Equal(t, "00000000-0000-0000-0000-000000000000", d.Get("application_id"))
179173
assert.Equal(t, "Example Service Principal", d.Get("display_name"))
180174
assert.Equal(t, true, d.Get("allow_cluster_create"))
181175
}
@@ -192,7 +186,6 @@ func TestResourceServicePrincipalCreate_Error(t *testing.T) {
192186
Resource: ResourceServicePrincipal(),
193187
Create: true,
194188
HCL: `
195-
application_id = "00000000-0000-0000-0000-000000000000"
196189
display_name = "Example Service Principal"
197190
allow_cluster_create = true
198191
`,
@@ -202,10 +195,9 @@ func TestResourceServicePrincipalCreate_Error(t *testing.T) {
202195

203196
func TestResourceServicePrincipalUpdate(t *testing.T) {
204197
newServicePrincipal := ScimUser{
205-
Schemas: []URN{ServicePrincipalSchema},
206-
DisplayName: "Changed Name",
207-
ApplicationID: "00000000-0000-0000-0000-000000000000",
208-
Active: true,
198+
Schemas: []URN{ServicePrincipalSchema},
199+
DisplayName: "Changed Name",
200+
Active: true,
209201
Entitlements: []entitlementsListItem{
210202
{
211203
Value: AllowInstancePoolCreateEntitlement,
@@ -228,10 +220,9 @@ func TestResourceServicePrincipalUpdate(t *testing.T) {
228220
Method: "GET",
229221
Resource: "/api/2.0/preview/scim/v2/ServicePrincipals/abc",
230222
Response: ScimUser{
231-
DisplayName: "Example Service Principal",
232-
Active: true,
233-
ApplicationID: "00000000-0000-0000-0000-000000000000",
234-
ID: "abc",
223+
DisplayName: "Example Service Principal",
224+
Active: true,
225+
ID: "abc",
235226
Entitlements: []entitlementsListItem{
236227
{
237228
Value: AllowClusterCreateEntitlement,
@@ -264,15 +255,13 @@ func TestResourceServicePrincipalUpdate(t *testing.T) {
264255
Update: true,
265256
ID: "abc",
266257
HCL: `
267-
application_id = "00000000-0000-0000-0000-000000000000"
268258
display_name = "Changed Name"
269259
allow_cluster_create = false
270260
allow_instance_pool_create = true
271261
`,
272262
}.Apply(t)
273263
require.NoError(t, err, err)
274264
assert.Equal(t, "abc", d.Id(), "Id should not be empty")
275-
assert.Equal(t, "00000000-0000-0000-0000-000000000000", d.Get("application_id"))
276265
assert.Equal(t, "Changed Name", d.Get("display_name"))
277266
assert.Equal(t, false, d.Get("allow_cluster_create"))
278267
assert.Equal(t, true, d.Get("allow_instance_pool_create"))
@@ -291,7 +280,6 @@ func TestResourceServicePrincipalUpdate_Error(t *testing.T) {
291280
Update: true,
292281
ID: "abc",
293282
HCL: `
294-
application_id = "00000000-0000-0000-0000-000000000000"
295283
display_name = "Changed Name"
296284
allow_cluster_create = false
297285
allow_instance_pool_create = true
@@ -307,10 +295,9 @@ func TestResourceServicePrincipalUpdate_ErrorPut(t *testing.T) {
307295
Method: "GET",
308296
Resource: "/api/2.0/preview/scim/v2/ServicePrincipals/abc",
309297
Response: ScimUser{
310-
DisplayName: "Example Service Principal",
311-
Active: true,
312-
ApplicationID: "00000000-0000-0000-0000-000000000000",
313-
ID: "abc",
298+
DisplayName: "Example Service Principal",
299+
Active: true,
300+
ID: "abc",
314301
Entitlements: []entitlementsListItem{
315302
{
316303
Value: AllowClusterCreateEntitlement,
@@ -338,7 +325,6 @@ func TestResourceServicePrincipalUpdate_ErrorPut(t *testing.T) {
338325
Update: true,
339326
ID: "abc",
340327
HCL: `
341-
application_id = "00000000-0000-0000-0000-000000000000"
342328
display_name = "Changed Name"
343329
allow_cluster_create = false
344330
allow_instance_pool_create = true

0 commit comments

Comments
 (0)