Skip to content

Commit

Permalink
fix: materialized view grant incorrectly requires schema_name (#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
josharrington committed Aug 20, 2021
1 parent a1c4067 commit faf0767
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
43 changes: 43 additions & 0 deletions docs/resources/function.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "snowflake_function Resource - terraform-provider-snowflake"
subcategory: ""
description: |-
---

# snowflake_function (Resource)





<!-- schema generated by tfplugindocs -->
## Schema

### Required

- **database** (String) The database in which to create the function. Don't use the | character.
- **name** (String) Specifies the identifier for the function; does not have to be unique for the schema in which the function is created. Don't use the | character.
- **return_type** (String) The return type of the function
- **schema** (String) The schema in which to create the function. Don't use the | character.
- **statement** (String) Specifies the javascript / java / sql code used to create the function.

### Optional

- **arguments** (Block List) List of the arguments for the function (see [below for nested schema](#nestedblock--arguments))
- **comment** (String) Specifies a comment for the function.
- **id** (String) The ID of this resource.
- **language** (String) The language of the statement
- **null_input_behavior** (String) Specifies the behavior of the function when called with null inputs.
- **return_behavior** (String) Specifies the behavior of the function when returning results

<a id="nestedblock--arguments"></a>
### Nested Schema for `arguments`

Required:

- **name** (String) The argument name
- **type** (String) The argument type


2 changes: 1 addition & 1 deletion docs/resources/materialized_view_grant.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ resource "snowflake_materialized_view_grant" "grant" {
### Required

- **database_name** (String) The name of the database containing the current or future materialized views on which to grant privileges.
- **schema_name** (String) The name of the schema containing the current or future materialized views on which to grant privileges.

### Optional

Expand All @@ -49,6 +48,7 @@ resource "snowflake_materialized_view_grant" "grant" {
- **on_future** (Boolean) When this is set to true and a schema_name is provided, apply this grant on all future materialized views in the given schema. When this is true and no schema_name is provided apply this grant on all future materialized views in the given database. The materialized_view_name and shares fields must be unset in order to use on_future.
- **privilege** (String) The privilege to grant on the current or future materialized view view.
- **roles** (Set of String) Grants privilege to these roles.
- **schema_name** (String) The name of the schema containing the current or future materialized views on which to grant privileges.
- **shares** (Set of String) Grants privilege to these shares (only valid if on_future is false).
- **with_grant_option** (Boolean) When this is set to true, allows the recipient role to grant the privileges to other roles.

Expand Down
Binary file added pkg/resources/__debug_bin
Binary file not shown.
6 changes: 5 additions & 1 deletion pkg/resources/materialized_view_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var materializedViewGrantSchema = map[string]*schema.Schema{
},
"schema_name": {
Type: schema.TypeString,
Required: true,
Optional: true,
Description: "The name of the schema containing the current or future materialized views on which to grant privileges.",
ForceNew: true,
},
Expand Down Expand Up @@ -104,6 +104,10 @@ func CreateMaterializedViewGrant(d *schema.ResourceData, meta interface{}) error
futureMaterializedViews := d.Get("on_future").(bool)
grantOption := d.Get("with_grant_option").(bool)

if (schemaName == "") && !futureMaterializedViews {
return errors.New("schema_name must be set unless on_future is true.")
}

if (materializedViewName == "") && !futureMaterializedViews {
return errors.New("materialized_view_name must be set unless on_future is true.")
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/resources/materialized_view_grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,24 @@ func TestFutureMaterializedViewGrantCreate(t *testing.T) {
err := resources.CreateMaterializedViewGrant(d, db)
b.NoError(err)
})

// Validate specifying on_future=false and schema_name="" generates an error
m := require.New(t)

in = map[string]interface{}{
"on_future": false,
"database_name": "test-db",
"privilege": "SELECT",
"roles": []interface{}{"test-role-1", "test-role-2"},
"with_grant_option": false,
}
d = schema.TestResourceDataRaw(t, resources.MaterializedViewGrant().Resource.Schema, in)
m.NotNil(d)

WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
err := resources.CreateMaterializedViewGrant(d, db)
m.Error(err)
})
}

func expectReadFutureMaterializedViewGrant(mock sqlmock.Sqlmock) {
Expand Down

0 comments on commit faf0767

Please sign in to comment.