Skip to content

Commit

Permalink
databricks#3468: made changes to data_users.go to explicitly define s…
Browse files Browse the repository at this point in the history
…chema, and only request the id, userName, and displayName attributes. added succesful initial test_case on data_users_test.go
  • Loading branch information
dgomez04 committed Oct 22, 2024
1 parent 5c056b5 commit b2c7ccb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
25 changes: 16 additions & 9 deletions scim/data_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ import (

func DataSourceDataUsers() common.Resource {

type UserInfo struct {
Id string `json:"id,omitempty" tf:"computed"`
UserName string `json:"user_name,omitempty" tf:"computed"`
DisplayName string `json:"display_name,omitempty" tf:"computed"`
}

type DataUsers struct {
Id string `json:"id,omitempty" tf:"computed"`
DisplayNameContains string `json:"display_name_contains,omitempty" tf:"computed"`
Users []map[string]interface{} `json:"users,omitempty" tf:"computed"`
DisplayNameContains string `json:"display_name_contains,omitempty" tf:"computed"`
Users []UserInfo `json:"users,omitempty" tf:"computed"`
}

return common.AccountData(func(ctx context.Context, data *DataUsers, acc *databricks.AccountClient) error {
listRequest := iam.ListAccountUsersRequest{}
listRequest := iam.ListAccountUsersRequest{
Attributes: "id,userName,displayName",
}

if data.DisplayNameContains != "" {
listRequest.Filter = fmt.Sprintf("displayName co \"%s\"", data.DisplayNameContains)
Expand All @@ -34,13 +41,13 @@ func DataSourceDataUsers() common.Resource {
return fmt.Errorf("cannot find users with display name containing %s", data.DisplayNameContains)
}

var users []map[string]interface{}
var users []UserInfo

for _, u := range userList {
user := map[string]interface{}{
"id": u.Id,
"user_name": u.UserName,
"display_name": u.DisplayName,
user := UserInfo{
Id: u.Id,
UserName: u.UserName,
DisplayName: u.DisplayName,
}
users = append(users, user)
}
Expand Down
37 changes: 34 additions & 3 deletions scim/data_users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,50 @@ package scim
import (
"testing"

"github.com/databricks/databricks-sdk-go/experimental/mocks"
"github.com/databricks/databricks-sdk-go/service/iam"
"github.com/databricks/terraform-provider-databricks/qa"
"github.com/stretchr/testify/mock"
)

func TestDataSourceDataUsers(t *testing.T) {
qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
// TODO: run this test to get fixtures
MockAccountClientFunc: func(m *mocks.MockAccountClient) {
e := m.GetMockAccountUsersAPI()
e.On("ListAll",
mock.Anything,
mock.MatchedBy(func(req iam.ListAccountUsersRequest) bool {
return req.Filter == `displayName co "testuser"` &&
req.Attributes == "id,userName,displayName"
})).Return([]iam.User{
{
Id: "123",
UserName: "[email protected]",
DisplayName: "testuser",
},
{
Id: "456",
UserName: "[email protected]",
DisplayName: "testuser2",
},
}, nil)
},
Resource: DataSourceDataUsers(),
Read: true,
NonWritable: true,
ID: "_",
}.ApplyNoError(t)
HCL: `
display_name_contains = "testuser"
`,
}.ApplyAndExpectData(t, map[string]any{
"users.#": 2,
"users.0.id": "123",
"users.0.user_name": "[email protected]",
"users.0.display_name": "testuser",
"users.1.id": "456",
"users.1.user_name": "[email protected]",
"users.1.display_name": "testuser2",
})
}

func TestCatalogsData_Error(t *testing.T) {
Expand Down

0 comments on commit b2c7ccb

Please sign in to comment.