Skip to content

Commit

Permalink
chore: replace warning in new grant resources with info log (#2521)
Browse files Browse the repository at this point in the history
To prevent warning spam for new grant resources, I replaced the warning
diag with an info log stating that with the chosen option no Snowflake
changes will be reflected by the Terraform config. (maybe the log
message could be changed a little to make if more obvious to the users?)
  • Loading branch information
sfc-gh-jcieslak authored Feb 19, 2024
1 parent a523a6b commit c3014b9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 71 deletions.
41 changes: 12 additions & 29 deletions pkg/resources/grant_privileges_to_account_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
"log"
"slices"
"strings"

Expand Down Expand Up @@ -720,19 +721,13 @@ func ReadGrantPrivilegesToAccountRole(ctx context.Context, d *schema.ResourceDat
}

if id.AllPrivileges {
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Warning,
Summary: "Show with all_privileges option is skipped.",
// TODO: link to the design decisions doc (SNOW-990811)
Detail: "See our document on design decisions for grants: <LINK (coming soon)>",
},
}
log.Printf("[INFO] Show with all_privileges option is skipped. No changes in privileges in Snowflake will be detected. Consider specyfying all privileges in 'privileges' block.")
return nil
}

opts, grantedOn, diags := prepareShowGrantsRequestForAccountRole(id)
if len(diags) != 0 {
return diags
opts, grantedOn := prepareShowGrantsRequestForAccountRole(id)
if opts == nil {
return nil
}

db := meta.(*sql.DB)
Expand Down Expand Up @@ -807,7 +802,7 @@ func ReadGrantPrivilegesToAccountRole(ctx context.Context, d *schema.ResourceDat
return nil
}

func prepareShowGrantsRequestForAccountRole(id GrantPrivilegesToAccountRoleId) (*sdk.ShowGrantOptions, sdk.ObjectType, diag.Diagnostics) {
func prepareShowGrantsRequestForAccountRole(id GrantPrivilegesToAccountRoleId) (*sdk.ShowGrantOptions, sdk.ObjectType) {
opts := new(sdk.ShowGrantOptions)
var grantedOn sdk.ObjectType

Expand Down Expand Up @@ -839,14 +834,8 @@ func prepareShowGrantsRequestForAccountRole(id GrantPrivilegesToAccountRoleId) (
},
}
case OnAllSchemasInDatabaseSchemaGrantKind:
return nil, "", diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Warning,
Summary: "Show with OnAllSchemasInDatabase option is skipped.",
// TODO: link to the design decisions doc (SNOW-990811)
Detail: "See our document on design decisions for grants: <LINK (coming soon)>",
},
}
log.Printf("[INFO] Show with on_schema.all_schemas_in_database option is skipped. No changes in privileges in Snowflake will be detected.")
return nil, ""
case OnFutureSchemasInDatabaseSchemaGrantKind:
opts.Future = sdk.Bool(true)
opts.In = &sdk.ShowGrantsIn{
Expand All @@ -863,14 +852,8 @@ func prepareShowGrantsRequestForAccountRole(id GrantPrivilegesToAccountRoleId) (
Object: data.Object,
}
case OnAllSchemaObjectGrantKind:
return nil, "", diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Warning,
Summary: "Show with OnAll option is skipped.",
// TODO: link to the design decisions doc (SNOW-990811)
Detail: "See our document on design decisions for grants: <LINK (coming soon)>",
},
}
log.Printf("[INFO] Show with on_schema_object.on_all option is skipped. No changes in privileges in Snowflake will be detected.")
return nil, ""
case OnFutureSchemaObjectGrantKind:
grantedOn = data.OnAllOrFuture.ObjectNamePlural.Singular()
opts.Future = sdk.Bool(true)
Expand All @@ -888,7 +871,7 @@ func prepareShowGrantsRequestForAccountRole(id GrantPrivilegesToAccountRoleId) (
}
}

return opts, grantedOn, nil
return opts, grantedOn
}

func getAccountRolePrivilegesFromSchema(d *schema.ResourceData) *sdk.AccountRoleGrantPrivileges {
Expand Down
41 changes: 12 additions & 29 deletions pkg/resources/grant_privileges_to_database_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
"log"
"slices"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
Expand Down Expand Up @@ -627,19 +628,13 @@ func ReadGrantPrivilegesToDatabaseRole(ctx context.Context, d *schema.ResourceDa
}

if id.AllPrivileges {
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Warning,
Summary: "Show with all_privileges option is skipped.",
// TODO: link to the design decisions doc (SNOW-990811)
Detail: "See our document on design decisions for grants: <LINK (coming soon)>",
},
}
log.Printf("[INFO] Show with all_privileges option is skipped. No changes in privileges in Snowflake will be detected. Consider specyfying all privileges in 'privileges' block.")
return nil
}

opts, grantedOn, diags := prepareShowGrantsRequest(id)
if len(diags) != 0 {
return diags
opts, grantedOn := prepareShowGrantsRequest(id)
if opts == nil {
return nil
}

db := meta.(*sql.DB)
Expand Down Expand Up @@ -694,7 +689,7 @@ func ReadGrantPrivilegesToDatabaseRole(ctx context.Context, d *schema.ResourceDa
return nil
}

func prepareShowGrantsRequest(id GrantPrivilegesToDatabaseRoleId) (*sdk.ShowGrantOptions, sdk.ObjectType, diag.Diagnostics) {
func prepareShowGrantsRequest(id GrantPrivilegesToDatabaseRoleId) (*sdk.ShowGrantOptions, sdk.ObjectType) {
opts := new(sdk.ShowGrantOptions)
var grantedOn sdk.ObjectType

Expand All @@ -721,14 +716,8 @@ func prepareShowGrantsRequest(id GrantPrivilegesToDatabaseRoleId) (*sdk.ShowGran
},
}
case OnAllSchemasInDatabaseSchemaGrantKind:
return nil, "", diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Warning,
Summary: "Show with OnAllSchemasInDatabase option is skipped.",
// TODO: link to the design decisions doc (SNOW-990811)
Detail: "See our document on design decisions for grants: <LINK (coming soon)>",
},
}
log.Printf("[INFO] Show with on_schema.all_schemas_in_database option is skipped. No changes in privileges in Snowflake will be detected.")
return nil, ""
case OnFutureSchemasInDatabaseSchemaGrantKind:
opts.Future = sdk.Bool(true)
opts.In = &sdk.ShowGrantsIn{
Expand All @@ -745,14 +734,8 @@ func prepareShowGrantsRequest(id GrantPrivilegesToDatabaseRoleId) (*sdk.ShowGran
Object: data.Object,
}
case OnAllSchemaObjectGrantKind:
return nil, "", diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Warning,
Summary: "Show with OnAll option is skipped.",
// TODO: link to the design decisions doc (SNOW-990811)
Detail: "See our document on design decisions for grants: <LINK (coming soon)>",
},
}
log.Printf("[INFO] Show with on_schema_object.on_all option is skipped. No changes in privileges in Snowflake will be detected.")
return nil, ""
case OnFutureSchemaObjectGrantKind:
grantedOn = data.OnAllOrFuture.ObjectNamePlural.Singular()
opts.Future = sdk.Bool(true)
Expand All @@ -770,7 +753,7 @@ func prepareShowGrantsRequest(id GrantPrivilegesToDatabaseRoleId) (*sdk.ShowGran
}
}

return opts, grantedOn, nil
return opts, grantedOn
}

func getDatabaseRolePrivilegesFromSchema(d *schema.ResourceData) *sdk.DatabaseRoleGrantPrivileges {
Expand Down
20 changes: 7 additions & 13 deletions pkg/resources/grant_privileges_to_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,9 @@ func ReadGrantPrivilegesToShare(ctx context.Context, d *schema.ResourceData, met
}
}

opts, grantedOn, diags := prepareShowGrantsRequestForShare(id)
if len(diags) != 0 {
return diags
opts, grantedOn := prepareShowGrantsRequestForShare(id)
if opts == nil {
return nil
}

db := meta.(*sql.DB)
Expand Down Expand Up @@ -440,7 +440,7 @@ func getShareGrantOn(d *schema.ResourceData) *sdk.ShareGrantOn {
return grantOn
}

func prepareShowGrantsRequestForShare(id GrantPrivilegesToShareId) (*sdk.ShowGrantOptions, sdk.ObjectType, diag.Diagnostics) {
func prepareShowGrantsRequestForShare(id GrantPrivilegesToShareId) (*sdk.ShowGrantOptions, sdk.ObjectType) {
opts := new(sdk.ShowGrantOptions)
var objectType sdk.ObjectType

Expand All @@ -452,14 +452,8 @@ func prepareShowGrantsRequestForShare(id GrantPrivilegesToShareId) (*sdk.ShowGra
case OnTableShareGrantKind:
objectType = sdk.ObjectTypeTable
case OnAllTablesInSchemaShareGrantKind:
return nil, "", diag.Diagnostics{
diag.Diagnostic{
// TODO: link to the design decisions doc (SNOW-990811)
Severity: diag.Warning,
Summary: "Show with OnAll option is skipped.",
Detail: "See our document on design decisions for grants: <LINK (coming soon)>",
},
}
log.Printf("[INFO] Show with on_all_tables_in_schema option is skipped. No changes in privileges in Snowflake will be detected.")
return nil, ""
case OnTagShareGrantKind:
objectType = sdk.ObjectTypeTag
case OnViewShareGrantKind:
Expand All @@ -473,5 +467,5 @@ func prepareShowGrantsRequestForShare(id GrantPrivilegesToShareId) (*sdk.ShowGra
},
}

return opts, objectType, nil
return opts, objectType
}

0 comments on commit c3014b9

Please sign in to comment.