-
Notifications
You must be signed in to change notification settings - Fork 427
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
feat: add on_account to session and object params #1685
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8a178ee
add on_account to session and object params
sfc-gh-swinkler 3cff2eb
add on_account to session and object params
sfc-gh-swinkler a0b47af
fix parameters data source acc tests
sfc-gh-swinkler ae21f4d
refactor parameters to remove code duplication
sfc-gh-swinkler 3449e6a
refactor parameters to remove code duplication
sfc-gh-swinkler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,11 @@ | ||
resource "snowflake_session_parameter" "s" { | ||
key = "AUTOCOMMIT" | ||
value = "false" | ||
user = "TEST_USER" | ||
} | ||
|
||
resource "snowflake_session_parameter" "s2" { | ||
key = "BINARY_OUTPUT_FORMAT" | ||
value = "BASE64" | ||
on_account = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,100 @@ | ||
package datasources_test | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAcc_Parameters(t *testing.T) { | ||
func TestAcc_ParametersOnAccount(t *testing.T) { | ||
resource.ParallelTest(t, resource.TestCase{ | ||
Providers: providers(), | ||
CheckDestroy: nil, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: parameters(), | ||
Config: parametersConfigOnAccount(), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.snowflake_parameters.p", "pattern", "AUTOCOMMIT"), | ||
resource.TestCheckResourceAttr("data.snowflake_parameters.p", "parameters.#", "1"), | ||
resource.TestCheckResourceAttr("data.snowflake_parameters.p", "parameters.0.key", "AUTOCOMMIT"), | ||
resource.TestCheckResourceAttrSet("data.snowflake_parameters.p", "parameters.0.value"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAcc_ParametersOnSession(t *testing.T) { | ||
userName := "TEST_USER_" + strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) | ||
resource.ParallelTest(t, resource.TestCase{ | ||
Providers: providers(), | ||
CheckDestroy: nil, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: parametersConfigOnSession(userName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.snowflake_parameters.p", "parameters.#"), | ||
resource.TestCheckResourceAttrSet("data.snowflake_parameters.p", "parameters.0.key"), | ||
resource.TestCheckResourceAttrSet("data.snowflake_parameters.p", "parameters.0.value"), | ||
resource.TestCheckResourceAttr("data.snowflake_parameters.p", "user", userName), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func parameters() string { | ||
return `data "snowflake_parameters" "p" {}` | ||
func TestAcc_ParametersOnObject(t *testing.T) { | ||
dbName := "TEST_DB_" + strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) | ||
resource.ParallelTest(t, resource.TestCase{ | ||
Providers: providers(), | ||
CheckDestroy: nil, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: parametersConfigOnObject(dbName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.snowflake_parameters.p", "parameters.#"), | ||
resource.TestCheckResourceAttrSet("data.snowflake_parameters.p", "parameters.0.key"), | ||
resource.TestCheckResourceAttrSet("data.snowflake_parameters.p", "parameters.0.value"), | ||
resource.TestCheckResourceAttr("data.snowflake_parameters.p", "object_type", "DATABASE"), | ||
resource.TestCheckResourceAttr("data.snowflake_parameters.p", "object_name", dbName), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func parametersConfigOnAccount() string { | ||
return `data "snowflake_parameters" "p" { | ||
parameter_type = "ACCOUNT" | ||
pattern = "AUTOCOMMIT" | ||
}` | ||
} | ||
|
||
func parametersConfigOnSession(user string) string { | ||
s := ` | ||
resource "snowflake_user" "u" { | ||
name = "%s" | ||
} | ||
|
||
data "snowflake_parameters" "p" { | ||
parameter_type = "SESSION" | ||
user = snowflake_user.u.name | ||
}` | ||
return fmt.Sprintf(s, user) | ||
} | ||
|
||
func parametersConfigOnObject(name string) string { | ||
stmt := ` | ||
resource "snowflake_database" "d" { | ||
name = "%s" | ||
} | ||
data "snowflake_parameters" "p" { | ||
parameter_type = "OBJECT" | ||
object_type = "DATABASE" | ||
object_name = snowflake_database.d.name | ||
}` | ||
return fmt.Sprintf(stmt, name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,12 @@ var objectParameterSchema = map[string]*schema.Schema{ | |
Required: true, | ||
Description: "Value of object parameter, as a string. Constraints are the same as those for the parameters in Snowflake documentation.", | ||
}, | ||
"on_account": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
Description: "If true, the object parameter will be set on the account level.", | ||
}, | ||
"object_type": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
|
@@ -99,7 +105,12 @@ func CreateObjectParameter(d *schema.ResourceData, meta interface{}) error { | |
value = fmt.Sprintf("'%s'", snowflake.EscapeString(value)) | ||
} | ||
|
||
builder := snowflake.NewParameter(key, value, snowflake.ParameterTypeObject, db) | ||
builder := snowflake.NewObjectParameter(key, value, db) | ||
|
||
onAccount := d.Get("on_account").(bool) | ||
if onAccount { | ||
builder.SetOnAccount(onAccount) | ||
} | ||
|
||
var fullyQualifierObjectIdentifier string | ||
if v, ok := d.GetOk("object_identifier"); ok { | ||
|
@@ -121,7 +132,7 @@ func CreateObjectParameter(d *schema.ResourceData, meta interface{}) error { | |
if err != nil { | ||
return fmt.Errorf("error creating object parameter err = %w", err) | ||
} | ||
id := fmt.Sprintf("%v❄️%v❄️%v", key, objectType, fullyQualifierObjectIdentifier) | ||
id := fmt.Sprintf("%v|%v|%v", key, objectType, fullyQualifierObjectIdentifier) | ||
d.SetId(id) | ||
var p *snowflake.Parameter | ||
if fullyQualifierObjectIdentifier != "" { | ||
|
@@ -143,9 +154,12 @@ func CreateObjectParameter(d *schema.ResourceData, meta interface{}) error { | |
func ReadObjectParameter(d *schema.ResourceData, meta interface{}) error { | ||
db := meta.(*sql.DB) | ||
id := d.Id() | ||
parts := strings.Split(id, "❄️") | ||
parts := strings.Split(id, "|") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe worth extracting "|" into a global variable to avoid magic values. |
||
if len(parts) != 3 { | ||
parts = strings.Split(id, "❄️") // for backwards compatibility | ||
} | ||
if len(parts) != 3 { | ||
return fmt.Errorf("unexpected format of ID (%v), expected key❄️object_type❄️object_identifier", id) | ||
return fmt.Errorf("unexpected format of ID (%v), expected key|object_type|object_identifier", id) | ||
} | ||
key := parts[0] | ||
var p *snowflake.Parameter | ||
|
@@ -185,14 +199,20 @@ func DeleteObjectParameter(d *schema.ResourceData, meta interface{}) error { | |
if reflect.TypeOf(parameterDefault.DefaultValue) == typeString { | ||
value = fmt.Sprintf("'%s'", value) | ||
} | ||
builder := snowflake.NewParameter(key, value, snowflake.ParameterTypeObject, db) | ||
builder := snowflake.NewObjectParameter(key, value, db) | ||
|
||
onAccount := d.Get("on_account").(bool) | ||
if onAccount { | ||
builder.SetOnAccount(onAccount) | ||
} | ||
|
||
var fullyQualifierObjectIdentifier string | ||
if v, ok := d.GetOk("object_identifier"); ok { | ||
objectDatabase, objectSchema, objectName := expandObjectIdentifier(v.([]interface{})) | ||
fullyQualifierObjectIdentifier = snowflakeValidation.FormatFullyQualifiedObjectID(objectDatabase, objectSchema, objectName) | ||
builder.WithObjectIdentifier(fullyQualifierObjectIdentifier) | ||
} | ||
|
||
var objectType snowflake.ObjectType | ||
if v, ok := d.GetOk("object_type"); ok { | ||
objectType = snowflake.ObjectType(v.(string)) | ||
|
@@ -201,18 +221,11 @@ func DeleteObjectParameter(d *schema.ResourceData, meta interface{}) error { | |
} | ||
builder.WithObjectType(objectType) | ||
} | ||
|
||
err := builder.SetParameter() | ||
if err != nil { | ||
return fmt.Errorf("error deleting object parameter err = %w", err) | ||
} | ||
if fullyQualifierObjectIdentifier != "" { | ||
_, err = snowflake.ShowObjectParameter(db, key, objectType, fullyQualifierObjectIdentifier) | ||
} else { | ||
_, err = snowflake.ShowAccountParameter(db, key) | ||
} | ||
if err != nil { | ||
return fmt.Errorf("error reading object parameter err = %w", err) | ||
} | ||
|
||
d.SetId("") | ||
return nil | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this backward compatible?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the old behavior "SESSION" was actually the same as "ACCOUNT", hence setting it as "ACCOUNT" is the most sensible. This is generally what people want to do anyways