Skip to content

Commit 43ead70

Browse files
authored
Fix integration tests for databricks_entitlements (#3467)
* work * More work
1 parent e7c8ff0 commit 43ead70

File tree

3 files changed

+313
-163
lines changed

3 files changed

+313
-163
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,7 @@ scripts/tt
342342

343343
.metals
344344

345-
provider/completeness.md
345+
provider/completeness.md
346+
347+
# VS Code debugging binaries
348+
__debug_bin*
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
package acceptance
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/databricks/databricks-sdk-go"
8+
"github.com/databricks/databricks-sdk-go/httpclient"
9+
"github.com/databricks/databricks-sdk-go/service/iam"
10+
)
11+
12+
type entitlementResource interface {
13+
resourceType() string
14+
setDisplayName(string)
15+
setWorkspaceClient(*databricks.WorkspaceClient)
16+
create(context.Context) error
17+
getEntitlements(context.Context) ([]iam.ComplexValue, error)
18+
cleanUp(context.Context) error
19+
dataSourceTemplate() string
20+
tfReference() string
21+
}
22+
23+
type userResource struct {
24+
email string
25+
id string
26+
w *databricks.WorkspaceClient
27+
}
28+
29+
func (u *userResource) resourceType() string {
30+
return "user"
31+
}
32+
33+
func (u *userResource) setDisplayName(displayName string) {
34+
u.email = displayName + "@example.com"
35+
}
36+
37+
func (u *userResource) setWorkspaceClient(w *databricks.WorkspaceClient) {
38+
u.w = w
39+
}
40+
41+
func (u *userResource) create(ctx context.Context) error {
42+
user, err := u.w.Users.Create(ctx, iam.User{
43+
UserName: u.email,
44+
})
45+
if err != nil {
46+
return err
47+
}
48+
u.id = user.Id
49+
return nil
50+
}
51+
52+
func (u *userResource) getEntitlements(ctx context.Context) ([]iam.ComplexValue, error) {
53+
c, err := u.w.Config.NewApiClient()
54+
if err != nil {
55+
return nil, err
56+
}
57+
res := iam.User{}
58+
err = c.Do(ctx, "GET", fmt.Sprintf("/api/2.0/preview/scim/v2/Users/%s?attributes=entitlements", u.id),
59+
httpclient.WithResponseUnmarshal(&res))
60+
if err != nil {
61+
return nil, err
62+
}
63+
return res.Entitlements, nil
64+
}
65+
66+
func (u *userResource) cleanUp(ctx context.Context) error {
67+
return u.w.Users.DeleteById(ctx, u.id)
68+
}
69+
70+
func (u *userResource) dataSourceTemplate() string {
71+
return fmt.Sprintf(`
72+
data "databricks_user" "example" {
73+
user_name = "%s"
74+
}`, u.email)
75+
}
76+
77+
func (u *userResource) tfReference() string {
78+
return "user_id = data.databricks_user.example.id"
79+
}
80+
81+
type groupResource struct {
82+
displayName string
83+
id string
84+
w *databricks.WorkspaceClient
85+
}
86+
87+
func (g *groupResource) resourceType() string {
88+
return "group"
89+
}
90+
91+
func (g *groupResource) setDisplayName(displayName string) {
92+
g.displayName = displayName
93+
}
94+
95+
func (g *groupResource) setWorkspaceClient(w *databricks.WorkspaceClient) {
96+
g.w = w
97+
}
98+
99+
func (g *groupResource) create(ctx context.Context) error {
100+
group, err := g.w.Groups.Create(ctx, iam.Group{
101+
DisplayName: g.displayName,
102+
})
103+
if err != nil {
104+
return err
105+
}
106+
g.id = group.Id
107+
return nil
108+
}
109+
110+
func (g *groupResource) getEntitlements(ctx context.Context) ([]iam.ComplexValue, error) {
111+
c, err := g.w.Config.NewApiClient()
112+
if err != nil {
113+
return nil, err
114+
}
115+
res := iam.Group{}
116+
err = c.Do(ctx, "GET", fmt.Sprintf("/api/2.0/preview/scim/v2/Groups/%s?attributes=entitlements", g.id),
117+
httpclient.WithResponseUnmarshal(&res))
118+
if err != nil {
119+
return nil, err
120+
}
121+
return res.Entitlements, nil
122+
}
123+
124+
func (g *groupResource) cleanUp(ctx context.Context) error {
125+
return g.w.Groups.DeleteById(ctx, g.id)
126+
}
127+
128+
func (g *groupResource) dataSourceTemplate() string {
129+
return fmt.Sprintf(`
130+
data "databricks_group" "example" {
131+
display_name = "%s"
132+
}`, g.displayName)
133+
}
134+
135+
func (g *groupResource) tfReference() string {
136+
return "group_id = data.databricks_group.example.id"
137+
}
138+
139+
type servicePrincipalResource struct {
140+
applicationId string
141+
cleanup bool
142+
displayName string
143+
id string
144+
w *databricks.WorkspaceClient
145+
}
146+
147+
func (s *servicePrincipalResource) resourceType() string {
148+
return "service_principal"
149+
}
150+
151+
func (s *servicePrincipalResource) setDisplayName(displayName string) {
152+
s.displayName = displayName
153+
}
154+
155+
func (s *servicePrincipalResource) setWorkspaceClient(w *databricks.WorkspaceClient) {
156+
s.w = w
157+
}
158+
159+
func (s *servicePrincipalResource) create(ctx context.Context) error {
160+
sp, err := s.create0(ctx)
161+
if err != nil {
162+
return err
163+
}
164+
if s.applicationId == "" {
165+
s.applicationId = sp.ApplicationId
166+
}
167+
s.id = sp.Id
168+
return nil
169+
}
170+
171+
func (s *servicePrincipalResource) create0(ctx context.Context) (iam.ServicePrincipal, error) {
172+
if s.applicationId != "" {
173+
sps := s.w.ServicePrincipals.List(ctx, iam.ListServicePrincipalsRequest{
174+
Filter: fmt.Sprintf(`applicationId eq "%s"`, s.applicationId),
175+
})
176+
if !sps.HasNext(ctx) {
177+
return iam.ServicePrincipal{}, fmt.Errorf("service principal with applicationId %s not found", s.applicationId)
178+
}
179+
return sps.Next(ctx)
180+
}
181+
sp, err := s.w.ServicePrincipals.Create(ctx, iam.ServicePrincipal{
182+
DisplayName: s.displayName,
183+
})
184+
return *sp, err
185+
}
186+
187+
func (s *servicePrincipalResource) getEntitlements(ctx context.Context) ([]iam.ComplexValue, error) {
188+
c, err := s.w.Config.NewApiClient()
189+
if err != nil {
190+
return nil, err
191+
}
192+
res := iam.ServicePrincipal{}
193+
err = c.Do(ctx, "GET", fmt.Sprintf("/api/2.0/preview/scim/v2/ServicePrincipals/%s?attributes=entitlements", s.id),
194+
httpclient.WithResponseUnmarshal(&res))
195+
if err != nil {
196+
return nil, err
197+
}
198+
return res.Entitlements, nil
199+
}
200+
201+
func (s *servicePrincipalResource) cleanUp(ctx context.Context) error {
202+
if !s.cleanup {
203+
return nil
204+
}
205+
return s.w.ServicePrincipals.DeleteById(ctx, s.applicationId)
206+
}
207+
208+
func (s *servicePrincipalResource) dataSourceTemplate() string {
209+
fragment := fmt.Sprintf(`display_name = "%s"`, s.displayName)
210+
if s.applicationId != "" {
211+
fragment = fmt.Sprintf(`application_id = "%s"`, s.applicationId)
212+
}
213+
return fmt.Sprintf(`
214+
data "databricks_service_principal" "example" {
215+
%s
216+
}`, fragment)
217+
}
218+
219+
func (s *servicePrincipalResource) tfReference() string {
220+
return "service_principal_id = data.databricks_service_principal.example.id"
221+
}

0 commit comments

Comments
 (0)