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: SDK Connection #3155

Merged
merged 17 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
Loading