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

feat: Parameters sdk v2 #1914

Merged
merged 27 commits into from
Sep 12, 2023
Merged

feat: Parameters sdk v2 #1914

merged 27 commits into from
Sep 12, 2023

Conversation

sfc-gh-pbosak
Copy link
Contributor

@sfc-gh-pbosak sfc-gh-pbosak commented Jun 27, 2023

  • updates session and account parameter resources to use new sdk implementation

pkg/resources/account_parameter.go Outdated Show resolved Hide resolved
pkg/resources/account_parameter.go Outdated Show resolved Hide resolved
pkg/resources/account_parameter.go Outdated Show resolved Hide resolved
pkg/resources/session_parameter.go Outdated Show resolved Hide resolved
@github-actions
Copy link

Integration tests failure for 1a304b8e54ca755bb7664b3ee9978cba1cf62e3a

@github-actions
Copy link

Integration tests failure for 3dd4b1ec0c67bf6aeb689277dc978e90d722697f

@sfc-gh-pbosak sfc-gh-pbosak changed the title Parameters sdk v2 feat: Parameters sdk v2 Jun 28, 2023
@github-actions
Copy link

Integration tests failure for 5aa65eb9b74e76b70887a8f00c7416b5ce03e396

1 similar comment
@github-actions
Copy link

Integration tests failure for 5aa65eb9b74e76b70887a8f00c7416b5ce03e396

@github-actions
Copy link

Integration tests failure for fbf0e75c04cf5688b207caf4988ee2279156f681

@github-actions
Copy link

Integration tests success for 1807dda8ce2ed3e45a4d736a9661aab9c434d984

@github-actions
Copy link

Integration tests success for f8d2ffa56eb67142d95bf1d70f08d31cb0bd60dd

@github-actions
Copy link

Integration tests success for 0e88f4f323a25121ee508695e4acc769af6681d2

@sfc-gh-pbosak sfc-gh-pbosak marked this pull request as ready for review June 28, 2023 13:44
}

func (parameters *parameters) SetAccountParameter(ctx context.Context, parameter AccountParameter, value string) error {
opts := AlterAccountOptions{Set: &AccountSet{Parameters: &AccountLevelParameters{AccountParameters: &AccountParameters{}}}}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is hard to read, can you split this over multiple lines?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

pkg/resources/session_parameter.go Outdated Show resolved Hide resolved
err := client.Parameters.SetSessionParameterForAccount(ctx, parameter, value)
if err != nil {
return err
}
} else {
if user == "" {
return fmt.Errorf("user is required if on_account is false")
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we move this logic to a validation function in the resource schema?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if we can do that since we only check if it's empty if on_account is false. validation functions in resource schema cannot access another attribute value afaik

Copy link
Collaborator

Choose a reason for hiding this comment

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

in the tf plugin framework it is possible to use a plan modifier to validate this as Nathan is describing. it is something we should revisit, for sure

if reflect.TypeOf(parameterDefault.DefaultValue) == typeString {
value = fmt.Sprintf("'%s'", snowflake.EscapeString(value))
}
builder := snowflake.NewSessionParameter(key, value, db)
Copy link
Contributor

Choose a reason for hiding this comment

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

Still using the old SDK here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

Comment on lines 86 to 89
typeString := reflect.TypeOf("")
if reflect.TypeOf(parameterDefault.DefaultValue) == typeString {
value = fmt.Sprintf("'%s'", snowflake.EscapeString(value))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

pkg/resources/session_parameter.go Outdated Show resolved Hide resolved
if user == "" {
return fmt.Errorf("user is required if on_account is false")
}
parameterDefault := snowflake.GetParameterDefaults(snowflake.ParameterTypeSession)[key]
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove dependency on old SDK.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@@ -91,6 +93,9 @@ func CreateObjectParameter(d *schema.ResourceData, meta interface{}) error {
db := meta.(*sql.DB)
key := d.Get("key").(string)
value := d.Get("value").(string)
client := sdk.NewClientFromDB(db)
ctx := context.Background()
parameter := sdk.ObjectParameter(key)

parameterDefault := snowflake.GetParameterDefaults(snowflake.ParameterTypeObject)[key]
Copy link
Contributor

Choose a reason for hiding this comment

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

Old SDK here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think we can get rid of that in this case, at least not fully. In order to make use of new sdk here, we need to have new sdk implementation for all objects which can take parameters and that's not the case just yet. It can be improved in the future, but I think it has to use old sdk for now


parameterDefault := snowflake.GetParameterDefaults(snowflake.ParameterTypeAccount)[key]
parameterDefault := snowflake.GetParameterDefaults(snowflake.ParameterTypeSession)[key]
Copy link
Contributor

Choose a reason for hiding this comment

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

Old SDK here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

Comment on lines 52 to 66
func TestAcc_AccountParameter_CLIENT_METADATA_USE_SESSION_DATABASE(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
Providers: providers(),
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: accountParameterBasic("CLIENT_METADATA_USE_SESSION_DATABASE", "true"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_account_parameter.p", "key", "CLIENT_METADATA_USE_SESSION_DATABASE"),
resource.TestCheckResourceAttr("snowflake_account_parameter.p", "value", "true"),
),
},
},
})
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we removing this test? It should still pass, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test tries to set CLIENT_METADATA_USE_SESSION_DATABASE for account.
CLIENT_METADATA_USE_SESSION_DATABASE is a session parameter and I think we should only allow to set it using a session_parameter. For now I modified it to use an actual account parameter, but if you think we should allow to set session parameters using account_parameter resource then we can discuss it

if err != nil {
return fmt.Errorf("error reading account parameter err = %w", err)
return fmt.Errorf("error creating account parameter err = %w", err)
Copy link
Collaborator

Choose a reason for hiding this comment

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

this should probably say "error resetting account parameter to default value"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

pkg/resources/session_parameter.go Outdated Show resolved Hide resolved
@github-actions
Copy link

Integration tests success for 555e6f156f276102504ea164f90c1260c9082775

@github-actions
Copy link

Integration tests failure for d2b679e1cc53bf4a943bf83bc062d04df4ca16dc

@github-actions
Copy link

Integration tests failure for 98f91238b1bc918acee47e7cfbd7fd11dd3be7d4

pkg/sdk/parameters.go Outdated Show resolved Hide resolved
pkg/sdk/parameters.go Outdated Show resolved Hide resolved
pkg/sdk/parameters.go Outdated Show resolved Hide resolved
pkg/sdk/parameters.go Outdated Show resolved Hide resolved
pkg/sdk/parameters.go Outdated Show resolved Hide resolved
pkg/sdk/parameters.go Outdated Show resolved Hide resolved
pkg/sdk/parameters.go Outdated Show resolved Hide resolved
pkg/sdk/parameters.go Outdated Show resolved Hide resolved
pkg/sdk/validation_test.go Outdated Show resolved Hide resolved
pkg/datasources/parameters.go Outdated Show resolved Hide resolved
pkg/resources/database_grant.go Outdated Show resolved Hide resolved
pkg/resources/external_table_grant.go Outdated Show resolved Hide resolved
@github-actions
Copy link

Integration tests failure for ad693953fadd6c92212f6260607504b7d855b4c9

@github-actions
Copy link

Integration tests failure for 44283b2a181d7b69208ec969bf91df40223a0cb0

@github-actions
Copy link

Integration tests failure for b45d015595bf3d3755037e77676d63cd3a6e55a7

@github-actions
Copy link

Integration tests failure for a306eadc47dc57d4c0f1810d94b0ae56613decbb

@github-actions
Copy link

Integration tests failure for 8411935d901d257ffe3fdf73bb7246f8d3fcabd3

@github-actions
Copy link

Integration tests failure for b41d4d4a80243af477d7d5675ddba56bd83e20d2

@github-actions
Copy link

Integration tests failure for 6e4883eaf195e9fc4630f43decde18beb89cb89b

@github-actions
Copy link

Integration tests failure for 76a384754b02267502dbda6d786a48b53299f1a6

@github-actions
Copy link

Integration tests failure for 3665a78bb563c3746cae573e8e3fb30258905c94

@github-actions
Copy link

Integration tests success for 3ae991be2c7d81813dabf68bbaec7a29fb5af733

@github-actions
Copy link

Integration tests success for 4c091ac2585c8178a9cde37f9c535fe9fba62115

err := client.Parameters.SetSessionParameterForAccount(ctx, parameter, value)
if err != nil {
return err
}
} else {
if user == "" {
return fmt.Errorf("user is required if on_account is false")
Copy link
Collaborator

Choose a reason for hiding this comment

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

in the tf plugin framework it is possible to use a plan modifier to validate this as Nathan is describing. it is something we should revisit, for sure

SetSessionParameterForAccount(ctx context.Context, parameter SessionParameter, value string) error
SetSessionParameterForUser(ctx context.Context, id AccountObjectIdentifier, parameter SessionParameter, value string) error
SetObjectParameterForAccount(ctx context.Context, parameter ObjectParameter, value string) error
SetObjectParameterForUser(ctx context.Context, id AccountObjectIdentifier, parameter ObjectParameter, value string) error
Copy link
Collaborator

Choose a reason for hiding this comment

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

this makes no sense. you cannot set an object parameter for a user. Object parameters can only be set for objects or at the account level. Perhaps this should be renamed to SetObjectParameterForObject

return nil
}

func (parameters *parameters) SetSessionParameterForUser(ctx context.Context, id AccountObjectIdentifier, parameter SessionParameter, value string) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

this code is copypasted from above. maybe should be refactored?

func (parameters *parameters) SetObjectParameterForUser(ctx context.Context, id AccountObjectIdentifier, parameter ObjectParameter, value string) error {
opts := AlterUserOptions{Set: &UserSet{ObjectParameters: &UserObjectParameters{}}}
switch parameter {
case ObjectParameterNetworkPolicy:
Copy link
Collaborator

Choose a reason for hiding this comment

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

both of these are actually session parameters

AllowedObjectTypes []ObjectType
}

// ParameterDefaults returns a map of default values for all parameters.
Copy link
Collaborator

Choose a reason for hiding this comment

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

these parameter defaults feel clunky

return ParameterDefaults()[key]
}

func parseBooleanParameter(parameter, value string) (_ *bool, err error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

this could be any kind of parse function, no?

@sfc-gh-swinkler sfc-gh-swinkler merged commit 1d15355 into main Sep 12, 2023
7 of 8 checks passed
@sfc-gh-swinkler sfc-gh-swinkler deleted the parameters-sdk-v2 branch September 12, 2023 22:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants