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: Connection datasource #3173

Merged
merged 59 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 56 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
c321654
initial commit for connections sdk
sfc-gh-fbudzynski Oct 18, 2024
6b989e3
added show and drop
sfc-gh-fbudzynski Oct 21, 2024
fb8d018
connection assert generated
sfc-gh-fbudzynski Oct 21, 2024
6db06d8
assertions generated
sfc-gh-fbudzynski Oct 22, 2024
24a2c80
integration test
sfc-gh-fbudzynski Oct 23, 2024
5c8f84f
before self-review
sfc-gh-fbudzynski Oct 24, 2024
a5d59b6
integration tests
sfc-gh-fbudzynski Oct 24, 2024
f831c02
simplified disable connection failover struct
sfc-gh-fbudzynski Oct 24, 2024
fa3db2b
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Oct 24, 2024
e140406
init resource for connections
sfc-gh-fbudzynski Oct 25, 2024
fc4cd95
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Oct 25, 2024
1dee95b
adjusted to comments from review
sfc-gh-fbudzynski Oct 25, 2024
f0915c4
Merge branch 'connection-sdk' into connection-resource
sfc-gh-fbudzynski Oct 25, 2024
5291ab3
create for connection resource
sfc-gh-fbudzynski Oct 25, 2024
a6668d9
merge conflict with main resolved
sfc-gh-fbudzynski Oct 28, 2024
f8c64f7
linter adjustemnts
sfc-gh-fbudzynski Oct 28, 2024
68fce3d
pre-push adjustment
sfc-gh-fbudzynski Oct 28, 2024
661a8a4
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Oct 28, 2024
993da0d
resource CRUD functions
sfc-gh-fbudzynski Oct 28, 2024
7197faa
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Oct 28, 2024
fd52498
added drop with if exists and HasPrimaryIdentifier extended function
sfc-gh-fbudzynski Oct 28, 2024
b739e99
pre-push adjustments
sfc-gh-fbudzynski Oct 28, 2024
d7b13de
added skips for tests
sfc-gh-fbudzynski Oct 28, 2024
147f85a
acceptance tests for connection
sfc-gh-fbudzynski Oct 28, 2024
5394364
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Oct 28, 2024
fb3d563
with exists to connection resource delete
sfc-gh-fbudzynski Oct 29, 2024
f9c4f6a
resolved comments
sfc-gh-fbudzynski Oct 29, 2024
e665b38
Merge branch 'connection-sdk' into connection-resource
sfc-gh-fbudzynski Oct 29, 2024
44c1c05
connection resource with docs and migration guide
sfc-gh-fbudzynski Oct 29, 2024
215d6af
pre-push adjustments
sfc-gh-fbudzynski Oct 29, 2024
d0fb45c
self review small changes
sfc-gh-fbudzynski Oct 29, 2024
ba98db7
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Oct 30, 2024
9becdb9
changed naming for Set and Unset for connection
sfc-gh-fbudzynski Oct 30, 2024
5cd8364
initial commit for connections datasource
sfc-gh-fbudzynski Oct 30, 2024
ffe98a1
before discussing whether to split resource
sfc-gh-fbudzynski Oct 30, 2024
958a596
rename unset and set to unsetConnection and setConnection
sfc-gh-fbudzynski Oct 30, 2024
284e920
rename unset and set in tests
sfc-gh-fbudzynski Oct 30, 2024
dc58de7
extracted getConnectionUrl to connectionClient
sfc-gh-fbudzynski Oct 31, 2024
8ced116
seperate connection resource to primary and secondary
sfc-gh-fbudzynski Oct 31, 2024
dc08d74
linter adjustemnts
sfc-gh-fbudzynski Oct 31, 2024
ca44fcb
secondary connection custom diff
sfc-gh-fbudzynski Oct 31, 2024
47edb32
pre-push adjustments
sfc-gh-fbudzynski Oct 31, 2024
9ed9076
merge request resolved
sfc-gh-fbudzynski Oct 31, 2024
31729c6
merge with connection-resource and add secondary_connection to filtering
sfc-gh-fbudzynski Oct 31, 2024
4efce94
changed naming of primary connection, and adjusted to comments in pr
sfc-gh-fbudzynski Nov 4, 2024
f86d26e
added docs
sfc-gh-fbudzynski Nov 4, 2024
49ee968
datasource small changes
sfc-gh-fbudzynski Nov 4, 2024
dcc24e0
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Nov 5, 2024
e779da4
custom diff for secondary_connection
sfc-gh-fbudzynski Nov 5, 2024
633c78e
Merge branch 'connection-resource' of github.com:Snowflake-Labs/terra…
sfc-gh-fbudzynski Nov 5, 2024
1263a91
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Nov 5, 2024
a8000bf
Merge branch 'connection-resource' of github.com:Snowflake-Labs/terra…
sfc-gh-fbudzynski Nov 5, 2024
893ba1e
gosec g601 adjustment to connections datasource
sfc-gh-fbudzynski Nov 5, 2024
64a7ac1
spelling
sfc-gh-fbudzynski Nov 5, 2024
0335f54
removed unwanted files and use predefined likeSchema
sfc-gh-fbudzynski Nov 7, 2024
fa87e25
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Nov 7, 2024
32f54eb
add HasCreatedOnNotEmpty
sfc-gh-fbudzynski Nov 7, 2024
35929d6
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Nov 7, 2024
aeee056
add info about connections-datasource to migration guide
sfc-gh-fbudzynski Nov 7, 2024
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
79 changes: 79 additions & 0 deletions docs/data-sources/connections.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
page_title: "snowflake_connections Data Source - terraform-provider-snowflake"
subcategory: ""
description: |-
Datasource used to get details of filtered connections. Filtering is aligned with the current possibilities for SHOW CONNECTIONS https://docs.snowflake.com/en/sql-reference/sql/show-connections query. The results of SHOW is encapsulated in one output collection connections.
---

!> **V1 release candidate** This data source 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 data source if needed. Any errors reported will be resolved with a higher priority. We encourage checking this data source out before the V1 release. Please follow the [migration guide](https://github.com/Snowflake-Labs/terraform-provider-snowflake/blob/main/MIGRATION_GUIDE.md#v0970--v0980) to use it.

# snowflake_connections (Data Source)

Datasource used to get details of filtered connections. Filtering is aligned with the current possibilities for [SHOW CONNECTIONS](https://docs.snowflake.com/en/sql-reference/sql/show-connections) query. The results of SHOW is encapsulated in one output collection `connections`.

## Example Usage

```terraform
# Simple usage
data "snowflake_connections" "simple" {
}

output "simple_output" {
value = data.snowflake_connections.simple.connections
}

# Filtering (like)
data "snowflake_connections" "like" {
like = "connection-name"
}

output "like_output" {
value = data.snowflake_connections.like.connections
}

# Filtering by prefix (like)
data "snowflake_connections" "like_prefix" {
like = "prefix%"
}

output "like_prefix_output" {
value = data.snowflake_connections.like_prefix.connections
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `like` (String) Filters the output with **case-insensitive** pattern, with support for SQL wildcard characters (`%` and `_`).

### Read-Only

- `connections` (List of Object) Holds the aggregated output of all connections details queries. (see [below for nested schema](#nestedatt--connections))
- `id` (String) The ID of this resource.

<a id="nestedatt--connections"></a>
### Nested Schema for `connections`

Read-Only:

- `show_output` (List of Object) (see [below for nested schema](#nestedobjatt--connections--show_output))

<a id="nestedobjatt--connections--show_output"></a>
### Nested Schema for `connections.show_output`

Read-Only:

- `account_locator` (String)
- `account_name` (String)
- `comment` (String)
- `connection_url` (String)
- `created_on` (String)
- `failover_allowed_to_accounts` (List of String)
- `is_primary` (Boolean)
- `name` (String)
- `organization_name` (String)
- `primary` (String)
- `region_group` (String)
- `snowflake_region` (String)
25 changes: 25 additions & 0 deletions examples/data-sources/snowflake_connections/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Simple usage
data "snowflake_connections" "simple" {
}

output "simple_output" {
value = data.snowflake_connections.simple.connections
}

# Filtering (like)
data "snowflake_connections" "like" {
like = "connection-name"
}

output "like_output" {
value = data.snowflake_connections.like.connections
}

# Filtering by prefix (like)
data "snowflake_connections" "like_prefix" {
like = "prefix%"
}

output "like_prefix_output" {
value = data.snowflake_connections.like_prefix.connections
}
68 changes: 68 additions & 0 deletions pkg/datasources/connections.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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 connectionsSchema = map[string]*schema.Schema{
"like": likeSchema,
"connections": {
Type: schema.TypeList,
Computed: true,
Description: "Holds the aggregated output of all connections details queries.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
resources.ShowOutputAttributeName: {
Type: schema.TypeList,
Computed: true,
Description: "Holds the output of SHOW CONNECTIONS.",
Elem: &schema.Resource{
Schema: schemas.ShowConnectionSchema,
},
},
},
},
},
}

func Connections() *schema.Resource {
return &schema.Resource{
ReadContext: ReadConnections,
Schema: connectionsSchema,
Description: "Datasource used to get details of filtered connections. Filtering is aligned with the current possibilities for [SHOW CONNECTIONS](https://docs.snowflake.com/en/sql-reference/sql/show-connections) query. The results of SHOW is encapsulated in one output collection `connections`.",
}
}

func ReadConnections(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*provider.Context).Client
req := sdk.ShowConnectionRequest{}

handleLike(d, &req.Like)

connections, err := client.Connections.Show(ctx, &req)
if err != nil {
return diag.FromErr(err)
}

d.SetId("connections_read")

flattenedConnections := make([]map[string]any, len(connections))
for i, connection := range connections {
connection := connection
flattenedConnections[i] = map[string]any{
resources.ShowOutputAttributeName: []map[string]any{schemas.ConnectionToSchema(&connection)},
}
}
if err := d.Set("connections", flattenedConnections); err != nil {
return diag.FromErr(err)
}

return nil
}
220 changes: 220 additions & 0 deletions pkg/datasources/connections_acceptance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
package datasources_test

import (
"fmt"
"strings"
"testing"

acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert/resourceshowoutputassert"
accConfig "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/config"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/config/model"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

func connectionsData() string {
return `
data "snowflake_connections" "test" {
depends_on = [snowflake_primary_connection.test]
}`
}

func TestAcc_Connections_Minimal(t *testing.T) {
// TODO: [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed
_ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups)

accountId := acc.TestClient().Account.GetAccountIdentifier(t)
id := acc.TestClient().Ids.RandomAccountObjectIdentifier()
connectionModel := model.PrimaryConnection("test", id.Name())

primaryConnectionAsExternalId := sdk.NewExternalObjectIdentifier(accountId, id)

dataConnections := accConfig.FromModel(t, connectionModel) + connectionsData()

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
CheckDestroy: acc.CheckDestroy(t, resources.PrimaryConnection),
Steps: []resource.TestStep{
{
Config: dataConnections,
Check: assert.AssertThat(t,
assert.Check(resource.TestCheckResourceAttr("data.snowflake_connections.test", "connections.#", "1")),
resourceshowoutputassert.ConnectionShowOutput(t, "snowflake_primary_connection.test").
HasName(id.Name()).
HasSnowflakeRegion(acc.TestClient().Context.CurrentRegion(t)).
HasAccountLocator(acc.TestClient().GetAccountLocator()).
HasAccountName(accountId.AccountName()).
HasOrganizationName(accountId.OrganizationName()).
HasComment("").
HasIsPrimary(true).
HasPrimaryIdentifier(primaryConnectionAsExternalId).
HasFailoverAllowedToAccounts(accountId).
HasConnectionUrl(
acc.TestClient().Connection.GetConnectionUrl(accountId.OrganizationName(), id.Name()),
),
),
},
},
})
}

func TestAcc_Connections_Complete(t *testing.T) {
// TODO: [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed
_ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups)

accountId := acc.TestClient().Account.GetAccountIdentifier(t)
secondaryAccountId := acc.SecondaryTestClient().Account.GetAccountIdentifier(t)

id := acc.TestClient().Ids.RandomAccountObjectIdentifier()
connectionModel := model.PrimaryConnection("test", id.Name()).
WithEnableFailover(secondaryAccountId).
WithComment("test comment")

primaryConnectionAsExternalId := sdk.NewExternalObjectIdentifier(accountId, id)

dataConnections := accConfig.FromModel(t, connectionModel) + connectionsData()

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
CheckDestroy: acc.CheckDestroy(t, resources.PrimaryConnection),
Steps: []resource.TestStep{
{
Config: dataConnections,
Check: assert.AssertThat(t,
assert.Check(resource.TestCheckResourceAttr("data.snowflake_connections.test", "connections.#", "1")),
resourceshowoutputassert.ConnectionShowOutput(t, "snowflake_connection.test").
HasName(id.Name()).
HasSnowflakeRegion(acc.TestClient().Context.CurrentRegion(t)).
HasAccountLocator(acc.TestClient().GetAccountLocator()).
sfc-gh-jmichalak marked this conversation as resolved.
Show resolved Hide resolved
HasAccountName(accountId.AccountName()).
HasOrganizationName(accountId.OrganizationName()).
HasComment("test comment").
HasIsPrimary(true).
HasPrimaryIdentifier(primaryConnectionAsExternalId).
HasFailoverAllowedToAccounts(accountId, secondaryAccountId).
HasConnectionUrl(
acc.TestClient().Connection.GetConnectionUrl(accountId.OrganizationName(), id.Name()),
),
),
},
},
})
}

func TestAcc_Connections_Filtering(t *testing.T) {
// TODO: [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed
_ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups)

// TODO: [SNOW-1788041] - need to uppercase as connection name in snowflake is returned in uppercase
prefix := random.AlphaN(4)
prefix = strings.ToUpper(prefix)

idOne := acc.TestClient().Ids.RandomAccountObjectIdentifierWithPrefix(prefix)
idTwo := acc.TestClient().Ids.RandomAccountObjectIdentifierWithPrefix(prefix)
idThree := acc.TestClient().Ids.RandomAccountObjectIdentifier()

connectionModelOne := model.PrimaryConnection("c1", idOne.Name())
connectionModelTwo := model.PrimaryConnection("c2", idTwo.Name())
connectionModelThree := model.PrimaryConnection("c3", idThree.Name())

configWithLike := accConfig.FromModel(t, connectionModelOne) +
accConfig.FromModel(t, connectionModelTwo) +
accConfig.FromModel(t, connectionModelThree)

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
CheckDestroy: acc.CheckDestroy(t, resources.PrimaryConnection),
Steps: []resource.TestStep{
// with like
{
Config: configWithLike + connectionDatasourceWithLike(prefix+"%"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.snowflake_connections.test", "connections.#", "2"),
),
},
},
})
}

func TestAcc_Connections_FilteringWithReplica(t *testing.T) {
// TODO: [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed
_ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups)

// TODO: [SNOW-1788041] - need to uppercase as connection name in snowflake is returned in uppercase
prefix := random.AlphaN(4)
prefix = strings.ToUpper(prefix)

idOne := acc.TestClient().Ids.RandomAccountObjectIdentifierWithPrefix(prefix)
idTwo := acc.SecondaryTestClient().Ids.RandomAccountObjectIdentifierWithPrefix(prefix)

accountId := acc.TestClient().Account.GetAccountIdentifier(t)

_, cleanup := acc.SecondaryTestClient().Connection.Create(t, idTwo)
t.Cleanup(cleanup)

primaryConnectionAsExternalId := sdk.NewExternalObjectIdentifier(accountId, idTwo)
acc.SecondaryTestClient().Connection.Alter(t, sdk.NewAlterConnectionRequest(idTwo).
WithEnableConnectionFailover(*sdk.NewEnableConnectionFailoverRequest([]sdk.AccountIdentifier{accountId})))

connectionModelOne := model.PrimaryConnection("c1", idOne.Name())
connectionModelTwo := model.SecondaryConnection("c2", primaryConnectionAsExternalId.FullyQualifiedName(), idTwo.Name())

configWithLike := accConfig.FromModel(t, connectionModelOne) +
accConfig.FromModel(t, connectionModelTwo)

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
CheckDestroy: acc.ComposeCheckDestroy(t, resources.PrimaryConnection, resources.SecondaryConnection),
Steps: []resource.TestStep{
// with like
{
Config: configWithLike + connectionAndSecondaryConnectionDatasourceWithLike(prefix+"%"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.snowflake_connections.test", "connections.#", "2"),
),
},
},
})
}

func connectionDatasourceWithLike(like string) string {
return fmt.Sprintf(`
data "snowflake_connections" "test" {
depends_on = [snowflake_primary_connection.c1, snowflake_primary_connection.c2, snowflake_primary_connection.c3]

like = "%s"
}
`, like)
}

func connectionAndSecondaryConnectionDatasourceWithLike(like string) string {
return fmt.Sprintf(`
data "snowflake_connections" "test" {
depends_on = [snowflake_primary_connection.c1, snowflake_secondary_connection.c2]

like = "%s"
}
`, like)
}
Loading
Loading