Skip to content

Commit dcc95c3

Browse files
Wait for resource to be updated during grants resource modification (#3026)
* Wait for resource to be updated during grants resource modification * Fix typo * Fix typo
1 parent 3c89aee commit dcc95c3

File tree

2 files changed

+178
-1
lines changed

2 files changed

+178
-1
lines changed

catalog/resource_grants.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package catalog
33
import (
44
"context"
55
"fmt"
6+
"log"
67
"sort"
78
"strings"
9+
"time"
810

911
"github.com/databricks/databricks-sdk-go/apierr"
1012
"github.com/databricks/terraform-provider-databricks/common"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1114
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1215
)
1316

@@ -126,7 +129,31 @@ func (a PermissionsAPI) replacePermissions(securable, name string, list Permissi
126129
if err != nil {
127130
return err
128131
}
129-
return a.updatePermissions(securable, name, list.diff(existing))
132+
err = a.updatePermissions(securable, name, list.diff(existing))
133+
if err != nil {
134+
return err
135+
}
136+
return a.waitForStatus(securable, name, list)
137+
}
138+
139+
func (a PermissionsAPI) defaultTimeout() time.Duration {
140+
return 1 * time.Minute
141+
}
142+
143+
func (a PermissionsAPI) waitForStatus(securable, name string, desired PermissionsList) (err error) {
144+
return resource.RetryContext(a.context, a.defaultTimeout(), func() *resource.RetryError {
145+
permissions, err := a.getPermissions(securable, name)
146+
if err != nil {
147+
return resource.NonRetryableError(err)
148+
}
149+
log.Printf("[DEBUG] Permissions for %s-%s are: %v", securable, name, permissions)
150+
if permissions.diff(desired).Changes == nil {
151+
return nil
152+
}
153+
return resource.RetryableError(
154+
fmt.Errorf("permissions for %s-%s are %v, but have to be %v",
155+
securable, name, permissions, desired))
156+
})
130157
}
131158

132159
type securableMapping map[string]map[string]bool

catalog/resource_grants_test.go

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,108 @@ func TestGrantCreate(t *testing.T) {
5959
},
6060
},
6161
},
62+
{
63+
Method: "GET",
64+
Resource: "/api/2.1/unity-catalog/permissions/table/foo.bar.baz",
65+
Response: PermissionsList{
66+
Assignments: []PrivilegeAssignment{
67+
{
68+
Principal: "me",
69+
Privileges: []string{"MODIFY"},
70+
},
71+
},
72+
},
73+
},
74+
},
75+
Resource: ResourceGrants(),
76+
Create: true,
77+
HCL: `
78+
table = "foo.bar.baz"
79+
80+
grant {
81+
principal = "me"
82+
privileges = ["MODIFY"]
83+
}`,
84+
}.ApplyNoError(t)
85+
}
86+
87+
func TestWaitUntilReady(t *testing.T) {
88+
qa.ResourceFixture{
89+
Fixtures: []qa.HTTPFixture{
90+
{
91+
Method: "GET",
92+
Resource: "/api/2.1/unity-catalog/permissions/table/foo.bar.baz",
93+
Response: PermissionsList{
94+
Assignments: []PrivilegeAssignment{
95+
{
96+
Principal: "me",
97+
Privileges: []string{"SELECT"},
98+
},
99+
{
100+
Principal: "someone-else",
101+
Privileges: []string{"MODIFY", "SELECT"},
102+
},
103+
},
104+
},
105+
},
106+
{
107+
Method: "PATCH",
108+
Resource: "/api/2.1/unity-catalog/permissions/table/foo.bar.baz",
109+
ExpectedRequest: permissionsDiff{
110+
Changes: []permissionsChange{
111+
{
112+
Principal: "me",
113+
Add: []string{"MODIFY"},
114+
Remove: []string{"SELECT"},
115+
},
116+
{
117+
Principal: "someone-else",
118+
Remove: []string{"MODIFY", "SELECT"},
119+
},
120+
},
121+
},
122+
},
123+
// This one is still the first one, to simulate a delay on updating the permissions
124+
{
125+
Method: "GET",
126+
Resource: "/api/2.1/unity-catalog/permissions/table/foo.bar.baz",
127+
Response: PermissionsList{
128+
Assignments: []PrivilegeAssignment{
129+
{
130+
Principal: "me",
131+
Privileges: []string{"SELECT"},
132+
},
133+
{
134+
Principal: "someone-else",
135+
Privileges: []string{"MODIFY", "SELECT"},
136+
},
137+
},
138+
},
139+
},
140+
{
141+
Method: "GET",
142+
Resource: "/api/2.1/unity-catalog/permissions/table/foo.bar.baz",
143+
Response: PermissionsList{
144+
Assignments: []PrivilegeAssignment{
145+
{
146+
Principal: "me",
147+
Privileges: []string{"MODIFY"},
148+
},
149+
},
150+
},
151+
},
152+
{
153+
Method: "GET",
154+
Resource: "/api/2.1/unity-catalog/permissions/table/foo.bar.baz",
155+
Response: PermissionsList{
156+
Assignments: []PrivilegeAssignment{
157+
{
158+
Principal: "me",
159+
Privileges: []string{"MODIFY"},
160+
},
161+
},
162+
},
163+
},
62164
},
63165
Resource: ResourceGrants(),
64166
Create: true,
@@ -106,6 +208,18 @@ func TestGrantUpdate(t *testing.T) {
106208
},
107209
},
108210
},
211+
{
212+
Method: "GET",
213+
Resource: "/api/2.1/unity-catalog/permissions/table/foo.bar.baz",
214+
Response: PermissionsList{
215+
Assignments: []PrivilegeAssignment{
216+
{
217+
Principal: "me",
218+
Privileges: []string{"MODIFY", "SELECT"},
219+
},
220+
},
221+
},
222+
},
109223
},
110224
Resource: ResourceGrants(),
111225
Update: true,
@@ -275,6 +389,18 @@ func TestShareGrantCreate(t *testing.T) {
275389
},
276390
},
277391
},
392+
{
393+
Method: "GET",
394+
Resource: "/api/2.1/unity-catalog/shares/myshare/permissions",
395+
Response: PermissionsList{
396+
Assignments: []PrivilegeAssignment{
397+
{
398+
Principal: "me",
399+
Privileges: []string{"SELECT"},
400+
},
401+
},
402+
},
403+
},
278404
},
279405
Resource: ResourceGrants(),
280406
Create: true,
@@ -331,6 +457,18 @@ func TestShareGrantUpdate(t *testing.T) {
331457
},
332458
},
333459
},
460+
{
461+
Method: "GET",
462+
Resource: "/api/2.1/unity-catalog/shares/myshare/permissions",
463+
Response: PermissionsList{
464+
Assignments: []PrivilegeAssignment{
465+
{
466+
Principal: "you",
467+
Privileges: []string{"SELECT"},
468+
},
469+
},
470+
},
471+
},
334472
},
335473
Resource: ResourceGrants(),
336474
Update: true,
@@ -406,6 +544,18 @@ func TestConnectionGrantCreate(t *testing.T) {
406544
},
407545
},
408546
},
547+
{
548+
Method: "GET",
549+
Resource: "/api/2.1/unity-catalog/permissions/connection/myconn",
550+
Response: PermissionsList{
551+
Assignments: []PrivilegeAssignment{
552+
{
553+
Principal: "me",
554+
Privileges: []string{"USE_CONNECTION"},
555+
},
556+
},
557+
},
558+
},
409559
},
410560
Resource: ResourceGrants(),
411561
Create: true,

0 commit comments

Comments
 (0)