Skip to content

Commit

Permalink
fix: network policy update (#2647)
Browse files Browse the repository at this point in the history
fixes #2593 

## Test Plan
<!-- detail ways in which this PR has been tested or needs to be tested
-->
* [ ] acceptance tests
<!-- add more below if you think they are relevant -->
* [ ] …

## References
<!-- issues documentation links, etc  -->

*
  • Loading branch information
sfc-gh-swinkler authored Apr 16, 2024
1 parent a23d312 commit 8126b28
Show file tree
Hide file tree
Showing 8 changed files with 406 additions and 68 deletions.
36 changes: 32 additions & 4 deletions pkg/sdk/network_policies_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ var (
ip = g.NewQueryStruct("IP").
Text("IP", g.KeywordOptions().SingleQuotes().Required())

networkPoliciesAddNetworkRule = g.NewQueryStruct("AddNetworkRule").
ListAssignment("ALLOWED_NETWORK_RULE_LIST", "SchemaObjectIdentifier", g.ParameterOptions().Parentheses()).
ListAssignment("BLOCKED_NETWORK_RULE_LIST", "SchemaObjectIdentifier", g.ParameterOptions().Parentheses()).
WithValidation(g.ExactlyOneValueSet, "AllowedNetworkRuleList", "BlockedNetworkRuleList")

networkPoliciesRemoveNetworkRule = g.NewQueryStruct("RemoveNetworkRule").
ListAssignment("ALLOWED_NETWORK_RULE_LIST", "SchemaObjectIdentifier", g.ParameterOptions().Parentheses()).
ListAssignment("BLOCKED_NETWORK_RULE_LIST", "SchemaObjectIdentifier", g.ParameterOptions().Parentheses()).
WithValidation(g.ExactlyOneValueSet, "AllowedNetworkRuleList", "BlockedNetworkRuleList")

NetworkPoliciesDef = g.NewInterface(
"NetworkPolicies",
"NetworkPolicy",
Expand All @@ -20,6 +30,8 @@ var (
OrReplace().
SQL("NETWORK POLICY").
Name().
ListAssignment("ALLOWED_NETWORK_RULE_LIST", "SchemaObjectIdentifier", g.ParameterOptions().Parentheses()).
ListAssignment("BLOCKED_NETWORK_RULE_LIST", "SchemaObjectIdentifier", g.ParameterOptions().Parentheses()).
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()).
Expand All @@ -35,16 +47,28 @@ var (
OptionalQueryStructField(
"Set",
g.NewQueryStruct("NetworkPolicySet").
ListAssignment("ALLOWED_NETWORK_RULE_LIST", "SchemaObjectIdentifier", g.ParameterOptions().Parentheses()).
ListAssignment("BLOCKED_NETWORK_RULE_LIST", "SchemaObjectIdentifier", g.ParameterOptions().Parentheses()).
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"),
WithValidation(g.AtLeastOneValueSet, "AllowedIpList", "BlockedIpList", "Comment", "AllowedNetworkRuleList", "BlockedNetworkRuleList"),
g.KeywordOptions().SQL("SET"),
).
OptionalQueryStructField(
"Add",
networkPoliciesAddNetworkRule,
g.KeywordOptions().SQL("ADD"),
).
OptionalQueryStructField(
"Remove",
networkPoliciesRemoveNetworkRule,
g.KeywordOptions().SQL("REMOVE"),
).
OptionalSQL("UNSET COMMENT").
Identifier("RenameTo", g.KindOfTPointer[AccountObjectIdentifier](), g.IdentifierOptions().SQL("RENAME TO")).
WithValidation(g.ValidIdentifier, "name").
WithValidation(g.ExactlyOneValueSet, "Set", "UnsetComment", "RenameTo").
WithValidation(g.ExactlyOneValueSet, "Set", "UnsetComment", "RenameTo", "Add", "Remove").
WithValidation(g.ValidIdentifierIfSet, "RenameTo"),
).
DropOperation(
Expand All @@ -63,13 +87,17 @@ var (
Field("name", "string").
Field("comment", "string").
Field("entries_in_allowed_ip_list", "int").
Field("entries_in_blocked_ip_list", "int"),
Field("entries_in_blocked_ip_list", "int").
Field("entries_in_allowed_network_rules", "int").
Field("entries_in_blocked_network_rules", "int"),
g.PlainStruct("NetworkPolicy").
Field("CreatedOn", "string").
Field("Name", "string").
Field("Comment", "string").
Field("EntriesInAllowedIpList", "int").
Field("EntriesInBlockedIpList", "int"),
Field("EntriesInBlockedIpList", "int").
Field("EntriesInAllowedNetworkRules", "int").
Field("EntriesInBlockedNetworkRules", "int"),
g.NewQueryStruct("ShowNetworkPolicies").
Show().
SQL("NETWORK POLICIES"),
Expand Down
60 changes: 60 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.

32 changes: 24 additions & 8 deletions pkg/sdk/network_policies_dto_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ var (
)

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

func (r *CreateNetworkPolicyRequest) GetName() AccountObjectIdentifier {
Expand All @@ -30,14 +32,28 @@ type AlterNetworkPolicyRequest struct {
IfExists *bool
name AccountObjectIdentifier // required
Set *NetworkPolicySetRequest
Add *AddNetworkRuleRequest
Remove *RemoveNetworkRuleRequest
UnsetComment *bool
RenameTo *AccountObjectIdentifier
}

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

type AddNetworkRuleRequest struct {
AllowedNetworkRuleList []SchemaObjectIdentifier
BlockedNetworkRuleList []SchemaObjectIdentifier
}

type RemoveNetworkRuleRequest struct {
AllowedNetworkRuleList []SchemaObjectIdentifier
BlockedNetworkRuleList []SchemaObjectIdentifier
}

type DropNetworkPolicyRequest struct {
Expand Down
60 changes: 40 additions & 20 deletions pkg/sdk/network_policies_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ type NetworkPolicies interface {

// CreateNetworkPolicyOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-network-policy.
type CreateNetworkPolicyOptions struct {
create bool `ddl:"static" sql:"CREATE"`
OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"`
networkPolicy bool `ddl:"static" sql:"NETWORK POLICY"`
name AccountObjectIdentifier `ddl:"identifier"`
AllowedIpList []IP `ddl:"parameter,parentheses" sql:"ALLOWED_IP_LIST"`
BlockedIpList []IP `ddl:"parameter,parentheses" sql:"BLOCKED_IP_LIST"`
Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"`
create bool `ddl:"static" sql:"CREATE"`
OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"`
networkPolicy bool `ddl:"static" sql:"NETWORK POLICY"`
name AccountObjectIdentifier `ddl:"identifier"`
AllowedNetworkRuleList []SchemaObjectIdentifier `ddl:"parameter,parentheses" sql:"ALLOWED_NETWORK_RULE_LIST"`
BlockedNetworkRuleList []SchemaObjectIdentifier `ddl:"parameter,parentheses" sql:"BLOCKED_NETWORK_RULE_LIST"`
AllowedIpList []IP `ddl:"parameter,parentheses" sql:"ALLOWED_IP_LIST"`
BlockedIpList []IP `ddl:"parameter,parentheses" sql:"BLOCKED_IP_LIST"`
Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"`
}

type IP struct {
Expand All @@ -33,14 +35,28 @@ type AlterNetworkPolicyOptions struct {
IfExists *bool `ddl:"keyword" sql:"IF EXISTS"`
name AccountObjectIdentifier `ddl:"identifier"`
Set *NetworkPolicySet `ddl:"keyword" sql:"SET"`
Add *AddNetworkRule `ddl:"keyword" sql:"ADD"`
Remove *RemoveNetworkRule `ddl:"keyword" sql:"REMOVE"`
UnsetComment *bool `ddl:"keyword" sql:"UNSET COMMENT"`
RenameTo *AccountObjectIdentifier `ddl:"identifier" sql:"RENAME TO"`
}

type NetworkPolicySet struct {
AllowedIpList []IP `ddl:"parameter,parentheses" sql:"ALLOWED_IP_LIST"`
BlockedIpList []IP `ddl:"parameter,parentheses" sql:"BLOCKED_IP_LIST"`
Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"`
AllowedNetworkRuleList []SchemaObjectIdentifier `ddl:"parameter,parentheses" sql:"ALLOWED_NETWORK_RULE_LIST"`
BlockedNetworkRuleList []SchemaObjectIdentifier `ddl:"parameter,parentheses" sql:"BLOCKED_NETWORK_RULE_LIST"`
AllowedIpList []IP `ddl:"parameter,parentheses" sql:"ALLOWED_IP_LIST"`
BlockedIpList []IP `ddl:"parameter,parentheses" sql:"BLOCKED_IP_LIST"`
Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"`
}

type AddNetworkRule struct {
AllowedNetworkRuleList []SchemaObjectIdentifier `ddl:"parameter,parentheses" sql:"ALLOWED_NETWORK_RULE_LIST"`
BlockedNetworkRuleList []SchemaObjectIdentifier `ddl:"parameter,parentheses" sql:"BLOCKED_NETWORK_RULE_LIST"`
}

type RemoveNetworkRule struct {
AllowedNetworkRuleList []SchemaObjectIdentifier `ddl:"parameter,parentheses" sql:"ALLOWED_NETWORK_RULE_LIST"`
BlockedNetworkRuleList []SchemaObjectIdentifier `ddl:"parameter,parentheses" sql:"BLOCKED_NETWORK_RULE_LIST"`
}

// DropNetworkPolicyOptions is based on https://docs.snowflake.com/en/sql-reference/sql/drop-network-policy.
Expand All @@ -58,19 +74,23 @@ type ShowNetworkPolicyOptions struct {
}

type showNetworkPolicyDBRow struct {
CreatedOn string `db:"created_on"`
Name string `db:"name"`
Comment string `db:"comment"`
EntriesInAllowedIpList int `db:"entries_in_allowed_ip_list"`
EntriesInBlockedIpList int `db:"entries_in_blocked_ip_list"`
CreatedOn string `db:"created_on"`
Name string `db:"name"`
Comment string `db:"comment"`
EntriesInAllowedIpList int `db:"entries_in_allowed_ip_list"`
EntriesInBlockedIpList int `db:"entries_in_blocked_ip_list"`
EntriesInAllowedNetworkRules int `db:"entries_in_allowed_network_rules"`
EntriesInBlockedNetworkRules int `db:"entries_in_blocked_network_rules"`
}

type NetworkPolicy struct {
CreatedOn string
Name string
Comment string
EntriesInAllowedIpList int
EntriesInBlockedIpList int
CreatedOn string
Name string
Comment string
EntriesInAllowedIpList int
EntriesInBlockedIpList int
EntriesInAllowedNetworkRules int
EntriesInBlockedNetworkRules int
}

// DescribeNetworkPolicyOptions is based on https://docs.snowflake.com/en/sql-reference/sql/desc-network-policy.
Expand Down
Loading

0 comments on commit 8126b28

Please sign in to comment.