Skip to content

Commit 314c1ad

Browse files
authored
Automatically add CAN_MANAGE permission on databricks_sql_endpoint for calling user (#2168)
* fix sql warehouse permission * feedback * update tests
1 parent 9e1eb13 commit 314c1ad

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

permissions/resource_permissions.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
"github.com/databricks/databricks-sdk-go"
1212
"github.com/databricks/databricks-sdk-go/apierr"
1313
"github.com/databricks/terraform-provider-databricks/common"
14-
"github.com/databricks/terraform-provider-databricks/jobs"
15-
"github.com/databricks/terraform-provider-databricks/pipelines"
1614

1715
"github.com/hashicorp/go-cty/cty"
1816
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -128,7 +126,7 @@ func urlPathForObjectID(objectID string) string {
128126
// permissions when POSTing permissions changes through the REST API, to avoid accidentally
129127
// revoking the calling user's ability to manage the current object.
130128
func (a PermissionsAPI) shouldExplicitlyGrantCallingUserManagePermissions(objectID string) bool {
131-
for _, prefix := range [...]string{"/registered-models/", "/clusters/", "/queries/"} {
129+
for _, prefix := range [...]string{"/registered-models/", "/clusters/", "/queries/", "/sql/warehouses"} {
132130
if strings.HasPrefix(objectID, prefix) {
133131
return true
134132
}
@@ -220,8 +218,16 @@ func (a PermissionsAPI) Delete(objectID string) error {
220218
}
221219
}
222220
}
221+
w, err := a.client.WorkspaceClient()
222+
if err != nil {
223+
return err
224+
}
223225
if strings.HasPrefix(objectID, "/jobs") {
224-
job, err := jobs.NewJobsAPI(a.context, a.client).Read(strings.ReplaceAll(objectID, "/jobs/", ""))
226+
jobId, err := strconv.ParseInt(strings.ReplaceAll(objectID, "/jobs/", ""), 10, 0)
227+
if err != nil {
228+
return err
229+
}
230+
job, err := w.Jobs.GetByJobId(a.context, jobId)
225231
if err != nil {
226232
return err
227233
}
@@ -230,7 +236,7 @@ func (a PermissionsAPI) Delete(objectID string) error {
230236
PermissionLevel: "IS_OWNER",
231237
})
232238
} else if strings.HasPrefix(objectID, "/pipelines") {
233-
job, err := pipelines.NewPipelinesAPI(a.context, a.client).Read(strings.ReplaceAll(objectID, "/pipelines/", ""))
239+
job, err := w.Pipelines.GetByPipelineId(a.context, strings.ReplaceAll(objectID, "/pipelines/", ""))
234240
if err != nil {
235241
return err
236242
}

permissions/resource_permissions_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"github.com/databricks/databricks-sdk-go"
1010
"github.com/databricks/databricks-sdk-go/apierr"
1111
"github.com/databricks/databricks-sdk-go/config"
12+
"github.com/databricks/databricks-sdk-go/service/jobs"
1213
"github.com/databricks/terraform-provider-databricks/common"
13-
"github.com/databricks/terraform-provider-databricks/jobs"
1414
"github.com/databricks/terraform-provider-databricks/scim"
1515

1616
"github.com/databricks/terraform-provider-databricks/qa"
@@ -824,6 +824,10 @@ func TestResourcePermissionsCreate_SQLA_Endpoint(t *testing.T) {
824824
UserName: TestingUser,
825825
PermissionLevel: "CAN_USE",
826826
},
827+
{
828+
UserName: TestingAdminUser,
829+
PermissionLevel: "CAN_MANAGE",
830+
},
827831
},
828832
},
829833
},
@@ -838,6 +842,10 @@ func TestResourcePermissionsCreate_SQLA_Endpoint(t *testing.T) {
838842
UserName: TestingUser,
839843
PermissionLevel: "CAN_USE",
840844
},
845+
{
846+
UserName: TestingAdminUser,
847+
PermissionLevel: "CAN_MANAGE",
848+
},
841849
},
842850
},
843851
},
@@ -1160,7 +1168,7 @@ func TestShouldKeepAdminsOnAnythingExceptPasswordsAndAssignsOwnerForJob(t *testi
11601168
},
11611169
{
11621170
Method: "GET",
1163-
Resource: "/api/2.0/jobs/get?job_id=123",
1171+
Resource: "/api/2.1/jobs/get?job_id=123",
11641172
Response: jobs.Job{
11651173
CreatorUserName: "[email protected]",
11661174
},
@@ -1215,7 +1223,7 @@ func TestShouldKeepAdminsOnAnythingExceptPasswordsAndAssignsOwnerForPipeline(t *
12151223
},
12161224
{
12171225
Method: "GET",
1218-
Resource: "/api/2.0/pipelines/123",
1226+
Resource: "/api/2.0/pipelines/123?",
12191227
Response: jobs.Job{
12201228
CreatorUserName: "[email protected]",
12211229
},

0 commit comments

Comments
 (0)