From e3ce91724ed66fe0d5b5a250f610f2610b2050fa Mon Sep 17 00:00:00 2001 From: Benny Lu Date: Sat, 21 Jan 2023 15:39:10 -0700 Subject: [PATCH 1/3] fix: schema name is optional for future file_format_grant --- docs/resources/file_format_grant.md | 2 +- pkg/resources/file_format_grant.go | 66 ++++++++++++++--------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/docs/resources/file_format_grant.md b/docs/resources/file_format_grant.md index d3d74b3bef..7692dc14ef 100644 --- a/docs/resources/file_format_grant.md +++ b/docs/resources/file_format_grant.md @@ -32,7 +32,6 @@ resource "snowflake_file_format_grant" "grant" { ### Required - `database_name` (String) The name of the database containing the current or future file formats on which to grant privileges. -- `schema_name` (String) The name of the schema containing the current or future file formats on which to grant privileges. ### Optional @@ -41,6 +40,7 @@ resource "snowflake_file_format_grant" "grant" { - `on_future` (Boolean) When this is set to true and a schema_name is provided, apply this grant on all future file formats in the given schema. When this is true and no schema_name is provided apply this grant on all future file formats in the given database. The file_format_name field must be unset in order to use on_future. - `privilege` (String) The privilege to grant on the current or future file format. - `roles` (Set of String) Grants privilege to these roles. +- `schema_name` (String) The name of the schema containing the current or future file formats on which to grant privileges. - `with_grant_option` (Boolean) When this is set to true, allows the recipient role to grant the privileges to other roles. ### Read-Only diff --git a/pkg/resources/file_format_grant.go b/pkg/resources/file_format_grant.go index 0efb22d330..26049ffb26 100644 --- a/pkg/resources/file_format_grant.go +++ b/pkg/resources/file_format_grant.go @@ -14,22 +14,30 @@ var validFileFormatPrivileges = NewPrivilegeSet( ) var fileFormatGrantSchema = map[string]*schema.Schema{ - "file_format_name": { + "database_name": { Type: schema.TypeString, + Required: true, + Description: "The name of the database containing the current or future file formats on which to grant privileges.", + ForceNew: true, + }, + "enable_multiple_grants": { + Type: schema.TypeBool, Optional: true, - Description: "The name of the file format on which to grant privileges immediately (only valid if on_future is false).", + Description: "When this is set to true, multiple grants of the same type can be created. This will cause Terraform to not revoke grants applied to roles and objects outside Terraform.", + Default: false, ForceNew: true, }, - "schema_name": { + "file_format_name": { Type: schema.TypeString, - Required: true, - Description: "The name of the schema containing the current or future file formats on which to grant privileges.", + Optional: true, + Description: "The name of the file format on which to grant privileges immediately (only valid if on_future is false).", ForceNew: true, }, - "database_name": { - Type: schema.TypeString, - Required: true, - Description: "The name of the database containing the current or future file formats on which to grant privileges.", + "on_future": { + Type: schema.TypeBool, + Optional: true, + Description: "When this is set to true and a schema_name is provided, apply this grant on all future file formats in the given schema. When this is true and no schema_name is provided apply this grant on all future file formats in the given database. The file_format_name field must be unset in order to use on_future.", + Default: false, ForceNew: true, }, "privilege": { @@ -46,11 +54,10 @@ var fileFormatGrantSchema = map[string]*schema.Schema{ Optional: true, Description: "Grants privilege to these roles.", }, - "on_future": { - Type: schema.TypeBool, + "schema_name": { + Type: schema.TypeString, Optional: true, - Description: "When this is set to true and a schema_name is provided, apply this grant on all future file formats in the given schema. When this is true and no schema_name is provided apply this grant on all future file formats in the given database. The file_format_name field must be unset in order to use on_future.", - Default: false, + Description: "The name of the schema containing the current or future file formats on which to grant privileges.", ForceNew: true, }, "with_grant_option": { @@ -60,13 +67,6 @@ var fileFormatGrantSchema = map[string]*schema.Schema{ Default: false, ForceNew: true, }, - "enable_multiple_grants": { - Type: schema.TypeBool, - Optional: true, - Description: "When this is set to true, multiple grants of the same type can be created. This will cause Terraform to not revoke grants applied to roles and objects outside Terraform.", - Default: false, - ForceNew: true, - }, } // FileFormatGrant returns a pointer to the resource representing a file format grant. @@ -96,19 +96,19 @@ func CreateFileFormatGrant(d *schema.ResourceData, meta interface{}) error { dbName := d.Get("database_name").(string) schemaName := d.Get("schema_name").(string) priv := d.Get("privilege").(string) - futureFileFormats := d.Get("on_future").(bool) + onFuture := d.Get("on_future").(bool) grantOption := d.Get("with_grant_option").(bool) roles := expandStringList(d.Get("roles").(*schema.Set).List()) - if (fileFormatName == "") && !futureFileFormats { + if (fileFormatName == "") && !onFuture { return errors.New("file_format_name must be set unless on_future is true") } - if (fileFormatName != "") && futureFileFormats { + if (fileFormatName != "") && onFuture { return errors.New("file_format_name must be empty if on_future is true") } var builder snowflake.GrantBuilder - if futureFileFormats { + if onFuture { builder = snowflake.FutureFileFormatGrant(dbName, schemaName) } else { builder = snowflake.FileFormatGrant(dbName, schemaName, fileFormatName) @@ -152,14 +152,14 @@ func ReadFileFormatGrant(d *schema.ResourceData, meta interface{}) error { if err := d.Set("schema_name", schemaName); err != nil { return err } - futureFileFormatsEnabled := false + onFuture := false if fileFormatName == "" { - futureFileFormatsEnabled = true + onFuture = true } if err := d.Set("file_format_name", fileFormatName); err != nil { return err } - if err := d.Set("on_future", futureFileFormatsEnabled); err != nil { + if err := d.Set("on_future", onFuture); err != nil { return err } if err := d.Set("privilege", priv); err != nil { @@ -170,13 +170,13 @@ func ReadFileFormatGrant(d *schema.ResourceData, meta interface{}) error { } var builder snowflake.GrantBuilder - if futureFileFormatsEnabled { + if onFuture { builder = snowflake.FutureFileFormatGrant(dbName, schemaName) } else { builder = snowflake.FileFormatGrant(dbName, schemaName, fileFormatName) } - return readGenericGrant(d, meta, fileFormatGrantSchema, builder, futureFileFormatsEnabled, validFileFormatPrivileges) + return readGenericGrant(d, meta, fileFormatGrantSchema, builder, onFuture, validFileFormatPrivileges) } // DeleteFileFormatGrant implements schema.DeleteFunc. @@ -189,10 +189,10 @@ func DeleteFileFormatGrant(d *schema.ResourceData, meta interface{}) error { schemaName := grantID.SchemaName fileFormatName := grantID.ObjectName - futureFileFormats := (fileFormatName == "") + onFuture := (fileFormatName == "") var builder snowflake.GrantBuilder - if futureFileFormats { + if onFuture { builder = snowflake.FutureFileFormatGrant(dbName, schemaName) } else { builder = snowflake.FileFormatGrant(dbName, schemaName, fileFormatName) @@ -223,11 +223,11 @@ func UpdateFileFormatGrant(d *schema.ResourceData, meta interface{}) error { dbName := grantID.ResourceName schemaName := grantID.SchemaName fileFormatName := grantID.ObjectName - futureFileFormats := (fileFormatName == "") + onFuture := (fileFormatName == "") // create the builder var builder snowflake.GrantBuilder - if futureFileFormats { + if onFuture { builder = snowflake.FutureFileFormatGrant(dbName, schemaName) } else { builder = snowflake.FileFormatGrant(dbName, schemaName, fileFormatName) From a6894f3d61fc9535f9c6aa66a8e92e0bea52e797 Mon Sep 17 00:00:00 2001 From: Benny Lu Date: Sat, 21 Jan 2023 15:41:28 -0700 Subject: [PATCH 2/3] roles required --- pkg/resources/file_format_grant.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/resources/file_format_grant.go b/pkg/resources/file_format_grant.go index 26049ffb26..54a94d4d8c 100644 --- a/pkg/resources/file_format_grant.go +++ b/pkg/resources/file_format_grant.go @@ -50,8 +50,8 @@ var fileFormatGrantSchema = map[string]*schema.Schema{ }, "roles": { Type: schema.TypeSet, + Required: true, Elem: &schema.Schema{Type: schema.TypeString}, - Optional: true, Description: "Grants privilege to these roles.", }, "schema_name": { @@ -106,6 +106,9 @@ func CreateFileFormatGrant(d *schema.ResourceData, meta interface{}) error { if (fileFormatName != "") && onFuture { return errors.New("file_format_name must be empty if on_future is true") } + if (schemaName == "") && !onFuture { + return errors.New("schema_name must be set unless on_future is true") + } var builder snowflake.GrantBuilder if onFuture { From 61ce8652a8d68e6e746a94c878f1501d7756610f Mon Sep 17 00:00:00 2001 From: Benny Lu Date: Sat, 21 Jan 2023 15:42:44 -0700 Subject: [PATCH 3/3] docs --- docs/resources/file_format_grant.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/resources/file_format_grant.md b/docs/resources/file_format_grant.md index 7692dc14ef..6e0944d575 100644 --- a/docs/resources/file_format_grant.md +++ b/docs/resources/file_format_grant.md @@ -32,6 +32,7 @@ resource "snowflake_file_format_grant" "grant" { ### Required - `database_name` (String) The name of the database containing the current or future file formats on which to grant privileges. +- `roles` (Set of String) Grants privilege to these roles. ### Optional @@ -39,7 +40,6 @@ resource "snowflake_file_format_grant" "grant" { - `file_format_name` (String) The name of the file format on which to grant privileges immediately (only valid if on_future is false). - `on_future` (Boolean) When this is set to true and a schema_name is provided, apply this grant on all future file formats in the given schema. When this is true and no schema_name is provided apply this grant on all future file formats in the given database. The file_format_name field must be unset in order to use on_future. - `privilege` (String) The privilege to grant on the current or future file format. -- `roles` (Set of String) Grants privilege to these roles. - `schema_name` (String) The name of the schema containing the current or future file formats on which to grant privileges. - `with_grant_option` (Boolean) When this is set to true, allows the recipient role to grant the privileges to other roles.