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: add app packages #2323

Merged
merged 5 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
160 changes: 160 additions & 0 deletions pkg/sdk/application_packages_def.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package sdk

import g "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator"

//go:generate go run ./poc/main.go

// https://medium.com/snowflake/hello-world-snowflake-native-apps-76e1ce82e1be
sfc-gh-asawicki marked this conversation as resolved.
Show resolved Hide resolved

var applicationPackageModifyReleaseDirective = g.NewQueryStruct("ModifyReleaseDirective").
Text("ReleaseDirective", g.KeywordOptions().NoQuotes().Required()).
TextAssignment("VERSION", g.ParameterOptions().NoQuotes().Required()).
NumberAssignment("PATCH", g.ParameterOptions().NoQuotes().Required())

var applicationPackageSetReleaseDirective = g.NewQueryStruct("SetReleaseDirective").
Text("ReleaseDirective", g.KeywordOptions().NoQuotes().Required()).
PredefinedQueryStructField("Accounts", "[]string", g.ParameterOptions().MustParentheses().NoQuotes().Required().SQL("ACCOUNTS")).
TextAssignment("VERSION", g.ParameterOptions().NoQuotes().Required()).
NumberAssignment("PATCH", g.ParameterOptions().NoQuotes().Required())

var applicationPackageUnsetReleaseDirective = g.NewQueryStruct("UnsetReleaseDirective").
Text("ReleaseDirective", g.KeywordOptions().NoQuotes().Required())

var applicationPackageSetDefaultReleaseDirective = g.NewQueryStruct("SetDefaultReleaseDirective").
TextAssignment("VERSION", g.ParameterOptions().NoQuotes().Required()).
NumberAssignment("PATCH", g.ParameterOptions().NoQuotes().Required())

var applicationPackageAddVersion = g.NewQueryStruct("AddVersion").
OptionalText("VersionIdentifier", g.KeywordOptions().NoQuotes()).
TextAssignment("USING", g.ParameterOptions().NoEquals().SingleQuotes().Required()).
OptionalTextAssignment("Label", g.ParameterOptions().SingleQuotes())

var applicationPackageDropVersion = g.NewQueryStruct("DropVersion").
Text("VersionIdentifier", g.KeywordOptions().NoQuotes().Required())

var applicationPackageAddPatchForVersion = g.NewQueryStruct("AddPatchForVersion").
OptionalText("VersionIdentifier", g.KeywordOptions().NoQuotes().Required()).
TextAssignment("USING", g.ParameterOptions().NoEquals().SingleQuotes().Required()).
OptionalTextAssignment("Label", g.ParameterOptions().SingleQuotes())

var applicationPackageSet = g.NewQueryStruct("ApplicationPackageSet").
OptionalNumberAssignment("DATA_RETENTION_TIME_IN_DAYS", g.ParameterOptions().NoQuotes()).
OptionalNumberAssignment("MAX_DATA_EXTENSION_TIME_IN_DAYS", g.ParameterOptions().NoQuotes()).
OptionalTextAssignment("DEFAULT_DDL_COLLATION", g.ParameterOptions().SingleQuotes()).
OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()).
PredefinedQueryStructField("Distribution", "*Distribution", g.ParameterOptions().SQL("DISTRIBUTION"))

var ApplicationPackagesDef = g.NewInterface(
"ApplicationPackages",
"ApplicationPackage",
g.KindOfT[AccountObjectIdentifier](),
).CreateOperation(
"https://docs.snowflake.com/en/sql-reference/sql/create-application-package",
g.NewQueryStruct("CreateApplicationPackage").
Create().
SQL("APPLICATION PACKAGE").
IfNotExists().
Name().
OptionalNumberAssignment("DATA_RETENTION_TIME_IN_DAYS", g.ParameterOptions().NoQuotes()).
OptionalNumberAssignment("MAX_DATA_EXTENSION_TIME_IN_DAYS", g.ParameterOptions().NoQuotes()).
OptionalTextAssignment("DEFAULT_DDL_COLLATION", g.ParameterOptions().SingleQuotes()).
OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()).
PredefinedQueryStructField("Distribution", "*Distribution", g.ParameterOptions().SQL("DISTRIBUTION")).
OptionalTags().
sfc-gh-asawicki marked this conversation as resolved.
Show resolved Hide resolved
WithValidation(g.ValidIdentifier, "name"),
).AlterOperation(
"https://docs.snowflake.com/en/sql-reference/sql/alter-application-package",
g.NewQueryStruct("AlterApplicationPackage").
Alter().
SQL("APPLICATION PACKAGE").
IfExists().
Name().
OptionalQueryStructField(
"Set",
applicationPackageSet,
g.KeywordOptions().SQL("SET"),
).
OptionalSQL("UNSET DATA_RETENTION_TIME_IN_DAYS").
sfc-gh-jcieslak marked this conversation as resolved.
Show resolved Hide resolved
sfc-gh-asawicki marked this conversation as resolved.
Show resolved Hide resolved
OptionalSQL("UNSET MAX_DATA_EXTENSION_TIME_IN_DAYS").
OptionalSQL("UNSET DEFAULT_DDL_COLLATION").
OptionalSQL("UNSET COMMENT").
OptionalSQL("UNSET DISTRIBUTION").
OptionalQueryStructField(
"ModifyReleaseDirective",
applicationPackageModifyReleaseDirective,
g.KeywordOptions().SQL("MODIFY RELEASE DIRECTIVE"),
).
OptionalQueryStructField(
"SetDefaultReleaseDirective",
applicationPackageSetDefaultReleaseDirective,
g.KeywordOptions().SQL("SET DEFAULT RELEASE DIRECTIVE"),
).
OptionalQueryStructField(
"SetReleaseDirective",
applicationPackageSetReleaseDirective,
g.KeywordOptions().SQL("SET RELEASE DIRECTIVE"),
).
OptionalQueryStructField(
"UnsetReleaseDirective",
applicationPackageUnsetReleaseDirective,
g.KeywordOptions().SQL("UNSET RELEASE DIRECTIVE"),
).
OptionalQueryStructField(
"AddVersion",
applicationPackageAddVersion,
g.KeywordOptions().SQL("ADD VERSION"),
).
OptionalQueryStructField(
"DropVersion",
applicationPackageDropVersion,
g.KeywordOptions().SQL("DROP VERSION"),
).
OptionalQueryStructField(
"AddPatchForVersion",
applicationPackageAddPatchForVersion,
g.KeywordOptions().SQL("ADD PATCH FOR VERSION"),
).
OptionalSetTags().
OptionalUnsetTags().
WithValidation(g.ValidIdentifier, "name").
WithValidation(g.ExactlyOneValueSet, "Set", "UnsetDataRetentionTimeInDays", "UnsetMaxDataExtensionTimeInDays", "UnsetDefaultDdlCollation", "UnsetComment", "UnsetDistribution", "ModifyReleaseDirective", "SetDefaultReleaseDirective", "SetReleaseDirective", "UnsetReleaseDirective", "AddVersion", "DropVersion", "AddPatchForVersion", "SetTags", "UnsetTags"),
Copy link
Collaborator

Choose a reason for hiding this comment

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

why you treat set and unset differently (I mean these 5: "UnsetDataRetentionTimeInDays", "UnsetMaxDataExtensionTimeInDays", "UnsetDefaultDdlCollation", "UnsetComment", "UnsetDistribution")? And why we are limiting the unset query to just one unset per statement? According to the docs we can unset multiple parameters as one statement. This is also what we are doing for other resources.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tried, set multiple params works, but unset multiple params does not work

Copy link
Collaborator

@sfc-gh-asawicki sfc-gh-asawicki Jan 17, 2024

Choose a reason for hiding this comment

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

It works according to the docs: https://docs.snowflake.com/en/sql-reference/sql/alter-application-package#syntax. I checked it with a sample code:

-- DROP APPLICATION PACKAGE "aswtesting17012024";
CREATE APPLICATION PACKAGE "aswtesting17012024";
ALTER APPLICATION PACKAGE  "aswtesting17012024" SET COMMENT = 'some comment' DISTRIBUTION = INTERNAL;
ALTER APPLICATION PACKAGE  "aswtesting17012024" UNSET COMMENT, DISTRIBUTION;

I guess that the comma is missing for the UNSET in the docs but it seems to work just fine.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

created an unset helper struct

Copy link
Collaborator

Choose a reason for hiding this comment

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

But still, you limited it to just one field that can be unset, while according to the SQL statements I have attached above you can do multiple unset.

It should be configured similarly to e.g. storage integration.

So in your case please change:

OptionalQueryStructField(
			"Unset",
			applicationPackageUnset,
			g.KeywordOptions().SQL("UNSET"),
		).

to

OptionalQueryStructField(
			"Unset",
			applicationPackageUnset,
			g.ListOptions().NoParentheses().SQL("UNSET"),
		).

and
validation type in applicationPackageUnset from ExactlyOneValueSet to AtLeastOneValueSet.

We can merge this one and you will correct it in the follow-up PR. WDYT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sure, i will fix in a follow up PR

).DropOperation(
"https://docs.snowflake.com/en/sql-reference/sql/drop-application-package",
g.NewQueryStruct("DropApplicationPackage").
Drop().
SQL("APPLICATION PACKAGE").
Name().
WithValidation(g.ValidIdentifier, "name"),
).ShowOperation(
"https://docs.snowflake.com/en/sql-reference/sql/show-application-packages",
g.DbStruct("applicationPackageRow").
Field("created_on", "string").
Field("name", "string").
Field("is_default", "string").
Field("is_current", "string").
Field("distribution", "string").
Field("owner", "string").
Field("comment", "string").
Field("retention_time", "int").
Field("options", "string").
Field("dropped_on", "sql.NullString").
Field("application_class", "sql.NullString"),
g.PlainStruct("ApplicationPackage").
Field("CreatedOn", "string").
Field("Name", "string").
Field("IsDefault", "bool").
Field("IsCurrent", "bool").
Field("Distribution", "string").
Field("Owner", "string").
Field("Comment", "string").
Field("RetentionTime", "int").
Field("Options", "string").
Field("DroppedOn", "string").
Field("ApplicationClass", "string"),
g.NewQueryStruct("ShowApplicationPackages").
Show().
SQL("APPLICATION PACKAGES").
OptionalLike().
OptionalStartsWith().
OptionalLimit(),
).ShowByIdOperation()
Loading
Loading