[Snowflake Official] SDK V2 Design Document #1767
Unanswered
sfc-gh-swinkler
asked this question in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Problem Statement
As part of our ongoing effort to improve stability of the provider, the Snowflake Terraform Provider team would like to introduce design standards for a new v2 SDK. The purpose of this SDK is to replace the
snowflake
package (herein referred to as v1 SDK), and also make it easier to write scripts for other sorts of applications which could benefit from a consistent SDK.Over the years the library of functions in the
snowflake
package has grown immensely, and multiple competing design patterns have emerged for how to implement certain API endpoints. The most common pattern seems to be the builder pattern with helper functions.Builder Pattern
Helper Functions
In both the builder pattern with helper functions design, the v1 SDK is implemented by appending strings together until the final statement is constructed using many if/else blocks. This works, but is a bit tedious, as it results in a lot of duplicated code.
The second most commonly used pattern is what I call the “generic sql builder” pattern. This used to be the standard for all resources, but the trend has been to move away from it in recent times, since it doesn’t always put properties in the right order, and is difficult to debug. Also if you “fix” a bug for one issue, you often introduce a bug somewhere else. There is also not much room for customization, other than a raw SQL escape hatch, so helper functions are still employed to shore up the gaps. There’s not much difference between using the generic sql builder vs non generic from a user perspective, since there are wrapper builders that hide this detail.
Generic Builder
The main problem of the v1 provider is consistency and testability. I have discussed the most popular designs, but there are also one-off designs for specific resources. Examples are Materialized View, which uses a token parser, and ExternalOAuth integration which uses another kind of generic SQL generator in the same vein as Generic Builder.
V2 SDK Proposal
We have developed a new design pattern which we intend to have completely replace the existing v1 SDK. We call this the “v2 SDK”. The basic idea is that there is an overall Client which manages connections, and holds services. Each service implements an interface that matches as closely as possible to the Snowflake SQL API. For example:
Password Policy Interface
Example Usage of the v2 SDK
This new design closely matches the style of Snowflake API, and each function is unambiguously named. You can see the new design in the “pkg/sdk” folder. It relies on structs to define not only the interface, but how the SQL will be parsed.
Here is the PasswordPolicyCreateOptions. As you can see, the structure closely matches that of the Snowflake API https://docs.snowflake.com/en/sql-reference/sql/create-password-policy. This is intentional, as the way the SQL statement is constructed is by recursively looping through the struct fields and using the “ddl” and “db” tags to build up a list of clauses which are then joined together.
Options struct defines inputs
The “ddl” and “db” tags have special meaning. The tag “ddl” is used to specify the type of clause and how it should be rendered, while the tag “db” is used to set any word as it would appear in the query.
DDL Type List
Keyword - this means that the field may or may not be set. Like a switch that is either on or off. Keywords can appear on any struct type. If the “db” tag is set, then it will use that, otherwise it will use the field value (expecting string type).
Static - this is a special field that always appears no matter what. It is always a private unexported field, since it is not something the user can change. It only uses the “db” tag.
Identifier - identifiers implement the object identifier interface. They are used to properly display fully qualified names. Identifiers do not use the “db” tag.
Parameter - most fields are parameters. This means that they follow the syntax “key = value”. Parameters can be quoted via the “ddl” tag. E.g. “ddl:
Command - special kinds of parameters that don’t use equals signs. E.g. “key value”
These basic types can be applied to any struct or nested struct, allowing for more complexity than would otherwise be apparent.
Nested Structs
All ddl types can be modified with quotes. For example, the following specifies a keyword that uses single quotes. I.e. ‘’
DDL Keyword with Quotes Modifier
Contributing
We understand that it will be a long process to migrate the existing resources to use the new SDK, so we will continue to accept PRs using the old design patterns to fix bugs and such. For new resources and data sources however, we kindly ask that you adopt the new SDK style. If you have any questions, please feel free to reach out to the team.
Thank you,
Snowflake Terraform Provider Team
Beta Was this translation helpful? Give feedback.
All reactions