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 7 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
Expand Up @@ -3,6 +3,7 @@ package objectassert
import (
"fmt"
"slices"
"strings"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
Expand All @@ -29,3 +30,27 @@ func (c *ConnectionAssert) HasNoComment() *ConnectionAssert {
})
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 string) *ConnectionAssert {
c.AddAssertion(func(t *testing.T, o *sdk.Connection) error {
t.Helper()
expected = strings.ReplaceAll(expected, `"`, "")
if o.Primary != expected {
return fmt.Errorf("expected primary identifier: %v; got: %v", expected, o.Primary)
}
return nil
})
return c
}
12 changes: 2 additions & 10 deletions pkg/acceptance/helpers/connection_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func (c *ConnectionClient) Create(t *testing.T, id sdk.AccountObjectIdentifier)
func (c *ConnectionClient) CreateReplication(t *testing.T, id sdk.AccountObjectIdentifier, replicaOf sdk.ExternalObjectIdentifier) (*sdk.Connection, func()) {
t.Helper()
ctx := context.Background()
request := sdk.NewCreateReplicatedConnectionRequest(id, replicaOf)
err := c.client().CreateReplicated(ctx, request)
request := sdk.NewCreateConnectionRequest(id).WithAsReplicaOf(sdk.AsReplicaOfRequest{AsReplicaOf: replicaOf})
err := c.client().Create(ctx, request)
require.NoError(t, err)
connection, err := c.client().ShowByID(ctx, id)
require.NoError(t, err)
Expand All @@ -54,14 +54,6 @@ func (c *ConnectionClient) Alter(t *testing.T, id sdk.AccountObjectIdentifier, r
require.NoError(t, err)
}

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

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

func (c *ConnectionClient) DropFunc(t *testing.T, id sdk.AccountObjectIdentifier) func() {
t.Helper()
ctx := context.Background()
Expand Down
76 changes: 32 additions & 44 deletions pkg/sdk/connections_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,26 @@ var ConnectionDef = g.NewInterface(
SQL("CONNECTION").
IfNotExists().
Name().
OptionalQueryStructField(
"AsReplicaOf",
g.NewQueryStruct("AsReplicaOf").
Identifier("AsReplicaOf", g.KindOfT[ExternalObjectIdentifier](), g.IdentifierOptions().Required().SQL("AS REPLICA OF")).
WithValidation(g.ValidIdentifier, "AsReplicaOf"),
g.IdentifierOptions(),
).
OptionalComment().
WithValidation(g.ValidIdentifier, "name"),
).CustomOperation(
"CreateReplicated",
"https://docs.snowflake.com/en/sql-reference/sql/create-connection",
g.NewQueryStruct("CreateReplicated").
Create().
SQL("CONNECTION").
IfNotExists().
Name().
Identifier("ReplicaOf", g.KindOfT[ExternalObjectIdentifier](), g.IdentifierOptions().Required().SQL("AS REPLICA OF")).
OptionalComment().
WithValidation(g.ValidIdentifier, "name").
WithValidation(g.ValidIdentifier, "ReplicaOf"),
).CustomOperation(
"AlterFailover",
).AlterOperation(
"https://docs.snowflake.com/en/sql-reference/sql/alter-connection",
g.NewQueryStruct("AlterFailover").
g.NewQueryStruct("Alter").
Alter().
SQL("CONNECTION").
IfExists().
Name().
sfc-gh-jmichalak marked this conversation as resolved.
Show resolved Hide resolved
OptionalQueryStructField(
"EnableConnectionFailover",
g.NewQueryStruct("EnableConnectionFailover").
List("ToAccounts", "AccountIdentifier", g.ListOptions().NoParentheses()).
OptionalSQL("IGNORE EDITION CHECK"),
List("ToAccounts", "AccountIdentifier", g.ListOptions().NoParentheses()),
g.KeywordOptions().SQL("ENABLE FAILOVER TO ACCOUNTS"),
).
OptionalQueryStructField(
Expand All @@ -57,14 +51,6 @@ var ConnectionDef = g.NewInterface(
g.KeywordOptions().SQL("DISABLE FAILOVER"),
).
OptionalSQL("PRIMARY").
WithValidation(g.ExactlyOneValueSet, "EnableConnectionFailover", "DisableConnectionFailover", "Primary"),
).AlterOperation(
"https://docs.snowflake.com/en/sql-reference/sql/alter-connection",
g.NewQueryStruct("Alter").
Alter().
SQL("CONNECTION").
IfExists().
Name().
OptionalQueryStructField(
"Set",
g.NewQueryStruct("Set").
Expand All @@ -79,7 +65,7 @@ var ConnectionDef = g.NewInterface(
WithValidation(g.AtLeastOneValueSet, "Comment"),
g.KeywordOptions().SQL("UNSET"),
).
WithValidation(g.ExactlyOneValueSet, "Set", "Unset"),
WithValidation(g.ExactlyOneValueSet, "EnableConnectionFailover", "DisableConnectionFailover", "Primary", "Set", "Unset"),
).DropOperation(
"https://docs.snowflake.com/en/sql-reference/sql/drop-connection",
g.NewQueryStruct("DropConnection").
Expand All @@ -91,29 +77,31 @@ var ConnectionDef = g.NewInterface(
).ShowOperation(
"https://docs.snowflake.com/en/sql-reference/sql/show-connections",
g.DbStruct("connectionRow").
Field("snowflake_region", "string").
OptionalText("region_group").
Text("snowflake_region").
Field("created_on", "time.Time").
sfc-gh-jmichalak marked this conversation as resolved.
Show resolved Hide resolved
Field("account_name", "string").
Field("name", "string").
Text("account_name").
Text("name").
Field("comment", "sql.NullString").
Field("is_primary", "string").
Field("primary", "string").
Field("failover_allowed_to_accounts", "string").
Field("connection_url", "string").
Field("organization_name", "string").
Field("account_locator", "string"),
Text("is_primary").
Text("primary").
Text("failover_allowed_to_accounts").
Text("connection_url").
Text("organization_name").
Text("account_locator"),
g.PlainStruct("Connection").
Field("SnowflakeRegion", "string").
OptionalText("RegionGroup").
Text("SnowflakeRegion").
Field("CreatedOn", "time.Time").
Field("AccountName", "string").
Field("Name", "string").
Field("Comment", "*string").
Field("IsPrimary", "bool").
Field("Primary", "string").
Text("AccountName").
Text("Name").
OptionalText("Comment").
Bool("IsPrimary").
Text("Primary").
Field("FailoverAllowedToAccounts", "[]string").
Field("ConnectionUrl", "string").
Field("OrganizationName", "string").
Field("AccountLocator", "string"),
Text("ConnectionUrl").
Text("OrganizationName").
Text("AccountLocator"),
g.NewQueryStruct("ShowConnections").
Show().
SQL("CONNECTIONS").
Expand Down
80 changes: 29 additions & 51 deletions pkg/sdk/connections_dto_builders_gen.go

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

33 changes: 12 additions & 21 deletions pkg/sdk/connections_dto_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,35 @@ package sdk
//go:generate go run ./dto-builder-generator/main.go

var (
_ optionsProvider[CreateConnectionOptions] = new(CreateConnectionRequest)
_ optionsProvider[CreateReplicatedConnectionOptions] = new(CreateReplicatedConnectionRequest)
_ optionsProvider[AlterFailoverConnectionOptions] = new(AlterFailoverConnectionRequest)
_ optionsProvider[AlterConnectionOptions] = new(AlterConnectionRequest)
_ optionsProvider[DropConnectionOptions] = new(DropConnectionRequest)
_ optionsProvider[ShowConnectionOptions] = new(ShowConnectionRequest)
_ optionsProvider[CreateConnectionOptions] = new(CreateConnectionRequest)
_ optionsProvider[AlterConnectionOptions] = new(AlterConnectionRequest)
_ optionsProvider[DropConnectionOptions] = new(DropConnectionRequest)
_ optionsProvider[ShowConnectionOptions] = new(ShowConnectionRequest)
)

type CreateConnectionRequest struct {
IfNotExists *bool
name AccountObjectIdentifier // required
AsReplicaOf *AsReplicaOfRequest
Comment *string
}

type CreateReplicatedConnectionRequest struct {
IfNotExists *bool
name AccountObjectIdentifier // required
ReplicaOf ExternalObjectIdentifier // required
Comment *string
type AsReplicaOfRequest struct {
AsReplicaOf ExternalObjectIdentifier // required
}

type AlterFailoverConnectionRequest struct {
type AlterConnectionRequest struct {
IfExists *bool
name AccountObjectIdentifier // required
EnableConnectionFailover *EnableConnectionFailoverRequest
DisableConnectionFailover *DisableConnectionFailoverRequest
Primary *bool
Set *SetRequest
Unset *UnsetRequest
}

type EnableConnectionFailoverRequest struct {
ToAccounts []AccountIdentifier
IgnoreEditionCheck *bool
ToAccounts []AccountIdentifier
}

type DisableConnectionFailoverRequest struct {
Expand All @@ -44,13 +42,6 @@ type ToAccountsRequest struct {
Accounts []AccountIdentifier
}

type AlterConnectionRequest struct {
IfExists *bool
name AccountObjectIdentifier // required
Set *SetRequest
Unset *UnsetRequest
}

type SetRequest struct {
Comment *string
}
Expand Down
Loading
Loading