Skip to content

Commit 31e132f

Browse files
authored
Fix ADMIN assignment in databricks_permission_assignment resource (#5109)
## Changes <!-- Summary of your changes that are easy to understand --> It was found that assignment by name works only for the `USER` role, so the direct assignment as `ADMIN` fails. This PR fixes this issue by doing assignment as `USER` first, and then changing it to `ADMIN`. Resolves #5106 ## Tests <!-- How is this tested? Please see the checklist below and also describe any other relevant tests --> - [x] `make test` run locally - [ ] relevant change in `docs/` folder - [ ] covered with integration tests in `internal/acceptance` - [ ] using Go SDK - [ ] using TF Plugin Framework - [x] has entry in `NEXT_CHANGELOG.md` file
1 parent 870024c commit 31e132f

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

NEXT_CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### Bug Fixes
1010

1111
* Fix crash when error happens during reading `databricks_job` ([#5110](https://github.com/databricks/terraform-provider-databricks/pull/5110))
12+
* Fix `ADMIN` assignment in `databricks_permission_assignment` resource ([#5109](https://github.com/databricks/terraform-provider-databricks/pull/5109))
1213

1314
### Documentation
1415

access/resource_permission_assignment.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"log"
78
"net/http"
9+
"slices"
810
"strconv"
911

1012
"github.com/databricks/databricks-sdk-go/apierr"
@@ -148,11 +150,28 @@ func ResourcePermissionAssignment() common.Resource {
148150
var assignment permissionAssignmentEntity
149151
common.DataToStructPointer(d, s, &assignment)
150152
api := NewPermissionAssignmentAPI(ctx, c)
153+
// We need this because assignment by name doesn't work for admins, so we need to
154+
// first assign them as users. And then reassign them as admins.
155+
shouldReassignAdmin := false
156+
if assignment.PrincipalId == 0 && slices.Contains(assignment.Permissions, "ADMIN") {
157+
shouldReassignAdmin = true
158+
assignment.Permissions = []string{"USER"}
159+
}
151160
principal, err := api.CreateOrUpdate(assignment)
152161
if err != nil {
153162
return err
154163
}
155164
d.SetId(strconv.FormatInt(principal.PrincipalID, 10))
165+
if shouldReassignAdmin {
166+
common.DataToStructPointer(d, s, &assignment)
167+
assignment.PrincipalId = principal.PrincipalID
168+
_, err := api.CreateOrUpdate(assignment)
169+
if err != nil {
170+
log.Printf("[WARN] error reassigning admin permissions: %v", err)
171+
api.Remove(d.Id())
172+
}
173+
return err
174+
}
156175
return nil
157176
},
158177
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {

access/resource_permission_assignment_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,128 @@ func TestPermissionAssignmentCreateWithUserName(t *testing.T) {
101101
}.ApplyNoError(t)
102102
}
103103

104+
func TestPermissionAssignmentCreateWithUserNameAndAdminPermissions(t *testing.T) {
105+
qa.ResourceFixture{
106+
Fixtures: []qa.HTTPFixture{
107+
{
108+
Method: "POST",
109+
Resource: "/api/2.0/preview/permissionassignments",
110+
ExpectedRequest: permissionAssignmentRequest{
111+
PermissionAssignments: []permissionAssignmentRequestItem{
112+
{
113+
principalInfo: principalInfo{
114+
UserName: "[email protected]",
115+
},
116+
Permissions: []string{"USER"},
117+
},
118+
},
119+
},
120+
Response: permissionAssignmentResponse{
121+
PermissionAssignments: []permissionAssignmentResponseItem{
122+
{
123+
Permissions: []string{"USER"},
124+
Principal: principalInfo{
125+
PrincipalID: 123,
126+
UserName: "[email protected]",
127+
},
128+
},
129+
},
130+
},
131+
},
132+
{
133+
Method: "PUT",
134+
Resource: "/api/2.0/preview/permissionassignments/principals/123",
135+
ExpectedRequest: Permissions{
136+
Permissions: []string{"ADMIN"},
137+
},
138+
Response: permissionAssignmentResponseItem{
139+
Permissions: []string{"ADMIN"},
140+
Principal: principalInfo{
141+
PrincipalID: 123,
142+
UserName: "[email protected]",
143+
},
144+
},
145+
},
146+
{
147+
Method: "GET",
148+
Resource: "/api/2.0/preview/permissionassignments",
149+
Response: permissionAssignmentResponse{
150+
PermissionAssignments: []permissionAssignmentResponseItem{
151+
{
152+
Permissions: []string{"ADMIN"},
153+
Principal: principalInfo{
154+
PrincipalID: 123,
155+
UserName: "[email protected]",
156+
},
157+
},
158+
},
159+
},
160+
},
161+
},
162+
Resource: ResourcePermissionAssignment(),
163+
Create: true,
164+
New: true,
165+
HCL: `
166+
user_name = "[email protected]"
167+
permissions = ["ADMIN"]
168+
`,
169+
}.ApplyNoError(t)
170+
}
171+
172+
func TestPermissionAssignmentCreateWithUserNameAndAdminPermissionsError(t *testing.T) {
173+
qa.ResourceFixture{
174+
Fixtures: []qa.HTTPFixture{
175+
{
176+
Method: "POST",
177+
Resource: "/api/2.0/preview/permissionassignments",
178+
ExpectedRequest: permissionAssignmentRequest{
179+
PermissionAssignments: []permissionAssignmentRequestItem{
180+
{
181+
principalInfo: principalInfo{
182+
UserName: "[email protected]",
183+
},
184+
Permissions: []string{"USER"},
185+
},
186+
},
187+
},
188+
Response: permissionAssignmentResponse{
189+
PermissionAssignments: []permissionAssignmentResponseItem{
190+
{
191+
Permissions: []string{"USER"},
192+
Principal: principalInfo{
193+
PrincipalID: 123,
194+
UserName: "[email protected]",
195+
},
196+
},
197+
},
198+
},
199+
},
200+
{
201+
Method: "PUT",
202+
Resource: "/api/2.0/preview/permissionassignments/principals/123",
203+
ExpectedRequest: Permissions{
204+
Permissions: []string{"ADMIN"},
205+
},
206+
Status: 500,
207+
Response: permissionAssignmentResponseItem{
208+
Error: "Internal error",
209+
},
210+
},
211+
{
212+
Method: "DELETE",
213+
Resource: "/api/2.0/preview/permissionassignments/principals/123",
214+
},
215+
},
216+
Resource: ResourcePermissionAssignment(),
217+
Create: true,
218+
New: true,
219+
HCL: `
220+
user_name = "[email protected]"
221+
permissions = ["ADMIN"]
222+
`,
223+
}.ExpectError(t, "Internal error")
224+
}
225+
104226
func TestPermissionAssignmentCreateWithServicePrincipalName(t *testing.T) {
105227
qa.ResourceFixture{
106228
Fixtures: []qa.HTTPFixture{

0 commit comments

Comments
 (0)