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 row access policy to the SDK #2363

Merged
merged 18 commits into from
Jan 18, 2024
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
2 changes: 2 additions & 0 deletions pkg/sdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Client struct {
Procedures Procedures
ResourceMonitors ResourceMonitors
Roles Roles
RowAccessPolicies RowAccessPolicies
Schemas Schemas
SessionPolicies SessionPolicies
Sessions Sessions
Expand Down Expand Up @@ -204,6 +205,7 @@ func (c *Client) initialize() {
c.ReplicationFunctions = &replicationFunctions{client: c}
c.ResourceMonitors = &resourceMonitors{client: c}
c.Roles = &roles{client: c}
c.RowAccessPolicies = &rowAccessPolicies{client: c}
c.Schemas = &schemas{client: c}
c.SessionPolicies = &sessionPolicies{client: c}
c.Sessions = &sessions{client: c}
Expand Down
8 changes: 8 additions & 0 deletions pkg/sdk/poc/generator/keyword_builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,11 @@ func (v *QueryStruct) OptionalLimit() *QueryStruct {
func (v *QueryStruct) OptionalCopyGrants() *QueryStruct {
return v.OptionalSQL("COPY GRANTS")
}

func (v *QueryStruct) BodyWithPrecedingArrow() *QueryStruct {
return v.PredefinedQueryStructField("body", "string", ParameterOptions().NoEquals().NoQuotes().SQL("->").Required())
}

func (v *QueryStruct) OptionalSetBodyWithPrecedingArrow() *QueryStruct {
return v.PredefinedQueryStructField("SetBody", "*string", ParameterOptions().NoEquals().NoQuotes().SQL("SET BODY ->"))
}
1 change: 1 addition & 0 deletions pkg/sdk/poc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var definitionMapping = map[string]*generator.Interface{
"event_tables_def.go": sdk.EventTablesDef,
"storage_integration_def.go": sdk.StorageIntegrationDef,
"managed_accounts_def.go": sdk.ManagedAccountsDef,
"row_access_policies_def.go": sdk.RowAccessPoliciesDef,
}

func main() {
Expand Down
111 changes: 111 additions & 0 deletions pkg/sdk/row_access_policies_def.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package sdk

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

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

var rowAccessPolicyDbRow = g.DbStruct("rowAccessPolicyDBRow").
Text("created_on").
Text("name").
Text("database_name").
Text("schema_name").
Text("kind").
Text("owner").
OptionalText("comment").
Text("options").
Text("owner_role_type")

var rowAccessPolicy = g.PlainStruct("RowAccessPolicy").
Text("CreatedOn").
Text("Name").
Text("DatabaseName").
Text("SchemaName").
Text("Kind").
Text("Owner").
Text("Comment").
Text("Options").
Text("OwnerRoleType")

var RowAccessPoliciesDef = g.NewInterface(
"RowAccessPolicies",
"RowAccessPolicy",
g.KindOfT[SchemaObjectIdentifier](),
).
CreateOperation(
"https://docs.snowflake.com/en/sql-reference/sql/create-row-access-policy",
g.NewQueryStruct("CreateRowAccessPolicy").
Create().
OrReplace().
SQL("ROW ACCESS POLICY").
IfNotExists().
Name().
SQL("AS").
ListQueryStructField(
"args",
g.NewQueryStruct("CreateRowAccessPolicyArgs").
Text("Name", g.KeywordOptions().NoQuotes().Required()).
PredefinedQueryStructField("Type", "DataType", g.KeywordOptions().NoQuotes().Required()),
g.ParameterOptions().Parentheses().NoEquals().Required(),
).
SQL("RETURNS BOOLEAN").
BodyWithPrecedingArrow().
OptionalComment().
WithValidation(g.ValidIdentifier, "name").
WithValidation(g.ValidateValueSet, "args").
WithValidation(g.ValidateValueSet, "body").
WithValidation(g.ConflictingFields, "OrReplace", "IfNotExists"),
).
AlterOperation(
"https://docs.snowflake.com/en/sql-reference/sql/alter-row-access-policy",
g.NewQueryStruct("AlterRowAccessPolicy").
Alter().
SQL("ROW ACCESS POLICY").
Name().
OptionalIdentifier("RenameTo", g.KindOfT[SchemaObjectIdentifier](), g.IdentifierOptions().SQL("RENAME TO")).
OptionalSetBodyWithPrecedingArrow().
OptionalSetTags().
OptionalUnsetTags().
OptionalTextAssignment("SET COMMENT", g.ParameterOptions().SingleQuotes()).
OptionalSQL("UNSET COMMENT").
WithValidation(g.ValidIdentifier, "name").
WithValidation(g.ExactlyOneValueSet, "RenameTo", "SetBody", "SetTags", "UnsetTags", "SetComment", "UnsetComment"),
).
DropOperation(
"https://docs.snowflake.com/en/sql-reference/sql/drop-row-access-policy",
g.NewQueryStruct("DropRowAccessPolicy").
Drop().
SQL("ROW ACCESS POLICY").
IfExists().
Name().
WithValidation(g.ValidIdentifier, "name"),
).
ShowOperation(
"https://docs.snowflake.com/en/sql-reference/sql/show-row-access-policies",
rowAccessPolicyDbRow,
rowAccessPolicy,
g.NewQueryStruct("ShowRowAccessPolicies").
Show().
SQL("ROW ACCESS POLICIES").
OptionalLike().
OptionalIn(),
).
ShowByIdOperation().
DescribeOperation(
g.DescriptionMappingKindSingleValue,
"https://docs.snowflake.com/en/sql-reference/sql/desc-row-access-policy",
g.DbStruct("describeRowAccessPolicyDBRow").
Field("name", "string").
Field("signature", "string").
Field("return_type", "string").
Field("body", "string"),
g.PlainStruct("RowAccessPolicyDescription").
Field("Name", "string").
Field("Signature", "string").
Field("ReturnType", "string").
Field("Body", "string"),
g.NewQueryStruct("DescribeRowAccessPolicy").
Describe().
SQL("ROW ACCESS POLICY").
Name().
WithValidation(g.ValidIdentifier, "name"),
)
119 changes: 119 additions & 0 deletions pkg/sdk/row_access_policies_dto_builders_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions pkg/sdk/row_access_policies_dto_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package sdk

//go:generate go run ./dto-builder-generator/main.go

var (
_ optionsProvider[CreateRowAccessPolicyOptions] = new(CreateRowAccessPolicyRequest)
_ optionsProvider[AlterRowAccessPolicyOptions] = new(AlterRowAccessPolicyRequest)
_ optionsProvider[DropRowAccessPolicyOptions] = new(DropRowAccessPolicyRequest)
_ optionsProvider[ShowRowAccessPolicyOptions] = new(ShowRowAccessPolicyRequest)
_ optionsProvider[DescribeRowAccessPolicyOptions] = new(DescribeRowAccessPolicyRequest)
)

type CreateRowAccessPolicyRequest struct {
OrReplace *bool
IfNotExists *bool
name SchemaObjectIdentifier // required
args []CreateRowAccessPolicyArgsRequest // required
body string // required
Comment *string
}

type CreateRowAccessPolicyArgsRequest struct {
Name string // required
Type DataType // required
}

type AlterRowAccessPolicyRequest struct {
name SchemaObjectIdentifier // required
RenameTo *SchemaObjectIdentifier
SetBody *string
SetTags []TagAssociation
UnsetTags []ObjectIdentifier
SetComment *string
UnsetComment *bool
}

type DropRowAccessPolicyRequest struct {
IfExists *bool
name SchemaObjectIdentifier // required
}

type ShowRowAccessPolicyRequest struct {
Like *Like
In *In
}

type DescribeRowAccessPolicyRequest struct {
name SchemaObjectIdentifier // required
}
Loading
Loading