Skip to content

Commit

Permalink
feat: Go Generator + Network Policy migration to the new SDK (#2061)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak authored Sep 28, 2023
1 parent 21f069a commit 231b081
Show file tree
Hide file tree
Showing 42 changed files with 2,450 additions and 525 deletions.
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,22 @@ clean-generator-poc:
rm -f ./pkg/sdk/poc/example/*_gen.go
rm -f ./pkg/sdk/poc/example/*_gen_test.go
.PHONY: run-generator-poc

# TODO Below run-generator-% script won't generate, because there's some complications with the syntax and order how it runs
#run-generator-%: ./pkg/sdk/%_def.go ## Run go generate on definition
# go generate ./pkg/sdk/$*_def.go && go generate ./pkg/sdk/$*_dto_gen.go
#
#clean-generator-%: ./pkg/sdk/%_*_gen.go ## Clean generated files for specified resource
# rm -f ./pkg/sdk/$*_*_gen.go

# TODO For now use those like this => make file=network_policies clean-generator run-generator
clean-generator: ## Remove generated files and tests for given ${file}
rm -f ./pkg/sdk/${file}*_gen.go
rm -f ./pkg/sdk/${file}*_gen_test.go
rm -f ./pkg/sdk/${file}*_gen_integration_test.go
.PHONY: clean-generator

run-generator: ## Generate files and tests for given ${file}
go generate ./pkg/sdk/${file}_def.go
go generate ./pkg/sdk/${file}_dto_gen.go
.PHONY: run-generator
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a
golang.org/x/crypto v0.7.0
golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0
golang.org/x/text v0.8.0
golang.org/x/tools v0.7.0
)

Expand Down Expand Up @@ -120,7 +121,6 @@ require (
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions pkg/sdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Client struct {
FileFormats FileFormats
Grants Grants
MaskingPolicies MaskingPolicies
NetworkPolicies NetworkPolicies
Parameters Parameters
PasswordPolicies PasswordPolicies
Pipes Pipes
Expand Down Expand Up @@ -134,6 +135,7 @@ func (c *Client) initialize() {
c.FileFormats = &fileFormats{client: c}
c.Grants = &grants{client: c}
c.MaskingPolicies = &maskingPolicies{client: c}
c.NetworkPolicies = &networkPolicies{client: c}
c.Parameters = &parameters{client: c}
c.PasswordPolicies = &passwordPolicies{client: c}
c.Pipes = &pipes{client: c}
Expand Down
10 changes: 10 additions & 0 deletions pkg/sdk/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,3 +722,13 @@ func createStageWithURL(t *testing.T, client *Client, name AccountObjectIdentifi
require.NoError(t, err)
}
}

func createNetworkPolicy(t *testing.T, client *Client, req *CreateNetworkPolicyRequest) (error, func()) {
t.Helper()
ctx := context.Background()
err := client.NetworkPolicies.Create(ctx, req)
return err, func() {
err := client.NetworkPolicies.Drop(ctx, NewDropNetworkPolicyRequest(req.name))
require.NoError(t, err)
}
}
92 changes: 92 additions & 0 deletions pkg/sdk/network_policies_def.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package sdk

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

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

var (
ip = g.QueryStruct("IP").
Text("IP", g.KeywordOptions().SingleQuotes().Required())

NetworkPoliciesDef = g.NewInterface(
"NetworkPolicies",
"NetworkPolicy",
g.KindOfT[AccountObjectIdentifier](),
).
CreateOperation(
"https://docs.snowflake.com/en/sql-reference/sql/create-network-policy",
g.QueryStruct("CreateNetworkPolicies").
Create().
OrReplace().
SQL("NETWORK POLICY").
Name().
ListQueryStructField("AllowedIpList", ip, g.ParameterOptions().SQL("ALLOWED_IP_LIST").Parentheses()).
ListQueryStructField("BlockedIpList", ip, g.ParameterOptions().SQL("BLOCKED_IP_LIST").Parentheses()).
OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()).
WithValidation(g.ValidIdentifier, "name"),
).
AlterOperation(
"https://docs.snowflake.com/en/sql-reference/sql/alter-network-policy",
g.QueryStruct("AlterNetworkPolicy").
Alter().
SQL("NETWORK POLICY").
IfExists().
Name().
OptionalQueryStructField(
"Set",
g.QueryStruct("NetworkPolicySet").
ListQueryStructField("AllowedIpList", ip, g.ParameterOptions().SQL("ALLOWED_IP_LIST").Parentheses()).
ListQueryStructField("BlockedIpList", ip, g.ParameterOptions().SQL("BLOCKED_IP_LIST").Parentheses()).
OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()).
WithValidation(g.AtLeastOneValueSet, "AllowedIpList", "BlockedIpList", "Comment"),
g.KeywordOptions().SQL("SET"),
).
OptionalSQL("UNSET COMMENT").
Identifier("RenameTo", g.KindOfTPointer[AccountObjectIdentifier](), g.IdentifierOptions().SQL("RENAME TO")).
WithValidation(g.ValidIdentifier, "name").
WithValidation(g.AtLeastOneValueSet, "Set", "UnsetComment", "RenameTo").
WithValidation(g.ValidIdentifierIfSet, "RenameTo"),
).
DropOperation(
"https://docs.snowflake.com/en/sql-reference/sql/drop-network-policy",
g.QueryStruct("DropNetworkPolicy").
Drop().
SQL("NETWORK POLICY").
IfExists().
Name().
WithValidation(g.ValidIdentifier, "name"),
).
ShowOperation(
"https://docs.snowflake.com/en/sql-reference/sql/show-network-policies",
g.DbStruct("showNetworkPolicyDBRow").
Field("created_on", "string").
Field("name", "string").
Field("comment", "string").
Field("entries_in_allowed_ip_list", "int").
Field("entries_in_blocked_ip_list", "int"),
g.PlainStruct("NetworkPolicy").
Field("CreatedOn", "string").
Field("Name", "string").
Field("Comment", "string").
Field("EntriesInAllowedIpList", "int").
Field("EntriesInBlockedIpList", "int"),
g.QueryStruct("ShowNetworkPolicies").
Show().
SQL("NETWORK POLICIES"),
).
DescribeOperation(
g.DescriptionMappingKindSlice,
"https://docs.snowflake.com/en/sql-reference/sql/desc-network-policy",
g.DbStruct("describeNetworkPolicyDBRow").
Field("name", "string").
Field("value", "string"),
g.PlainStruct("NetworkPolicyDescription").
Field("Name", "string").
Field("Value", "string"),
g.QueryStruct("DescribeNetworkPolicy").
Describe().
SQL("NETWORK POLICY").
Name().
WithValidation(g.ValidIdentifier, "name"),
)
)
113 changes: 113 additions & 0 deletions pkg/sdk/network_policies_dto_builders_gen.go

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

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

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

var (
_ optionsProvider[CreateNetworkPolicyOptions] = new(CreateNetworkPolicyRequest)
_ optionsProvider[AlterNetworkPolicyOptions] = new(AlterNetworkPolicyRequest)
_ optionsProvider[DropNetworkPolicyOptions] = new(DropNetworkPolicyRequest)
_ optionsProvider[ShowNetworkPolicyOptions] = new(ShowNetworkPolicyRequest)
_ optionsProvider[DescribeNetworkPolicyOptions] = new(DescribeNetworkPolicyRequest)
)

type CreateNetworkPolicyRequest struct {
OrReplace *bool
name AccountObjectIdentifier // required
AllowedIpList []IPRequest
BlockedIpList []IPRequest
Comment *string
}

type IPRequest struct {
IP string // required
}

type AlterNetworkPolicyRequest struct {
IfExists *bool
name AccountObjectIdentifier // required
Set *NetworkPolicySetRequest
UnsetComment *bool
RenameTo *AccountObjectIdentifier
}

type NetworkPolicySetRequest struct {
AllowedIpList []IPRequest
BlockedIpList []IPRequest
Comment *string
}

type DropNetworkPolicyRequest struct {
IfExists *bool
name AccountObjectIdentifier // required
}

type ShowNetworkPolicyRequest struct{}

type DescribeNetworkPolicyRequest struct {
name AccountObjectIdentifier // required
}
Loading

0 comments on commit 231b081

Please sign in to comment.