Skip to content

Commit

Permalink
Merge branch 'main' into fix-create-storage-credentials-with-owner
Browse files Browse the repository at this point in the history
  • Loading branch information
donatasm authored Jan 30, 2024
2 parents 67860a1 + 86d78e2 commit a763dc8
Show file tree
Hide file tree
Showing 30 changed files with 1,262 additions and 630 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: "CodeQL"

on:
push:
branches: [ master ]
branches: [ main ]
pull_request:
branches: [ master ]
branches: [ main ]
schedule:
- cron: '41 1 * * 0'

Expand Down
23 changes: 3 additions & 20 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,11 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v1
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 1.21.x

- name: Cache packages
uses: actions/cache@v2
with:
path: |
~/go/pkg/mod
~/.cache/go-build
/opt/hostedtoolcache/go
vendor
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Set go env
run: |
echo "GOPATH=$(go env GOPATH)" >> $GITHUB_ENV
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
- name: Pull external libraries
run: make vendor

Expand All @@ -52,7 +35,7 @@ jobs:
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: 1.21.x

Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ jobs:
with:
fetch-depth: 0

-
name: Set up Go
uses: actions/setup-go@v1
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 1.21.x
-
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ on:
inputs:
base:
description: "Base ref"
default: "master"
default: "main"
required: true
head:
description: "Head ref"
default: "master"
default: "main"
required: true

jobs:
Expand Down Expand Up @@ -43,7 +43,7 @@ jobs:
ref: ${{ github.event.inputs.head }}

- name: "Setup Go"
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: 1.21.x

Expand Down
26 changes: 25 additions & 1 deletion catalog/resource_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ func ResourceGrant() *schema.Resource {
return common.Resource{
Schema: s,
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
err = validateMetastoreId(ctx, w, d.Get("metastore").(string))
if err != nil {
return err
}
principal := d.Get("principal").(string)
privileges := permissions.SetToSlice(d.Get("privileges").(*schema.Set))
var grants = catalog.PermissionsList{
Expand All @@ -156,7 +164,7 @@ func ResourceGrant() *schema.Resource {
}
securable, name := permissions.Mappings.KeyValue(d)
unityCatalogPermissionsAPI := permissions.NewUnityCatalogPermissionsAPI(ctx, c)
err := replacePermissionsForPrincipal(unityCatalogPermissionsAPI, securable, name, principal, grants)
err = replacePermissionsForPrincipal(unityCatalogPermissionsAPI, securable, name, principal, grants)
if err != nil {
return err
}
Expand All @@ -179,6 +187,14 @@ func ResourceGrant() *schema.Resource {
return common.StructToData(*grantsForPrincipal, s, d)
},
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
err = validateMetastoreId(ctx, w, d.Get("metastore").(string))
if err != nil {
return err
}
securable, name, principal, err := parseSecurableId(d)
if err != nil {
return err
Expand All @@ -196,6 +212,14 @@ func ResourceGrant() *schema.Resource {
return replacePermissionsForPrincipal(unityCatalogPermissionsAPI, securable, name, principal, grants)
},
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
err = validateMetastoreId(ctx, w, d.Get("metastore").(string))
if err != nil {
return err
}
securable, name, principal, err := parseSecurableId(d)
if err != nil {
return err
Expand Down
104 changes: 104 additions & 0 deletions catalog/resource_grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,110 @@ func TestResourceGrantCreate(t *testing.T) {
}.ApplyNoError(t)
}

func TestResourceGrantCreateMetastoreId(t *testing.T) {
qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
{
Method: "GET",
Resource: "/api/2.1/unity-catalog/current-metastore-assignment",
Response: catalog.MetastoreAssignment{
MetastoreId: "metastore_id",
},
},
{
Method: "GET",
Resource: "/api/2.1/unity-catalog/permissions/metastore/metastore_id?",
Response: catalog.PermissionsList{
PrivilegeAssignments: []catalog.PrivilegeAssignment{
{
Principal: "me",
Privileges: []catalog.Privilege{"SELECT"},
},
{
Principal: "someone-else",
Privileges: []catalog.Privilege{"MODIFY", "SELECT"},
},
},
},
},
{
Method: "PATCH",
Resource: "/api/2.1/unity-catalog/permissions/metastore/metastore_id",
ExpectedRequest: catalog.UpdatePermissions{
Changes: []catalog.PermissionsChange{
{
Principal: "me",
Add: []catalog.Privilege{"MODIFY"},
Remove: []catalog.Privilege{"SELECT"},
},
},
},
},
{
Method: "GET",
Resource: "/api/2.1/unity-catalog/permissions/metastore/metastore_id?",
Response: catalog.PermissionsList{
PrivilegeAssignments: []catalog.PrivilegeAssignment{
{
Principal: "me",
Privileges: []catalog.Privilege{"MODIFY"},
},
{
Principal: "someone-else",
Privileges: []catalog.Privilege{"MODIFY", "SELECT"},
},
},
},
},
{
Method: "GET",
Resource: "/api/2.1/unity-catalog/permissions/metastore/metastore_id?",
Response: catalog.PermissionsList{
PrivilegeAssignments: []catalog.PrivilegeAssignment{
{
Principal: "me",
Privileges: []catalog.Privilege{"MODIFY"},
},
{
Principal: "someone-else",
Privileges: []catalog.Privilege{"MODIFY", "SELECT"},
},
},
},
},
},
Resource: ResourceGrant(),
Create: true,
HCL: `
metastore = "metastore_id"
principal = "me"
privileges = ["MODIFY"]
`,
}.ApplyNoError(t)
}

func TestFailsWhenWrongMetastoreId(t *testing.T) {
qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
{
Method: "GET",
Resource: "/api/2.1/unity-catalog/current-metastore-assignment",
Response: catalog.MetastoreAssignment{
MetastoreId: "old_id",
},
},
},
Resource: ResourceGrant(),
Create: true,
HCL: `
metastore = "new_id"
principal = "me"
privileges = ["MODIFY"]
`,
}.ExpectError(t, "metastore_id must be empty or equal to the metastore id assigned to the workspace: old_id. "+
"If the metastore assigned to the workspace has changed, the new metastore id must be explicitly set")
}

func TestResourceGrantWaitUntilReady(t *testing.T) {
qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
Expand Down
26 changes: 25 additions & 1 deletion catalog/resource_grants.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,19 @@ func ResourceGrants() *schema.Resource {
return mapping.validate(d, grants)
},
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
err = validateMetastoreId(ctx, w, d.Get("metastore").(string))
if err != nil {
return err
}
var grants PermissionsList
common.DataToStructPointer(d, s, &grants)
securable, name := mapping.kv(d)
unityCatalogPermissionsAPI := permissions.NewUnityCatalogPermissionsAPI(ctx, c)
err := replaceAllPermissions(unityCatalogPermissionsAPI, securable, name, grants.toSdkPermissionsList())
err = replaceAllPermissions(unityCatalogPermissionsAPI, securable, name, grants.toSdkPermissionsList())
if err != nil {
return err
}
Expand All @@ -346,6 +354,14 @@ func ResourceGrants() *schema.Resource {
return common.StructToData(sdkPermissionsListToPermissionsList(*grants), s, d)
},
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
err = validateMetastoreId(ctx, w, d.Get("metastore").(string))
if err != nil {
return err
}
securable, name, err := parseId(d)
if err != nil {
return err
Expand All @@ -356,6 +372,14 @@ func ResourceGrants() *schema.Resource {
return replaceAllPermissions(unityCatalogPermissionsAPI, securable, name, grants.toSdkPermissionsList())
},
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
err = validateMetastoreId(ctx, w, d.Get("metastore").(string))
if err != nil {
return err
}
securable, name, err := parseId(d)
if err != nil {
return err
Expand Down
Loading

0 comments on commit a763dc8

Please sign in to comment.