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: function not exist and integration grant #1154

Merged
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
7 changes: 5 additions & 2 deletions pkg/resources/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,11 @@ func ReadFunction(d *schema.ResourceData, meta interface{}) error {
return err
}
rows, err := snowflake.Query(db, stmt)
if err != nil {
return err
if err != nil && snowflake.IsResourceNotExistOrNotAuthorized(err.Error(), "Function") {
// If not found, mark resource to be removed from statefile during apply or refresh
log.Printf("[DEBUG] function (%s) not found or we are not authorized.Err:\n%s", d.Id(), err.Error())
d.SetId("")
return nil
}
defer rows.Close()
descPropValues, err := snowflake.ScanFunctionDescription(rows)
Expand Down
18 changes: 17 additions & 1 deletion pkg/resources/grant_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,29 @@ func readGenericGrant(
}
return err
}

priv := d.Get("privilege").(string)
grantOption := d.Get("with_grant_option").(bool)

var relevantGrants []*grant
for _, grant := range grants {
if grant.Privilege == priv && grant.GrantOption == grantOption {
relevantGrants = append(relevantGrants, grant)
}
}

// If no relevant grants, set id to blank and return
if len(relevantGrants) == 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you check error code rather than use a hack?

Copy link
Contributor Author

@israel israel Sep 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that returning empty ID is the hack. (from the comment above that mentions the HACK). Regardless, I think that the check is OK. There is no error code in this case. A grant (or grants were) was found but non relevant to our search since they do not match the privilege or grant option properties.

d.SetId("")
return nil
}

// Map of roles to privileges
rolePrivileges := map[string]PrivilegeSet{}
sharePrivileges := map[string]PrivilegeSet{}

// List of all grants for each schema_database
for _, grant := range grants {
for _, grant := range relevantGrants {
switch grant.GranteeType {
case "ROLE":
roleName := grant.GranteeName
Expand Down Expand Up @@ -273,6 +287,7 @@ func readGenericGrant(
existingRoles := d.Get("roles").(*schema.Set)
multipleGrantFeatureFlag := d.Get("enable_multiple_grants").(bool)
var roles, shares []string

// Now see which roles have our privilege
for roleName, privileges := range rolePrivileges {
// Where priv is not all so it should match exactly
Expand Down Expand Up @@ -312,6 +327,7 @@ func readGenericGrant(
if err != nil {
return err
}

return nil
}

Expand Down
8 changes: 1 addition & 7 deletions pkg/resources/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package resources
import (
"database/sql"
"log"
"regexp"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -141,11 +140,6 @@ var userSchema = map[string]*schema.Schema{
// MINS_TO_BYPASS_NETWORK POLICY = <integer>
}

func isUserNotExistOrNotAuthorized(errorString string) bool {
var userNotExistOrNotAuthorizedRegEx, _ = regexp.Compile("SQL compilation error:User '.*' does not exist or not authorized.")
return userNotExistOrNotAuthorizedRegEx.MatchString(strings.ReplaceAll(errorString, "\n", ""))
}

func User() *schema.Resource {
return &schema.Resource{
Create: CreateUser,
Expand Down Expand Up @@ -173,7 +167,7 @@ func ReadUser(d *schema.ResourceData, meta interface{}) error {
stmt := snowflake.User(id).Describe()
rows, err := snowflake.Query(db, stmt)

if err != nil && isUserNotExistOrNotAuthorized(err.Error()) {
if err != nil && snowflake.IsResourceNotExistOrNotAuthorized(err.Error(), "User") {
// If not found, mark resource to be removed from statefile during apply or refresh
log.Printf("[DEBUG] user (%s) not found or we are not authorized.Err:\n%s", d.Id(), err.Error())
d.SetId("")
Expand Down
12 changes: 12 additions & 0 deletions pkg/snowflake/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
package snowflake

import (
"fmt"
"regexp"
"strings"
)

// Generic Errors
var (
ErrNoRowInRS = "sql: no rows in result set"
)

func IsResourceNotExistOrNotAuthorized(errorString string, resourceType string) bool {
regexStr := fmt.Sprintf("SQL compilation error:%s '.*' does not exist or not authorized.", resourceType)
var userNotExistOrNotAuthorizedRegEx, _ = regexp.Compile(regexStr)
return userNotExistOrNotAuthorizedRegEx.MatchString(strings.ReplaceAll(errorString, "\n", ""))
}