-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0c136d
commit 69f7492
Showing
26 changed files
with
1,259 additions
and
365 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
page_title: "snowflake_network_policies Data Source - terraform-provider-snowflake" | ||
subcategory: "" | ||
description: |- | ||
Datasource used to get details of filtered network policies. Filtering is aligned with the current possibilities for SHOW NETWORK POLICIES https://docs.snowflake.com/en/sql-reference/sql/show-network-policies query (like is supported). The results of SHOW and DESCRIBE are encapsulated in one output collection. | ||
--- | ||
|
||
!> **V1 release candidate** This resource was reworked and is a release candidate for the V1. We do not expect significant changes in it before the V1. We will welcome any feedback and adjust the resource if needed. Any errors reported will be resolved with a higher priority. We encourage checking this resource out before the V1 release. Please follow the [migration guide](https://github.com/Snowflake-Labs/terraform-provider-snowflake/blob/main/MIGRATION_GUIDE.md#v0920--v0930) to use it. | ||
|
||
# snowflake_network_policies (Data Source) | ||
|
||
Datasource used to get details of filtered network policies. Filtering is aligned with the current possibilities for [SHOW NETWORK POLICIES](https://docs.snowflake.com/en/sql-reference/sql/show-network-policies) query (`like` is supported). The results of SHOW and DESCRIBE are encapsulated in one output collection. | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Optional | ||
|
||
- `like` (String) Filters the output with **case-insensitive** pattern, with support for SQL wildcard characters (`%` and `_`). | ||
- `with_describe` (Boolean) Runs DESC NETWORK POLICY for each network policy returned by SHOW NETWORK POLICIES. The output of describe is saved to the description field. By default this value is set to true. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
- `network_policies` (List of Object) Holds the aggregated output of all network policies details queries. (see [below for nested schema](#nestedatt--network_policies)) | ||
|
||
<a id="nestedatt--network_policies"></a> | ||
### Nested Schema for `network_policies` | ||
|
||
Read-Only: | ||
|
||
- `describe_output` (List of Object) (see [below for nested schema](#nestedobjatt--network_policies--describe_output)) | ||
- `show_output` (List of Object) (see [below for nested schema](#nestedobjatt--network_policies--show_output)) | ||
|
||
<a id="nestedobjatt--network_policies--describe_output"></a> | ||
### Nested Schema for `network_policies.describe_output` | ||
|
||
Read-Only: | ||
|
||
- `allowed_ip_list` (String) | ||
- `allowed_network_rule_list` (String) | ||
- `blocked_ip_list` (String) | ||
- `blocked_network_rule_list` (String) | ||
|
||
|
||
<a id="nestedobjatt--network_policies--show_output"></a> | ||
### Nested Schema for `network_policies.show_output` | ||
|
||
Read-Only: | ||
|
||
- `comment` (String) | ||
- `created_on` (String) | ||
- `entries_in_allowed_ip_list` (Number) | ||
- `entries_in_allowed_network_rules` (Number) | ||
- `entries_in_blocked_ip_list` (Number) | ||
- `entries_in_blocked_network_rules` (Number) | ||
- `name` (String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package helpers | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type NetworkRuleClient struct { | ||
context *TestClientContext | ||
ids *IdsGenerator | ||
} | ||
|
||
func NewNetworkRuleClient(context *TestClientContext, idsGenerator *IdsGenerator) *NetworkRuleClient { | ||
return &NetworkRuleClient{ | ||
context: context, | ||
ids: idsGenerator, | ||
} | ||
} | ||
|
||
func (c *NetworkRuleClient) client() sdk.NetworkRules { | ||
return c.context.client.NetworkRules | ||
} | ||
|
||
func (c *NetworkRuleClient) Create(t *testing.T) *sdk.NetworkRule { | ||
t.Helper() | ||
return c.CreateWithName(t, c.ids.Alpha()) | ||
} | ||
|
||
func (c *NetworkRuleClient) CreateWithName(t *testing.T, name string) *sdk.NetworkRule { | ||
t.Helper() | ||
return c.CreateWithIdentifier(t, c.ids.NewSchemaObjectIdentifier(name)) | ||
} | ||
|
||
func (c *NetworkRuleClient) CreateWithIdentifier(t *testing.T, id sdk.SchemaObjectIdentifier) *sdk.NetworkRule { | ||
t.Helper() | ||
ctx := context.Background() | ||
|
||
err := c.client().Create(ctx, sdk.NewCreateNetworkRuleRequest(id, sdk.NetworkRuleTypeIpv4, []sdk.NetworkRuleValue{}, sdk.NetworkRuleModeIngress)) | ||
require.NoError(t, err) | ||
|
||
t.Cleanup(func() { | ||
_ = c.client().Drop(ctx, sdk.NewDropNetworkRuleRequest(id).WithIfExists(sdk.Bool(true))) | ||
}) | ||
|
||
networkRule, err := c.client().ShowByID(ctx, id) | ||
require.NoError(t, err) | ||
require.NotNil(t, networkRule) | ||
|
||
return networkRule | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package datasources | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" | ||
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" | ||
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/schemas" | ||
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
var networkPoliciesSchema = map[string]*schema.Schema{ | ||
"with_describe": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: true, | ||
Description: "Runs DESC NETWORK POLICY for each network policy returned by SHOW NETWORK POLICIES. The output of describe is saved to the description field. By default this value is set to true.", | ||
}, | ||
"like": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "Filters the output with **case-insensitive** pattern, with support for SQL wildcard characters (`%` and `_`).", | ||
}, | ||
"network_policies": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: "Holds the aggregated output of all network policies details queries.", | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
resources.ShowOutputAttributeName: { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: "Holds the output of SHOW NETWORK POLICIES.", | ||
Elem: &schema.Resource{ | ||
Schema: schemas.ShowNetworkPolicySchema, | ||
}, | ||
}, | ||
resources.DescribeOutputAttributeName: { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: "Holds the output of DESCRIBE NETWORK POLICIES.", | ||
Elem: &schema.Resource{ | ||
Schema: schemas.DescribeNetworkPolicySchema, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
func NetworkPolicies() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: ReadNetworkPolicies, | ||
Schema: networkPoliciesSchema, | ||
Description: "Datasource used to get details of filtered network policies. Filtering is aligned with the current possibilities for [SHOW NETWORK POLICIES](https://docs.snowflake.com/en/sql-reference/sql/show-network-policies) query (`like` is supported). The results of SHOW and DESCRIBE are encapsulated in one output collection.", | ||
} | ||
} | ||
|
||
func ReadNetworkPolicies(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { | ||
client := meta.(*provider.Context).Client | ||
req := sdk.NewShowNetworkPolicyRequest() | ||
|
||
if likePattern, ok := d.GetOk("like"); ok { | ||
req.WithLike(sdk.Like{ | ||
Pattern: sdk.String(likePattern.(string)), | ||
}) | ||
} | ||
|
||
networkPolicies, err := client.NetworkPolicies.Show(ctx, req) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId("network_policies_read") | ||
|
||
flattenedNetworkPolicies := make([]map[string]any, len(networkPolicies)) | ||
for i, networkPolicy := range networkPolicies { | ||
networkPolicy := networkPolicy | ||
|
||
var networkPolicyDescribeOutput []map[string]any | ||
if d.Get("with_describe").(bool) { | ||
describeResult, err := client.NetworkPolicies.Describe(ctx, sdk.NewAccountObjectIdentifier(networkPolicy.Name)) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
networkPolicyDescribeOutput = []map[string]any{schemas.NetworkPolicyPropertiesToSchema(describeResult)} | ||
} | ||
|
||
flattenedNetworkPolicies[i] = map[string]any{ | ||
resources.ShowOutputAttributeName: []map[string]any{schemas.NetworkPolicyToSchema(&networkPolicy)}, | ||
resources.DescribeOutputAttributeName: networkPolicyDescribeOutput, | ||
} | ||
} | ||
|
||
if err = d.Set("network_policies", flattenedNetworkPolicies); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.