Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: schema name is optional for future file_format_grant #1484

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/resources/file_format_grant.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ 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.
- `roles` (Set of String) Grants privilege to these roles.

### Optional

- `enable_multiple_grants` (Boolean) 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.
- `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.

### Read-Only
Expand Down
71 changes: 37 additions & 34 deletions pkg/resources/file_format_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -42,15 +50,14 @@ 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.",
},
"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": {
Expand All @@ -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.
Expand Down Expand Up @@ -96,19 +96,22 @@ 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")
}
if (schemaName == "") && !onFuture {
return errors.New("schema_name must be set unless on_future is true")
}

var builder snowflake.GrantBuilder
if futureFileFormats {
if onFuture {
builder = snowflake.FutureFileFormatGrant(dbName, schemaName)
} else {
builder = snowflake.FileFormatGrant(dbName, schemaName, fileFormatName)
Expand Down Expand Up @@ -152,14 +155,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 {
Expand All @@ -170,13 +173,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.
Expand All @@ -189,10 +192,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)
Expand Down Expand Up @@ -223,11 +226,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)
Expand Down