Skip to content

Commit

Permalink
Schema done
Browse files Browse the repository at this point in the history
  • Loading branch information
sjauld committed Jun 27, 2019
1 parent 0080813 commit cb1a153
Show file tree
Hide file tree
Showing 13 changed files with 439 additions and 58 deletions.
1 change: 1 addition & 0 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func Provider() *schema.Provider {
"snowflake_managed_account": resources.ManagedAccount(),
"snowflake_role": resources.Role(),
"snowflake_role_grants": resources.RoleGrants(),
"snowflake_schema": resources.Schema(),
"snowflake_share": resources.Share(),
"snowflake_user": resources.User(),
"snowflake_view": resources.View(),
Expand Down
28 changes: 14 additions & 14 deletions pkg/resources/database_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,40 @@ var validDatabasePrivileges = []string{"USAGE", "REFERENCE_USAGE"}

var databaseGrantSchema = map[string]*schema.Schema{
"database_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
Description: "The name of the database on which to grant privileges.",
ForceNew: true,
ForceNew: true,
},
"privilege": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The privilege to grant on the database.",
Default: "USAGE",
Type: schema.TypeString,
Optional: true,
Description: "The privilege to grant on the database.",
Default: "USAGE",
ValidateFunc: validation.StringInSlice(validDatabasePrivileges, true),
ForceNew: 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,
ForceNew: true,
},
"shares": &schema.Schema{
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "Grants privilege to these shares.",
ForceNew: true,
ForceNew: true,
},
}

// DatabaseGrant returns a pointer to the resource representing a database grant
func DatabaseGrant() *schema.Resource {
return &schema.Resource{
Create: CreateDatabaseGrant,
Read: ReadDatabaseGrant,
Read: ReadDatabaseGrant,
Delete: DeleteDatabaseGrant,

Schema: databaseGrantSchema,
Expand Down
13 changes: 9 additions & 4 deletions pkg/resources/database_grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ func TestDatabaseGrantCreate(t *testing.T) {
func expectReadDatabaseGrant(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), "USAGE", "DATABASE", "test-database", "ROLE", "test-role-1", false, "bob",
).AddRow(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), "USAGE", "DATABASE", "test-database", "ROLE", "test-role-2", false, "bob",
).AddRow(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), "USAGE", "DATABASE", "test-database", "SHARE", "test-share-1", false, "bob",
).AddRow(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), "USAGE", "DATABASE", "test-database", "SHARE", "test-share-2", false, "bob")
}).AddRow(
time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), "USAGE", "DATABASE", "test-database", "ROLE", "test-role-1", false, "bob",
).AddRow(
time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), "USAGE", "DATABASE", "test-database", "ROLE", "test-role-2", false, "bob",
).AddRow(
time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), "USAGE", "DATABASE", "test-database", "SHARE", "test-share-1", false, "bob",
).AddRow(
time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), "USAGE", "DATABASE", "test-database", "SHARE", "test-share-2", false, "bob",
)
mock.ExpectQuery(`^SHOW GRANTS ON DATABASE "test-database"$`).WillReturnRows(rows)
}
6 changes: 3 additions & 3 deletions pkg/resources/grant_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// grant represents a generic grant of a privilge from a grant (the target) to a
// grantee. This type can be used in conjunction with github.com/jmoiron/sqlx to
// build a nice go replresentation of a grant
// build a nice go representation of a grant
type grant struct {
CreatedOn time.Time `db:"created_on"`
Privilege string `db:"privilege"`
Expand Down Expand Up @@ -44,7 +44,7 @@ func createGenericGrant(data *schema.ResourceData, meta interface{}, builder *sn
roles := expandStringList(data.Get("roles").(*schema.Set).List())
shares := expandStringList(data.Get("shares").(*schema.Set).List())

if len(roles) + len(shares) == 0 {
if len(roles)+len(shares) == 0 {
return fmt.Errorf("no roles or shares specified for this grant")
}

Expand Down Expand Up @@ -127,7 +127,7 @@ func readGenericGrants(db *sql.DB, builder *snowflake.GrantBuilder) ([]*grant, e
}
grants = append(grants, grant)
}

return grants, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/resources/managed_account_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
func TestAccManagedAccount(t *testing.T) {
if _, ok := os.LookupEnv("SKIP_MANAGED_ACCOUNT_TEST"); ok {
t.Skip("Skipping TestACcManagedAccount")
}
}

accName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
adminName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
Expand Down
Loading

0 comments on commit cb1a153

Please sign in to comment.