-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] account and account object grants (#154)
Introduces `GRANT`s for: - `ACCOUNT` - `INTEGRATION` - `RESOURCE MONITOR`
- Loading branch information
Showing
13 changed files
with
712 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package resources | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
|
||
"github.com/chanzuckerberg/terraform-provider-snowflake/pkg/snowflake" | ||
) | ||
|
||
var validAccountPrivileges = newPrivilegeSet( | ||
privilegeCreateRole, | ||
privilegeCreateUser, | ||
privilegeCreateWarehouse, | ||
privilegeCreateDatabase, | ||
privilegeCreateIntegration, | ||
privilegeManageGrants, | ||
privilegeMonitorUsage, | ||
) | ||
|
||
var accountGrantSchema = map[string]*schema.Schema{ | ||
"privilege": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "The privilege to grant on the schema.", | ||
Default: "USAGE", | ||
ValidateFunc: validation.StringInSlice(validAccountPrivileges.toList(), true), | ||
ForceNew: true, | ||
}, | ||
"roles": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Optional: true, | ||
Description: "Grants privilege to these roles.", | ||
ForceNew: true, | ||
}, | ||
} | ||
|
||
// ViewGrant returns a pointer to the resource representing a view grant | ||
func AccountGrant() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: CreateAccountGrant, | ||
Read: ReadAccountGrant, | ||
Delete: DeleteAccountGrant, | ||
|
||
Schema: accountGrantSchema, | ||
} | ||
} | ||
|
||
// CreateAccountGrant implements schema.CreateFunc | ||
func CreateAccountGrant(data *schema.ResourceData, meta interface{}) error { | ||
priv := data.Get("privilege").(string) | ||
|
||
builder := snowflake.AccountGrant() | ||
|
||
err := createGenericGrant(data, meta, builder) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
grantID := &grantID{ | ||
ResourceName: "ACCOUNT", | ||
Privilege: priv, | ||
} | ||
dataIDInput, err := grantID.String() | ||
if err != nil { | ||
return err | ||
} | ||
data.SetId(dataIDInput) | ||
|
||
return ReadAccountGrant(data, meta) | ||
} | ||
|
||
// ReadAccountGrant implements schema.ReadFunc | ||
func ReadAccountGrant(data *schema.ResourceData, meta interface{}) error { | ||
grantID, err := grantIDFromString(data.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
err = data.Set("privilege", grantID.Privilege) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
builder := snowflake.AccountGrant() | ||
|
||
return readGenericGrant(data, meta, builder, false, validAccountPrivileges) | ||
} | ||
|
||
// DeleteAccountGrant implements schema.DeleteFunc | ||
func DeleteAccountGrant(data *schema.ResourceData, meta interface{}) error { | ||
builder := snowflake.AccountGrant() | ||
|
||
return deleteGenericGrant(data, meta, builder) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package resources_test | ||
|
||
import ( | ||
"database/sql" | ||
"testing" | ||
"time" | ||
|
||
sqlmock "github.com/DATA-DOG/go-sqlmock" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
|
||
"github.com/chanzuckerberg/terraform-provider-snowflake/pkg/provider" | ||
"github.com/chanzuckerberg/terraform-provider-snowflake/pkg/resources" | ||
. "github.com/chanzuckerberg/terraform-provider-snowflake/pkg/testhelpers" | ||
) | ||
|
||
func TestAccountGrant(t *testing.T) { | ||
r := require.New(t) | ||
err := resources.AccountGrant().InternalValidate(provider.Provider().Schema, true) | ||
r.NoError(err) | ||
} | ||
|
||
func TestAccountGrantCreate(t *testing.T) { | ||
a := assert.New(t) | ||
|
||
in := map[string]interface{}{ | ||
"privilege": "CREATE DATABASE", | ||
"roles": []interface{}{"test-role-1", "test-role-2"}, | ||
} | ||
d := schema.TestResourceDataRaw(t, resources.AccountGrant().Schema, in) | ||
a.NotNil(d) | ||
|
||
WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) { | ||
mock.ExpectExec(`^GRANT CREATE DATABASE ON ACCOUNT TO ROLE "test-role-1"$`).WillReturnResult(sqlmock.NewResult(1, 1)) | ||
mock.ExpectExec(`^GRANT CREATE DATABASE ON ACCOUNT TO ROLE "test-role-2"$`).WillReturnResult(sqlmock.NewResult(1, 1)) | ||
expectReadAccountGrant(mock) | ||
err := resources.CreateAccountGrant(d, db) | ||
a.NoError(err) | ||
}) | ||
} | ||
|
||
func TestAccountGrantRead(t *testing.T) { | ||
a := assert.New(t) | ||
|
||
d := accountGrant(t, "ACCOUNT|||MANAGE GRANTS", map[string]interface{}{ | ||
"privilege": "MANAGE GRANTS", | ||
"roles": []interface{}{"test-role-1", "test-role-2"}, | ||
}) | ||
|
||
a.NotNil(d) | ||
|
||
WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) { | ||
expectReadAccountGrant(mock) | ||
err := resources.ReadAccountGrant(d, db) | ||
a.NoError(err) | ||
}) | ||
} | ||
|
||
func expectReadAccountGrant(mock sqlmock.Sqlmock) { | ||
rows := sqlmock.NewRows([]string{ | ||
"created_on", "privilege", "granted_on", "name", "granted_to", "grantee_name", "grant_option", "granted_by", | ||
}).AddRow( | ||
time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), "MANAGE GRANTS", "ACCOUNT", "", "ROLE", "test-role-1", false, "bob", | ||
).AddRow( | ||
time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), "MANAGE GRANTS", "ACCOUNT", "", "ROLE", "test-role-2", false, "bob", | ||
) | ||
mock.ExpectQuery(`^SHOW GRANTS ON ACCOUNT$`).WillReturnRows(rows) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.