Skip to content

Commit

Permalink
feat: Connection datasource (#3173)
Browse files Browse the repository at this point in the history
<!-- Feel free to delete comments as you fill this in -->

<!-- summary of changes -->
## Changes
* Connections Datasource
* Generated Documentation for datasource

## Test Plan
<!-- detail ways in which this PR has been tested or needs to be tested
-->
* [x] acceptance tests

## References
<!-- issues documentation links, etc  -->
https://docs.snowflake.com/en/sql-reference/sql/create-connection

## TODO
* SNOW-1788041
  • Loading branch information
sfc-gh-fbudzynski authored Nov 8, 2024
1 parent 34bbbc1 commit 4127b3f
Show file tree
Hide file tree
Showing 8 changed files with 432 additions and 0 deletions.
8 changes: 8 additions & 0 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ across different versions.
## v0.97.0 ➞ v0.98.0

### *(new feature)* snowflake_connections datasource
Added a new datasource enabling querying and filtering connections. Notes:
- all results are stored in `connections` field.
- `like` field enables connections filtering.
- SHOW CONNECTIONS output is enclosed in `show_output` field inside `connections`.
It's important to limit the records and calls to Snowflake to the minimum. That's why we recommend assessing which information you need from the data source and then providing strong filters and turning off additional fields for better plan performance.


### *(new feature)* connection resources

Added a new resources for managing connections. We decided to split connection into two separate resources based on whether the connection is primary or a replica (secondary). i.e.:
Expand Down
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
)

func (c *ConnectionShowOutputAssert) HasCreatedOnNotEmpty() *ConnectionShowOutputAssert {
c.AddAssertion(assert.ResourceShowOutputValuePresent("created_on"))
return c
}

func (c *ConnectionShowOutputAssert) HasPrimaryIdentifier(expected sdk.ExternalObjectIdentifier) *ConnectionShowOutputAssert {
c.AddAssertion(assert.ResourceShowOutputValueSet("primary", expected.FullyQualifiedName()))
return c
Expand Down
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
}
Loading

0 comments on commit 4127b3f

Please sign in to comment.