Skip to content

Commit

Permalink
feat: SDK Connection (#3155)
Browse files Browse the repository at this point in the history
<!-- Feel free to delete comments as you fill this in -->
## Info
- `ignore edition check` has been removed since I can no longer execute
an sql operation with it on snowflake
- `Syntax error: unexpected 'IGNORE'`
<!-- summary of changes -->
## Changes
- connection sdk
- connection snowflake object assertions

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

## TODO
* [uncomment integration tests for
failover](https://snowflakecomputing.atlassian.net/browse/SNOW-1763442)
* [Unskip integration
tests](https://snowflakecomputing.atlassian.net/browse/SNOW-1002023)
* Resource
* Datasource

## References
<!-- issues documentation links, etc  -->
 * https://docs.snowflake.com/en/sql-reference/sql/create-connection
  • Loading branch information
sfc-gh-fbudzynski authored Oct 30, 2024
1 parent 5b7412f commit bd11e0f
Show file tree
Hide file tree
Showing 16 changed files with 1,445 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package objectassert

import (
"fmt"
"slices"
"strings"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
)

func (c *ConnectionAssert) HasFailoverAllowedToAccounts(expected []string) *ConnectionAssert {
c.AddAssertion(func(t *testing.T, o *sdk.Connection) error {
t.Helper()
if !slices.Equal(expected, o.FailoverAllowedToAccounts) {
return fmt.Errorf("expected failover_allowed_to_accounts to be: %v; got: %v", expected, o.FailoverAllowedToAccounts)
}
return nil
})
return c
}

func (c *ConnectionAssert) HasNoComment() *ConnectionAssert {
c.AddAssertion(func(t *testing.T, o *sdk.Connection) error {
t.Helper()
if o.Comment != nil {
return fmt.Errorf("expected comment to have nil; got: %s", *o.Comment)
}
return nil
})
return c
}

func (c *ConnectionAssert) HasConnectionUrlNotEmpty() *ConnectionAssert {
c.AddAssertion(func(t *testing.T, o *sdk.Connection) error {
t.Helper()
if o.ConnectionUrl == "" {
return fmt.Errorf("expected connection url not empty, got: %s", o.ConnectionUrl)
}
return nil
})

return c
}

func (c *ConnectionAssert) HasPrimaryIdentifier(expected sdk.ExternalObjectIdentifier) *ConnectionAssert {
c.AddAssertion(func(t *testing.T, o *sdk.Connection) error {
t.Helper()
expectedString := strings.ReplaceAll(expected.FullyQualifiedName(), `"`, "")
if o.Primary != expectedString {
return fmt.Errorf("expected primary identifier: %v; got: %v", expectedString, o.Primary)
}
return nil
})
return c
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ var allStructs = []SdkObjectDef{
ObjectType: sdk.ObjectTypeDatabase,
ObjectStruct: sdk.Database{},
},
{
IdType: "sdk.AccountObjectIdentifier",
ObjectType: sdk.ObjectTypeConnection,
ObjectStruct: sdk.Connection{},
},
{
IdType: "sdk.DatabaseObjectIdentifier",
ObjectType: sdk.ObjectTypeDatabaseRole,
Expand Down
72 changes: 72 additions & 0 deletions pkg/acceptance/helpers/connection_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package helpers

import (
"context"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/stretchr/testify/require"
)

type ConnectionClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewConnectionClient(context *TestClientContext, idsGenerator *IdsGenerator) *ConnectionClient {
return &ConnectionClient{
context: context,
ids: idsGenerator,
}
}

func (c *ConnectionClient) client() sdk.Connections {
return c.context.client.Connections
}

func (c *ConnectionClient) Create(t *testing.T, id sdk.AccountObjectIdentifier) (*sdk.Connection, func()) {
t.Helper()
ctx := context.Background()
request := sdk.NewCreateConnectionRequest(id)
err := c.client().Create(ctx, request)
require.NoError(t, err)
connection, err := c.client().ShowByID(ctx, id)
require.NoError(t, err)
return connection, c.DropFunc(t, id)
}

func (c *ConnectionClient) CreateReplication(t *testing.T, id sdk.AccountObjectIdentifier, replicaOf sdk.ExternalObjectIdentifier) (*sdk.Connection, func()) {
t.Helper()
ctx := context.Background()
request := sdk.NewCreateConnectionRequest(id).WithAsReplicaOf(replicaOf)
err := c.client().Create(ctx, request)
require.NoError(t, err)
connection, err := c.client().ShowByID(ctx, id)
require.NoError(t, err)
return connection, c.DropFunc(t, id)
}

func (c *ConnectionClient) Alter(t *testing.T, id sdk.AccountObjectIdentifier, req *sdk.AlterConnectionRequest) {
t.Helper()
ctx := context.Background()

err := c.client().Alter(ctx, req)
require.NoError(t, err)
}

func (c *ConnectionClient) DropFunc(t *testing.T, id sdk.AccountObjectIdentifier) func() {
t.Helper()
ctx := context.Background()

return func() {
err := c.client().Drop(ctx, sdk.NewDropConnectionRequest(id).WithIfExists(true))
require.NoError(t, err)
}
}

func (c *ConnectionClient) Show(t *testing.T, id sdk.AccountObjectIdentifier) (*sdk.Connection, error) {
t.Helper()
ctx := context.Background()

return c.client().ShowByID(ctx, id)
}
2 changes: 2 additions & 0 deletions pkg/acceptance/helpers/test_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type TestClient struct {
ApplicationPackage *ApplicationPackageClient
AuthenticationPolicy *AuthenticationPolicyClient
BcrBundles *BcrBundlesClient
Connection *ConnectionClient
Context *ContextClient
CortexSearchService *CortexSearchServiceClient
CatalogIntegration *CatalogIntegrationClient
Expand Down Expand Up @@ -84,6 +85,7 @@ func NewTestClient(c *sdk.Client, database string, schema string, warehouse stri
ApplicationPackage: NewApplicationPackageClient(context, idsGenerator),
AuthenticationPolicy: NewAuthenticationPolicyClient(context, idsGenerator),
BcrBundles: NewBcrBundlesClient(context),
Connection: NewConnectionClient(context, idsGenerator),
Context: NewContextClient(context),
CortexSearchService: NewCortexSearchServiceClient(context, idsGenerator),
CatalogIntegration: NewCatalogIntegrationClient(context, idsGenerator),
Expand Down
2 changes: 2 additions & 0 deletions pkg/sdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Client struct {
Applications Applications
AuthenticationPolicies AuthenticationPolicies
Comments Comments
Connections Connections
CortexSearchServices CortexSearchServices
DatabaseRoles DatabaseRoles
Databases Databases
Expand Down Expand Up @@ -205,6 +206,7 @@ func (c *Client) initialize() {
c.Applications = &applications{client: c}
c.AuthenticationPolicies = &authenticationPolicies{client: c}
c.Comments = &comments{client: c}
c.Connections = &connections{client: c}
c.ContextFunctions = &contextFunctions{client: c}
c.ConversionFunctions = &conversionFunctions{client: c}
c.CortexSearchServices = &cortexSearchServices{client: c}
Expand Down
Loading

0 comments on commit bd11e0f

Please sign in to comment.