Skip to content

Commit 5d7bfc2

Browse files
authored
Added users, service_principals to group data resource (#1103)
Fixes #1085
1 parent 5ddd6f3 commit 5d7bfc2

File tree

6 files changed

+132
-9
lines changed

6 files changed

+132
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66
* Added support for shared clusters in multi-task `databricks_job` ([#1082](https://github.com/databrickslabs/terraform-provider-databricks/issues/1082)).
77
* Added diff suppression for `external_id` in `databricks_group` ([#1099](https://github.com/databrickslabs/terraform-provider-databricks/issues/1099)).
88
* Added diff suppression for `external_id` in `databricks_user` ([#1097](https://github.com/databrickslabs/terraform-provider-databricks/issues/1097)).
9+
* Added `users`, `service_principals`, and `child_groups` exported properties to `databricks_group` data resource ([#1085](https://github.com/databrickslabs/terraform-provider-databricks/issues/1085)).
910
* Added various documentation improvements.
1011

12+
**Deprecations**
13+
14+
* `databricks_group`.`members` is deprecated in favor of `users`, `service_principals`, and `child_groups` exported properties. Please do slight modifications of your configuration.
15+
1116
Updated dependency versions:
1217

1318
* Bump google.golang.org/api from 0.66.0 to 0.67.0

docs/data-sources/group.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ Data source exposes the following attributes:
3939

4040
* `id` - The id for the group object.
4141
* `external_id` - ID of the group in an external identity provider.
42-
* `members` - Set of [user](../resources/user.md) identifiers, that can be modified with [databricks_group_member](../resources/group_member.md) resource.
42+
* `users` - Set of [databricks_user](../resources/user.md) identifiers, that can be modified with [databricks_group_member](../resources/group_member.md) resource.
43+
* `service_principals` - Set of [databricks_service_principal](../resources/service_principal.md) identifiers, that can be modified with [databricks_group_member](../resources/group_member.md) resource.
44+
* `child_groups` - Set of [databricks_group](../resources/group.md) identifiers, that can be modified with [databricks_group_member](../resources/group_member.md) resource.
4345
* `groups` - Set of [group](../resources/group.md) identifiers, that can be modified with [databricks_group_member](../resources/group_member.md) resource.
4446
* `instance_profiles` - Set of [instance profile](../resources/instance_profile.md) ARNs, that can be modified by [databricks_group_instance_profile](../resources/group_instance_profile.md) resource.
4547
* `allow_cluster_create` - True if group members can create [clusters](../resources/cluster.md)

scim/acceptance/data_group_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package acceptance
2+
3+
import (
4+
"context"
5+
"crypto/rand"
6+
"fmt"
7+
"os"
8+
"testing"
9+
10+
"github.com/databrickslabs/terraform-provider-databricks/common"
11+
"github.com/databrickslabs/terraform-provider-databricks/internal/acceptance"
12+
"github.com/databrickslabs/terraform-provider-databricks/qa"
13+
"github.com/databrickslabs/terraform-provider-databricks/scim"
14+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
15+
"github.com/stretchr/testify/assert"
16+
"github.com/stretchr/testify/require"
17+
)
18+
19+
func createUuid() string {
20+
b := make([]byte, 16)
21+
_, err := rand.Read(b)
22+
if err != nil {
23+
return "10000000-2000-3000-4000-500000000000"
24+
}
25+
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
26+
}
27+
28+
func TestAccGroupDataSplitMembers(t *testing.T) {
29+
if cloudEnv, ok := os.LookupEnv("CLOUD_ENV"); !ok && cloudEnv != "azure" {
30+
t.Skip("This test will only run on Azure. For simplicity.")
31+
}
32+
33+
ctx := context.Background()
34+
client := common.CommonEnvironmentClient()
35+
36+
usersAPI := scim.NewUsersAPI(ctx, client)
37+
groupsAPI := scim.NewGroupsAPI(ctx, client)
38+
spAPI := scim.NewServicePrincipalsAPI(ctx, client)
39+
40+
user, err := usersAPI.Create(scim.User{
41+
UserName: fmt.Sprintf("%[email protected]", qa.RandomName("tfuser-")),
42+
})
43+
assert.NoError(t, err)
44+
defer usersAPI.Delete(user.ID)
45+
46+
sp, err := spAPI.Create(scim.User{
47+
ApplicationID: createUuid(),
48+
DisplayName: qa.RandomName("spn-"),
49+
})
50+
assert.NoError(t, err)
51+
defer spAPI.Delete(sp.ID)
52+
53+
childGroup, err := groupsAPI.Create(scim.Group{
54+
DisplayName: qa.RandomName("child-"),
55+
})
56+
assert.NoError(t, err)
57+
defer groupsAPI.Delete(childGroup.ID)
58+
59+
parentGroup, err := groupsAPI.Create(scim.Group{
60+
DisplayName: qa.RandomName("parent-"),
61+
Members: []scim.ComplexValue{
62+
{Value: user.ID},
63+
{Value: sp.ID},
64+
{Value: childGroup.ID},
65+
},
66+
})
67+
assert.NoError(t, err)
68+
defer groupsAPI.Delete(parentGroup.ID)
69+
70+
acceptance.Test(t, []acceptance.Step{
71+
{
72+
Template: `data "databricks_group" "this" {
73+
display_name = "` + parentGroup.DisplayName + `"
74+
}`,
75+
Check: func(s *terraform.State) error {
76+
r, ok := s.Modules[0].Resources["data.databricks_group.this"]
77+
require.True(t, ok, "data.databricks_group.this has to be there")
78+
attr := r.Primary.Attributes
79+
assert.Equal(t, user.ID, attr["users.0"])
80+
assert.Equal(t, sp.ID, attr["service_principals.0"])
81+
assert.Equal(t, childGroup.ID, attr["child_groups.0"])
82+
return nil
83+
},
84+
},
85+
})
86+
}

scim/acceptance/resource_user_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ func TestAccForceUserImport(t *testing.T) {
2020
t.Skip("Acceptance tests skipped unless env 'CLOUD_ENV' is set")
2121
}
2222
username := qa.RandomEmail()
23-
os.Setenv("TEST_USERNAME", username)
23+
os.Setenv("TEST_USERNAME", username)
2424
ctx := context.Background()
2525
client := common.CommonEnvironmentClient()
2626
usersAPI := scim.NewUsersAPI(ctx, client)
2727
user, err := usersAPI.Create(scim.User{
28-
UserName: username,
28+
UserName: username,
2929
ExternalID: qa.RandomName("ext-id"),
3030
})
3131
assert.NoError(t, err)

scim/data_group.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package scim
33
import (
44
"context"
55
"sort"
6+
"strings"
67

78
"github.com/databrickslabs/terraform-provider-databricks/common"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -13,19 +14,23 @@ import (
1314
// DataSourceGroup returns information about group specified by display name
1415
func DataSourceGroup() *schema.Resource {
1516
type entity struct {
16-
DisplayName string `json:"display_name"`
17-
Recursive bool `json:"recursive,omitempty"`
18-
Members []string `json:"members,omitempty" tf:"slice_set,computed"`
19-
Groups []string `json:"groups,omitempty" tf:"slice_set,computed"`
20-
InstanceProfiles []string `json:"instance_profiles,omitempty" tf:"slice_set,computed"`
21-
ExternalID string `json:"external_id,omitempty" tf:"computed"`
17+
DisplayName string `json:"display_name"`
18+
Recursive bool `json:"recursive,omitempty"`
19+
Members []string `json:"members,omitempty" tf:"slice_set,computed"`
20+
Users []string `json:"users,omitempty" tf:"slice_set,computed"`
21+
ServicePrincipals []string `json:"service_principals,omitempty" tf:"slice_set,computed"`
22+
ChildGroups []string `json:"child_groups,omitempty" tf:"slice_set,computed"`
23+
Groups []string `json:"groups,omitempty" tf:"slice_set,computed"`
24+
InstanceProfiles []string `json:"instance_profiles,omitempty" tf:"slice_set,computed"`
25+
ExternalID string `json:"external_id,omitempty" tf:"computed"`
2226
}
2327

2428
s := common.StructToSchema(entity{}, func(
2529
s map[string]*schema.Schema) map[string]*schema.Schema {
2630
// nolint once SDKv2 has Diagnostics-returning validators, change
2731
s["display_name"].ValidateFunc = validation.StringIsNotEmpty
2832
s["recursive"].Default = true
33+
s["members"].Deprecated = "Please use `users`, `service_principals`, and `child_groups` instead"
2934
addEntitlementsToSchema(&s)
3035
return s
3136
})
@@ -47,6 +52,15 @@ func DataSourceGroup() *schema.Resource {
4752
queue = queue[1:]
4853
for _, x := range current.Members {
4954
this.Members = append(this.Members, x.Value)
55+
if strings.HasPrefix(x.Ref, "Users/") {
56+
this.Users = append(this.Users, x.Value)
57+
}
58+
if strings.HasPrefix(x.Ref, "Groups/") {
59+
this.ChildGroups = append(this.ChildGroups, x.Value)
60+
}
61+
if strings.HasPrefix(x.Ref, "ServicePrincipals/") {
62+
this.ServicePrincipals = append(this.ServicePrincipals, x.Value)
63+
}
5064
}
5165
for _, x := range current.Roles {
5266
this.InstanceProfiles = append(this.InstanceProfiles, x.Value)
@@ -66,6 +80,9 @@ func DataSourceGroup() *schema.Resource {
6680
this.ExternalID = group.ExternalID
6781
sort.Strings(this.Groups)
6882
sort.Strings(this.Members)
83+
sort.Strings(this.Users)
84+
sort.Strings(this.ChildGroups)
85+
sort.Strings(this.ServicePrincipals)
6986
sort.Strings(this.InstanceProfiles)
7087
err = common.StructToData(this, s, d)
7188
if err != nil {

scim/data_group_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,17 @@ func TestDataSourceGroup(t *testing.T) {
3636
},
3737
Members: []ComplexValue{
3838
{
39+
Ref: "Users/1112",
3940
Value: "1112",
4041
},
42+
{
43+
Ref: "ServicePrincipals/1113",
44+
Value: "1113",
45+
},
46+
{
47+
Ref: "Groups/1114",
48+
Value: "1114",
49+
},
4150
},
4251
Groups: []ComplexValue{
4352
{
@@ -89,4 +98,8 @@ func TestDataSourceGroup(t *testing.T) {
8998
assertContains(t, d.Get("groups"), "abc")
9099
assert.Equal(t, true, d.Get("allow_instance_pool_create"))
91100
assert.Equal(t, true, d.Get("allow_cluster_create"))
101+
102+
assertContains(t, d.Get("users"), "1112")
103+
assertContains(t, d.Get("service_principals"), "1113")
104+
assertContains(t, d.Get("child_groups"), "1114")
92105
}

0 commit comments

Comments
 (0)