From c32165467103eb83ef10d34ba219eaaa74878ecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Fri, 18 Oct 2024 17:28:58 +0200 Subject: [PATCH 01/13] initial commit for connections sdk --- pkg/sdk/connections_def.go | 92 ++++++++++ pkg/sdk/connections_dto_builders_gen.go | 139 +++++++++++++++ pkg/sdk/connections_dto_gen.go | 58 +++++++ pkg/sdk/connections_gen.go | 67 ++++++++ pkg/sdk/connections_gen_integration_test.go | 23 +++ pkg/sdk/connections_gen_test.go | 180 ++++++++++++++++++++ pkg/sdk/connections_impl_gen.go | 97 +++++++++++ pkg/sdk/connections_validations_gen.go | 65 +++++++ pkg/sdk/poc/main.go | 1 + pkg/sdk/random_test.go | 1 + 10 files changed, 723 insertions(+) create mode 100644 pkg/sdk/connections_def.go create mode 100644 pkg/sdk/connections_dto_builders_gen.go create mode 100644 pkg/sdk/connections_dto_gen.go create mode 100644 pkg/sdk/connections_gen.go create mode 100644 pkg/sdk/connections_gen_integration_test.go create mode 100644 pkg/sdk/connections_gen_test.go create mode 100644 pkg/sdk/connections_impl_gen.go create mode 100644 pkg/sdk/connections_validations_gen.go diff --git a/pkg/sdk/connections_def.go b/pkg/sdk/connections_def.go new file mode 100644 index 0000000000..4b464e4c8f --- /dev/null +++ b/pkg/sdk/connections_def.go @@ -0,0 +1,92 @@ +package sdk + +//go:generate go run ./poc/main.go +import ( + g "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator" +) + +var enabledFailoverAccounts = g.NewQueryStruct("EnabledFailoverAccounts"). + Text("Account", g.KeywordOptions().NoQuotes()) + +var ConnectionDef = g.NewInterface( + "Conntections", + "Connection", + g.KindOfT[AccountObjectIdentifier](), +).CustomOperation( + "CreateConnection", + "https://docs.snowflake.com/en/sql-reference/sql/create-connection", + g.NewQueryStruct("CreateConnection"). + Create(). + SQL("CONNECTION"). + IfNotExists(). + Name(). + OptionalComment(). + WithValidation(g.ValidIdentifier, "name"), +).CustomOperation( + "CreateReplicatedConnection", + "https://docs.snowflake.com/en/sql-reference/sql/create-connection", + g.NewQueryStruct("CreateReplicatedConnection"). + Create(). + SQL("CONNECTION"). + IfNotExists(). + Name(). + SQL("AS REPLICA OF"). + // external reference to connection: .. + Identifier("ReplicaOf", g.KindOfT[ExternalObjectIdentifier](), g.IdentifierOptions().Required()). + OptionalComment(). + WithValidation(g.ValidIdentifier, "name"). + WithValidation(g.ValidIdentifier, "ReplicaOf"), +).CustomOperation( + "AlterConnectionFailover", + "https://docs.snowflake.com/en/sql-reference/sql/alter-connection", + g.NewQueryStruct("AlterConnectionFailover"). + Alter(). + SQL("CONNECTION"). + Name(). + OptionalQueryStructField( + "EnableConnectionFailover", + g.NewQueryStruct("EnableConnectionFailover"). + // ListQueryStructField("Accounts", enabledFailoverAccounts, g.ListOptions().NoParentheses()). + List("Accounts", "ExternalObjectIdentifier", g.ListOptions().NoParentheses()). + OptionalSQL("IGNORE EDITION CHECK"), + g.KeywordOptions().SQL("ENABLE FAILOVER TO ACCOUNTS"), + ). + OptionalQueryStructField( + "DisableConnectionFailover", + g.NewQueryStruct("DisableConnectionFailover"). + OptionalSQL("TO ACCOUNTS"). + // ListQueryStructField("Accounts", enabledFailoverAccounts, g.ListOptions().NoParentheses()), + List("Accounts", "ExternalObjectIdentifier", g.ListOptions().NoParentheses()), + g.KeywordOptions().SQL("DISABLE FAILOVER"), + ). + OptionalQueryStructField( + "Primary", + g.NewQueryStruct("Primary"). + SQL("PRIMARY"), + g.KeywordOptions(), + ). + WithValidation(g.ExactlyOneValueSet, "EnableConnectionFailover", "DisableConnectionFailover", "Primary"), +).CustomOperation( + "AlterConnection", + "https://docs.snowflake.com/en/sql-reference/sql/alter-connection", + g.NewQueryStruct("AlterConnection"). + Alter(). + SQL("CONNECTION"). + IfExists(). + Name(). + OptionalQueryStructField( + "Set", + g.NewQueryStruct("Set"). + OptionalComment(). + WithValidation(g.AtLeastOneValueSet, "Comment"), + g.KeywordOptions().SQL("SET"), + ). + OptionalQueryStructField( + "Unset", + g.NewQueryStruct("Unset"). + OptionalSQL("COMMENT"). + WithValidation(g.AtLeastOneValueSet, "Comment"), + g.KeywordOptions().SQL("UNSET"), + ). + WithValidation(g.ExactlyOneValueSet, "Set", "Unset"), +) diff --git a/pkg/sdk/connections_dto_builders_gen.go b/pkg/sdk/connections_dto_builders_gen.go new file mode 100644 index 0000000000..0ad6960b4e --- /dev/null +++ b/pkg/sdk/connections_dto_builders_gen.go @@ -0,0 +1,139 @@ +// Code generated by dto builder generator; DO NOT EDIT. + +package sdk + +import () + +func NewCreateConnectionConnectionRequest( + name AccountObjectIdentifier, +) *CreateConnectionConnectionRequest { + s := CreateConnectionConnectionRequest{} + s.name = name + return &s +} + +func (s *CreateConnectionConnectionRequest) WithIfNotExists(IfNotExists bool) *CreateConnectionConnectionRequest { + s.IfNotExists = &IfNotExists + return s +} + +func (s *CreateConnectionConnectionRequest) WithComment(Comment string) *CreateConnectionConnectionRequest { + s.Comment = &Comment + return s +} + +func NewCreateReplicatedConnectionConnectionRequest( + name AccountObjectIdentifier, + ReplicaOf ExternalObjectIdentifier, +) *CreateReplicatedConnectionConnectionRequest { + s := CreateReplicatedConnectionConnectionRequest{} + s.name = name + s.ReplicaOf = ReplicaOf + return &s +} + +func (s *CreateReplicatedConnectionConnectionRequest) WithIfNotExists(IfNotExists bool) *CreateReplicatedConnectionConnectionRequest { + s.IfNotExists = &IfNotExists + return s +} + +func (s *CreateReplicatedConnectionConnectionRequest) WithComment(Comment string) *CreateReplicatedConnectionConnectionRequest { + s.Comment = &Comment + return s +} + +func NewAlterConnectionFailoverConnectionRequest( + name AccountObjectIdentifier, +) *AlterConnectionFailoverConnectionRequest { + s := AlterConnectionFailoverConnectionRequest{} + s.name = name + return &s +} + +func (s *AlterConnectionFailoverConnectionRequest) WithEnableConnectionFailover(EnableConnectionFailover EnableConnectionFailoverRequest) *AlterConnectionFailoverConnectionRequest { + s.EnableConnectionFailover = &EnableConnectionFailover + return s +} + +func (s *AlterConnectionFailoverConnectionRequest) WithDisableConnectionFailover(DisableConnectionFailover DisableConnectionFailoverRequest) *AlterConnectionFailoverConnectionRequest { + s.DisableConnectionFailover = &DisableConnectionFailover + return s +} + +func (s *AlterConnectionFailoverConnectionRequest) WithPrimary(Primary PrimaryRequest) *AlterConnectionFailoverConnectionRequest { + s.Primary = &Primary + return s +} + +func NewEnableConnectionFailoverRequest() *EnableConnectionFailoverRequest { + return &EnableConnectionFailoverRequest{} +} + +func (s *EnableConnectionFailoverRequest) WithAccounts(Accounts []ExternalObjectIdentifier) *EnableConnectionFailoverRequest { + s.Accounts = Accounts + return s +} + +func (s *EnableConnectionFailoverRequest) WithIgnoreEditionCheck(IgnoreEditionCheck bool) *EnableConnectionFailoverRequest { + s.IgnoreEditionCheck = &IgnoreEditionCheck + return s +} + +func NewDisableConnectionFailoverRequest() *DisableConnectionFailoverRequest { + return &DisableConnectionFailoverRequest{} +} + +func (s *DisableConnectionFailoverRequest) WithToAccounts(ToAccounts bool) *DisableConnectionFailoverRequest { + s.ToAccounts = &ToAccounts + return s +} + +func (s *DisableConnectionFailoverRequest) WithAccounts(Accounts []ExternalObjectIdentifier) *DisableConnectionFailoverRequest { + s.Accounts = Accounts + return s +} + +func NewPrimaryRequest() *PrimaryRequest { + return &PrimaryRequest{} +} + +func NewAlterConnectionConnectionRequest( + name AccountObjectIdentifier, +) *AlterConnectionConnectionRequest { + s := AlterConnectionConnectionRequest{} + s.name = name + return &s +} + +func (s *AlterConnectionConnectionRequest) WithIfExists(IfExists bool) *AlterConnectionConnectionRequest { + s.IfExists = &IfExists + return s +} + +func (s *AlterConnectionConnectionRequest) WithSet(Set SetRequest) *AlterConnectionConnectionRequest { + s.Set = &Set + return s +} + +func (s *AlterConnectionConnectionRequest) WithUnset(Unset UnsetRequest) *AlterConnectionConnectionRequest { + s.Unset = &Unset + return s +} + +func NewSetRequest() *SetRequest { + return &SetRequest{} +} + +func (s *SetRequest) WithComment(Comment string) *SetRequest { + s.Comment = &Comment + return s +} + +func NewUnsetRequest() *UnsetRequest { + return &UnsetRequest{} +} + +func (s *UnsetRequest) WithComment(Comment bool) *UnsetRequest { + s.Comment = &Comment + return s +} diff --git a/pkg/sdk/connections_dto_gen.go b/pkg/sdk/connections_dto_gen.go new file mode 100644 index 0000000000..9d0935ba8d --- /dev/null +++ b/pkg/sdk/connections_dto_gen.go @@ -0,0 +1,58 @@ +package sdk + +//go:generate go run ./dto-builder-generator/main.go + +var ( + _ optionsProvider[CreateConnectionConnectionOptions] = new(CreateConnectionConnectionRequest) + _ optionsProvider[CreateReplicatedConnectionConnectionOptions] = new(CreateReplicatedConnectionConnectionRequest) + _ optionsProvider[AlterConnectionFailoverConnectionOptions] = new(AlterConnectionFailoverConnectionRequest) + _ optionsProvider[AlterConnectionConnectionOptions] = new(AlterConnectionConnectionRequest) +) + +type CreateConnectionConnectionRequest struct { + IfNotExists *bool + name AccountObjectIdentifier // required + Comment *string +} + +type CreateReplicatedConnectionConnectionRequest struct { + IfNotExists *bool + name AccountObjectIdentifier // required + ReplicaOf ExternalObjectIdentifier // required + Comment *string +} + +type AlterConnectionFailoverConnectionRequest struct { + name AccountObjectIdentifier // required + EnableConnectionFailover *EnableConnectionFailoverRequest + DisableConnectionFailover *DisableConnectionFailoverRequest + Primary *PrimaryRequest +} + +type EnableConnectionFailoverRequest struct { + Accounts []ExternalObjectIdentifier + IgnoreEditionCheck *bool +} + +type DisableConnectionFailoverRequest struct { + ToAccounts *bool + Accounts []ExternalObjectIdentifier +} + +type PrimaryRequest struct { +} + +type AlterConnectionConnectionRequest struct { + IfExists *bool + name AccountObjectIdentifier // required + Set *SetRequest + Unset *UnsetRequest +} + +type SetRequest struct { + Comment *string +} + +type UnsetRequest struct { + Comment *bool +} diff --git a/pkg/sdk/connections_gen.go b/pkg/sdk/connections_gen.go new file mode 100644 index 0000000000..0e0a7344db --- /dev/null +++ b/pkg/sdk/connections_gen.go @@ -0,0 +1,67 @@ +package sdk + +import "context" + +type Conntections interface { + CreateConnection(ctx context.Context, request *CreateConnectionConnectionRequest) error + CreateReplicatedConnection(ctx context.Context, request *CreateReplicatedConnectionConnectionRequest) error + AlterConnectionFailover(ctx context.Context, request *AlterConnectionFailoverConnectionRequest) error + AlterConnection(ctx context.Context, request *AlterConnectionConnectionRequest) error +} + +// CreateConnectionConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-connection. +type CreateConnectionConnectionOptions struct { + create bool `ddl:"static" sql:"CREATE"` + connection bool `ddl:"static" sql:"CONNECTION"` + IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` + name AccountObjectIdentifier `ddl:"identifier"` + Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` +} + +// CreateReplicatedConnectionConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-connection. +type CreateReplicatedConnectionConnectionOptions struct { + create bool `ddl:"static" sql:"CREATE"` + connection bool `ddl:"static" sql:"CONNECTION"` + IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` + name AccountObjectIdentifier `ddl:"identifier"` + asReplicaOf bool `ddl:"static" sql:"AS REPLICA OF"` + ReplicaOf ExternalObjectIdentifier `ddl:"identifier"` + Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` +} + +// AlterConnectionFailoverConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/alter-connection. +type AlterConnectionFailoverConnectionOptions struct { + alter bool `ddl:"static" sql:"ALTER"` + connection bool `ddl:"static" sql:"CONNECTION"` + name AccountObjectIdentifier `ddl:"identifier"` + EnableConnectionFailover *EnableConnectionFailover `ddl:"keyword" sql:"ENABLE FAILOVER TO ACCOUNTS"` + DisableConnectionFailover *DisableConnectionFailover `ddl:"keyword" sql:"DISABLE FAILOVER"` + Primary *Primary `ddl:"keyword"` +} +type EnableConnectionFailover struct { + Accounts []ExternalObjectIdentifier `ddl:"list,no_parentheses"` + IgnoreEditionCheck *bool `ddl:"keyword" sql:"IGNORE EDITION CHECK"` +} +type DisableConnectionFailover struct { + ToAccounts *bool `ddl:"keyword" sql:"TO ACCOUNTS"` + Accounts []ExternalObjectIdentifier `ddl:"list,no_parentheses"` +} +type Primary struct { + primary bool `ddl:"static" sql:"PRIMARY"` +} + +// AlterConnectionConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/alter-connection. +type AlterConnectionConnectionOptions struct { + alter bool `ddl:"static" sql:"ALTER"` + connection bool `ddl:"static" sql:"CONNECTION"` + IfExists *bool `ddl:"keyword" sql:"IF EXISTS"` + name AccountObjectIdentifier `ddl:"identifier"` + Set *Set `ddl:"keyword" sql:"SET"` + Unset *Unset `ddl:"keyword" sql:"UNSET"` +} +type Set struct { + Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` +} +type Unset struct { + Comment *bool `ddl:"keyword" sql:"COMMENT"` +} diff --git a/pkg/sdk/connections_gen_integration_test.go b/pkg/sdk/connections_gen_integration_test.go new file mode 100644 index 0000000000..40007b140b --- /dev/null +++ b/pkg/sdk/connections_gen_integration_test.go @@ -0,0 +1,23 @@ +package sdk + +import "testing" + +func TestInt_Conntections(t *testing.T) { + // TODO: prepare common resources + + t.Run("CreateConnection", func(t *testing.T) { + // TODO: fill me + }) + + t.Run("CreateReplicatedConnection", func(t *testing.T) { + // TODO: fill me + }) + + t.Run("AlterConnectionFailover", func(t *testing.T) { + // TODO: fill me + }) + + t.Run("AlterConnection", func(t *testing.T) { + // TODO: fill me + }) +} diff --git a/pkg/sdk/connections_gen_test.go b/pkg/sdk/connections_gen_test.go new file mode 100644 index 0000000000..e899ca2d64 --- /dev/null +++ b/pkg/sdk/connections_gen_test.go @@ -0,0 +1,180 @@ +package sdk + +import "testing" + +func TestConntections_CreateConnection(t *testing.T) { + id := randomAccountObjectIdentifier() + defaultOpts := func() *CreateConnectionConnectionOptions { + return &CreateConnectionConnectionOptions{ + name: id, + } + } + + t.Run("validation: nil options", func(t *testing.T) { + var opts *CreateConnectionConnectionOptions = nil + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) + }) + t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { + opts := defaultOpts() + opts.name = emptyAccountObjectIdentifier + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) + }) + + t.Run("basic", func(t *testing.T) { + opts := defaultOpts() + opts.name = id + assertOptsValidAndSQLEquals(t, opts, "CREATE CONNECTION %s", id.FullyQualifiedName()) + }) + + t.Run("all options", func(t *testing.T) { + opts := defaultOpts() + opts.name = id + opts.IfNotExists = Bool(true) + opts.Comment = String("comment") + assertOptsValidAndSQLEquals(t, opts, "CREATE CONNECTION IF NOT EXISTS %s COMMENT = 'comment'", id.FullyQualifiedName()) + }) +} + +func TestConntections_CreateReplicatedConnection(t *testing.T) { + id := randomAccountObjectIdentifier() + externalId := randomExternalObjectIdentifier() + defaultOpts := func() *CreateReplicatedConnectionConnectionOptions { + return &CreateReplicatedConnectionConnectionOptions{ + name: id, + } + } + + t.Run("validation: nil options", func(t *testing.T) { + var opts *CreateReplicatedConnectionConnectionOptions = nil + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) + }) + t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { + opts := defaultOpts() + opts.name = emptyAccountObjectIdentifier + opts.ReplicaOf = externalId + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) + }) + + t.Run("validation: valid identifier for [opts.ReplicaOf]", func(t *testing.T) { + opts := defaultOpts() + opts.name = id + opts.ReplicaOf = emptyExtenalObjectIdentifier + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) + }) + + t.Run("basic", func(t *testing.T) { + opts := defaultOpts() + opts.name = id + opts.ReplicaOf = externalId + assertOptsValidAndSQLEquals(t, opts, "CREATE CONNECTION %s AS REPLICA OF %s", id.FullyQualifiedName(), externalId.FullyQualifiedName()) + }) + + t.Run("all options", func(t *testing.T) { + opts := defaultOpts() + opts.name = id + opts.IfNotExists = Bool(true) + opts.ReplicaOf = externalId + opts.Comment = String("comment") + assertOptsValidAndSQLEquals(t, opts, "CREATE CONNECTION IF NOT EXISTS %s AS REPLICA OF %s COMMENT = 'comment'", id.FullyQualifiedName(), externalId.FullyQualifiedName()) + }) +} + +func TestConntections_AlterConnectionFailover(t *testing.T) { + id := randomAccountObjectIdentifier() + externalId := randomExternalObjectIdentifier() + externalIdTwo := randomExternalObjectIdentifier() + defaultOpts := func() *AlterConnectionFailoverConnectionOptions { + return &AlterConnectionFailoverConnectionOptions{ + name: id, + } + } + + t.Run("validation: nil options", func(t *testing.T) { + var opts *AlterConnectionFailoverConnectionOptions = nil + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) + }) + t.Run("validation: exactly one field from [opts.EnableConnectionFailover opts.DisableConnectionFailover opts.Primary] should be present", func(t *testing.T) { + opts := defaultOpts() + opts.EnableConnectionFailover = &EnableConnectionFailover{} + opts.DisableConnectionFailover = &DisableConnectionFailover{} + opts.Primary = &Primary{} + assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("AlterConnectionFailoverConnectionOptions", "EnableConnectionFailover", "DisableConnectionFailover", "Primary")) + }) + + t.Run("enable connection failover", func(t *testing.T) { + opts := defaultOpts() + opts.EnableConnectionFailover = &EnableConnectionFailover{ + Accounts: []ExternalObjectIdentifier{externalId, externalIdTwo}, + } + assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s ENABLE FAILOVER TO ACCOUNTS %s, %s", id.FullyQualifiedName(), externalId.FullyQualifiedName(), externalIdTwo.FullyQualifiedName()) + }) + + t.Run("enable connection failover with ignore edition check", func(t *testing.T) { + opts := defaultOpts() + opts.EnableConnectionFailover = &EnableConnectionFailover{ + Accounts: []ExternalObjectIdentifier{externalId, externalIdTwo}, + IgnoreEditionCheck: Bool(true), + } + assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s ENABLE FAILOVER TO ACCOUNTS %s, %s IGNORE EDITION CHECK", id.FullyQualifiedName(), externalId.FullyQualifiedName(), externalIdTwo.FullyQualifiedName()) + }) + + t.Run("disable connection failover", func(t *testing.T) { + opts := defaultOpts() + opts.DisableConnectionFailover = &DisableConnectionFailover{} + assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s DISABLE FAILOVER", id.FullyQualifiedName()) + }) + + t.Run("disable connection failover to accounts", func(t *testing.T) { + opts := defaultOpts() + opts.DisableConnectionFailover = &DisableConnectionFailover{ + ToAccounts: Bool(true), + Accounts: []ExternalObjectIdentifier{externalId, externalIdTwo}, + } + assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s DISABLE FAILOVER TO ACCOUNTS %s, %s", id.FullyQualifiedName(), externalId.FullyQualifiedName(), externalIdTwo.FullyQualifiedName()) + }) +} + +func TestConntections_AlterConnection(t *testing.T) { + id := randomAccountObjectIdentifier() + defaultOpts := func() *AlterConnectionConnectionOptions { + return &AlterConnectionConnectionOptions{ + name: id, + } + } + + t.Run("validation: nil options", func(t *testing.T) { + var opts *AlterConnectionConnectionOptions = nil + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) + }) + t.Run("validation: exactly one field from [opts.Set opts.Unset] should be present", func(t *testing.T) { + opts := defaultOpts() + opts.Set = &Set{} + opts.Unset = &Unset{} + assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("AlterConnectionConnectionOptions", "Set", "Unset")) + }) + + t.Run("validation: at least one of the fields [opts.Set.Comment] should be set", func(t *testing.T) { + opts := defaultOpts() + opts.Set = &Set{} + assertOptsInvalidJoinedErrors(t, opts, errAtLeastOneOf("AlterConnectionConnectionOptions.Set", "Comment")) + }) + + t.Run("validation: at least one of the fields [opts.Unset.Comment] should be set", func(t *testing.T) { + opts := defaultOpts() + opts.Unset = &Unset{} + assertOptsInvalidJoinedErrors(t, opts, errAtLeastOneOf("AlterConnectionConnectionOptions.Unset", "Comment")) + }) + + + t.Run("set comment", func(t *testing.T) { + opts := defaultOpts() + opts.Set = &Set{Comment: String("test comment")} + assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s SET COMMENT = 'test comment'", id.FullyQualifiedName()) + }) + + t.Run("unset comment", func(t *testing.T) { + opts := defaultOpts() + opts.Unset = &Unset{Comment: Bool(true)} + assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s UNSET COMMENT", id.FullyQualifiedName()) + }) +} diff --git a/pkg/sdk/connections_impl_gen.go b/pkg/sdk/connections_impl_gen.go new file mode 100644 index 0000000000..24f784b3ac --- /dev/null +++ b/pkg/sdk/connections_impl_gen.go @@ -0,0 +1,97 @@ +package sdk + +import ( + "context" +) + +var _ Conntections = (*conntections)(nil) + +type conntections struct { + client *Client +} + +func (v *conntections) CreateConnection(ctx context.Context, request *CreateConnectionConnectionRequest) error { + opts := request.toOpts() + return validateAndExec(v.client, ctx, opts) +} + +func (v *conntections) CreateReplicatedConnection(ctx context.Context, request *CreateReplicatedConnectionConnectionRequest) error { + opts := request.toOpts() + return validateAndExec(v.client, ctx, opts) +} + +func (v *conntections) AlterConnectionFailover(ctx context.Context, request *AlterConnectionFailoverConnectionRequest) error { + opts := request.toOpts() + return validateAndExec(v.client, ctx, opts) +} + +func (v *conntections) AlterConnection(ctx context.Context, request *AlterConnectionConnectionRequest) error { + opts := request.toOpts() + return validateAndExec(v.client, ctx, opts) +} + +func (r *CreateConnectionConnectionRequest) toOpts() *CreateConnectionConnectionOptions { + opts := &CreateConnectionConnectionOptions{ + IfNotExists: r.IfNotExists, + name: r.name, + Comment: r.Comment, + } + return opts +} + +func (r *CreateReplicatedConnectionConnectionRequest) toOpts() *CreateReplicatedConnectionConnectionOptions { + opts := &CreateReplicatedConnectionConnectionOptions{ + IfNotExists: r.IfNotExists, + name: r.name, + ReplicaOf: r.ReplicaOf, + Comment: r.Comment, + } + return opts +} + +func (r *AlterConnectionFailoverConnectionRequest) toOpts() *AlterConnectionFailoverConnectionOptions { + opts := &AlterConnectionFailoverConnectionOptions{ + name: r.name, + } + + if r.EnableConnectionFailover != nil { + opts.EnableConnectionFailover = &EnableConnectionFailover{ + Accounts: r.EnableConnectionFailover.Accounts, + IgnoreEditionCheck: r.EnableConnectionFailover.IgnoreEditionCheck, + } + } + + if r.DisableConnectionFailover != nil { + opts.DisableConnectionFailover = &DisableConnectionFailover{ + ToAccounts: r.DisableConnectionFailover.ToAccounts, + Accounts: r.DisableConnectionFailover.Accounts, + } + } + + if r.Primary != nil { + opts.Primary = &Primary{} + } + + return opts +} + +func (r *AlterConnectionConnectionRequest) toOpts() *AlterConnectionConnectionOptions { + opts := &AlterConnectionConnectionOptions{ + IfExists: r.IfExists, + name: r.name, + } + + if r.Set != nil { + opts.Set = &Set{ + Comment: r.Set.Comment, + } + } + + if r.Unset != nil { + opts.Unset = &Unset{ + Comment: r.Unset.Comment, + } + } + + return opts +} diff --git a/pkg/sdk/connections_validations_gen.go b/pkg/sdk/connections_validations_gen.go new file mode 100644 index 0000000000..8816dbd929 --- /dev/null +++ b/pkg/sdk/connections_validations_gen.go @@ -0,0 +1,65 @@ +package sdk + +var ( + _ validatable = new(CreateConnectionConnectionOptions) + _ validatable = new(CreateReplicatedConnectionConnectionOptions) + _ validatable = new(AlterConnectionFailoverConnectionOptions) + _ validatable = new(AlterConnectionConnectionOptions) +) + +func (opts *CreateConnectionConnectionOptions) validate() error { + if opts == nil { + return ErrNilOptions + } + var errs []error + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) + } + return JoinErrors(errs...) +} + +func (opts *CreateReplicatedConnectionConnectionOptions) validate() error { + if opts == nil { + return ErrNilOptions + } + var errs []error + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) + } + if !ValidObjectIdentifier(opts.ReplicaOf) { + errs = append(errs, ErrInvalidObjectIdentifier) + } + return JoinErrors(errs...) +} + +func (opts *AlterConnectionFailoverConnectionOptions) validate() error { + if opts == nil { + return ErrNilOptions + } + var errs []error + if !exactlyOneValueSet(opts.EnableConnectionFailover, opts.DisableConnectionFailover, opts.Primary) { + errs = append(errs, errExactlyOneOf("AlterConnectionFailoverConnectionOptions", "EnableConnectionFailover", "DisableConnectionFailover", "Primary")) + } + return JoinErrors(errs...) +} + +func (opts *AlterConnectionConnectionOptions) validate() error { + if opts == nil { + return ErrNilOptions + } + var errs []error + if !exactlyOneValueSet(opts.Set, opts.Unset) { + errs = append(errs, errExactlyOneOf("AlterConnectionConnectionOptions", "Set", "Unset")) + } + if valueSet(opts.Set) { + if !anyValueSet(opts.Set.Comment) { + errs = append(errs, errAtLeastOneOf("AlterConnectionConnectionOptions.Set", "Comment")) + } + } + if valueSet(opts.Unset) { + if !anyValueSet(opts.Unset.Comment) { + errs = append(errs, errAtLeastOneOf("AlterConnectionConnectionOptions.Unset", "Comment")) + } + } + return JoinErrors(errs...) +} diff --git a/pkg/sdk/poc/main.go b/pkg/sdk/poc/main.go index 72c675a73f..34e89d5fca 100644 --- a/pkg/sdk/poc/main.go +++ b/pkg/sdk/poc/main.go @@ -46,6 +46,7 @@ var definitionMapping = map[string]*generator.Interface{ "external_volumes_def.go": sdk.ExternalVolumesDef, "authentication_policies_def.go": sdk.AuthenticationPoliciesDef, "secrets_def.go": sdk.SecretsDef, + "connections_def.go": sdk.ConnectionDef, } func main() { diff --git a/pkg/sdk/random_test.go b/pkg/sdk/random_test.go index 0f58dc3a79..6c3660d90a 100644 --- a/pkg/sdk/random_test.go +++ b/pkg/sdk/random_test.go @@ -10,6 +10,7 @@ var ( // TODO: Add to the generator emptyAccountObjectIdentifier = NewAccountObjectIdentifier("") + emptyExtenalObjectIdentifier = NewExternalObjectIdentifier(NewAccountIdentifier("", ""), NewObjectIdentifierFromFullyQualifiedName("")) emptyDatabaseObjectIdentifier = NewDatabaseObjectIdentifier("", "") emptySchemaObjectIdentifier = NewSchemaObjectIdentifier("", "", "") emptySchemaObjectIdentifierWithArguments = NewSchemaObjectIdentifierWithArguments("", "", "") From 6b989e3ea37b87af379169ea2de15a5b5f8b3565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Mon, 21 Oct 2024 11:15:33 +0200 Subject: [PATCH 02/13] added show and drop --- .../assert/objectassert/gen/sdk_object_def.go | 7 ++ pkg/sdk/client.go | 2 + pkg/sdk/connections_def.go | 42 +++++++++- pkg/sdk/connections_dto_builders_gen.go | 22 ++++++ pkg/sdk/connections_dto_gen.go | 11 +++ pkg/sdk/connections_gen.go | 52 ++++++++++++- pkg/sdk/connections_gen_integration_test.go | 14 +++- pkg/sdk/connections_gen_test.go | 55 +++++++++++++- pkg/sdk/connections_impl_gen.go | 76 +++++++++++++++++-- pkg/sdk/connections_validations_gen.go | 21 +++++ .../connections_gen_integration_test.go | 27 +++++++ 11 files changed, 317 insertions(+), 12 deletions(-) create mode 100644 pkg/sdk/testint/connections_gen_integration_test.go diff --git a/pkg/acceptance/bettertestspoc/assert/objectassert/gen/sdk_object_def.go b/pkg/acceptance/bettertestspoc/assert/objectassert/gen/sdk_object_def.go index a5aae66a99..aa1563506c 100644 --- a/pkg/acceptance/bettertestspoc/assert/objectassert/gen/sdk_object_def.go +++ b/pkg/acceptance/bettertestspoc/assert/objectassert/gen/sdk_object_def.go @@ -17,6 +17,13 @@ var allStructs = []SdkObjectDef{ ObjectType: sdk.ObjectTypeDatabase, ObjectStruct: sdk.Database{}, }, + /* + { + IdType: "sdk.AccountObjectIdentifier", + ObjectType: sdk.ObjectTypeConnection, + ObjectStruct: sdk.Connection{}, + }, + */ { IdType: "sdk.DatabaseObjectIdentifier", ObjectType: sdk.ObjectTypeDatabaseRole, diff --git a/pkg/sdk/client.go b/pkg/sdk/client.go index b5a76425c9..d1c04d164a 100644 --- a/pkg/sdk/client.go +++ b/pkg/sdk/client.go @@ -47,6 +47,7 @@ type Client struct { Applications Applications AuthenticationPolicies AuthenticationPolicies Comments Comments + //Connections Connections CortexSearchServices CortexSearchServices DatabaseRoles DatabaseRoles Databases Databases @@ -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} diff --git a/pkg/sdk/connections_def.go b/pkg/sdk/connections_def.go index 4b464e4c8f..8f2257f334 100644 --- a/pkg/sdk/connections_def.go +++ b/pkg/sdk/connections_def.go @@ -9,7 +9,7 @@ var enabledFailoverAccounts = g.NewQueryStruct("EnabledFailoverAccounts"). Text("Account", g.KeywordOptions().NoQuotes()) var ConnectionDef = g.NewInterface( - "Conntections", + "Connections", "Connection", g.KindOfT[AccountObjectIdentifier](), ).CustomOperation( @@ -89,4 +89,42 @@ var ConnectionDef = g.NewInterface( g.KeywordOptions().SQL("UNSET"), ). WithValidation(g.ExactlyOneValueSet, "Set", "Unset"), -) +).DropOperation( + "https://docs.snowflake.com/en/sql-reference/sql/drop-connection", + g.NewQueryStruct("DropConnection"). + Drop(). + SQL("CONNECTION"). + IfExists(). + Name(). + WithValidation(g.ValidIdentifier, "name"), +).ShowOperation( + "https://docs.snowflake.com/en/sql-reference/sql/show-connections", + g.DbStruct("connectionRow"). + Field("snowflake_region", "string"). + Field("created_on", "time.Time"). + Field("account_name", "string"). + Field("name", "string"). + Field("comment", "sql.NullString"). + Field("is_primary", "string"). + Field("primary", "string"). + Field("failover_allowed_to_accounts", "string"). + Field("connection_url", "string"). + Field("orgnization_name", "string"). + Field("account_locator", "string"), + g.PlainStruct("Connection"). + Field("SnowflakeRegion", "string"). + Field("CreatedOn", "time.Time"). + Field("AccountName", "string"). + Field("Name", "string"). + Field("Comment", "*string"). + Field("IsPrimary", "bool"). + Field("Primary", "string"). + Field("FailoverAllowedToAccounts", "[]string"). + Field("ConnectionUrl", "string"). + Field("OrgnizationName", "string"). + Field("AccountLocator", "string"), + g.NewQueryStruct("ShowConnections"). + Show(). + SQL("CONNECTIONS"). + OptionalLike(), +).ShowByIdOperation() diff --git a/pkg/sdk/connections_dto_builders_gen.go b/pkg/sdk/connections_dto_builders_gen.go index 0ad6960b4e..94980a89ca 100644 --- a/pkg/sdk/connections_dto_builders_gen.go +++ b/pkg/sdk/connections_dto_builders_gen.go @@ -137,3 +137,25 @@ func (s *UnsetRequest) WithComment(Comment bool) *UnsetRequest { s.Comment = &Comment return s } + +func NewDropConnectionRequest( + name AccountObjectIdentifier, +) *DropConnectionRequest { + s := DropConnectionRequest{} + s.name = name + return &s +} + +func (s *DropConnectionRequest) WithIfExists(IfExists bool) *DropConnectionRequest { + s.IfExists = &IfExists + return s +} + +func NewShowConnectionRequest() *ShowConnectionRequest { + return &ShowConnectionRequest{} +} + +func (s *ShowConnectionRequest) WithLike(Like Like) *ShowConnectionRequest { + s.Like = &Like + return s +} diff --git a/pkg/sdk/connections_dto_gen.go b/pkg/sdk/connections_dto_gen.go index 9d0935ba8d..6583345a1e 100644 --- a/pkg/sdk/connections_dto_gen.go +++ b/pkg/sdk/connections_dto_gen.go @@ -7,6 +7,8 @@ var ( _ optionsProvider[CreateReplicatedConnectionConnectionOptions] = new(CreateReplicatedConnectionConnectionRequest) _ optionsProvider[AlterConnectionFailoverConnectionOptions] = new(AlterConnectionFailoverConnectionRequest) _ optionsProvider[AlterConnectionConnectionOptions] = new(AlterConnectionConnectionRequest) + _ optionsProvider[DropConnectionOptions] = new(DropConnectionRequest) + _ optionsProvider[ShowConnectionOptions] = new(ShowConnectionRequest) ) type CreateConnectionConnectionRequest struct { @@ -56,3 +58,12 @@ type SetRequest struct { type UnsetRequest struct { Comment *bool } + +type DropConnectionRequest struct { + IfExists *bool + name AccountObjectIdentifier // required +} + +type ShowConnectionRequest struct { + Like *Like +} diff --git a/pkg/sdk/connections_gen.go b/pkg/sdk/connections_gen.go index 0e0a7344db..8531c60a41 100644 --- a/pkg/sdk/connections_gen.go +++ b/pkg/sdk/connections_gen.go @@ -1,12 +1,19 @@ package sdk -import "context" +import ( + "context" + "database/sql" + "time" +) -type Conntections interface { +type Connections interface { CreateConnection(ctx context.Context, request *CreateConnectionConnectionRequest) error CreateReplicatedConnection(ctx context.Context, request *CreateReplicatedConnectionConnectionRequest) error AlterConnectionFailover(ctx context.Context, request *AlterConnectionFailoverConnectionRequest) error AlterConnection(ctx context.Context, request *AlterConnectionConnectionRequest) error + Drop(ctx context.Context, request *DropConnectionRequest) error + Show(ctx context.Context, request *ShowConnectionRequest) ([]Connection, error) + ShowByID(ctx context.Context, id AccountObjectIdentifier) (*Connection, error) } // CreateConnectionConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-connection. @@ -65,3 +72,44 @@ type Set struct { type Unset struct { Comment *bool `ddl:"keyword" sql:"COMMENT"` } + +// DropConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/drop-connection. +type DropConnectionOptions struct { + drop bool `ddl:"static" sql:"DROP"` + connection bool `ddl:"static" sql:"CONNECTION"` + IfExists *bool `ddl:"keyword" sql:"IF EXISTS"` + name AccountObjectIdentifier `ddl:"identifier"` +} + +// ShowConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/show-connections. +type ShowConnectionOptions struct { + show bool `ddl:"static" sql:"SHOW"` + connections bool `ddl:"static" sql:"CONNECTIONS"` + Like *Like `ddl:"keyword" sql:"LIKE"` +} +type connectionRow struct { + SnowflakeRegion string `db:"snowflake_region"` + CreatedOn time.Time `db:"created_on"` + AccountName string `db:"account_name"` + Name string `db:"name"` + Comment sql.NullString `db:"comment"` + IsPrimary string `db:"is_primary"` + Primary string `db:"primary"` + FailoverAllowedToAccounts string `db:"failover_allowed_to_accounts"` + ConnectionUrl string `db:"connection_url"` + OrgnizationName string `db:"orgnization_name"` + AccountLocator string `db:"account_locator"` +} +type Connection struct { + SnowflakeRegion string + CreatedOn time.Time + AccountName string + Name string + Comment *string + IsPrimary bool + Primary string + FailoverAllowedToAccounts []string + ConnectionUrl string + OrgnizationName string + AccountLocator string +} diff --git a/pkg/sdk/connections_gen_integration_test.go b/pkg/sdk/connections_gen_integration_test.go index 40007b140b..47141099ef 100644 --- a/pkg/sdk/connections_gen_integration_test.go +++ b/pkg/sdk/connections_gen_integration_test.go @@ -2,7 +2,7 @@ package sdk import "testing" -func TestInt_Conntections(t *testing.T) { +func TestInt_Connections(t *testing.T) { // TODO: prepare common resources t.Run("CreateConnection", func(t *testing.T) { @@ -20,4 +20,16 @@ func TestInt_Conntections(t *testing.T) { t.Run("AlterConnection", func(t *testing.T) { // TODO: fill me }) + + t.Run("Drop", func(t *testing.T) { + // TODO: fill me + }) + + t.Run("Show", func(t *testing.T) { + // TODO: fill me + }) + + t.Run("ShowByID", func(t *testing.T) { + // TODO: fill me + }) } diff --git a/pkg/sdk/connections_gen_test.go b/pkg/sdk/connections_gen_test.go index e899ca2d64..63558cd54c 100644 --- a/pkg/sdk/connections_gen_test.go +++ b/pkg/sdk/connections_gen_test.go @@ -165,7 +165,6 @@ func TestConntections_AlterConnection(t *testing.T) { assertOptsInvalidJoinedErrors(t, opts, errAtLeastOneOf("AlterConnectionConnectionOptions.Unset", "Comment")) }) - t.Run("set comment", func(t *testing.T) { opts := defaultOpts() opts.Set = &Set{Comment: String("test comment")} @@ -178,3 +177,57 @@ func TestConntections_AlterConnection(t *testing.T) { assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s UNSET COMMENT", id.FullyQualifiedName()) }) } + +func TestConnections_Drop(t *testing.T) { + id := randomAccountObjectIdentifier() + defaultOpts := func() *DropConnectionOptions { + return &DropConnectionOptions{ + name: id, + } + } + + t.Run("validation: nil options", func(t *testing.T) { + var opts *DropConnectionOptions = nil + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) + }) + t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { + opts := defaultOpts() + opts.name = emptyAccountObjectIdentifier + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) + }) + + t.Run("basic", func(t *testing.T) { + opts := defaultOpts() + assertOptsValidAndSQLEquals(t, opts, "DROP CONNECTION %s", id.FullyQualifiedName()) + }) + + t.Run("all options", func(t *testing.T) { + opts := defaultOpts() + opts.IfExists = Bool(true) + assertOptsValidAndSQLEquals(t, opts, "DROP CONNECTION IF EXISTS %s", id.FullyQualifiedName()) + }) +} + +func TestConnections_Show(t *testing.T) { + defaultOpts := func() *ShowConnectionOptions { + return &ShowConnectionOptions{} + } + + t.Run("validation: nil options", func(t *testing.T) { + var opts *ShowConnectionOptions = nil + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) + }) + + t.Run("basic", func(t *testing.T) { + opts := defaultOpts() + assertOptsValidAndSQLEquals(t, opts, "SHOW CONNECTIONS") + }) + + t.Run("all options", func(t *testing.T) { + opts := defaultOpts() + opts.Like = &Like{ + String("test_connection_name"), + } + assertOptsValidAndSQLEquals(t, opts, "SHOW CONNECTIONS LIKE 'test_connection_name'") + }) +} diff --git a/pkg/sdk/connections_impl_gen.go b/pkg/sdk/connections_impl_gen.go index 24f784b3ac..4ebc41123d 100644 --- a/pkg/sdk/connections_impl_gen.go +++ b/pkg/sdk/connections_impl_gen.go @@ -2,34 +2,59 @@ package sdk import ( "context" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/collections" ) -var _ Conntections = (*conntections)(nil) +var _ Connections = (*connections)(nil) -type conntections struct { +type connections struct { client *Client } -func (v *conntections) CreateConnection(ctx context.Context, request *CreateConnectionConnectionRequest) error { +func (v *connections) CreateConnection(ctx context.Context, request *CreateConnectionConnectionRequest) error { opts := request.toOpts() return validateAndExec(v.client, ctx, opts) } -func (v *conntections) CreateReplicatedConnection(ctx context.Context, request *CreateReplicatedConnectionConnectionRequest) error { +func (v *connections) CreateReplicatedConnection(ctx context.Context, request *CreateReplicatedConnectionConnectionRequest) error { opts := request.toOpts() return validateAndExec(v.client, ctx, opts) } -func (v *conntections) AlterConnectionFailover(ctx context.Context, request *AlterConnectionFailoverConnectionRequest) error { +func (v *connections) AlterConnectionFailover(ctx context.Context, request *AlterConnectionFailoverConnectionRequest) error { opts := request.toOpts() return validateAndExec(v.client, ctx, opts) } -func (v *conntections) AlterConnection(ctx context.Context, request *AlterConnectionConnectionRequest) error { +func (v *connections) AlterConnection(ctx context.Context, request *AlterConnectionConnectionRequest) error { opts := request.toOpts() return validateAndExec(v.client, ctx, opts) } +func (v *connections) Drop(ctx context.Context, request *DropConnectionRequest) error { + opts := request.toOpts() + return validateAndExec(v.client, ctx, opts) +} + +func (v *connections) Show(ctx context.Context, request *ShowConnectionRequest) ([]Connection, error) { + opts := request.toOpts() + dbRows, err := validateAndQuery[connectionRow](v.client, ctx, opts) + if err != nil { + return nil, err + } + resultList := convertRows[connectionRow, Connection](dbRows) + return resultList, nil +} + +func (v *connections) ShowByID(ctx context.Context, id AccountObjectIdentifier) (*Connection, error) { + connections, err := v.Show(ctx, NewShowConnectionRequest().WithLike(Like{String(id.Name())})) + if err != nil { + return nil, err + } + return collections.FindFirst(connections, func(r Connection) bool { return r.Name == id.Name() }) +} + func (r *CreateConnectionConnectionRequest) toOpts() *CreateConnectionConnectionOptions { opts := &CreateConnectionConnectionOptions{ IfNotExists: r.IfNotExists, @@ -95,3 +120,42 @@ func (r *AlterConnectionConnectionRequest) toOpts() *AlterConnectionConnectionOp return opts } + +func (r *DropConnectionRequest) toOpts() *DropConnectionOptions { + opts := &DropConnectionOptions{ + IfExists: r.IfExists, + name: r.name, + } + return opts +} + +func (r *ShowConnectionRequest) toOpts() *ShowConnectionOptions { + opts := &ShowConnectionOptions{ + Like: r.Like, + } + return opts +} + +func (r connectionRow) convert() *Connection { + c := &Connection{ + SnowflakeRegion: r.SnowflakeRegion, + CreatedOn: r.CreatedOn, + AccountName: r.AccountName, + Name: r.Name, + Primary: r.Primary, + FailoverAllowedToAccounts: ParseCommaSeparatedStringArray(r.FailoverAllowedToAccounts, false), + ConnectionUrl: r.ConnectionUrl, + OrgnizationName: r.OrgnizationName, + AccountLocator: r.AccountLocator, + } + b, err := parseBooleanParameter("IsPrimary", r.IsPrimary) + if err != nil { + return nil + } + c.IsPrimary = *b + if r.Comment.Valid { + c.Comment = String(r.Comment.String) + } + + return c +} diff --git a/pkg/sdk/connections_validations_gen.go b/pkg/sdk/connections_validations_gen.go index 8816dbd929..b5131bc180 100644 --- a/pkg/sdk/connections_validations_gen.go +++ b/pkg/sdk/connections_validations_gen.go @@ -5,6 +5,8 @@ var ( _ validatable = new(CreateReplicatedConnectionConnectionOptions) _ validatable = new(AlterConnectionFailoverConnectionOptions) _ validatable = new(AlterConnectionConnectionOptions) + _ validatable = new(DropConnectionOptions) + _ validatable = new(ShowConnectionOptions) ) func (opts *CreateConnectionConnectionOptions) validate() error { @@ -63,3 +65,22 @@ func (opts *AlterConnectionConnectionOptions) validate() error { } return JoinErrors(errs...) } + +func (opts *DropConnectionOptions) validate() error { + if opts == nil { + return ErrNilOptions + } + var errs []error + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) + } + return JoinErrors(errs...) +} + +func (opts *ShowConnectionOptions) validate() error { + if opts == nil { + return ErrNilOptions + } + var errs []error + return JoinErrors(errs...) +} diff --git a/pkg/sdk/testint/connections_gen_integration_test.go b/pkg/sdk/testint/connections_gen_integration_test.go new file mode 100644 index 0000000000..cdeb528602 --- /dev/null +++ b/pkg/sdk/testint/connections_gen_integration_test.go @@ -0,0 +1,27 @@ +package testint + +import "testing" + +func TestInt_Conntections(t *testing.T) { + client := testClient(t) + _ = client + ctx := testContext(t) + _ = ctx + + t.Run("CreateConnection", func(t *testing.T) { + id := testClientHelper().Ids.RandomAccountObjectIdentifier() + _ = id + }) + + t.Run("CreateReplicatedConnection", func(t *testing.T) { + // TODO: fill me + }) + + t.Run("AlterConnectionFailover", func(t *testing.T) { + // TODO: fill me + }) + + t.Run("AlterConnection", func(t *testing.T) { + // TODO: fill me + }) +} From fb8d018ed125bc5a920de164791d3189192d8fc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Mon, 21 Oct 2024 11:31:35 +0200 Subject: [PATCH 03/13] connection assert generated --- .../objectassert/connection_snowflake_gen.go | 156 ++++++++++++++++++ .../assert/objectassert/gen/sdk_object_def.go | 2 - pkg/acceptance/helpers/connection_client.go | 3 + pkg/sdk/client.go | 4 +- 4 files changed, 161 insertions(+), 4 deletions(-) create mode 100644 pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_gen.go create mode 100644 pkg/acceptance/helpers/connection_client.go diff --git a/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_gen.go b/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_gen.go new file mode 100644 index 0000000000..571abf963f --- /dev/null +++ b/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_gen.go @@ -0,0 +1,156 @@ +// Code generated by assertions generator; DO NOT EDIT. + +package objectassert + +import ( + "fmt" + "testing" + "time" + + 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/sdk" +) + +type ConnectionAssert struct { + *assert.SnowflakeObjectAssert[sdk.Connection, sdk.AccountObjectIdentifier] +} + +func Connection(t *testing.T, id sdk.AccountObjectIdentifier) *ConnectionAssert { + t.Helper() + return &ConnectionAssert{ + assert.NewSnowflakeObjectAssertWithProvider(sdk.ObjectTypeConnection, id, acc.TestClient().Connection.Show), + } +} + +func ConnectionFromObject(t *testing.T, connection *sdk.Connection) *ConnectionAssert { + t.Helper() + return &ConnectionAssert{ + assert.NewSnowflakeObjectAssertWithObject(sdk.ObjectTypeConnection, connection.ID(), connection), + } +} + +func (c *ConnectionAssert) HasSnowflakeRegion(expected string) *ConnectionAssert { + c.AddAssertion(func(t *testing.T, o *sdk.Connection) error { + t.Helper() + if o.SnowflakeRegion != expected { + return fmt.Errorf("expected snowflake region: %v; got: %v", expected, o.SnowflakeRegion) + } + return nil + }) + return c +} + +func (c *ConnectionAssert) HasCreatedOn(expected time.Time) *ConnectionAssert { + c.AddAssertion(func(t *testing.T, o *sdk.Connection) error { + t.Helper() + if o.CreatedOn != expected { + return fmt.Errorf("expected created on: %v; got: %v", expected, o.CreatedOn) + } + return nil + }) + return c +} + +func (c *ConnectionAssert) HasAccountName(expected string) *ConnectionAssert { + c.AddAssertion(func(t *testing.T, o *sdk.Connection) error { + t.Helper() + if o.AccountName != expected { + return fmt.Errorf("expected account name: %v; got: %v", expected, o.AccountName) + } + return nil + }) + return c +} + +func (c *ConnectionAssert) HasName(expected string) *ConnectionAssert { + c.AddAssertion(func(t *testing.T, o *sdk.Connection) error { + t.Helper() + if o.Name != expected { + return fmt.Errorf("expected name: %v; got: %v", expected, o.Name) + } + return nil + }) + return c +} + +func (c *ConnectionAssert) HasComment(expected string) *ConnectionAssert { + c.AddAssertion(func(t *testing.T, o *sdk.Connection) error { + t.Helper() + if o.Comment == nil { + return fmt.Errorf("expected comment to have value; got: nil") + } + if *o.Comment != expected { + return fmt.Errorf("expected comment: %v; got: %v", expected, *o.Comment) + } + return nil + }) + return c +} + +func (c *ConnectionAssert) HasIsPrimary(expected bool) *ConnectionAssert { + c.AddAssertion(func(t *testing.T, o *sdk.Connection) error { + t.Helper() + if o.IsPrimary != expected { + return fmt.Errorf("expected is primary: %v; got: %v", expected, o.IsPrimary) + } + return nil + }) + return c +} + +func (c *ConnectionAssert) HasPrimary(expected string) *ConnectionAssert { + c.AddAssertion(func(t *testing.T, o *sdk.Connection) error { + t.Helper() + if o.Primary != expected { + return fmt.Errorf("expected primary: %v; got: %v", expected, o.Primary) + } + return nil + }) + return c +} + +func (c *ConnectionAssert) HasFailoverAllowedToAccounts(expected []string) *ConnectionAssert { + c.AddAssertion(func(t *testing.T, o *sdk.Connection) error { + t.Helper() + if o.FailoverAllowedToAccounts != expected { + return fmt.Errorf("expected failover allowed to accounts: %v; got: %v", expected, o.FailoverAllowedToAccounts) + } + return nil + }) + return c +} + +func (c *ConnectionAssert) HasConnectionUrl(expected string) *ConnectionAssert { + c.AddAssertion(func(t *testing.T, o *sdk.Connection) error { + t.Helper() + if o.ConnectionUrl != expected { + return fmt.Errorf("expected connection url: %v; got: %v", expected, o.ConnectionUrl) + } + return nil + }) + return c +} + +func (c *ConnectionAssert) HasOrgnizationName(expected string) *ConnectionAssert { + c.AddAssertion(func(t *testing.T, o *sdk.Connection) error { + t.Helper() + if o.OrgnizationName != expected { + return fmt.Errorf("expected orgnization name: %v; got: %v", expected, o.OrgnizationName) + } + return nil + }) + return c +} + +func (c *ConnectionAssert) HasAccountLocator(expected string) *ConnectionAssert { + c.AddAssertion(func(t *testing.T, o *sdk.Connection) error { + t.Helper() + if o.AccountLocator != expected { + return fmt.Errorf("expected account locator: %v; got: %v", expected, o.AccountLocator) + } + return nil + }) + return c +} diff --git a/pkg/acceptance/bettertestspoc/assert/objectassert/gen/sdk_object_def.go b/pkg/acceptance/bettertestspoc/assert/objectassert/gen/sdk_object_def.go index aa1563506c..e29ebbac17 100644 --- a/pkg/acceptance/bettertestspoc/assert/objectassert/gen/sdk_object_def.go +++ b/pkg/acceptance/bettertestspoc/assert/objectassert/gen/sdk_object_def.go @@ -17,13 +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, diff --git a/pkg/acceptance/helpers/connection_client.go b/pkg/acceptance/helpers/connection_client.go new file mode 100644 index 0000000000..c64a4e389e --- /dev/null +++ b/pkg/acceptance/helpers/connection_client.go @@ -0,0 +1,3 @@ +package helpers + + diff --git a/pkg/sdk/client.go b/pkg/sdk/client.go index d1c04d164a..7467a1cf59 100644 --- a/pkg/sdk/client.go +++ b/pkg/sdk/client.go @@ -47,7 +47,7 @@ type Client struct { Applications Applications AuthenticationPolicies AuthenticationPolicies Comments Comments - //Connections Connections + Connections Connections CortexSearchServices CortexSearchServices DatabaseRoles DatabaseRoles Databases Databases @@ -206,7 +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.Connections = &connections{client: c} c.ContextFunctions = &contextFunctions{client: c} c.ConversionFunctions = &conversionFunctions{client: c} c.CortexSearchServices = &cortexSearchServices{client: c} From 6db06d835a08fd6fe1f4403bdf6304a6f32d6ae7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Tue, 22 Oct 2024 10:35:12 +0200 Subject: [PATCH 04/13] assertions generated --- .../objectassert/connection_snowflake_ext.go | 20 +++++++ .../objectassert/connection_snowflake_gen.go | 2 + pkg/acceptance/helpers/connection_client.go | 58 ++++++++++++++++++ pkg/acceptance/helpers/test_client.go | 2 + pkg/sdk/connections_def.go | 11 ++-- pkg/sdk/connections_dto_builders_gen.go | 46 +++++++------- pkg/sdk/connections_dto_gen.go | 20 +++---- pkg/sdk/connections_gen.go | 32 ++++++---- pkg/sdk/connections_gen_integration_test.go | 8 +-- pkg/sdk/connections_gen_test.go | 24 ++++---- pkg/sdk/connections_impl_gen.go | 60 ++++++++----------- pkg/sdk/connections_validations_gen.go | 24 ++++---- 12 files changed, 194 insertions(+), 113 deletions(-) create mode 100644 pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_ext.go diff --git a/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_ext.go b/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_ext.go new file mode 100644 index 0000000000..5be3983741 --- /dev/null +++ b/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_ext.go @@ -0,0 +1,20 @@ +package objectassert + +import ( + "fmt" + "slices" + "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: %v; got: %v", expected, o.FailoverAllowedToAccounts) + } + return nil + }) + return c +} diff --git a/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_gen.go b/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_gen.go index 571abf963f..736492a596 100644 --- a/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_gen.go +++ b/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_gen.go @@ -111,6 +111,7 @@ func (c *ConnectionAssert) HasPrimary(expected string) *ConnectionAssert { return c } +/* func (c *ConnectionAssert) HasFailoverAllowedToAccounts(expected []string) *ConnectionAssert { c.AddAssertion(func(t *testing.T, o *sdk.Connection) error { t.Helper() @@ -121,6 +122,7 @@ func (c *ConnectionAssert) HasFailoverAllowedToAccounts(expected []string) *Conn }) return c } +*/ func (c *ConnectionAssert) HasConnectionUrl(expected string) *ConnectionAssert { c.AddAssertion(func(t *testing.T, o *sdk.Connection) error { diff --git a/pkg/acceptance/helpers/connection_client.go b/pkg/acceptance/helpers/connection_client.go index c64a4e389e..5ea629d4e2 100644 --- a/pkg/acceptance/helpers/connection_client.go +++ b/pkg/acceptance/helpers/connection_client.go @@ -1,3 +1,61 @@ 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) CreateConnection(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) 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) +} diff --git a/pkg/acceptance/helpers/test_client.go b/pkg/acceptance/helpers/test_client.go index 9ddfd21102..cbfc8de2b9 100644 --- a/pkg/acceptance/helpers/test_client.go +++ b/pkg/acceptance/helpers/test_client.go @@ -17,6 +17,7 @@ type TestClient struct { ApplicationPackage *ApplicationPackageClient AuthenticationPolicy *AuthenticationPolicyClient BcrBundles *BcrBundlesClient + Connection *ConnectionClient Context *ContextClient CortexSearchService *CortexSearchServiceClient CatalogIntegration *CatalogIntegrationClient @@ -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), diff --git a/pkg/sdk/connections_def.go b/pkg/sdk/connections_def.go index 8f2257f334..c62821fb01 100644 --- a/pkg/sdk/connections_def.go +++ b/pkg/sdk/connections_def.go @@ -1,6 +1,7 @@ package sdk //go:generate go run ./poc/main.go + import ( g "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator" ) @@ -13,7 +14,7 @@ var ConnectionDef = g.NewInterface( "Connection", g.KindOfT[AccountObjectIdentifier](), ).CustomOperation( - "CreateConnection", + "Create", "https://docs.snowflake.com/en/sql-reference/sql/create-connection", g.NewQueryStruct("CreateConnection"). Create(). @@ -23,9 +24,9 @@ var ConnectionDef = g.NewInterface( OptionalComment(). WithValidation(g.ValidIdentifier, "name"), ).CustomOperation( - "CreateReplicatedConnection", + "CreateReplicated", "https://docs.snowflake.com/en/sql-reference/sql/create-connection", - g.NewQueryStruct("CreateReplicatedConnection"). + g.NewQueryStruct("CreateReplicated"). Create(). SQL("CONNECTION"). IfNotExists(). @@ -67,9 +68,9 @@ var ConnectionDef = g.NewInterface( ). WithValidation(g.ExactlyOneValueSet, "EnableConnectionFailover", "DisableConnectionFailover", "Primary"), ).CustomOperation( - "AlterConnection", + "Alter", "https://docs.snowflake.com/en/sql-reference/sql/alter-connection", - g.NewQueryStruct("AlterConnection"). + g.NewQueryStruct("Alter"). Alter(). SQL("CONNECTION"). IfExists(). diff --git a/pkg/sdk/connections_dto_builders_gen.go b/pkg/sdk/connections_dto_builders_gen.go index 94980a89ca..3cf604d0da 100644 --- a/pkg/sdk/connections_dto_builders_gen.go +++ b/pkg/sdk/connections_dto_builders_gen.go @@ -2,65 +2,63 @@ package sdk -import () - -func NewCreateConnectionConnectionRequest( +func NewCreateConnectionRequest( name AccountObjectIdentifier, -) *CreateConnectionConnectionRequest { - s := CreateConnectionConnectionRequest{} +) *CreateConnectionRequest { + s := CreateConnectionRequest{} s.name = name return &s } -func (s *CreateConnectionConnectionRequest) WithIfNotExists(IfNotExists bool) *CreateConnectionConnectionRequest { +func (s *CreateConnectionRequest) WithIfNotExists(IfNotExists bool) *CreateConnectionRequest { s.IfNotExists = &IfNotExists return s } -func (s *CreateConnectionConnectionRequest) WithComment(Comment string) *CreateConnectionConnectionRequest { +func (s *CreateConnectionRequest) WithComment(Comment string) *CreateConnectionRequest { s.Comment = &Comment return s } -func NewCreateReplicatedConnectionConnectionRequest( +func NewCreateReplicatedConnectionRequest( name AccountObjectIdentifier, ReplicaOf ExternalObjectIdentifier, -) *CreateReplicatedConnectionConnectionRequest { - s := CreateReplicatedConnectionConnectionRequest{} +) *CreateReplicatedConnectionRequest { + s := CreateReplicatedConnectionRequest{} s.name = name s.ReplicaOf = ReplicaOf return &s } -func (s *CreateReplicatedConnectionConnectionRequest) WithIfNotExists(IfNotExists bool) *CreateReplicatedConnectionConnectionRequest { +func (s *CreateReplicatedConnectionRequest) WithIfNotExists(IfNotExists bool) *CreateReplicatedConnectionRequest { s.IfNotExists = &IfNotExists return s } -func (s *CreateReplicatedConnectionConnectionRequest) WithComment(Comment string) *CreateReplicatedConnectionConnectionRequest { +func (s *CreateReplicatedConnectionRequest) WithComment(Comment string) *CreateReplicatedConnectionRequest { s.Comment = &Comment return s } -func NewAlterConnectionFailoverConnectionRequest( +func NewAlterConnectionFailoverRequest( name AccountObjectIdentifier, -) *AlterConnectionFailoverConnectionRequest { - s := AlterConnectionFailoverConnectionRequest{} +) *AlterConnectionFailoverRequest { + s := AlterConnectionFailoverRequest{} s.name = name return &s } -func (s *AlterConnectionFailoverConnectionRequest) WithEnableConnectionFailover(EnableConnectionFailover EnableConnectionFailoverRequest) *AlterConnectionFailoverConnectionRequest { +func (s *AlterConnectionFailoverRequest) WithEnableConnectionFailover(EnableConnectionFailover EnableConnectionFailoverRequest) *AlterConnectionFailoverRequest { s.EnableConnectionFailover = &EnableConnectionFailover return s } -func (s *AlterConnectionFailoverConnectionRequest) WithDisableConnectionFailover(DisableConnectionFailover DisableConnectionFailoverRequest) *AlterConnectionFailoverConnectionRequest { +func (s *AlterConnectionFailoverRequest) WithDisableConnectionFailover(DisableConnectionFailover DisableConnectionFailoverRequest) *AlterConnectionFailoverRequest { s.DisableConnectionFailover = &DisableConnectionFailover return s } -func (s *AlterConnectionFailoverConnectionRequest) WithPrimary(Primary PrimaryRequest) *AlterConnectionFailoverConnectionRequest { +func (s *AlterConnectionFailoverRequest) WithPrimary(Primary PrimaryRequest) *AlterConnectionFailoverRequest { s.Primary = &Primary return s } @@ -97,25 +95,25 @@ func NewPrimaryRequest() *PrimaryRequest { return &PrimaryRequest{} } -func NewAlterConnectionConnectionRequest( +func NewAlterConnectionRequest( name AccountObjectIdentifier, -) *AlterConnectionConnectionRequest { - s := AlterConnectionConnectionRequest{} +) *AlterConnectionRequest { + s := AlterConnectionRequest{} s.name = name return &s } -func (s *AlterConnectionConnectionRequest) WithIfExists(IfExists bool) *AlterConnectionConnectionRequest { +func (s *AlterConnectionRequest) WithIfExists(IfExists bool) *AlterConnectionRequest { s.IfExists = &IfExists return s } -func (s *AlterConnectionConnectionRequest) WithSet(Set SetRequest) *AlterConnectionConnectionRequest { +func (s *AlterConnectionRequest) WithSet(Set SetRequest) *AlterConnectionRequest { s.Set = &Set return s } -func (s *AlterConnectionConnectionRequest) WithUnset(Unset UnsetRequest) *AlterConnectionConnectionRequest { +func (s *AlterConnectionRequest) WithUnset(Unset UnsetRequest) *AlterConnectionRequest { s.Unset = &Unset return s } diff --git a/pkg/sdk/connections_dto_gen.go b/pkg/sdk/connections_dto_gen.go index 6583345a1e..e7c77fdf3c 100644 --- a/pkg/sdk/connections_dto_gen.go +++ b/pkg/sdk/connections_dto_gen.go @@ -3,28 +3,28 @@ package sdk //go:generate go run ./dto-builder-generator/main.go var ( - _ optionsProvider[CreateConnectionConnectionOptions] = new(CreateConnectionConnectionRequest) - _ optionsProvider[CreateReplicatedConnectionConnectionOptions] = new(CreateReplicatedConnectionConnectionRequest) - _ optionsProvider[AlterConnectionFailoverConnectionOptions] = new(AlterConnectionFailoverConnectionRequest) - _ optionsProvider[AlterConnectionConnectionOptions] = new(AlterConnectionConnectionRequest) - _ optionsProvider[DropConnectionOptions] = new(DropConnectionRequest) - _ optionsProvider[ShowConnectionOptions] = new(ShowConnectionRequest) + _ optionsProvider[CreateConnectionOptions] = new(CreateConnectionRequest) + _ optionsProvider[CreateReplicatedConnectionOptions] = new(CreateReplicatedConnectionRequest) + _ optionsProvider[AlterFailoverConnectionOptions] = new(AlterConnectionFailoverRequest) + _ optionsProvider[AlterConnectionOptions] = new(AlterConnectionRequest) + _ optionsProvider[DropConnectionOptions] = new(DropConnectionRequest) + _ optionsProvider[ShowConnectionOptions] = new(ShowConnectionRequest) ) -type CreateConnectionConnectionRequest struct { +type CreateConnectionRequest struct { IfNotExists *bool name AccountObjectIdentifier // required Comment *string } -type CreateReplicatedConnectionConnectionRequest struct { +type CreateReplicatedConnectionRequest struct { IfNotExists *bool name AccountObjectIdentifier // required ReplicaOf ExternalObjectIdentifier // required Comment *string } -type AlterConnectionFailoverConnectionRequest struct { +type AlterConnectionFailoverRequest struct { name AccountObjectIdentifier // required EnableConnectionFailover *EnableConnectionFailoverRequest DisableConnectionFailover *DisableConnectionFailoverRequest @@ -44,7 +44,7 @@ type DisableConnectionFailoverRequest struct { type PrimaryRequest struct { } -type AlterConnectionConnectionRequest struct { +type AlterConnectionRequest struct { IfExists *bool name AccountObjectIdentifier // required Set *SetRequest diff --git a/pkg/sdk/connections_gen.go b/pkg/sdk/connections_gen.go index 8531c60a41..0a914dc6da 100644 --- a/pkg/sdk/connections_gen.go +++ b/pkg/sdk/connections_gen.go @@ -7,17 +7,17 @@ import ( ) type Connections interface { - CreateConnection(ctx context.Context, request *CreateConnectionConnectionRequest) error - CreateReplicatedConnection(ctx context.Context, request *CreateReplicatedConnectionConnectionRequest) error - AlterConnectionFailover(ctx context.Context, request *AlterConnectionFailoverConnectionRequest) error - AlterConnection(ctx context.Context, request *AlterConnectionConnectionRequest) error + Create(ctx context.Context, request *CreateConnectionRequest) error + CreateReplicated(ctx context.Context, request *CreateReplicatedConnectionRequest) error + AlterFailover(ctx context.Context, request *AlterConnectionFailoverRequest) error + Alter(ctx context.Context, request *AlterConnectionRequest) error Drop(ctx context.Context, request *DropConnectionRequest) error Show(ctx context.Context, request *ShowConnectionRequest) ([]Connection, error) ShowByID(ctx context.Context, id AccountObjectIdentifier) (*Connection, error) } -// CreateConnectionConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-connection. -type CreateConnectionConnectionOptions struct { +// CreateConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-connection. +type CreateConnectionOptions struct { create bool `ddl:"static" sql:"CREATE"` connection bool `ddl:"static" sql:"CONNECTION"` IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` @@ -25,8 +25,8 @@ type CreateConnectionConnectionOptions struct { Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` } -// CreateReplicatedConnectionConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-connection. -type CreateReplicatedConnectionConnectionOptions struct { +// CreateReplicatedConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-connection. +type CreateReplicatedConnectionOptions struct { create bool `ddl:"static" sql:"CREATE"` connection bool `ddl:"static" sql:"CONNECTION"` IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` @@ -36,8 +36,8 @@ type CreateReplicatedConnectionConnectionOptions struct { Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` } -// AlterConnectionFailoverConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/alter-connection. -type AlterConnectionFailoverConnectionOptions struct { +// AlterFailoverConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/alter-connection. +type AlterFailoverConnectionOptions struct { alter bool `ddl:"static" sql:"ALTER"` connection bool `ddl:"static" sql:"CONNECTION"` name AccountObjectIdentifier `ddl:"identifier"` @@ -57,8 +57,8 @@ type Primary struct { primary bool `ddl:"static" sql:"PRIMARY"` } -// AlterConnectionConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/alter-connection. -type AlterConnectionConnectionOptions struct { +// AlterConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/alter-connection. +type AlterConnectionOptions struct { alter bool `ddl:"static" sql:"ALTER"` connection bool `ddl:"static" sql:"CONNECTION"` IfExists *bool `ddl:"keyword" sql:"IF EXISTS"` @@ -113,3 +113,11 @@ type Connection struct { OrgnizationName string AccountLocator string } + +func (v *Connection) ID() AccountObjectIdentifier { + return NewAccountObjectIdentifier(v.Name) +} + +func (v *Connection) ObjectType() ObjectType { + return ObjectTypeConnection +} diff --git a/pkg/sdk/connections_gen_integration_test.go b/pkg/sdk/connections_gen_integration_test.go index 47141099ef..7fe0a30c7c 100644 --- a/pkg/sdk/connections_gen_integration_test.go +++ b/pkg/sdk/connections_gen_integration_test.go @@ -5,19 +5,19 @@ import "testing" func TestInt_Connections(t *testing.T) { // TODO: prepare common resources - t.Run("CreateConnection", func(t *testing.T) { + t.Run("Create", func(t *testing.T) { // TODO: fill me }) - t.Run("CreateReplicatedConnection", func(t *testing.T) { + t.Run("CreateReplicated", func(t *testing.T) { // TODO: fill me }) - t.Run("AlterConnectionFailover", func(t *testing.T) { + t.Run("AlterFailover", func(t *testing.T) { // TODO: fill me }) - t.Run("AlterConnection", func(t *testing.T) { + t.Run("Alter", func(t *testing.T) { // TODO: fill me }) diff --git a/pkg/sdk/connections_gen_test.go b/pkg/sdk/connections_gen_test.go index 63558cd54c..5579cd143e 100644 --- a/pkg/sdk/connections_gen_test.go +++ b/pkg/sdk/connections_gen_test.go @@ -4,14 +4,14 @@ import "testing" func TestConntections_CreateConnection(t *testing.T) { id := randomAccountObjectIdentifier() - defaultOpts := func() *CreateConnectionConnectionOptions { - return &CreateConnectionConnectionOptions{ + defaultOpts := func() *CreateConnectionOptions { + return &CreateConnectionOptions{ name: id, } } t.Run("validation: nil options", func(t *testing.T) { - var opts *CreateConnectionConnectionOptions = nil + var opts *CreateConnectionOptions = nil assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { @@ -38,14 +38,14 @@ func TestConntections_CreateConnection(t *testing.T) { func TestConntections_CreateReplicatedConnection(t *testing.T) { id := randomAccountObjectIdentifier() externalId := randomExternalObjectIdentifier() - defaultOpts := func() *CreateReplicatedConnectionConnectionOptions { - return &CreateReplicatedConnectionConnectionOptions{ + defaultOpts := func() *CreateReplicatedConnectionOptions { + return &CreateReplicatedConnectionOptions{ name: id, } } t.Run("validation: nil options", func(t *testing.T) { - var opts *CreateReplicatedConnectionConnectionOptions = nil + var opts *CreateReplicatedConnectionOptions = nil assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { @@ -83,14 +83,14 @@ func TestConntections_AlterConnectionFailover(t *testing.T) { id := randomAccountObjectIdentifier() externalId := randomExternalObjectIdentifier() externalIdTwo := randomExternalObjectIdentifier() - defaultOpts := func() *AlterConnectionFailoverConnectionOptions { - return &AlterConnectionFailoverConnectionOptions{ + defaultOpts := func() *AlterFailoverConnectionOptions { + return &AlterFailoverConnectionOptions{ name: id, } } t.Run("validation: nil options", func(t *testing.T) { - var opts *AlterConnectionFailoverConnectionOptions = nil + var opts *AlterFailoverConnectionOptions = nil assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: exactly one field from [opts.EnableConnectionFailover opts.DisableConnectionFailover opts.Primary] should be present", func(t *testing.T) { @@ -136,14 +136,14 @@ func TestConntections_AlterConnectionFailover(t *testing.T) { func TestConntections_AlterConnection(t *testing.T) { id := randomAccountObjectIdentifier() - defaultOpts := func() *AlterConnectionConnectionOptions { - return &AlterConnectionConnectionOptions{ + defaultOpts := func() *AlterConnectionOptions { + return &AlterConnectionOptions{ name: id, } } t.Run("validation: nil options", func(t *testing.T) { - var opts *AlterConnectionConnectionOptions = nil + var opts *AlterConnectionOptions = nil assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: exactly one field from [opts.Set opts.Unset] should be present", func(t *testing.T) { diff --git a/pkg/sdk/connections_impl_gen.go b/pkg/sdk/connections_impl_gen.go index 4ebc41123d..f7cad9778d 100644 --- a/pkg/sdk/connections_impl_gen.go +++ b/pkg/sdk/connections_impl_gen.go @@ -12,22 +12,22 @@ type connections struct { client *Client } -func (v *connections) CreateConnection(ctx context.Context, request *CreateConnectionConnectionRequest) error { +func (v *connections) Create(ctx context.Context, request *CreateConnectionRequest) error { opts := request.toOpts() return validateAndExec(v.client, ctx, opts) } -func (v *connections) CreateReplicatedConnection(ctx context.Context, request *CreateReplicatedConnectionConnectionRequest) error { +func (v *connections) CreateReplicated(ctx context.Context, request *CreateReplicatedConnectionRequest) error { opts := request.toOpts() return validateAndExec(v.client, ctx, opts) } -func (v *connections) AlterConnectionFailover(ctx context.Context, request *AlterConnectionFailoverConnectionRequest) error { +func (v *connections) AlterFailover(ctx context.Context, request *AlterConnectionFailoverRequest) error { opts := request.toOpts() return validateAndExec(v.client, ctx, opts) } -func (v *connections) AlterConnection(ctx context.Context, request *AlterConnectionConnectionRequest) error { +func (v *connections) Alter(ctx context.Context, request *AlterConnectionRequest) error { opts := request.toOpts() return validateAndExec(v.client, ctx, opts) } @@ -48,15 +48,16 @@ func (v *connections) Show(ctx context.Context, request *ShowConnectionRequest) } func (v *connections) ShowByID(ctx context.Context, id AccountObjectIdentifier) (*Connection, error) { - connections, err := v.Show(ctx, NewShowConnectionRequest().WithLike(Like{String(id.Name())})) + // TODO: adjust request if e.g. LIKE is supported for the resource + connections, err := v.Show(ctx, NewShowConnectionRequest()) if err != nil { return nil, err } return collections.FindFirst(connections, func(r Connection) bool { return r.Name == id.Name() }) } -func (r *CreateConnectionConnectionRequest) toOpts() *CreateConnectionConnectionOptions { - opts := &CreateConnectionConnectionOptions{ +func (r *CreateConnectionRequest) toOpts() *CreateConnectionOptions { + opts := &CreateConnectionOptions{ IfNotExists: r.IfNotExists, name: r.name, Comment: r.Comment, @@ -64,8 +65,8 @@ func (r *CreateConnectionConnectionRequest) toOpts() *CreateConnectionConnection return opts } -func (r *CreateReplicatedConnectionConnectionRequest) toOpts() *CreateReplicatedConnectionConnectionOptions { - opts := &CreateReplicatedConnectionConnectionOptions{ +func (r *CreateReplicatedConnectionRequest) toOpts() *CreateReplicatedConnectionOptions { + opts := &CreateReplicatedConnectionOptions{ IfNotExists: r.IfNotExists, name: r.name, ReplicaOf: r.ReplicaOf, @@ -74,48 +75,58 @@ func (r *CreateReplicatedConnectionConnectionRequest) toOpts() *CreateReplicated return opts } -func (r *AlterConnectionFailoverConnectionRequest) toOpts() *AlterConnectionFailoverConnectionOptions { - opts := &AlterConnectionFailoverConnectionOptions{ +func (r *AlterConnectionFailoverRequest) toOpts() *AlterFailoverConnectionOptions { + opts := &AlterFailoverConnectionOptions{ name: r.name, } if r.EnableConnectionFailover != nil { + opts.EnableConnectionFailover = &EnableConnectionFailover{ Accounts: r.EnableConnectionFailover.Accounts, IgnoreEditionCheck: r.EnableConnectionFailover.IgnoreEditionCheck, } + } if r.DisableConnectionFailover != nil { + opts.DisableConnectionFailover = &DisableConnectionFailover{ ToAccounts: r.DisableConnectionFailover.ToAccounts, Accounts: r.DisableConnectionFailover.Accounts, } + } if r.Primary != nil { + opts.Primary = &Primary{} + } return opts } -func (r *AlterConnectionConnectionRequest) toOpts() *AlterConnectionConnectionOptions { - opts := &AlterConnectionConnectionOptions{ +func (r *AlterConnectionRequest) toOpts() *AlterConnectionOptions { + opts := &AlterConnectionOptions{ IfExists: r.IfExists, name: r.name, } if r.Set != nil { + opts.Set = &Set{ Comment: r.Set.Comment, } + } if r.Unset != nil { + opts.Unset = &Unset{ Comment: r.Unset.Comment, } + } return opts @@ -137,25 +148,6 @@ func (r *ShowConnectionRequest) toOpts() *ShowConnectionOptions { } func (r connectionRow) convert() *Connection { - c := &Connection{ - SnowflakeRegion: r.SnowflakeRegion, - CreatedOn: r.CreatedOn, - AccountName: r.AccountName, - Name: r.Name, - Primary: r.Primary, - FailoverAllowedToAccounts: ParseCommaSeparatedStringArray(r.FailoverAllowedToAccounts, false), - ConnectionUrl: r.ConnectionUrl, - OrgnizationName: r.OrgnizationName, - AccountLocator: r.AccountLocator, - } - b, err := parseBooleanParameter("IsPrimary", r.IsPrimary) - if err != nil { - return nil - } - c.IsPrimary = *b - if r.Comment.Valid { - c.Comment = String(r.Comment.String) - } - - return c + // TODO: Mapping + return &Connection{} } diff --git a/pkg/sdk/connections_validations_gen.go b/pkg/sdk/connections_validations_gen.go index b5131bc180..f7f9be1f8b 100644 --- a/pkg/sdk/connections_validations_gen.go +++ b/pkg/sdk/connections_validations_gen.go @@ -1,15 +1,15 @@ package sdk var ( - _ validatable = new(CreateConnectionConnectionOptions) - _ validatable = new(CreateReplicatedConnectionConnectionOptions) - _ validatable = new(AlterConnectionFailoverConnectionOptions) - _ validatable = new(AlterConnectionConnectionOptions) + _ validatable = new(CreateConnectionOptions) + _ validatable = new(CreateReplicatedConnectionOptions) + _ validatable = new(AlterFailoverConnectionOptions) + _ validatable = new(AlterConnectionOptions) _ validatable = new(DropConnectionOptions) _ validatable = new(ShowConnectionOptions) ) -func (opts *CreateConnectionConnectionOptions) validate() error { +func (opts *CreateConnectionOptions) validate() error { if opts == nil { return ErrNilOptions } @@ -20,7 +20,7 @@ func (opts *CreateConnectionConnectionOptions) validate() error { return JoinErrors(errs...) } -func (opts *CreateReplicatedConnectionConnectionOptions) validate() error { +func (opts *CreateReplicatedConnectionOptions) validate() error { if opts == nil { return ErrNilOptions } @@ -34,33 +34,33 @@ func (opts *CreateReplicatedConnectionConnectionOptions) validate() error { return JoinErrors(errs...) } -func (opts *AlterConnectionFailoverConnectionOptions) validate() error { +func (opts *AlterFailoverConnectionOptions) validate() error { if opts == nil { return ErrNilOptions } var errs []error if !exactlyOneValueSet(opts.EnableConnectionFailover, opts.DisableConnectionFailover, opts.Primary) { - errs = append(errs, errExactlyOneOf("AlterConnectionFailoverConnectionOptions", "EnableConnectionFailover", "DisableConnectionFailover", "Primary")) + errs = append(errs, errExactlyOneOf("AlterFailoverConnectionOptions", "EnableConnectionFailover", "DisableConnectionFailover", "Primary")) } return JoinErrors(errs...) } -func (opts *AlterConnectionConnectionOptions) validate() error { +func (opts *AlterConnectionOptions) validate() error { if opts == nil { return ErrNilOptions } var errs []error if !exactlyOneValueSet(opts.Set, opts.Unset) { - errs = append(errs, errExactlyOneOf("AlterConnectionConnectionOptions", "Set", "Unset")) + errs = append(errs, errExactlyOneOf("AlterConnectionOptions", "Set", "Unset")) } if valueSet(opts.Set) { if !anyValueSet(opts.Set.Comment) { - errs = append(errs, errAtLeastOneOf("AlterConnectionConnectionOptions.Set", "Comment")) + errs = append(errs, errAtLeastOneOf("AlterConnectionOptions.Set", "Comment")) } } if valueSet(opts.Unset) { if !anyValueSet(opts.Unset.Comment) { - errs = append(errs, errAtLeastOneOf("AlterConnectionConnectionOptions.Unset", "Comment")) + errs = append(errs, errAtLeastOneOf("AlterConnectionOptions.Unset", "Comment")) } } return JoinErrors(errs...) From 24a2c803e36f1fbeec7c58e3f9482282b4ef9afa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Wed, 23 Oct 2024 11:59:56 +0200 Subject: [PATCH 05/13] integration test --- .../objectassert/connection_snowflake_ext.go | 13 ++++- .../objectassert/connection_snowflake_gen.go | 19 +------ pkg/sdk/connections_def.go | 7 +-- pkg/sdk/connections_gen.go | 10 ++-- pkg/sdk/connections_gen_integration_test.go | 35 ------------ pkg/sdk/connections_gen_test.go | 22 ++++--- pkg/sdk/connections_impl_gen.go | 37 +++++++----- .../connections_gen_integration_test.go | 57 ++++++++++++++++--- 8 files changed, 103 insertions(+), 97 deletions(-) delete mode 100644 pkg/sdk/connections_gen_integration_test.go diff --git a/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_ext.go b/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_ext.go index 5be3983741..2535694276 100644 --- a/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_ext.go +++ b/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_ext.go @@ -12,7 +12,18 @@ func (c *ConnectionAssert) HasFailoverAllowedToAccounts(expected []string) *Conn 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: %v; got: %v", 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 }) diff --git a/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_gen.go b/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_gen.go index 736492a596..d1c0946276 100644 --- a/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_gen.go +++ b/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_gen.go @@ -111,19 +111,6 @@ func (c *ConnectionAssert) HasPrimary(expected string) *ConnectionAssert { return c } -/* -func (c *ConnectionAssert) HasFailoverAllowedToAccounts(expected []string) *ConnectionAssert { - c.AddAssertion(func(t *testing.T, o *sdk.Connection) error { - t.Helper() - if o.FailoverAllowedToAccounts != expected { - return fmt.Errorf("expected failover allowed to accounts: %v; got: %v", expected, o.FailoverAllowedToAccounts) - } - return nil - }) - return c -} -*/ - func (c *ConnectionAssert) HasConnectionUrl(expected string) *ConnectionAssert { c.AddAssertion(func(t *testing.T, o *sdk.Connection) error { t.Helper() @@ -135,11 +122,11 @@ func (c *ConnectionAssert) HasConnectionUrl(expected string) *ConnectionAssert { return c } -func (c *ConnectionAssert) HasOrgnizationName(expected string) *ConnectionAssert { +func (c *ConnectionAssert) HasOrganizationName(expected string) *ConnectionAssert { c.AddAssertion(func(t *testing.T, o *sdk.Connection) error { t.Helper() - if o.OrgnizationName != expected { - return fmt.Errorf("expected orgnization name: %v; got: %v", expected, o.OrgnizationName) + if o.OrganizationName != expected { + return fmt.Errorf("expected organization name: %v; got: %v", expected, o.OrganizationName) } return nil }) diff --git a/pkg/sdk/connections_def.go b/pkg/sdk/connections_def.go index c62821fb01..d6f4ca6003 100644 --- a/pkg/sdk/connections_def.go +++ b/pkg/sdk/connections_def.go @@ -13,8 +13,7 @@ var ConnectionDef = g.NewInterface( "Connections", "Connection", g.KindOfT[AccountObjectIdentifier](), -).CustomOperation( - "Create", +).CreateOperation( "https://docs.snowflake.com/en/sql-reference/sql/create-connection", g.NewQueryStruct("CreateConnection"). Create(). @@ -47,7 +46,6 @@ var ConnectionDef = g.NewInterface( OptionalQueryStructField( "EnableConnectionFailover", g.NewQueryStruct("EnableConnectionFailover"). - // ListQueryStructField("Accounts", enabledFailoverAccounts, g.ListOptions().NoParentheses()). List("Accounts", "ExternalObjectIdentifier", g.ListOptions().NoParentheses()). OptionalSQL("IGNORE EDITION CHECK"), g.KeywordOptions().SQL("ENABLE FAILOVER TO ACCOUNTS"), @@ -56,7 +54,6 @@ var ConnectionDef = g.NewInterface( "DisableConnectionFailover", g.NewQueryStruct("DisableConnectionFailover"). OptionalSQL("TO ACCOUNTS"). - // ListQueryStructField("Accounts", enabledFailoverAccounts, g.ListOptions().NoParentheses()), List("Accounts", "ExternalObjectIdentifier", g.ListOptions().NoParentheses()), g.KeywordOptions().SQL("DISABLE FAILOVER"), ). @@ -122,7 +119,7 @@ var ConnectionDef = g.NewInterface( Field("Primary", "string"). Field("FailoverAllowedToAccounts", "[]string"). Field("ConnectionUrl", "string"). - Field("OrgnizationName", "string"). + Field("OrganizationName", "string"). Field("AccountLocator", "string"), g.NewQueryStruct("ShowConnections"). Show(). diff --git a/pkg/sdk/connections_gen.go b/pkg/sdk/connections_gen.go index 0a914dc6da..fad5295599 100644 --- a/pkg/sdk/connections_gen.go +++ b/pkg/sdk/connections_gen.go @@ -97,7 +97,7 @@ type connectionRow struct { Primary string `db:"primary"` FailoverAllowedToAccounts string `db:"failover_allowed_to_accounts"` ConnectionUrl string `db:"connection_url"` - OrgnizationName string `db:"orgnization_name"` + OrganizationName string `db:"organization_name"` AccountLocator string `db:"account_locator"` } type Connection struct { @@ -110,14 +110,14 @@ type Connection struct { Primary string FailoverAllowedToAccounts []string ConnectionUrl string - OrgnizationName string + OrganizationName string AccountLocator string } -func (v *Connection) ID() AccountObjectIdentifier { - return NewAccountObjectIdentifier(v.Name) +func (c *Connection) ID() AccountObjectIdentifier { + return NewAccountObjectIdentifier(c.Name) } -func (v *Connection) ObjectType() ObjectType { +func (c *Connection) ObjectType() ObjectType { return ObjectTypeConnection } diff --git a/pkg/sdk/connections_gen_integration_test.go b/pkg/sdk/connections_gen_integration_test.go deleted file mode 100644 index 7fe0a30c7c..0000000000 --- a/pkg/sdk/connections_gen_integration_test.go +++ /dev/null @@ -1,35 +0,0 @@ -package sdk - -import "testing" - -func TestInt_Connections(t *testing.T) { - // TODO: prepare common resources - - t.Run("Create", func(t *testing.T) { - // TODO: fill me - }) - - t.Run("CreateReplicated", func(t *testing.T) { - // TODO: fill me - }) - - t.Run("AlterFailover", func(t *testing.T) { - // TODO: fill me - }) - - t.Run("Alter", func(t *testing.T) { - // TODO: fill me - }) - - t.Run("Drop", func(t *testing.T) { - // TODO: fill me - }) - - t.Run("Show", func(t *testing.T) { - // TODO: fill me - }) - - t.Run("ShowByID", func(t *testing.T) { - // TODO: fill me - }) -} diff --git a/pkg/sdk/connections_gen_test.go b/pkg/sdk/connections_gen_test.go index 5579cd143e..6dda680e66 100644 --- a/pkg/sdk/connections_gen_test.go +++ b/pkg/sdk/connections_gen_test.go @@ -2,7 +2,7 @@ package sdk import "testing" -func TestConntections_CreateConnection(t *testing.T) { +func TestConnections_CreateConnection(t *testing.T) { id := randomAccountObjectIdentifier() defaultOpts := func() *CreateConnectionOptions { return &CreateConnectionOptions{ @@ -35,7 +35,7 @@ func TestConntections_CreateConnection(t *testing.T) { }) } -func TestConntections_CreateReplicatedConnection(t *testing.T) { +func TestConnections_CreateReplicatedConnection(t *testing.T) { id := randomAccountObjectIdentifier() externalId := randomExternalObjectIdentifier() defaultOpts := func() *CreateReplicatedConnectionOptions { @@ -79,7 +79,7 @@ func TestConntections_CreateReplicatedConnection(t *testing.T) { }) } -func TestConntections_AlterConnectionFailover(t *testing.T) { +func TestConnections_AlterConnectionFailover(t *testing.T) { id := randomAccountObjectIdentifier() externalId := randomExternalObjectIdentifier() externalIdTwo := randomExternalObjectIdentifier() @@ -98,7 +98,7 @@ func TestConntections_AlterConnectionFailover(t *testing.T) { opts.EnableConnectionFailover = &EnableConnectionFailover{} opts.DisableConnectionFailover = &DisableConnectionFailover{} opts.Primary = &Primary{} - assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("AlterConnectionFailoverConnectionOptions", "EnableConnectionFailover", "DisableConnectionFailover", "Primary")) + assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("AlterFailoverConnectionOptions", "EnableConnectionFailover", "DisableConnectionFailover", "Primary")) }) t.Run("enable connection failover", func(t *testing.T) { @@ -134,7 +134,7 @@ func TestConntections_AlterConnectionFailover(t *testing.T) { }) } -func TestConntections_AlterConnection(t *testing.T) { +func TestConnections_AlterConnection(t *testing.T) { id := randomAccountObjectIdentifier() defaultOpts := func() *AlterConnectionOptions { return &AlterConnectionOptions{ @@ -148,21 +148,19 @@ func TestConntections_AlterConnection(t *testing.T) { }) t.Run("validation: exactly one field from [opts.Set opts.Unset] should be present", func(t *testing.T) { opts := defaultOpts() - opts.Set = &Set{} - opts.Unset = &Unset{} - assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("AlterConnectionConnectionOptions", "Set", "Unset")) + assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("AlterConnectionOptions", "Set", "Unset")) }) t.Run("validation: at least one of the fields [opts.Set.Comment] should be set", func(t *testing.T) { opts := defaultOpts() - opts.Set = &Set{} - assertOptsInvalidJoinedErrors(t, opts, errAtLeastOneOf("AlterConnectionConnectionOptions.Set", "Comment")) + opts.Set = &Set{} + assertOptsInvalidJoinedErrors(t, opts, errAtLeastOneOf("AlterConnectionOptions.Set", "Comment")) }) t.Run("validation: at least one of the fields [opts.Unset.Comment] should be set", func(t *testing.T) { opts := defaultOpts() - opts.Unset = &Unset{} - assertOptsInvalidJoinedErrors(t, opts, errAtLeastOneOf("AlterConnectionConnectionOptions.Unset", "Comment")) + opts.Unset = &Unset{} + assertOptsInvalidJoinedErrors(t, opts, errAtLeastOneOf("AlterConnectionOptions.Unset", "Comment")) }) t.Run("set comment", func(t *testing.T) { diff --git a/pkg/sdk/connections_impl_gen.go b/pkg/sdk/connections_impl_gen.go index f7cad9778d..3160de4287 100644 --- a/pkg/sdk/connections_impl_gen.go +++ b/pkg/sdk/connections_impl_gen.go @@ -2,6 +2,7 @@ package sdk import ( "context" + "strconv" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/collections" ) @@ -48,8 +49,10 @@ func (v *connections) Show(ctx context.Context, request *ShowConnectionRequest) } func (v *connections) ShowByID(ctx context.Context, id AccountObjectIdentifier) (*Connection, error) { - // TODO: adjust request if e.g. LIKE is supported for the resource - connections, err := v.Show(ctx, NewShowConnectionRequest()) + connections, err := v.Show(ctx, NewShowConnectionRequest().WithLike( + Like{ + Pattern: String(id.Name()), + })) if err != nil { return nil, err } @@ -81,27 +84,21 @@ func (r *AlterConnectionFailoverRequest) toOpts() *AlterFailoverConnectionOption } if r.EnableConnectionFailover != nil { - opts.EnableConnectionFailover = &EnableConnectionFailover{ Accounts: r.EnableConnectionFailover.Accounts, IgnoreEditionCheck: r.EnableConnectionFailover.IgnoreEditionCheck, } - } if r.DisableConnectionFailover != nil { - opts.DisableConnectionFailover = &DisableConnectionFailover{ ToAccounts: r.DisableConnectionFailover.ToAccounts, Accounts: r.DisableConnectionFailover.Accounts, } - } if r.Primary != nil { - opts.Primary = &Primary{} - } return opts @@ -114,19 +111,15 @@ func (r *AlterConnectionRequest) toOpts() *AlterConnectionOptions { } if r.Set != nil { - opts.Set = &Set{ Comment: r.Set.Comment, } - } if r.Unset != nil { - opts.Unset = &Unset{ Comment: r.Unset.Comment, } - } return opts @@ -148,6 +141,22 @@ func (r *ShowConnectionRequest) toOpts() *ShowConnectionOptions { } func (r connectionRow) convert() *Connection { - // TODO: Mapping - return &Connection{} + c := &Connection{ + SnowflakeRegion: r.SnowflakeRegion, + CreatedOn: r.CreatedOn, + AccountName: r.AccountName, + Name: r.Name, + Primary: r.Primary, + FailoverAllowedToAccounts: ParseCommaSeparatedStringArray(r.FailoverAllowedToAccounts, false), + ConnectionUrl: r.ConnectionUrl, + OrganizationName: r.OrganizationName, + AccountLocator: r.AccountLocator, + } + b, _ := strconv.ParseBool(r.IsPrimary) + c.IsPrimary = b + if r.Comment.Valid { + c.Comment = String(r.Comment.String) + } + + return c } diff --git a/pkg/sdk/testint/connections_gen_integration_test.go b/pkg/sdk/testint/connections_gen_integration_test.go index cdeb528602..7c6ade7ecc 100644 --- a/pkg/sdk/testint/connections_gen_integration_test.go +++ b/pkg/sdk/testint/connections_gen_integration_test.go @@ -1,27 +1,66 @@ package testint -import "testing" +import ( + "fmt" + "testing" -func TestInt_Conntections(t *testing.T) { + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/stretchr/testify/require" + + assertions "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert/objectassert" +) + +func TestInt_Connections(t *testing.T) { client := testClient(t) - _ = client ctx := testContext(t) - _ = ctx - t.Run("CreateConnection", func(t *testing.T) { + t.Run("Create minimal", func(t *testing.T) { id := testClientHelper().Ids.RandomAccountObjectIdentifier() - _ = id + err := client.Connections.Create(ctx, sdk.NewCreateConnectionRequest(id)) + require.NoError(t, err) + t.Cleanup(testClientHelper().Connection.DropFunc(t, id)) + + orgName, err := client.ContextFunctions.CurrentOrganizationName(ctx) + require.NoError(t, err) + + accountName, err := client.ContextFunctions.CurrentAccountName(ctx) + require.NoError(t, err) + + assertions.AssertThatObject(t, objectassert.Connection(t, id). + HasName(id.Name()). + HasNoComment(). + HasAccountLocator(client.GetAccountLocator()). + HasFailoverAllowedToAccounts( + []string{ + fmt.Sprintf("%s.%s", orgName, accountName), + }, + ). + HasIsPrimary(true), + ) + }) + + t.Run("CreateReplicated", func(t *testing.T) { + // TODO: fill me + }) + + t.Run("AlterFailover", func(t *testing.T) { + // TODO: fill me + }) + + t.Run("Alter", func(t *testing.T) { + // TODO: fill me }) - t.Run("CreateReplicatedConnection", func(t *testing.T) { + t.Run("Drop", func(t *testing.T) { // TODO: fill me }) - t.Run("AlterConnectionFailover", func(t *testing.T) { + t.Run("Show", func(t *testing.T) { // TODO: fill me }) - t.Run("AlterConnection", func(t *testing.T) { + t.Run("ShowByID", func(t *testing.T) { // TODO: fill me }) } From 5c8f84f193fb716e995a410a5ee8b75e69a486cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Thu, 24 Oct 2024 09:00:28 +0200 Subject: [PATCH 06/13] before self-review --- pkg/sdk/connections_def.go | 27 +- pkg/sdk/connections_dto_builders_gen.go | 24 +- pkg/sdk/connections_dto_gen.go | 13 +- pkg/sdk/connections_gen.go | 17 +- pkg/sdk/connections_gen_test.go | 38 ++- pkg/sdk/connections_impl_gen.go | 13 +- .../connections_gen_integration_test.go | 312 +++++++++++++++++- 7 files changed, 358 insertions(+), 86 deletions(-) diff --git a/pkg/sdk/connections_def.go b/pkg/sdk/connections_def.go index d6f4ca6003..7b9c79bc2e 100644 --- a/pkg/sdk/connections_def.go +++ b/pkg/sdk/connections_def.go @@ -6,9 +6,6 @@ import ( g "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator" ) -var enabledFailoverAccounts = g.NewQueryStruct("EnabledFailoverAccounts"). - Text("Account", g.KeywordOptions().NoQuotes()) - var ConnectionDef = g.NewInterface( "Connections", "Connection", @@ -30,23 +27,23 @@ var ConnectionDef = g.NewInterface( SQL("CONNECTION"). IfNotExists(). Name(). - SQL("AS REPLICA OF"). + //SQL("AS REPLICA OF"). // external reference to connection: .. - Identifier("ReplicaOf", g.KindOfT[ExternalObjectIdentifier](), g.IdentifierOptions().Required()). + Identifier("ReplicaOf", g.KindOfT[ExternalObjectIdentifier](), g.IdentifierOptions().Required().SQL("AS REPLICA OF")). OptionalComment(). WithValidation(g.ValidIdentifier, "name"). WithValidation(g.ValidIdentifier, "ReplicaOf"), ).CustomOperation( - "AlterConnectionFailover", + "AlterFailover", "https://docs.snowflake.com/en/sql-reference/sql/alter-connection", - g.NewQueryStruct("AlterConnectionFailover"). + g.NewQueryStruct("AlterFailover"). Alter(). SQL("CONNECTION"). Name(). OptionalQueryStructField( "EnableConnectionFailover", g.NewQueryStruct("EnableConnectionFailover"). - List("Accounts", "ExternalObjectIdentifier", g.ListOptions().NoParentheses()). + List("ToAccounts", "AccountIdentifier", g.ListOptions().NoParentheses()). OptionalSQL("IGNORE EDITION CHECK"), g.KeywordOptions().SQL("ENABLE FAILOVER TO ACCOUNTS"), ). @@ -54,18 +51,12 @@ var ConnectionDef = g.NewInterface( "DisableConnectionFailover", g.NewQueryStruct("DisableConnectionFailover"). OptionalSQL("TO ACCOUNTS"). - List("Accounts", "ExternalObjectIdentifier", g.ListOptions().NoParentheses()), + List("Accounts", "AccountIdentifier", g.ListOptions().NoParentheses()), g.KeywordOptions().SQL("DISABLE FAILOVER"), ). - OptionalQueryStructField( - "Primary", - g.NewQueryStruct("Primary"). - SQL("PRIMARY"), - g.KeywordOptions(), - ). + OptionalSQL("PRIMARY"). WithValidation(g.ExactlyOneValueSet, "EnableConnectionFailover", "DisableConnectionFailover", "Primary"), -).CustomOperation( - "Alter", +).AlterOperation( "https://docs.snowflake.com/en/sql-reference/sql/alter-connection", g.NewQueryStruct("Alter"). Alter(). @@ -107,7 +98,7 @@ var ConnectionDef = g.NewInterface( Field("primary", "string"). Field("failover_allowed_to_accounts", "string"). Field("connection_url", "string"). - Field("orgnization_name", "string"). + Field("organization_name", "string"). Field("account_locator", "string"), g.PlainStruct("Connection"). Field("SnowflakeRegion", "string"). diff --git a/pkg/sdk/connections_dto_builders_gen.go b/pkg/sdk/connections_dto_builders_gen.go index 3cf604d0da..a2a149b69a 100644 --- a/pkg/sdk/connections_dto_builders_gen.go +++ b/pkg/sdk/connections_dto_builders_gen.go @@ -2,6 +2,8 @@ package sdk +import () + func NewCreateConnectionRequest( name AccountObjectIdentifier, ) *CreateConnectionRequest { @@ -40,25 +42,25 @@ func (s *CreateReplicatedConnectionRequest) WithComment(Comment string) *CreateR return s } -func NewAlterConnectionFailoverRequest( +func NewAlterFailoverConnectionRequest( name AccountObjectIdentifier, -) *AlterConnectionFailoverRequest { - s := AlterConnectionFailoverRequest{} +) *AlterFailoverConnectionRequest { + s := AlterFailoverConnectionRequest{} s.name = name return &s } -func (s *AlterConnectionFailoverRequest) WithEnableConnectionFailover(EnableConnectionFailover EnableConnectionFailoverRequest) *AlterConnectionFailoverRequest { +func (s *AlterFailoverConnectionRequest) WithEnableConnectionFailover(EnableConnectionFailover EnableConnectionFailoverRequest) *AlterFailoverConnectionRequest { s.EnableConnectionFailover = &EnableConnectionFailover return s } -func (s *AlterConnectionFailoverRequest) WithDisableConnectionFailover(DisableConnectionFailover DisableConnectionFailoverRequest) *AlterConnectionFailoverRequest { +func (s *AlterFailoverConnectionRequest) WithDisableConnectionFailover(DisableConnectionFailover DisableConnectionFailoverRequest) *AlterFailoverConnectionRequest { s.DisableConnectionFailover = &DisableConnectionFailover return s } -func (s *AlterConnectionFailoverRequest) WithPrimary(Primary PrimaryRequest) *AlterConnectionFailoverRequest { +func (s *AlterFailoverConnectionRequest) WithPrimary(Primary bool) *AlterFailoverConnectionRequest { s.Primary = &Primary return s } @@ -67,8 +69,8 @@ func NewEnableConnectionFailoverRequest() *EnableConnectionFailoverRequest { return &EnableConnectionFailoverRequest{} } -func (s *EnableConnectionFailoverRequest) WithAccounts(Accounts []ExternalObjectIdentifier) *EnableConnectionFailoverRequest { - s.Accounts = Accounts +func (s *EnableConnectionFailoverRequest) WithToAccounts(ToAccounts []AccountIdentifier) *EnableConnectionFailoverRequest { + s.ToAccounts = ToAccounts return s } @@ -86,15 +88,11 @@ func (s *DisableConnectionFailoverRequest) WithToAccounts(ToAccounts bool) *Disa return s } -func (s *DisableConnectionFailoverRequest) WithAccounts(Accounts []ExternalObjectIdentifier) *DisableConnectionFailoverRequest { +func (s *DisableConnectionFailoverRequest) WithAccounts(Accounts []AccountIdentifier) *DisableConnectionFailoverRequest { s.Accounts = Accounts return s } -func NewPrimaryRequest() *PrimaryRequest { - return &PrimaryRequest{} -} - func NewAlterConnectionRequest( name AccountObjectIdentifier, ) *AlterConnectionRequest { diff --git a/pkg/sdk/connections_dto_gen.go b/pkg/sdk/connections_dto_gen.go index e7c77fdf3c..ae49fc8b34 100644 --- a/pkg/sdk/connections_dto_gen.go +++ b/pkg/sdk/connections_dto_gen.go @@ -5,7 +5,7 @@ package sdk var ( _ optionsProvider[CreateConnectionOptions] = new(CreateConnectionRequest) _ optionsProvider[CreateReplicatedConnectionOptions] = new(CreateReplicatedConnectionRequest) - _ optionsProvider[AlterFailoverConnectionOptions] = new(AlterConnectionFailoverRequest) + _ optionsProvider[AlterFailoverConnectionOptions] = new(AlterFailoverConnectionRequest) _ optionsProvider[AlterConnectionOptions] = new(AlterConnectionRequest) _ optionsProvider[DropConnectionOptions] = new(DropConnectionRequest) _ optionsProvider[ShowConnectionOptions] = new(ShowConnectionRequest) @@ -24,24 +24,21 @@ type CreateReplicatedConnectionRequest struct { Comment *string } -type AlterConnectionFailoverRequest struct { +type AlterFailoverConnectionRequest struct { name AccountObjectIdentifier // required EnableConnectionFailover *EnableConnectionFailoverRequest DisableConnectionFailover *DisableConnectionFailoverRequest - Primary *PrimaryRequest + Primary *bool } type EnableConnectionFailoverRequest struct { - Accounts []ExternalObjectIdentifier + ToAccounts []AccountIdentifier IgnoreEditionCheck *bool } type DisableConnectionFailoverRequest struct { ToAccounts *bool - Accounts []ExternalObjectIdentifier -} - -type PrimaryRequest struct { + Accounts []AccountIdentifier } type AlterConnectionRequest struct { diff --git a/pkg/sdk/connections_gen.go b/pkg/sdk/connections_gen.go index fad5295599..508a51676e 100644 --- a/pkg/sdk/connections_gen.go +++ b/pkg/sdk/connections_gen.go @@ -9,7 +9,7 @@ import ( type Connections interface { Create(ctx context.Context, request *CreateConnectionRequest) error CreateReplicated(ctx context.Context, request *CreateReplicatedConnectionRequest) error - AlterFailover(ctx context.Context, request *AlterConnectionFailoverRequest) error + AlterFailover(ctx context.Context, request *AlterFailoverConnectionRequest) error Alter(ctx context.Context, request *AlterConnectionRequest) error Drop(ctx context.Context, request *DropConnectionRequest) error Show(ctx context.Context, request *ShowConnectionRequest) ([]Connection, error) @@ -43,18 +43,15 @@ type AlterFailoverConnectionOptions struct { name AccountObjectIdentifier `ddl:"identifier"` EnableConnectionFailover *EnableConnectionFailover `ddl:"keyword" sql:"ENABLE FAILOVER TO ACCOUNTS"` DisableConnectionFailover *DisableConnectionFailover `ddl:"keyword" sql:"DISABLE FAILOVER"` - Primary *Primary `ddl:"keyword"` + Primary *bool `ddl:"keyword" sql:"PRIMARY"` } type EnableConnectionFailover struct { - Accounts []ExternalObjectIdentifier `ddl:"list,no_parentheses"` - IgnoreEditionCheck *bool `ddl:"keyword" sql:"IGNORE EDITION CHECK"` + ToAccounts []AccountIdentifier `ddl:"list,no_parentheses"` + IgnoreEditionCheck *bool `ddl:"keyword" sql:"IGNORE EDITION CHECK"` } type DisableConnectionFailover struct { - ToAccounts *bool `ddl:"keyword" sql:"TO ACCOUNTS"` - Accounts []ExternalObjectIdentifier `ddl:"list,no_parentheses"` -} -type Primary struct { - primary bool `ddl:"static" sql:"PRIMARY"` + ToAccounts *bool `ddl:"keyword" sql:"TO ACCOUNTS"` + Accounts []AccountIdentifier `ddl:"list,no_parentheses"` } // AlterConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/alter-connection. @@ -119,5 +116,5 @@ func (c *Connection) ID() AccountObjectIdentifier { } func (c *Connection) ObjectType() ObjectType { - return ObjectTypeConnection + return ObjectTypeDatabase } diff --git a/pkg/sdk/connections_gen_test.go b/pkg/sdk/connections_gen_test.go index 6dda680e66..1d596401fb 100644 --- a/pkg/sdk/connections_gen_test.go +++ b/pkg/sdk/connections_gen_test.go @@ -1,6 +1,8 @@ package sdk -import "testing" +import ( + "testing" +) func TestConnections_CreateConnection(t *testing.T) { id := randomAccountObjectIdentifier() @@ -81,8 +83,8 @@ func TestConnections_CreateReplicatedConnection(t *testing.T) { func TestConnections_AlterConnectionFailover(t *testing.T) { id := randomAccountObjectIdentifier() - externalId := randomExternalObjectIdentifier() - externalIdTwo := randomExternalObjectIdentifier() + accountId := NewAccountIdentifier("test_org", "test_acc") + accountIdTwo := NewAccountIdentifier("test_org", "test_acc_two") defaultOpts := func() *AlterFailoverConnectionOptions { return &AlterFailoverConnectionOptions{ name: id, @@ -97,25 +99,37 @@ func TestConnections_AlterConnectionFailover(t *testing.T) { opts := defaultOpts() opts.EnableConnectionFailover = &EnableConnectionFailover{} opts.DisableConnectionFailover = &DisableConnectionFailover{} - opts.Primary = &Primary{} + opts.Primary = Bool(true) assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("AlterFailoverConnectionOptions", "EnableConnectionFailover", "DisableConnectionFailover", "Primary")) }) + t.Run("promote to primary", func(t *testing.T) { + opts := defaultOpts() + opts.Primary = Bool(true) + assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s PRIMARY", id.FullyQualifiedName()) + }) + + t.Run("without promotion to primary", func(t *testing.T) { + opts := defaultOpts() + opts.Primary = Bool(false) + assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s", id.FullyQualifiedName()) + }) + t.Run("enable connection failover", func(t *testing.T) { opts := defaultOpts() opts.EnableConnectionFailover = &EnableConnectionFailover{ - Accounts: []ExternalObjectIdentifier{externalId, externalIdTwo}, + ToAccounts: []AccountIdentifier{accountId, accountIdTwo}, } - assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s ENABLE FAILOVER TO ACCOUNTS %s, %s", id.FullyQualifiedName(), externalId.FullyQualifiedName(), externalIdTwo.FullyQualifiedName()) + assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s ENABLE FAILOVER TO ACCOUNTS %s, %s", id.FullyQualifiedName(), accountId.FullyQualifiedName(), accountIdTwo.FullyQualifiedName()) }) t.Run("enable connection failover with ignore edition check", func(t *testing.T) { opts := defaultOpts() opts.EnableConnectionFailover = &EnableConnectionFailover{ - Accounts: []ExternalObjectIdentifier{externalId, externalIdTwo}, + ToAccounts: []AccountIdentifier{accountId, accountIdTwo}, IgnoreEditionCheck: Bool(true), } - assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s ENABLE FAILOVER TO ACCOUNTS %s, %s IGNORE EDITION CHECK", id.FullyQualifiedName(), externalId.FullyQualifiedName(), externalIdTwo.FullyQualifiedName()) + assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s ENABLE FAILOVER TO ACCOUNTS %s, %s IGNORE EDITION CHECK", id.FullyQualifiedName(), accountId.FullyQualifiedName(), accountIdTwo.FullyQualifiedName()) }) t.Run("disable connection failover", func(t *testing.T) { @@ -128,9 +142,9 @@ func TestConnections_AlterConnectionFailover(t *testing.T) { opts := defaultOpts() opts.DisableConnectionFailover = &DisableConnectionFailover{ ToAccounts: Bool(true), - Accounts: []ExternalObjectIdentifier{externalId, externalIdTwo}, + Accounts: []AccountIdentifier{accountId, accountIdTwo}, } - assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s DISABLE FAILOVER TO ACCOUNTS %s, %s", id.FullyQualifiedName(), externalId.FullyQualifiedName(), externalIdTwo.FullyQualifiedName()) + assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s DISABLE FAILOVER TO ACCOUNTS %s, %s", id.FullyQualifiedName(), accountId.FullyQualifiedName(), accountIdTwo.FullyQualifiedName()) }) } @@ -153,13 +167,13 @@ func TestConnections_AlterConnection(t *testing.T) { t.Run("validation: at least one of the fields [opts.Set.Comment] should be set", func(t *testing.T) { opts := defaultOpts() - opts.Set = &Set{} + opts.Set = &Set{} assertOptsInvalidJoinedErrors(t, opts, errAtLeastOneOf("AlterConnectionOptions.Set", "Comment")) }) t.Run("validation: at least one of the fields [opts.Unset.Comment] should be set", func(t *testing.T) { opts := defaultOpts() - opts.Unset = &Unset{} + opts.Unset = &Unset{} assertOptsInvalidJoinedErrors(t, opts, errAtLeastOneOf("AlterConnectionOptions.Unset", "Comment")) }) diff --git a/pkg/sdk/connections_impl_gen.go b/pkg/sdk/connections_impl_gen.go index 3160de4287..aa4f7f7526 100644 --- a/pkg/sdk/connections_impl_gen.go +++ b/pkg/sdk/connections_impl_gen.go @@ -23,7 +23,7 @@ func (v *connections) CreateReplicated(ctx context.Context, request *CreateRepli return validateAndExec(v.client, ctx, opts) } -func (v *connections) AlterFailover(ctx context.Context, request *AlterConnectionFailoverRequest) error { +func (v *connections) AlterFailover(ctx context.Context, request *AlterFailoverConnectionRequest) error { opts := request.toOpts() return validateAndExec(v.client, ctx, opts) } @@ -78,14 +78,16 @@ func (r *CreateReplicatedConnectionRequest) toOpts() *CreateReplicatedConnection return opts } -func (r *AlterConnectionFailoverRequest) toOpts() *AlterFailoverConnectionOptions { +func (r *AlterFailoverConnectionRequest) toOpts() *AlterFailoverConnectionOptions { opts := &AlterFailoverConnectionOptions{ name: r.name, + + Primary: r.Primary, } if r.EnableConnectionFailover != nil { opts.EnableConnectionFailover = &EnableConnectionFailover{ - Accounts: r.EnableConnectionFailover.Accounts, + ToAccounts: r.EnableConnectionFailover.ToAccounts, IgnoreEditionCheck: r.EnableConnectionFailover.IgnoreEditionCheck, } } @@ -97,10 +99,6 @@ func (r *AlterConnectionFailoverRequest) toOpts() *AlterFailoverConnectionOption } } - if r.Primary != nil { - opts.Primary = &Primary{} - } - return opts } @@ -157,6 +155,5 @@ func (r connectionRow) convert() *Connection { if r.Comment.Valid { c.Comment = String(r.Comment.String) } - return c } diff --git a/pkg/sdk/testint/connections_gen_integration_test.go b/pkg/sdk/testint/connections_gen_integration_test.go index 7c6ade7ecc..ea7deb16f9 100644 --- a/pkg/sdk/testint/connections_gen_integration_test.go +++ b/pkg/sdk/testint/connections_gen_integration_test.go @@ -15,52 +15,330 @@ func TestInt_Connections(t *testing.T) { client := testClient(t) ctx := testContext(t) + orgName, err := client.ContextFunctions.CurrentOrganizationName(ctx) + require.NoError(t, err) + + accountName, err := client.ContextFunctions.CurrentAccountName(ctx) + require.NoError(t, err) + + region, err := client.ContextFunctions.CurrentRegion(ctx) + require.NoError(t, err) + t.Run("Create minimal", func(t *testing.T) { id := testClientHelper().Ids.RandomAccountObjectIdentifier() err := client.Connections.Create(ctx, sdk.NewCreateConnectionRequest(id)) require.NoError(t, err) t.Cleanup(testClientHelper().Connection.DropFunc(t, id)) - orgName, err := client.ContextFunctions.CurrentOrganizationName(ctx) - require.NoError(t, err) + assertions.AssertThatObject(t, objectassert.Connection(t, id). + HasSnowflakeRegion(region). + HasAccountName(accountName). + HasName(id.Name()). + HasNoComment(). + HasIsPrimary(true). + HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, id.Name())). + HasFailoverAllowedToAccounts( + []string{ + fmt.Sprintf("%s.%s", orgName, accountName), + }, + ). + HasOrganizationName(orgName). + HasAccountLocator(client.GetAccountLocator()), + ) + }) - accountName, err := client.ContextFunctions.CurrentAccountName(ctx) + t.Run("Create all options", func(t *testing.T) { + id := testClientHelper().Ids.RandomAccountObjectIdentifier() + err := client.Connections.Create(ctx, sdk.NewCreateConnectionRequest(id). + WithIfNotExists(true). + WithComment("test comment for connection")) require.NoError(t, err) + t.Cleanup(testClientHelper().Connection.DropFunc(t, id)) assertions.AssertThatObject(t, objectassert.Connection(t, id). + HasSnowflakeRegion(region). + HasAccountName(accountName). HasName(id.Name()). - HasNoComment(). - HasAccountLocator(client.GetAccountLocator()). + HasComment("test comment for connection"). + HasIsPrimary(true). + HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, id.Name())). HasFailoverAllowedToAccounts( []string{ fmt.Sprintf("%s.%s", orgName, accountName), }, ). - HasIsPrimary(true), + HasOrganizationName(orgName). + HasAccountLocator(client.GetAccountLocator()), ) }) - t.Run("CreateReplicated", func(t *testing.T) { - // TODO: fill me - }) + // TODO: uncomment when able to set accounts to different regions + // Snowflake error: The connection cannot be failed over to an account in the same region + /* + t.Run("AlterFailover EnableFailover", func(t *testing.T) { + id := testClientHelper().Ids.RandomAccountObjectIdentifier() + secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() - t.Run("AlterFailover", func(t *testing.T) { - // TODO: fill me - }) + primaryConn, connectionCleanup := testClientHelper().Connection.CreateConnection(t, id) + t.Cleanup(connectionCleanup) + + err := client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(primaryConn.ID()). + WithEnableConnectionFailover( + *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( + []sdk.AccountIdentifier{ + secondaryAccountId, + }, + ), + ), + ) + require.NoError(t, err) + + assertions.AssertThatObject(t, objectassert.Connection(t, id). + HasSnowflakeRegion(region). + HasAccountName(accountName). + HasName(id.Name()). + HasNoComment(). + HasIsPrimary(true). + HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, id.Name())). + HasFailoverAllowedToAccounts( + []string{ + fmt.Sprintf("%s.%s", orgName, accountName), + fmt.Sprintf("%s.%s", orgName, secondaryAccountId.Name()), + }, + ). + HasOrganizationName(orgName). + HasAccountLocator(client.GetAccountLocator()), + ) + }) + + t.Run("AlterFailover EnableFailover With Ignore Edittion Check", func(t *testing.T) { + id := testClientHelper().Ids.RandomAccountObjectIdentifier() + secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() + + primaryConn, connectionCleanup := testClientHelper().Connection.CreateConnection(t, id) + t.Cleanup(connectionCleanup) + + err := client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(primaryConn.ID()). + WithEnableConnectionFailover( + *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( + []sdk.AccountIdentifier{ + secondaryAccountId, + }, + ).WithIgnoreEditionCheck(true), + ), + ) + require.NoError(t, err) + + assertions.AssertThatObject(t, objectassert.Connection(t, id). + HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, id.Name())). + HasFailoverAllowedToAccounts( + []string{ + fmt.Sprintf("%s.%s", orgName, accountName), + fmt.Sprintf("%s.%s", orgName, secondaryAccountId.Name()), + }, + ), + ) + + // try to alter enable failover to accoutns list + err = client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(primaryConn.ID()). + WithEnableConnectionFailover( + *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( + []sdk.AccountIdentifier{}, + ), + ), + ) + require.NoError(t, err) + + // assert that list has not been changed + assertions.AssertThatObject(t, objectassert.Connection(t, id). + HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, id.Name())). + HasFailoverAllowedToAccounts( + []string{ + fmt.Sprintf("%s.%s", orgName, accountName), + fmt.Sprintf("%s.%s", orgName, secondaryAccountId.Name()), + }, + ), + ) + }) + + t.Run("AlterFailover DisableFailover", func(t *testing.T) { + id := testClientHelper().Ids.RandomAccountObjectIdentifier() + accountId := testClientHelper().Ids.AccountIdentifierWithLocator() + secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() + + primaryConn, connectionCleanup := testClientHelper().Connection.CreateConnection(t, testClientHelper().Ids.RandomAccountObjectIdentifier()) + t.Cleanup(connectionCleanup) + + // add secondary account to failover list + err := client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(primaryConn.ID()). + WithEnableConnectionFailover( + *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( + []sdk.AccountIdentifier{ + secondaryAccountId, + }, + ), + ), + ) + require.NoError(t, err) + + assertions.AssertThatObject(t, objectassert.Connection(t, primaryConn.ID()). + HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, primaryConn.ID().Name())). + HasFailoverAllowedToAccounts( + []string{ + fmt.Sprintf("%s.%s", orgName, accountName), + fmt.Sprintf("%s.%s", orgName, secondaryAccountId.Name()), + }, + ), + ) + + // create repllication for secondary account + err = client.Connections.CreateReplicated(ctx, sdk.NewCreateReplicatedConnectionRequest(id, sdk.NewExternalObjectIdentifier(accountId, primaryConn.ID()))) + + // assert that it is not a primary connection + assertions.AssertThatObject(t, objectassert.Connection(t, id). + HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, secondaryAccountId.Name(), id.Name())). + HasIsPrimary(false), + ) + + // Promote to primary + err = client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(id). + WithPrimary(true)) + + assertions.AssertThatObject(t, objectassert.Connection(t, id). + HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, secondaryAccountId.Name(), id.Name())). + HasIsPrimary(true), + ) + }) + + t.Run("CreateReplicated", func(t *testing.T) { + id := testClientHelper().Ids.RandomAccountObjectIdentifier() + accountId := testClientHelper().Ids.AccountIdentifierWithLocator() + secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() - t.Run("Alter", func(t *testing.T) { - // TODO: fill me + primaryConn, connectionCleanup := testClientHelper().Connection.CreateConnection(t, testClientHelper().Ids.RandomAccountObjectIdentifier()) + t.Cleanup(connectionCleanup) + + err := client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(primaryConn.ID()). + WithEnableConnectionFailover( + *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( + []sdk.AccountIdentifier{ + secondaryAccountId, + }, + ), + ), + ) + require.NoError(t, err) + + err = client.Connections.CreateReplicated(ctx, sdk.NewCreateReplicatedConnectionRequest(id, sdk.NewExternalObjectIdentifier(accountId, primaryConn.ID()))) + require.NoError(t, err) + t.Cleanup(testClientHelper().Connection.DropFunc(t, id)) + + assertions.AssertThatObject(t, objectassert.Connection(t, id). + HasSnowflakeRegion(region). + HasAccountName(accountName). + HasName(id.Name()). + HasNoComment(). + HasIsPrimary(false). + HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, primaryConn.ID().Name())). + HasFailoverAllowedToAccounts( + []string{ + fmt.Sprintf("%s.%s", orgName, accountName), + fmt.Sprintf("%s.%s", orgName, secondaryAccountId.Name()), + }, + ). + HasOrganizationName(orgName). + HasAccountLocator(client.GetAccountLocator()), + ) + }) + */ + + t.Run("AlterConnection", func(t *testing.T) { + id := testClientHelper().Ids.RandomAccountObjectIdentifier() + _, connectionCleanup := testClientHelper().Connection.CreateConnection(t, id) + t.Cleanup(connectionCleanup) + + // Set + client.Connections.Alter(ctx, sdk.NewAlterConnectionRequest(id). + WithSet(*sdk.NewSetRequest(). + WithComment("new integration test comment"))) + + assertions.AssertThatObject(t, objectassert.Connection(t, id). + HasName(id.Name()). + HasComment("new integration test comment"), + ) + + // Unset + client.Connections.Alter(ctx, sdk.NewAlterConnectionRequest(id). + WithUnset(*sdk.NewUnsetRequest(). + WithComment(true))) + + assertions.AssertThatObject(t, objectassert.Connection(t, id). + HasName(id.Name()). + HasNoComment(), + ) }) t.Run("Drop", func(t *testing.T) { - // TODO: fill me + id := testClientHelper().Ids.RandomAccountObjectIdentifier() + _, connectionCleanup := testClientHelper().Connection.CreateConnection(t, id) + t.Cleanup(connectionCleanup) + + connection, err := client.Connections.ShowByID(ctx, id) + require.NoError(t, err) + require.NotNil(t, connection) + + err = client.Connections.Drop(ctx, sdk.NewDropConnectionRequest(id)) + require.NoError(t, err) + + connection, err = client.Connections.ShowByID(ctx, id) + require.Nil(t, connection) + require.Error(t, err) }) t.Run("Show", func(t *testing.T) { - // TODO: fill me + id1 := testClientHelper().Ids.RandomAccountObjectIdentifier() + id2 := testClientHelper().Ids.RandomAccountObjectIdentifier() + + connection1, connectionCleanup1 := testClientHelper().Connection.CreateConnection(t, id1) + t.Cleanup(connectionCleanup1) + + connection2, connectionCleanup2 := testClientHelper().Connection.CreateConnection(t, id2) + t.Cleanup(connectionCleanup2) + + returnedConnections, err := client.Connections.Show(ctx, sdk.NewShowConnectionRequest()) + require.NoError(t, err) + require.Contains(t, returnedConnections, *connection1) + require.Contains(t, returnedConnections, *connection2) + }) + + t.Run("Show with Like", func(t *testing.T) { + id1 := testClientHelper().Ids.RandomAccountObjectIdentifier() + id2 := testClientHelper().Ids.RandomAccountObjectIdentifier() + + connection1, connectionCleanup1 := testClientHelper().Connection.CreateConnection(t, id1) + t.Cleanup(connectionCleanup1) + + connection2, connectionCleanup2 := testClientHelper().Connection.CreateConnection(t, id2) + t.Cleanup(connectionCleanup2) + + returnedConnections, err := client.Connections.Show(ctx, sdk.NewShowConnectionRequest(). + WithLike(sdk.Like{ + Pattern: sdk.String(id1.Name()), + })) + require.NoError(t, err) + require.Contains(t, returnedConnections, *connection1) + require.NotContains(t, returnedConnections, *connection2) }) t.Run("ShowByID", func(t *testing.T) { - // TODO: fill me + id := testClientHelper().Ids.RandomAccountObjectIdentifier() + + _, connectionCleanup := testClientHelper().Connection.CreateConnection(t, id) + t.Cleanup(connectionCleanup) + + returnedConnection, err := client.Connections.ShowByID(ctx, id) + require.NoError(t, err) + require.Equal(t, id, returnedConnection.ID()) + require.Equal(t, id.Name(), returnedConnection.Name) }) } From a5d59b620606b8bb9563075d1756f4d799beaf2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Thu, 24 Oct 2024 10:36:43 +0200 Subject: [PATCH 07/13] integration tests --- pkg/sdk/connections_gen.go | 5 ++--- pkg/sdk/connections_gen_test.go | 16 +--------------- pkg/sdk/connections_impl_gen.go | 1 + .../testint/connections_gen_integration_test.go | 6 +++--- 4 files changed, 7 insertions(+), 21 deletions(-) diff --git a/pkg/sdk/connections_gen.go b/pkg/sdk/connections_gen.go index 508a51676e..6a944628af 100644 --- a/pkg/sdk/connections_gen.go +++ b/pkg/sdk/connections_gen.go @@ -31,8 +31,7 @@ type CreateReplicatedConnectionOptions struct { connection bool `ddl:"static" sql:"CONNECTION"` IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` name AccountObjectIdentifier `ddl:"identifier"` - asReplicaOf bool `ddl:"static" sql:"AS REPLICA OF"` - ReplicaOf ExternalObjectIdentifier `ddl:"identifier"` + ReplicaOf ExternalObjectIdentifier `ddl:"identifier" sql:"AS REPLICA OF"` Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` } @@ -112,7 +111,7 @@ type Connection struct { } func (c *Connection) ID() AccountObjectIdentifier { - return NewAccountObjectIdentifier(c.Name) + return NewAccountObjectIdentifier(c.Name) } func (c *Connection) ObjectType() ObjectType { diff --git a/pkg/sdk/connections_gen_test.go b/pkg/sdk/connections_gen_test.go index 1d596401fb..ba3ea6700f 100644 --- a/pkg/sdk/connections_gen_test.go +++ b/pkg/sdk/connections_gen_test.go @@ -1,8 +1,6 @@ package sdk -import ( - "testing" -) +import "testing" func TestConnections_CreateConnection(t *testing.T) { id := randomAccountObjectIdentifier() @@ -103,18 +101,6 @@ func TestConnections_AlterConnectionFailover(t *testing.T) { assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("AlterFailoverConnectionOptions", "EnableConnectionFailover", "DisableConnectionFailover", "Primary")) }) - t.Run("promote to primary", func(t *testing.T) { - opts := defaultOpts() - opts.Primary = Bool(true) - assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s PRIMARY", id.FullyQualifiedName()) - }) - - t.Run("without promotion to primary", func(t *testing.T) { - opts := defaultOpts() - opts.Primary = Bool(false) - assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s", id.FullyQualifiedName()) - }) - t.Run("enable connection failover", func(t *testing.T) { opts := defaultOpts() opts.EnableConnectionFailover = &EnableConnectionFailover{ diff --git a/pkg/sdk/connections_impl_gen.go b/pkg/sdk/connections_impl_gen.go index aa4f7f7526..a8ba9a8575 100644 --- a/pkg/sdk/connections_impl_gen.go +++ b/pkg/sdk/connections_impl_gen.go @@ -155,5 +155,6 @@ func (r connectionRow) convert() *Connection { if r.Comment.Valid { c.Comment = String(r.Comment.String) } + return c } diff --git a/pkg/sdk/testint/connections_gen_integration_test.go b/pkg/sdk/testint/connections_gen_integration_test.go index ea7deb16f9..350e8bf164 100644 --- a/pkg/sdk/testint/connections_gen_integration_test.go +++ b/pkg/sdk/testint/connections_gen_integration_test.go @@ -72,7 +72,7 @@ func TestInt_Connections(t *testing.T) { ) }) - // TODO: uncomment when able to set accounts to different regions + // TODO: uncomment when able to change accounts to different regions // Snowflake error: The connection cannot be failed over to an account in the same region /* t.Run("AlterFailover EnableFailover", func(t *testing.T) { @@ -82,7 +82,7 @@ func TestInt_Connections(t *testing.T) { primaryConn, connectionCleanup := testClientHelper().Connection.CreateConnection(t, id) t.Cleanup(connectionCleanup) - err := client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(primaryConn.ID()). + err := client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(id). WithEnableConnectionFailover( *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( []sdk.AccountIdentifier{ @@ -140,7 +140,7 @@ func TestInt_Connections(t *testing.T) { ) // try to alter enable failover to accoutns list - err = client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(primaryConn.ID()). + err = client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(id). WithEnableConnectionFailover( *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( []sdk.AccountIdentifier{}, From f831c022189687a62133fc573a43ee1749f9e285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Thu, 24 Oct 2024 11:54:53 +0200 Subject: [PATCH 08/13] simplified disable connection failover struct --- pkg/acceptance/helpers/connection_client.go | 21 +- pkg/sdk/client.go | 2 +- pkg/sdk/connections_def.go | 10 +- pkg/sdk/connections_dto_builders_gen.go | 8 +- pkg/sdk/connections_dto_gen.go | 7 +- pkg/sdk/connections_gen.go | 10 +- pkg/sdk/connections_gen_test.go | 5 +- pkg/sdk/connections_impl_gen.go | 14 +- pkg/sdk/poc/main.go | 2 +- pkg/sdk/random_test.go | 2 +- .../connections_gen_integration_test.go | 360 +++++++++--------- 11 files changed, 238 insertions(+), 203 deletions(-) diff --git a/pkg/acceptance/helpers/connection_client.go b/pkg/acceptance/helpers/connection_client.go index 5ea629d4e2..80bdfdf1f7 100644 --- a/pkg/acceptance/helpers/connection_client.go +++ b/pkg/acceptance/helpers/connection_client.go @@ -24,7 +24,7 @@ func (c *ConnectionClient) client() sdk.Connections { return c.context.client.Connections } -func (c *ConnectionClient) CreateConnection(t *testing.T, id sdk.AccountObjectIdentifier) (*sdk.Connection, func()) { +func (c *ConnectionClient) Create(t *testing.T, id sdk.AccountObjectIdentifier) (*sdk.Connection, func()) { t.Helper() ctx := context.Background() request := sdk.NewCreateConnectionRequest(id) @@ -35,6 +35,17 @@ func (c *ConnectionClient) CreateConnection(t *testing.T, id sdk.AccountObjectId 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.NewCreateReplicatedConnectionRequest(id, replicaOf) + err := c.client().CreateReplicated(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() @@ -43,6 +54,14 @@ 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() diff --git a/pkg/sdk/client.go b/pkg/sdk/client.go index 7467a1cf59..94b0f9f68a 100644 --- a/pkg/sdk/client.go +++ b/pkg/sdk/client.go @@ -206,7 +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.Connections = &connections{client: c} c.ContextFunctions = &contextFunctions{client: c} c.ConversionFunctions = &conversionFunctions{client: c} c.CortexSearchServices = &cortexSearchServices{client: c} diff --git a/pkg/sdk/connections_def.go b/pkg/sdk/connections_def.go index 7b9c79bc2e..6650f92d6c 100644 --- a/pkg/sdk/connections_def.go +++ b/pkg/sdk/connections_def.go @@ -27,8 +27,6 @@ var ConnectionDef = g.NewInterface( SQL("CONNECTION"). IfNotExists(). Name(). - //SQL("AS REPLICA OF"). - // external reference to connection: .. Identifier("ReplicaOf", g.KindOfT[ExternalObjectIdentifier](), g.IdentifierOptions().Required().SQL("AS REPLICA OF")). OptionalComment(). WithValidation(g.ValidIdentifier, "name"). @@ -50,8 +48,12 @@ var ConnectionDef = g.NewInterface( OptionalQueryStructField( "DisableConnectionFailover", g.NewQueryStruct("DisableConnectionFailover"). - OptionalSQL("TO ACCOUNTS"). - List("Accounts", "AccountIdentifier", g.ListOptions().NoParentheses()), + OptionalQueryStructField( + "ToAccounts", + g.NewQueryStruct("ToAccounts"). + List("Accounts", "AccountIdentifier", g.ListOptions().NoParentheses()), + g.KeywordOptions().SQL("TO ACCOUNTS"), + ), g.KeywordOptions().SQL("DISABLE FAILOVER"), ). OptionalSQL("PRIMARY"). diff --git a/pkg/sdk/connections_dto_builders_gen.go b/pkg/sdk/connections_dto_builders_gen.go index a2a149b69a..c6b924256f 100644 --- a/pkg/sdk/connections_dto_builders_gen.go +++ b/pkg/sdk/connections_dto_builders_gen.go @@ -83,12 +83,16 @@ func NewDisableConnectionFailoverRequest() *DisableConnectionFailoverRequest { return &DisableConnectionFailoverRequest{} } -func (s *DisableConnectionFailoverRequest) WithToAccounts(ToAccounts bool) *DisableConnectionFailoverRequest { +func (s *DisableConnectionFailoverRequest) WithToAccounts(ToAccounts ToAccountsRequest) *DisableConnectionFailoverRequest { s.ToAccounts = &ToAccounts return s } -func (s *DisableConnectionFailoverRequest) WithAccounts(Accounts []AccountIdentifier) *DisableConnectionFailoverRequest { +func NewToAccountsRequest() *ToAccountsRequest { + return &ToAccountsRequest{} +} + +func (s *ToAccountsRequest) WithAccounts(Accounts []AccountIdentifier) *ToAccountsRequest { s.Accounts = Accounts return s } diff --git a/pkg/sdk/connections_dto_gen.go b/pkg/sdk/connections_dto_gen.go index ae49fc8b34..f9be2316f4 100644 --- a/pkg/sdk/connections_dto_gen.go +++ b/pkg/sdk/connections_dto_gen.go @@ -37,8 +37,11 @@ type EnableConnectionFailoverRequest struct { } type DisableConnectionFailoverRequest struct { - ToAccounts *bool - Accounts []AccountIdentifier + ToAccounts *ToAccountsRequest +} + +type ToAccountsRequest struct { + Accounts []AccountIdentifier } type AlterConnectionRequest struct { diff --git a/pkg/sdk/connections_gen.go b/pkg/sdk/connections_gen.go index 6a944628af..9caecd487b 100644 --- a/pkg/sdk/connections_gen.go +++ b/pkg/sdk/connections_gen.go @@ -49,8 +49,10 @@ type EnableConnectionFailover struct { IgnoreEditionCheck *bool `ddl:"keyword" sql:"IGNORE EDITION CHECK"` } type DisableConnectionFailover struct { - ToAccounts *bool `ddl:"keyword" sql:"TO ACCOUNTS"` - Accounts []AccountIdentifier `ddl:"list,no_parentheses"` + ToAccounts *ToAccounts `ddl:"keyword" sql:"TO ACCOUNTS"` +} +type ToAccounts struct { + Accounts []AccountIdentifier `ddl:"list,no_parentheses"` } // AlterConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/alter-connection. @@ -111,9 +113,9 @@ type Connection struct { } func (c *Connection) ID() AccountObjectIdentifier { - return NewAccountObjectIdentifier(c.Name) + return NewAccountObjectIdentifier(c.Name) } func (c *Connection) ObjectType() ObjectType { - return ObjectTypeDatabase + return ObjectTypeConnection } diff --git a/pkg/sdk/connections_gen_test.go b/pkg/sdk/connections_gen_test.go index ba3ea6700f..323d138170 100644 --- a/pkg/sdk/connections_gen_test.go +++ b/pkg/sdk/connections_gen_test.go @@ -58,7 +58,7 @@ func TestConnections_CreateReplicatedConnection(t *testing.T) { t.Run("validation: valid identifier for [opts.ReplicaOf]", func(t *testing.T) { opts := defaultOpts() opts.name = id - opts.ReplicaOf = emptyExtenalObjectIdentifier + opts.ReplicaOf = emptyExternalObjectIdentifier assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) @@ -127,8 +127,7 @@ func TestConnections_AlterConnectionFailover(t *testing.T) { t.Run("disable connection failover to accounts", func(t *testing.T) { opts := defaultOpts() opts.DisableConnectionFailover = &DisableConnectionFailover{ - ToAccounts: Bool(true), - Accounts: []AccountIdentifier{accountId, accountIdTwo}, + ToAccounts: &ToAccounts{Accounts: []AccountIdentifier{accountId, accountIdTwo}}, } assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s DISABLE FAILOVER TO ACCOUNTS %s, %s", id.FullyQualifiedName(), accountId.FullyQualifiedName(), accountIdTwo.FullyQualifiedName()) }) diff --git a/pkg/sdk/connections_impl_gen.go b/pkg/sdk/connections_impl_gen.go index a8ba9a8575..8006112532 100644 --- a/pkg/sdk/connections_impl_gen.go +++ b/pkg/sdk/connections_impl_gen.go @@ -80,8 +80,7 @@ func (r *CreateReplicatedConnectionRequest) toOpts() *CreateReplicatedConnection func (r *AlterFailoverConnectionRequest) toOpts() *AlterFailoverConnectionOptions { opts := &AlterFailoverConnectionOptions{ - name: r.name, - + name: r.name, Primary: r.Primary, } @@ -93,12 +92,13 @@ func (r *AlterFailoverConnectionRequest) toOpts() *AlterFailoverConnectionOption } if r.DisableConnectionFailover != nil { - opts.DisableConnectionFailover = &DisableConnectionFailover{ - ToAccounts: r.DisableConnectionFailover.ToAccounts, - Accounts: r.DisableConnectionFailover.Accounts, + opts.DisableConnectionFailover = &DisableConnectionFailover{} + if r.DisableConnectionFailover.ToAccounts != nil { + opts.DisableConnectionFailover.ToAccounts = &ToAccounts{ + Accounts: r.DisableConnectionFailover.ToAccounts.Accounts, + } } } - return opts } @@ -119,7 +119,6 @@ func (r *AlterConnectionRequest) toOpts() *AlterConnectionOptions { Comment: r.Unset.Comment, } } - return opts } @@ -155,6 +154,5 @@ func (r connectionRow) convert() *Connection { if r.Comment.Valid { c.Comment = String(r.Comment.String) } - return c } diff --git a/pkg/sdk/poc/main.go b/pkg/sdk/poc/main.go index 34e89d5fca..6787167c25 100644 --- a/pkg/sdk/poc/main.go +++ b/pkg/sdk/poc/main.go @@ -46,7 +46,7 @@ var definitionMapping = map[string]*generator.Interface{ "external_volumes_def.go": sdk.ExternalVolumesDef, "authentication_policies_def.go": sdk.AuthenticationPoliciesDef, "secrets_def.go": sdk.SecretsDef, - "connections_def.go": sdk.ConnectionDef, + "connections_def.go": sdk.ConnectionDef, } func main() { diff --git a/pkg/sdk/random_test.go b/pkg/sdk/random_test.go index 6c3660d90a..30a13a94db 100644 --- a/pkg/sdk/random_test.go +++ b/pkg/sdk/random_test.go @@ -10,7 +10,7 @@ var ( // TODO: Add to the generator emptyAccountObjectIdentifier = NewAccountObjectIdentifier("") - emptyExtenalObjectIdentifier = NewExternalObjectIdentifier(NewAccountIdentifier("", ""), NewObjectIdentifierFromFullyQualifiedName("")) + emptyExternalObjectIdentifier = NewExternalObjectIdentifier(NewAccountIdentifier("", ""), NewObjectIdentifierFromFullyQualifiedName("")) emptyDatabaseObjectIdentifier = NewDatabaseObjectIdentifier("", "") emptySchemaObjectIdentifier = NewSchemaObjectIdentifier("", "", "") emptySchemaObjectIdentifierWithArguments = NewSchemaObjectIdentifierWithArguments("", "", "") diff --git a/pkg/sdk/testint/connections_gen_integration_test.go b/pkg/sdk/testint/connections_gen_integration_test.go index 350e8bf164..ca027d682d 100644 --- a/pkg/sdk/testint/connections_gen_integration_test.go +++ b/pkg/sdk/testint/connections_gen_integration_test.go @@ -75,192 +75,199 @@ func TestInt_Connections(t *testing.T) { // TODO: uncomment when able to change accounts to different regions // Snowflake error: The connection cannot be failed over to an account in the same region /* - t.Run("AlterFailover EnableFailover", func(t *testing.T) { - id := testClientHelper().Ids.RandomAccountObjectIdentifier() - secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() - - primaryConn, connectionCleanup := testClientHelper().Connection.CreateConnection(t, id) - t.Cleanup(connectionCleanup) - - err := client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(id). - WithEnableConnectionFailover( - *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( - []sdk.AccountIdentifier{ - secondaryAccountId, + t.Run("AlterFailover EnableFailover", func(t *testing.T) { + id := testClientHelper().Ids.RandomAccountObjectIdentifier() + secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() + + _, connectionCleanup := testClientHelper().Connection.Create(t, id) + t.Cleanup(connectionCleanup) + + err := client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(id). + WithEnableConnectionFailover( + *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( + []sdk.AccountIdentifier{ + secondaryAccountId, + }, + ), + ), + ) + require.NoError(t, err) + + assertions.AssertThatObject(t, objectassert.Connection(t, id). + HasSnowflakeRegion(region). + HasAccountName(accountName). + HasName(id.Name()). + HasNoComment(). + HasIsPrimary(true). + HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, id.Name())). + HasFailoverAllowedToAccounts( + []string{ + fmt.Sprintf("%s.%s", orgName, accountName), + fmt.Sprintf("%s.%s", orgName, secondaryAccountId.Name()), }, + ). + HasOrganizationName(orgName). + HasAccountLocator(client.GetAccountLocator()), + ) + }) + + t.Run("AlterFailover EnableFailover With Ignore Edittion Check", func(t *testing.T) { + id := testClientHelper().Ids.RandomAccountObjectIdentifier() + secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() + + primaryConn, connectionCleanup := testClientHelper().Connection.Create(t, id) + t.Cleanup(connectionCleanup) + + err := client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(primaryConn.ID()). + WithEnableConnectionFailover( + *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( + []sdk.AccountIdentifier{ + secondaryAccountId, + }, + ).WithIgnoreEditionCheck(true), ), - ), - ) - require.NoError(t, err) - - assertions.AssertThatObject(t, objectassert.Connection(t, id). - HasSnowflakeRegion(region). - HasAccountName(accountName). - HasName(id.Name()). - HasNoComment(). - HasIsPrimary(true). - HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, id.Name())). - HasFailoverAllowedToAccounts( - []string{ - fmt.Sprintf("%s.%s", orgName, accountName), - fmt.Sprintf("%s.%s", orgName, secondaryAccountId.Name()), - }, - ). - HasOrganizationName(orgName). - HasAccountLocator(client.GetAccountLocator()), - ) - }) - - t.Run("AlterFailover EnableFailover With Ignore Edittion Check", func(t *testing.T) { - id := testClientHelper().Ids.RandomAccountObjectIdentifier() - secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() - - primaryConn, connectionCleanup := testClientHelper().Connection.CreateConnection(t, id) - t.Cleanup(connectionCleanup) - - err := client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(primaryConn.ID()). - WithEnableConnectionFailover( - *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( - []sdk.AccountIdentifier{ - secondaryAccountId, + ) + require.NoError(t, err) + + assertions.AssertThatObject(t, objectassert.Connection(t, id). + HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, id.Name())). + HasFailoverAllowedToAccounts( + []string{ + fmt.Sprintf("%s.%s", orgName, accountName), + fmt.Sprintf("%s.%s", orgName, secondaryAccountId.Name()), }, - ).WithIgnoreEditionCheck(true), - ), - ) - require.NoError(t, err) - - assertions.AssertThatObject(t, objectassert.Connection(t, id). - HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, id.Name())). - HasFailoverAllowedToAccounts( - []string{ - fmt.Sprintf("%s.%s", orgName, accountName), - fmt.Sprintf("%s.%s", orgName, secondaryAccountId.Name()), - }, - ), - ) - - // try to alter enable failover to accoutns list - err = client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(id). - WithEnableConnectionFailover( - *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( - []sdk.AccountIdentifier{}, ), - ), - ) - require.NoError(t, err) - - // assert that list has not been changed - assertions.AssertThatObject(t, objectassert.Connection(t, id). - HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, id.Name())). - HasFailoverAllowedToAccounts( - []string{ - fmt.Sprintf("%s.%s", orgName, accountName), - fmt.Sprintf("%s.%s", orgName, secondaryAccountId.Name()), - }, - ), - ) - }) - - t.Run("AlterFailover DisableFailover", func(t *testing.T) { - id := testClientHelper().Ids.RandomAccountObjectIdentifier() - accountId := testClientHelper().Ids.AccountIdentifierWithLocator() - secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() - - primaryConn, connectionCleanup := testClientHelper().Connection.CreateConnection(t, testClientHelper().Ids.RandomAccountObjectIdentifier()) - t.Cleanup(connectionCleanup) - - // add secondary account to failover list - err := client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(primaryConn.ID()). - WithEnableConnectionFailover( - *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( - []sdk.AccountIdentifier{ - secondaryAccountId, + ) + + // try to alter enable failover to accoutns list + err = client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(id). + WithEnableConnectionFailover( + *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( + []sdk.AccountIdentifier{}, + ), + ), + ) + require.NoError(t, err) + + // assert that list has not been changed + assertions.AssertThatObject(t, objectassert.Connection(t, id). + HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, id.Name())). + HasFailoverAllowedToAccounts( + []string{ + fmt.Sprintf("%s.%s", orgName, accountName), + fmt.Sprintf("%s.%s", orgName, secondaryAccountId.Name()), }, ), - ), - ) - require.NoError(t, err) - - assertions.AssertThatObject(t, objectassert.Connection(t, primaryConn.ID()). - HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, primaryConn.ID().Name())). - HasFailoverAllowedToAccounts( - []string{ - fmt.Sprintf("%s.%s", orgName, accountName), - fmt.Sprintf("%s.%s", orgName, secondaryAccountId.Name()), - }, - ), - ) - - // create repllication for secondary account - err = client.Connections.CreateReplicated(ctx, sdk.NewCreateReplicatedConnectionRequest(id, sdk.NewExternalObjectIdentifier(accountId, primaryConn.ID()))) - - // assert that it is not a primary connection - assertions.AssertThatObject(t, objectassert.Connection(t, id). - HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, secondaryAccountId.Name(), id.Name())). - HasIsPrimary(false), - ) - - // Promote to primary - err = client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(id). - WithPrimary(true)) - - assertions.AssertThatObject(t, objectassert.Connection(t, id). - HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, secondaryAccountId.Name(), id.Name())). - HasIsPrimary(true), - ) - }) - - t.Run("CreateReplicated", func(t *testing.T) { - id := testClientHelper().Ids.RandomAccountObjectIdentifier() - accountId := testClientHelper().Ids.AccountIdentifierWithLocator() - secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() - - primaryConn, connectionCleanup := testClientHelper().Connection.CreateConnection(t, testClientHelper().Ids.RandomAccountObjectIdentifier()) - t.Cleanup(connectionCleanup) - - err := client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(primaryConn.ID()). - WithEnableConnectionFailover( - *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( - []sdk.AccountIdentifier{ - secondaryAccountId, + ) + }) + + t.Run("AlterFailover DisableFailover", func(t *testing.T) { + id := testClientHelper().Ids.RandomAccountObjectIdentifier() + accountId := testClientHelper().Ids.AccountIdentifierWithLocator() + secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() + + primaryConn, connectionCleanup := testClientHelper().Connection.Create(t, testClientHelper().Ids.RandomAccountObjectIdentifier()) + t.Cleanup(connectionCleanup) + + // Add secondary account to failover list + err := client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(primaryConn.ID()). + WithEnableConnectionFailover( + *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( + []sdk.AccountIdentifier{ + secondaryAccountId, + }, + ), + ), + ) + require.NoError(t, err) + + // Disable promotion of this connection + err = client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(primaryConn.ID()). + WithDisableConnectionFailover(*sdk.NewDisableConnectionFailoverRequest())) + require.NoError(t, err) + + assertions.AssertThatObject(t, objectassert.Connection(t, primaryConn.ID()). + HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, primaryConn.ID().Name())). + HasFailoverAllowedToAccounts( + []string{ + fmt.Sprintf("%s.%s", orgName, accountName), + fmt.Sprintf("%s.%s", orgName, secondaryAccountId.Name()), }, ), - ), - ) - require.NoError(t, err) - - err = client.Connections.CreateReplicated(ctx, sdk.NewCreateReplicatedConnectionRequest(id, sdk.NewExternalObjectIdentifier(accountId, primaryConn.ID()))) - require.NoError(t, err) - t.Cleanup(testClientHelper().Connection.DropFunc(t, id)) - - assertions.AssertThatObject(t, objectassert.Connection(t, id). - HasSnowflakeRegion(region). - HasAccountName(accountName). - HasName(id.Name()). - HasNoComment(). - HasIsPrimary(false). - HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, primaryConn.ID().Name())). - HasFailoverAllowedToAccounts( - []string{ - fmt.Sprintf("%s.%s", orgName, accountName), - fmt.Sprintf("%s.%s", orgName, secondaryAccountId.Name()), - }, - ). - HasOrganizationName(orgName). - HasAccountLocator(client.GetAccountLocator()), - ) - }) + ) + + // Create repllication for secondary account + err = client.Connections.CreateReplicated(ctx, sdk.NewCreateReplicatedConnectionRequest(id, sdk.NewExternalObjectIdentifier(accountId, primaryConn.ID()))) + + // Assert that it is not a primary connection + assertions.AssertThatObject(t, objectassert.Connection(t, id). + HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, secondaryAccountId.Name(), id.Name())). + HasIsPrimary(false), + ) + + // Try to promote to primary + err = client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(id). + WithPrimary(true)) + + // Assert tha promotion has been disabled + assertions.AssertThatObject(t, objectassert.Connection(t, id). + HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, secondaryAccountId.Name(), id.Name())). + HasIsPrimary(false), + ) + }) + + t.Run("CreateReplicated", func(t *testing.T) { + id := testClientHelper().Ids.RandomAccountObjectIdentifier() + accountId := testClientHelper().Ids.AccountIdentifierWithLocator() + secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() + + primaryConn, connectionCleanup := testClientHelper().Connection.Create(t, testClientHelper().Ids.RandomAccountObjectIdentifier()) + t.Cleanup(connectionCleanup) + + err := client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(primaryConn.ID()). + WithEnableConnectionFailover( + *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( + []sdk.AccountIdentifier{ + secondaryAccountId, + }, + ), + ), + ) + require.NoError(t, err) + + err = client.Connections.CreateReplicated(ctx, sdk.NewCreateReplicatedConnectionRequest(id, sdk.NewExternalObjectIdentifier(accountId, primaryConn.ID()))) + require.NoError(t, err) + t.Cleanup(testClientHelper().Connection.DropFunc(t, id)) + + assertions.AssertThatObject(t, objectassert.Connection(t, id). + HasSnowflakeRegion(region). + HasAccountName(accountName). + HasName(id.Name()). + HasNoComment(). + HasIsPrimary(false). + HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, primaryConn.ID().Name())). + HasFailoverAllowedToAccounts( + []string{ + fmt.Sprintf("%s.%s", orgName, accountName), + fmt.Sprintf("%s.%s", orgName, secondaryAccountId.Name()), + }, + ). + HasOrganizationName(orgName). + HasAccountLocator(client.GetAccountLocator()), + ) + }) */ t.Run("AlterConnection", func(t *testing.T) { id := testClientHelper().Ids.RandomAccountObjectIdentifier() - _, connectionCleanup := testClientHelper().Connection.CreateConnection(t, id) + _, connectionCleanup := testClientHelper().Connection.Create(t, id) t.Cleanup(connectionCleanup) // Set - client.Connections.Alter(ctx, sdk.NewAlterConnectionRequest(id). + err := client.Connections.Alter(ctx, sdk.NewAlterConnectionRequest(id). WithSet(*sdk.NewSetRequest(). WithComment("new integration test comment"))) + require.NoError(t, err) assertions.AssertThatObject(t, objectassert.Connection(t, id). HasName(id.Name()). @@ -268,9 +275,10 @@ func TestInt_Connections(t *testing.T) { ) // Unset - client.Connections.Alter(ctx, sdk.NewAlterConnectionRequest(id). + err = client.Connections.Alter(ctx, sdk.NewAlterConnectionRequest(id). WithUnset(*sdk.NewUnsetRequest(). WithComment(true))) + require.NoError(t, err) assertions.AssertThatObject(t, objectassert.Connection(t, id). HasName(id.Name()). @@ -280,7 +288,7 @@ func TestInt_Connections(t *testing.T) { t.Run("Drop", func(t *testing.T) { id := testClientHelper().Ids.RandomAccountObjectIdentifier() - _, connectionCleanup := testClientHelper().Connection.CreateConnection(t, id) + _, connectionCleanup := testClientHelper().Connection.Create(t, id) t.Cleanup(connectionCleanup) connection, err := client.Connections.ShowByID(ctx, id) @@ -299,10 +307,10 @@ func TestInt_Connections(t *testing.T) { id1 := testClientHelper().Ids.RandomAccountObjectIdentifier() id2 := testClientHelper().Ids.RandomAccountObjectIdentifier() - connection1, connectionCleanup1 := testClientHelper().Connection.CreateConnection(t, id1) + connection1, connectionCleanup1 := testClientHelper().Connection.Create(t, id1) t.Cleanup(connectionCleanup1) - connection2, connectionCleanup2 := testClientHelper().Connection.CreateConnection(t, id2) + connection2, connectionCleanup2 := testClientHelper().Connection.Create(t, id2) t.Cleanup(connectionCleanup2) returnedConnections, err := client.Connections.Show(ctx, sdk.NewShowConnectionRequest()) @@ -315,10 +323,10 @@ func TestInt_Connections(t *testing.T) { id1 := testClientHelper().Ids.RandomAccountObjectIdentifier() id2 := testClientHelper().Ids.RandomAccountObjectIdentifier() - connection1, connectionCleanup1 := testClientHelper().Connection.CreateConnection(t, id1) + connection1, connectionCleanup1 := testClientHelper().Connection.Create(t, id1) t.Cleanup(connectionCleanup1) - connection2, connectionCleanup2 := testClientHelper().Connection.CreateConnection(t, id2) + connection2, connectionCleanup2 := testClientHelper().Connection.Create(t, id2) t.Cleanup(connectionCleanup2) returnedConnections, err := client.Connections.Show(ctx, sdk.NewShowConnectionRequest(). @@ -333,7 +341,7 @@ func TestInt_Connections(t *testing.T) { t.Run("ShowByID", func(t *testing.T) { id := testClientHelper().Ids.RandomAccountObjectIdentifier() - _, connectionCleanup := testClientHelper().Connection.CreateConnection(t, id) + _, connectionCleanup := testClientHelper().Connection.Create(t, id) t.Cleanup(connectionCleanup) returnedConnection, err := client.Connections.ShowByID(ctx, id) From 1dee95b7f0c7803204b86d443392fd6e619f1612 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Fri, 25 Oct 2024 15:47:14 +0200 Subject: [PATCH 09/13] adjusted to comments from review --- .../objectassert/connection_snowflake_ext.go | 12 + pkg/acceptance/helpers/connection_client.go | 12 +- pkg/sdk/connections_def.go | 76 ++-- pkg/sdk/connections_dto_builders_gen.go | 80 ++-- pkg/sdk/connections_dto_gen.go | 33 +- pkg/sdk/connections_gen.go | 36 +- pkg/sdk/connections_gen_test.go | 128 +++--- pkg/sdk/connections_impl_gen.go | 48 +-- pkg/sdk/connections_validations_gen.go | 34 +- pkg/sdk/random_test.go | 4 + .../connections_gen_integration_test.go | 386 ++++++++---------- 11 files changed, 350 insertions(+), 499 deletions(-) diff --git a/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_ext.go b/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_ext.go index 2535694276..5eb76cb0fe 100644 --- a/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_ext.go +++ b/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_ext.go @@ -29,3 +29,15 @@ 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 +} diff --git a/pkg/acceptance/helpers/connection_client.go b/pkg/acceptance/helpers/connection_client.go index 80bdfdf1f7..5ea271f064 100644 --- a/pkg/acceptance/helpers/connection_client.go +++ b/pkg/acceptance/helpers/connection_client.go @@ -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) @@ -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() diff --git a/pkg/sdk/connections_def.go b/pkg/sdk/connections_def.go index 6650f92d6c..45a87feec4 100644 --- a/pkg/sdk/connections_def.go +++ b/pkg/sdk/connections_def.go @@ -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(). 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( @@ -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"). @@ -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"). @@ -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"). - 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"). diff --git a/pkg/sdk/connections_dto_builders_gen.go b/pkg/sdk/connections_dto_builders_gen.go index c6b924256f..30d75649c0 100644 --- a/pkg/sdk/connections_dto_builders_gen.go +++ b/pkg/sdk/connections_dto_builders_gen.go @@ -2,8 +2,6 @@ package sdk -import () - func NewCreateConnectionRequest( name AccountObjectIdentifier, ) *CreateConnectionRequest { @@ -17,51 +15,59 @@ func (s *CreateConnectionRequest) WithIfNotExists(IfNotExists bool) *CreateConne return s } +func (s *CreateConnectionRequest) WithAsReplicaOf(AsReplicaOf AsReplicaOfRequest) *CreateConnectionRequest { + s.AsReplicaOf = &AsReplicaOf + return s +} + func (s *CreateConnectionRequest) WithComment(Comment string) *CreateConnectionRequest { s.Comment = &Comment return s } -func NewCreateReplicatedConnectionRequest( +func NewAsReplicaOfRequest( + AsReplicaOf ExternalObjectIdentifier, +) *AsReplicaOfRequest { + s := AsReplicaOfRequest{} + s.AsReplicaOf = AsReplicaOf + return &s +} + +func NewAlterConnectionRequest( name AccountObjectIdentifier, - ReplicaOf ExternalObjectIdentifier, -) *CreateReplicatedConnectionRequest { - s := CreateReplicatedConnectionRequest{} +) *AlterConnectionRequest { + s := AlterConnectionRequest{} s.name = name - s.ReplicaOf = ReplicaOf return &s } -func (s *CreateReplicatedConnectionRequest) WithIfNotExists(IfNotExists bool) *CreateReplicatedConnectionRequest { - s.IfNotExists = &IfNotExists +func (s *AlterConnectionRequest) WithIfExists(IfExists bool) *AlterConnectionRequest { + s.IfExists = &IfExists return s } -func (s *CreateReplicatedConnectionRequest) WithComment(Comment string) *CreateReplicatedConnectionRequest { - s.Comment = &Comment +func (s *AlterConnectionRequest) WithEnableConnectionFailover(EnableConnectionFailover EnableConnectionFailoverRequest) *AlterConnectionRequest { + s.EnableConnectionFailover = &EnableConnectionFailover return s } -func NewAlterFailoverConnectionRequest( - name AccountObjectIdentifier, -) *AlterFailoverConnectionRequest { - s := AlterFailoverConnectionRequest{} - s.name = name - return &s +func (s *AlterConnectionRequest) WithDisableConnectionFailover(DisableConnectionFailover DisableConnectionFailoverRequest) *AlterConnectionRequest { + s.DisableConnectionFailover = &DisableConnectionFailover + return s } -func (s *AlterFailoverConnectionRequest) WithEnableConnectionFailover(EnableConnectionFailover EnableConnectionFailoverRequest) *AlterFailoverConnectionRequest { - s.EnableConnectionFailover = &EnableConnectionFailover +func (s *AlterConnectionRequest) WithPrimary(Primary bool) *AlterConnectionRequest { + s.Primary = &Primary return s } -func (s *AlterFailoverConnectionRequest) WithDisableConnectionFailover(DisableConnectionFailover DisableConnectionFailoverRequest) *AlterFailoverConnectionRequest { - s.DisableConnectionFailover = &DisableConnectionFailover +func (s *AlterConnectionRequest) WithSet(Set SetRequest) *AlterConnectionRequest { + s.Set = &Set return s } -func (s *AlterFailoverConnectionRequest) WithPrimary(Primary bool) *AlterFailoverConnectionRequest { - s.Primary = &Primary +func (s *AlterConnectionRequest) WithUnset(Unset UnsetRequest) *AlterConnectionRequest { + s.Unset = &Unset return s } @@ -74,11 +80,6 @@ func (s *EnableConnectionFailoverRequest) WithToAccounts(ToAccounts []AccountIde return s } -func (s *EnableConnectionFailoverRequest) WithIgnoreEditionCheck(IgnoreEditionCheck bool) *EnableConnectionFailoverRequest { - s.IgnoreEditionCheck = &IgnoreEditionCheck - return s -} - func NewDisableConnectionFailoverRequest() *DisableConnectionFailoverRequest { return &DisableConnectionFailoverRequest{} } @@ -97,29 +98,6 @@ func (s *ToAccountsRequest) WithAccounts(Accounts []AccountIdentifier) *ToAccoun return s } -func NewAlterConnectionRequest( - name AccountObjectIdentifier, -) *AlterConnectionRequest { - s := AlterConnectionRequest{} - s.name = name - return &s -} - -func (s *AlterConnectionRequest) WithIfExists(IfExists bool) *AlterConnectionRequest { - s.IfExists = &IfExists - return s -} - -func (s *AlterConnectionRequest) WithSet(Set SetRequest) *AlterConnectionRequest { - s.Set = &Set - return s -} - -func (s *AlterConnectionRequest) WithUnset(Unset UnsetRequest) *AlterConnectionRequest { - s.Unset = &Unset - return s -} - func NewSetRequest() *SetRequest { return &SetRequest{} } diff --git a/pkg/sdk/connections_dto_gen.go b/pkg/sdk/connections_dto_gen.go index f9be2316f4..2225137072 100644 --- a/pkg/sdk/connections_dto_gen.go +++ b/pkg/sdk/connections_dto_gen.go @@ -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 { @@ -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 } diff --git a/pkg/sdk/connections_gen.go b/pkg/sdk/connections_gen.go index 9caecd487b..9006c9c2a4 100644 --- a/pkg/sdk/connections_gen.go +++ b/pkg/sdk/connections_gen.go @@ -8,8 +8,6 @@ import ( type Connections interface { Create(ctx context.Context, request *CreateConnectionRequest) error - CreateReplicated(ctx context.Context, request *CreateReplicatedConnectionRequest) error - AlterFailover(ctx context.Context, request *AlterFailoverConnectionRequest) error Alter(ctx context.Context, request *AlterConnectionRequest) error Drop(ctx context.Context, request *DropConnectionRequest) error Show(ctx context.Context, request *ShowConnectionRequest) ([]Connection, error) @@ -22,31 +20,27 @@ type CreateConnectionOptions struct { connection bool `ddl:"static" sql:"CONNECTION"` IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` name AccountObjectIdentifier `ddl:"identifier"` + AsReplicaOf *AsReplicaOf `ddl:"identifier"` Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` } - -// CreateReplicatedConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-connection. -type CreateReplicatedConnectionOptions struct { - create bool `ddl:"static" sql:"CREATE"` - connection bool `ddl:"static" sql:"CONNECTION"` - IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` - name AccountObjectIdentifier `ddl:"identifier"` - ReplicaOf ExternalObjectIdentifier `ddl:"identifier" sql:"AS REPLICA OF"` - Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` +type AsReplicaOf struct { + AsReplicaOf ExternalObjectIdentifier `ddl:"identifier" sql:"AS REPLICA OF"` } -// AlterFailoverConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/alter-connection. -type AlterFailoverConnectionOptions struct { +// AlterConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/alter-connection. +type AlterConnectionOptions struct { alter bool `ddl:"static" sql:"ALTER"` connection bool `ddl:"static" sql:"CONNECTION"` + IfExists *bool `ddl:"keyword" sql:"IF EXISTS"` name AccountObjectIdentifier `ddl:"identifier"` EnableConnectionFailover *EnableConnectionFailover `ddl:"keyword" sql:"ENABLE FAILOVER TO ACCOUNTS"` DisableConnectionFailover *DisableConnectionFailover `ddl:"keyword" sql:"DISABLE FAILOVER"` Primary *bool `ddl:"keyword" sql:"PRIMARY"` + Set *Set `ddl:"keyword" sql:"SET"` + Unset *Unset `ddl:"keyword" sql:"UNSET"` } type EnableConnectionFailover struct { - ToAccounts []AccountIdentifier `ddl:"list,no_parentheses"` - IgnoreEditionCheck *bool `ddl:"keyword" sql:"IGNORE EDITION CHECK"` + ToAccounts []AccountIdentifier `ddl:"list,no_parentheses"` } type DisableConnectionFailover struct { ToAccounts *ToAccounts `ddl:"keyword" sql:"TO ACCOUNTS"` @@ -54,16 +48,6 @@ type DisableConnectionFailover struct { type ToAccounts struct { Accounts []AccountIdentifier `ddl:"list,no_parentheses"` } - -// AlterConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/alter-connection. -type AlterConnectionOptions struct { - alter bool `ddl:"static" sql:"ALTER"` - connection bool `ddl:"static" sql:"CONNECTION"` - IfExists *bool `ddl:"keyword" sql:"IF EXISTS"` - name AccountObjectIdentifier `ddl:"identifier"` - Set *Set `ddl:"keyword" sql:"SET"` - Unset *Unset `ddl:"keyword" sql:"UNSET"` -} type Set struct { Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` } @@ -86,6 +70,7 @@ type ShowConnectionOptions struct { Like *Like `ddl:"keyword" sql:"LIKE"` } type connectionRow struct { + RegionGroup sql.NullString `db:"region_group"` SnowflakeRegion string `db:"snowflake_region"` CreatedOn time.Time `db:"created_on"` AccountName string `db:"account_name"` @@ -99,6 +84,7 @@ type connectionRow struct { AccountLocator string `db:"account_locator"` } type Connection struct { + RegionGroup *string SnowflakeRegion string CreatedOn time.Time AccountName string diff --git a/pkg/sdk/connections_gen_test.go b/pkg/sdk/connections_gen_test.go index 323d138170..797beb8ee2 100644 --- a/pkg/sdk/connections_gen_test.go +++ b/pkg/sdk/connections_gen_test.go @@ -2,7 +2,7 @@ package sdk import "testing" -func TestConnections_CreateConnection(t *testing.T) { +func TestConnections_Create(t *testing.T) { id := randomAccountObjectIdentifier() defaultOpts := func() *CreateConnectionOptions { return &CreateConnectionOptions{ @@ -14,9 +14,17 @@ func TestConnections_CreateConnection(t *testing.T) { var opts *CreateConnectionOptions = nil assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) + t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { opts := defaultOpts() - opts.name = emptyAccountObjectIdentifier + opts.name = invalidAccountObjectIdentifier + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) + }) + + t.Run("validation: valid identifier for [opts.ReplicaOf]", func(t *testing.T) { + opts := defaultOpts() + opts.name = id + opts.AsReplicaOf = &AsReplicaOf{emptyExternalObjectIdentifier} assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) @@ -33,133 +41,89 @@ func TestConnections_CreateConnection(t *testing.T) { opts.Comment = String("comment") assertOptsValidAndSQLEquals(t, opts, "CREATE CONNECTION IF NOT EXISTS %s COMMENT = 'comment'", id.FullyQualifiedName()) }) -} - -func TestConnections_CreateReplicatedConnection(t *testing.T) { - id := randomAccountObjectIdentifier() - externalId := randomExternalObjectIdentifier() - defaultOpts := func() *CreateReplicatedConnectionOptions { - return &CreateReplicatedConnectionOptions{ - name: id, - } - } - - t.Run("validation: nil options", func(t *testing.T) { - var opts *CreateReplicatedConnectionOptions = nil - assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) - }) - t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { - opts := defaultOpts() - opts.name = emptyAccountObjectIdentifier - opts.ReplicaOf = externalId - assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) - }) - - t.Run("validation: valid identifier for [opts.ReplicaOf]", func(t *testing.T) { - opts := defaultOpts() - opts.name = id - opts.ReplicaOf = emptyExternalObjectIdentifier - assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) - }) - t.Run("basic", func(t *testing.T) { + t.Run("as replica of", func(t *testing.T) { + externalId := randomExternalObjectIdentifier() opts := defaultOpts() opts.name = id - opts.ReplicaOf = externalId + opts.AsReplicaOf = &AsReplicaOf{externalId} assertOptsValidAndSQLEquals(t, opts, "CREATE CONNECTION %s AS REPLICA OF %s", id.FullyQualifiedName(), externalId.FullyQualifiedName()) }) - t.Run("all options", func(t *testing.T) { + t.Run("as replica of - all options", func(t *testing.T) { + externalId := randomExternalObjectIdentifier() opts := defaultOpts() opts.name = id opts.IfNotExists = Bool(true) - opts.ReplicaOf = externalId + opts.AsReplicaOf = &AsReplicaOf{externalId} opts.Comment = String("comment") assertOptsValidAndSQLEquals(t, opts, "CREATE CONNECTION IF NOT EXISTS %s AS REPLICA OF %s COMMENT = 'comment'", id.FullyQualifiedName(), externalId.FullyQualifiedName()) }) } -func TestConnections_AlterConnectionFailover(t *testing.T) { +func TestConnections_Alter(t *testing.T) { id := randomAccountObjectIdentifier() - accountId := NewAccountIdentifier("test_org", "test_acc") - accountIdTwo := NewAccountIdentifier("test_org", "test_acc_two") - defaultOpts := func() *AlterFailoverConnectionOptions { - return &AlterFailoverConnectionOptions{ + defaultOpts := func() *AlterConnectionOptions { + return &AlterConnectionOptions{ name: id, } } t.Run("validation: nil options", func(t *testing.T) { - var opts *AlterFailoverConnectionOptions = nil + var opts *AlterConnectionOptions = nil assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) - t.Run("validation: exactly one field from [opts.EnableConnectionFailover opts.DisableConnectionFailover opts.Primary] should be present", func(t *testing.T) { + t.Run("validation: exactly one field from [opts.EnableConnectionFailover opts.DisableConnectionFailover opts.Primary opts.Set opts.Unset] should be present", func(t *testing.T) { opts := defaultOpts() opts.EnableConnectionFailover = &EnableConnectionFailover{} opts.DisableConnectionFailover = &DisableConnectionFailover{} opts.Primary = Bool(true) - assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("AlterFailoverConnectionOptions", "EnableConnectionFailover", "DisableConnectionFailover", "Primary")) + opts.Set = &Set{} + opts.Unset = &Unset{} + assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("AlterConnectionOptions", "EnableConnectionFailover", "DisableConnectionFailover", "Primary", "Set", "Unset")) }) - t.Run("enable connection failover", func(t *testing.T) { + t.Run("validation: at least one of the fields [opts.Set.Comment] should be set", func(t *testing.T) { opts := defaultOpts() - opts.EnableConnectionFailover = &EnableConnectionFailover{ - ToAccounts: []AccountIdentifier{accountId, accountIdTwo}, - } - assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s ENABLE FAILOVER TO ACCOUNTS %s, %s", id.FullyQualifiedName(), accountId.FullyQualifiedName(), accountIdTwo.FullyQualifiedName()) + opts.Set = &Set{} + assertOptsInvalidJoinedErrors(t, opts, errAtLeastOneOf("AlterConnectionOptions.Set", "Comment")) + }) + + t.Run("validation: at least one of the fields [opts.Unset.Comment] should be set", func(t *testing.T) { + opts := defaultOpts() + opts.Unset = &Unset{} + assertOptsInvalidJoinedErrors(t, opts, errAtLeastOneOf("AlterConnectionOptions.Unset", "Comment")) }) - t.Run("enable connection failover with ignore edition check", func(t *testing.T) { + t.Run("alter enable failover to accounts", func(t *testing.T) { + accountIdentifier := randomAccountIdentifier() + secondAccountIdentifier := randomAccountIdentifier() opts := defaultOpts() opts.EnableConnectionFailover = &EnableConnectionFailover{ - ToAccounts: []AccountIdentifier{accountId, accountIdTwo}, - IgnoreEditionCheck: Bool(true), + ToAccounts: []AccountIdentifier{accountIdentifier, secondAccountIdentifier}, } - assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s ENABLE FAILOVER TO ACCOUNTS %s, %s IGNORE EDITION CHECK", id.FullyQualifiedName(), accountId.FullyQualifiedName(), accountIdTwo.FullyQualifiedName()) + assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s ENABLE FAILOVER TO ACCOUNTS %s, %s", id.FullyQualifiedName(), accountIdentifier.FullyQualifiedName(), secondAccountIdentifier.FullyQualifiedName()) }) - t.Run("disable connection failover", func(t *testing.T) { + t.Run("alter disable failover to all accounts", func(t *testing.T) { opts := defaultOpts() opts.DisableConnectionFailover = &DisableConnectionFailover{} assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s DISABLE FAILOVER", id.FullyQualifiedName()) }) - t.Run("disable connection failover to accounts", func(t *testing.T) { + t.Run("alter disable failover to accounts", func(t *testing.T) { + accountIdentifier := randomAccountIdentifier() opts := defaultOpts() opts.DisableConnectionFailover = &DisableConnectionFailover{ - ToAccounts: &ToAccounts{Accounts: []AccountIdentifier{accountId, accountIdTwo}}, - } - assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s DISABLE FAILOVER TO ACCOUNTS %s, %s", id.FullyQualifiedName(), accountId.FullyQualifiedName(), accountIdTwo.FullyQualifiedName()) - }) -} - -func TestConnections_AlterConnection(t *testing.T) { - id := randomAccountObjectIdentifier() - defaultOpts := func() *AlterConnectionOptions { - return &AlterConnectionOptions{ - name: id, + ToAccounts: &ToAccounts{[]AccountIdentifier{accountIdentifier}}, } - } - - t.Run("validation: nil options", func(t *testing.T) { - var opts *AlterConnectionOptions = nil - assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) - }) - t.Run("validation: exactly one field from [opts.Set opts.Unset] should be present", func(t *testing.T) { - opts := defaultOpts() - assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("AlterConnectionOptions", "Set", "Unset")) - }) - - t.Run("validation: at least one of the fields [opts.Set.Comment] should be set", func(t *testing.T) { - opts := defaultOpts() - opts.Set = &Set{} - assertOptsInvalidJoinedErrors(t, opts, errAtLeastOneOf("AlterConnectionOptions.Set", "Comment")) + assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s DISABLE FAILOVER TO ACCOUNTS %s", id.FullyQualifiedName(), accountIdentifier.FullyQualifiedName()) }) - t.Run("validation: at least one of the fields [opts.Unset.Comment] should be set", func(t *testing.T) { + t.Run("primary", func(t *testing.T) { opts := defaultOpts() - opts.Unset = &Unset{} - assertOptsInvalidJoinedErrors(t, opts, errAtLeastOneOf("AlterConnectionOptions.Unset", "Comment")) + opts.Primary = Bool(true) + assertOptsValidAndSQLEquals(t, opts, "ALTER CONNECTION %s PRIMARY", id.FullyQualifiedName()) }) t.Run("set comment", func(t *testing.T) { diff --git a/pkg/sdk/connections_impl_gen.go b/pkg/sdk/connections_impl_gen.go index 8006112532..1d42ae96ae 100644 --- a/pkg/sdk/connections_impl_gen.go +++ b/pkg/sdk/connections_impl_gen.go @@ -18,16 +18,6 @@ func (v *connections) Create(ctx context.Context, request *CreateConnectionReque return validateAndExec(v.client, ctx, opts) } -func (v *connections) CreateReplicated(ctx context.Context, request *CreateReplicatedConnectionRequest) error { - opts := request.toOpts() - return validateAndExec(v.client, ctx, opts) -} - -func (v *connections) AlterFailover(ctx context.Context, request *AlterFailoverConnectionRequest) error { - opts := request.toOpts() - return validateAndExec(v.client, ctx, opts) -} - func (v *connections) Alter(ctx context.Context, request *AlterConnectionRequest) error { opts := request.toOpts() return validateAndExec(v.client, ctx, opts) @@ -63,49 +53,43 @@ func (r *CreateConnectionRequest) toOpts() *CreateConnectionOptions { opts := &CreateConnectionOptions{ IfNotExists: r.IfNotExists, name: r.name, - Comment: r.Comment, + + Comment: r.Comment, } - return opts -} -func (r *CreateReplicatedConnectionRequest) toOpts() *CreateReplicatedConnectionOptions { - opts := &CreateReplicatedConnectionOptions{ - IfNotExists: r.IfNotExists, - name: r.name, - ReplicaOf: r.ReplicaOf, - Comment: r.Comment, + if r.AsReplicaOf != nil { + opts.AsReplicaOf = &AsReplicaOf{ + AsReplicaOf: r.AsReplicaOf.AsReplicaOf, + } } + return opts } -func (r *AlterFailoverConnectionRequest) toOpts() *AlterFailoverConnectionOptions { - opts := &AlterFailoverConnectionOptions{ - name: r.name, +func (r *AlterConnectionRequest) toOpts() *AlterConnectionOptions { + opts := &AlterConnectionOptions{ + IfExists: r.IfExists, + name: r.name, + Primary: r.Primary, } if r.EnableConnectionFailover != nil { opts.EnableConnectionFailover = &EnableConnectionFailover{ - ToAccounts: r.EnableConnectionFailover.ToAccounts, - IgnoreEditionCheck: r.EnableConnectionFailover.IgnoreEditionCheck, + ToAccounts: r.EnableConnectionFailover.ToAccounts, } } if r.DisableConnectionFailover != nil { + opts.DisableConnectionFailover = &DisableConnectionFailover{} + if r.DisableConnectionFailover.ToAccounts != nil { opts.DisableConnectionFailover.ToAccounts = &ToAccounts{ Accounts: r.DisableConnectionFailover.ToAccounts.Accounts, } } - } - return opts -} -func (r *AlterConnectionRequest) toOpts() *AlterConnectionOptions { - opts := &AlterConnectionOptions{ - IfExists: r.IfExists, - name: r.name, } if r.Set != nil { @@ -119,6 +103,7 @@ func (r *AlterConnectionRequest) toOpts() *AlterConnectionOptions { Comment: r.Unset.Comment, } } + return opts } @@ -154,5 +139,6 @@ func (r connectionRow) convert() *Connection { if r.Comment.Valid { c.Comment = String(r.Comment.String) } + return c } diff --git a/pkg/sdk/connections_validations_gen.go b/pkg/sdk/connections_validations_gen.go index f7f9be1f8b..52ecb38c1c 100644 --- a/pkg/sdk/connections_validations_gen.go +++ b/pkg/sdk/connections_validations_gen.go @@ -2,8 +2,6 @@ package sdk var ( _ validatable = new(CreateConnectionOptions) - _ validatable = new(CreateReplicatedConnectionOptions) - _ validatable = new(AlterFailoverConnectionOptions) _ validatable = new(AlterConnectionOptions) _ validatable = new(DropConnectionOptions) _ validatable = new(ShowConnectionOptions) @@ -17,30 +15,10 @@ func (opts *CreateConnectionOptions) validate() error { if !ValidObjectIdentifier(opts.name) { errs = append(errs, ErrInvalidObjectIdentifier) } - return JoinErrors(errs...) -} - -func (opts *CreateReplicatedConnectionOptions) validate() error { - if opts == nil { - return ErrNilOptions - } - var errs []error - if !ValidObjectIdentifier(opts.name) { - errs = append(errs, ErrInvalidObjectIdentifier) - } - if !ValidObjectIdentifier(opts.ReplicaOf) { - errs = append(errs, ErrInvalidObjectIdentifier) - } - return JoinErrors(errs...) -} - -func (opts *AlterFailoverConnectionOptions) validate() error { - if opts == nil { - return ErrNilOptions - } - var errs []error - if !exactlyOneValueSet(opts.EnableConnectionFailover, opts.DisableConnectionFailover, opts.Primary) { - errs = append(errs, errExactlyOneOf("AlterFailoverConnectionOptions", "EnableConnectionFailover", "DisableConnectionFailover", "Primary")) + if valueSet(opts.AsReplicaOf) { + if !ValidObjectIdentifier(opts.AsReplicaOf.AsReplicaOf) { + errs = append(errs, ErrInvalidObjectIdentifier) + } } return JoinErrors(errs...) } @@ -50,8 +28,8 @@ func (opts *AlterConnectionOptions) validate() error { return ErrNilOptions } var errs []error - if !exactlyOneValueSet(opts.Set, opts.Unset) { - errs = append(errs, errExactlyOneOf("AlterConnectionOptions", "Set", "Unset")) + if !exactlyOneValueSet(opts.EnableConnectionFailover, opts.DisableConnectionFailover, opts.Primary, opts.Set, opts.Unset) { + errs = append(errs, errExactlyOneOf("AlterConnectionOptions", "EnableConnectionFailover", "DisableConnectionFailover", "Primary", "Set", "Unset")) } if valueSet(opts.Set) { if !anyValueSet(opts.Set.Comment) { diff --git a/pkg/sdk/random_test.go b/pkg/sdk/random_test.go index 30a13a94db..552eb68c15 100644 --- a/pkg/sdk/random_test.go +++ b/pkg/sdk/random_test.go @@ -40,6 +40,10 @@ func randomDatabaseObjectIdentifierInDatabase(databaseId AccountObjectIdentifier return NewDatabaseObjectIdentifier(databaseId.Name(), random.StringN(12)) } +func randomAccountIdentifier() AccountIdentifier { + return NewAccountIdentifier(random.StringN(12), random.StringN(12)) +} + func randomAccountObjectIdentifier() AccountObjectIdentifier { return NewAccountObjectIdentifier(random.StringN(12)) } diff --git a/pkg/sdk/testint/connections_gen_integration_test.go b/pkg/sdk/testint/connections_gen_integration_test.go index ca027d682d..f26e2ed798 100644 --- a/pkg/sdk/testint/connections_gen_integration_test.go +++ b/pkg/sdk/testint/connections_gen_integration_test.go @@ -2,6 +2,7 @@ package testint import ( "fmt" + "strings" "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" @@ -11,39 +12,43 @@ import ( "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert/objectassert" ) +const ConnectionFailoverToAccountInSameRegionErrorMessage = "The connection cannot be failed over to an account in the same region" + func TestInt_Connections(t *testing.T) { client := testClient(t) + secondaryClient := testSecondaryClient(t) ctx := testContext(t) - orgName, err := client.ContextFunctions.CurrentOrganizationName(ctx) - require.NoError(t, err) - - accountName, err := client.ContextFunctions.CurrentAccountName(ctx) - require.NoError(t, err) - - region, err := client.ContextFunctions.CurrentRegion(ctx) + sessionDetails, err := client.ContextFunctions.CurrentSessionDetails(ctx) require.NoError(t, err) t.Run("Create minimal", func(t *testing.T) { id := testClientHelper().Ids.RandomAccountObjectIdentifier() - err := client.Connections.Create(ctx, sdk.NewCreateConnectionRequest(id)) + require.NoError(t, err) + + err = client.Connections.Create(ctx, sdk.NewCreateConnectionRequest(id)) require.NoError(t, err) t.Cleanup(testClientHelper().Connection.DropFunc(t, id)) assertions.AssertThatObject(t, objectassert.Connection(t, id). - HasSnowflakeRegion(region). - HasAccountName(accountName). + HasSnowflakeRegion(sessionDetails.Region). + HasAccountName(sessionDetails.AccountName). HasName(id.Name()). HasNoComment(). HasIsPrimary(true). - HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, id.Name())). + HasPrimary(fmt.Sprintf("%s.%s.%s", sessionDetails.OrganizationName, sessionDetails.AccountName, id.Name())). HasFailoverAllowedToAccounts( []string{ - fmt.Sprintf("%s.%s", orgName, accountName), + fmt.Sprintf("%s.%s", sessionDetails.OrganizationName, sessionDetails.AccountName), }, ). - HasOrganizationName(orgName). - HasAccountLocator(client.GetAccountLocator()), + HasOrganizationName(sessionDetails.OrganizationName). + HasAccountLocator(client.GetAccountLocator()). + HasConnectionUrl( + strings.ToLower( + fmt.Sprintf("%s-%s.snowflakecomputing.com", sessionDetails.OrganizationName, id.Name()), + ), + ), ) }) @@ -56,209 +61,176 @@ func TestInt_Connections(t *testing.T) { t.Cleanup(testClientHelper().Connection.DropFunc(t, id)) assertions.AssertThatObject(t, objectassert.Connection(t, id). - HasSnowflakeRegion(region). - HasAccountName(accountName). + HasSnowflakeRegion(sessionDetails.Region). + HasAccountName(sessionDetails.AccountName). HasName(id.Name()). HasComment("test comment for connection"). HasIsPrimary(true). - HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, id.Name())). + HasPrimary(fmt.Sprintf("%s.%s.%s", sessionDetails.OrganizationName, sessionDetails.AccountName, id.Name())). HasFailoverAllowedToAccounts( []string{ - fmt.Sprintf("%s.%s", orgName, accountName), + fmt.Sprintf("%s.%s", sessionDetails.OrganizationName, sessionDetails.AccountName), }, ). - HasOrganizationName(orgName). - HasAccountLocator(client.GetAccountLocator()), + HasOrganizationName(sessionDetails.OrganizationName). + HasAccountLocator(client.GetAccountLocator()). + HasConnectionUrl( + strings.ToLower( + fmt.Sprintf("%s-%s.snowflakecomputing.com", sessionDetails.OrganizationName, id.Name()), + ), + ), ) }) - // TODO: uncomment when able to change accounts to different regions - // Snowflake error: The connection cannot be failed over to an account in the same region - /* - t.Run("AlterFailover EnableFailover", func(t *testing.T) { - id := testClientHelper().Ids.RandomAccountObjectIdentifier() - secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() - - _, connectionCleanup := testClientHelper().Connection.Create(t, id) - t.Cleanup(connectionCleanup) - - err := client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(id). - WithEnableConnectionFailover( - *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( - []sdk.AccountIdentifier{ - secondaryAccountId, - }, - ), - ), - ) - require.NoError(t, err) - - assertions.AssertThatObject(t, objectassert.Connection(t, id). - HasSnowflakeRegion(region). - HasAccountName(accountName). - HasName(id.Name()). - HasNoComment(). - HasIsPrimary(true). - HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, id.Name())). - HasFailoverAllowedToAccounts( - []string{ - fmt.Sprintf("%s.%s", orgName, accountName), - fmt.Sprintf("%s.%s", orgName, secondaryAccountId.Name()), - }, - ). - HasOrganizationName(orgName). - HasAccountLocator(client.GetAccountLocator()), - ) - }) - - t.Run("AlterFailover EnableFailover With Ignore Edittion Check", func(t *testing.T) { - id := testClientHelper().Ids.RandomAccountObjectIdentifier() - secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() - - primaryConn, connectionCleanup := testClientHelper().Connection.Create(t, id) - t.Cleanup(connectionCleanup) - - err := client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(primaryConn.ID()). - WithEnableConnectionFailover( - *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( - []sdk.AccountIdentifier{ - secondaryAccountId, - }, - ).WithIgnoreEditionCheck(true), - ), - ) - require.NoError(t, err) - - assertions.AssertThatObject(t, objectassert.Connection(t, id). - HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, id.Name())). - HasFailoverAllowedToAccounts( - []string{ - fmt.Sprintf("%s.%s", orgName, accountName), - fmt.Sprintf("%s.%s", orgName, secondaryAccountId.Name()), - }, - ), - ) - - // try to alter enable failover to accoutns list - err = client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(id). - WithEnableConnectionFailover( - *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( - []sdk.AccountIdentifier{}, - ), - ), - ) - require.NoError(t, err) - - // assert that list has not been changed - assertions.AssertThatObject(t, objectassert.Connection(t, id). - HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, id.Name())). - HasFailoverAllowedToAccounts( - []string{ - fmt.Sprintf("%s.%s", orgName, accountName), - fmt.Sprintf("%s.%s", orgName, secondaryAccountId.Name()), - }, - ), - ) - }) - - t.Run("AlterFailover DisableFailover", func(t *testing.T) { - id := testClientHelper().Ids.RandomAccountObjectIdentifier() - accountId := testClientHelper().Ids.AccountIdentifierWithLocator() - secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() - - primaryConn, connectionCleanup := testClientHelper().Connection.Create(t, testClientHelper().Ids.RandomAccountObjectIdentifier()) - t.Cleanup(connectionCleanup) - - // Add secondary account to failover list - err := client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(primaryConn.ID()). - WithEnableConnectionFailover( - *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( - []sdk.AccountIdentifier{ - secondaryAccountId, - }, - ), - ), - ) - require.NoError(t, err) - - // Disable promotion of this connection - err = client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(primaryConn.ID()). - WithDisableConnectionFailover(*sdk.NewDisableConnectionFailoverRequest())) - require.NoError(t, err) - - assertions.AssertThatObject(t, objectassert.Connection(t, primaryConn.ID()). - HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, primaryConn.ID().Name())). - HasFailoverAllowedToAccounts( - []string{ - fmt.Sprintf("%s.%s", orgName, accountName), - fmt.Sprintf("%s.%s", orgName, secondaryAccountId.Name()), - }, - ), - ) - - // Create repllication for secondary account - err = client.Connections.CreateReplicated(ctx, sdk.NewCreateReplicatedConnectionRequest(id, sdk.NewExternalObjectIdentifier(accountId, primaryConn.ID()))) - - // Assert that it is not a primary connection - assertions.AssertThatObject(t, objectassert.Connection(t, id). - HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, secondaryAccountId.Name(), id.Name())). - HasIsPrimary(false), - ) - - // Try to promote to primary - err = client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(id). - WithPrimary(true)) - - // Assert tha promotion has been disabled - assertions.AssertThatObject(t, objectassert.Connection(t, id). - HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, secondaryAccountId.Name(), id.Name())). - HasIsPrimary(false), - ) - }) - - t.Run("CreateReplicated", func(t *testing.T) { - id := testClientHelper().Ids.RandomAccountObjectIdentifier() - accountId := testClientHelper().Ids.AccountIdentifierWithLocator() - secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() - - primaryConn, connectionCleanup := testClientHelper().Connection.Create(t, testClientHelper().Ids.RandomAccountObjectIdentifier()) - t.Cleanup(connectionCleanup) - - err := client.Connections.AlterFailover(ctx, sdk.NewAlterFailoverConnectionRequest(primaryConn.ID()). - WithEnableConnectionFailover( - *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( - []sdk.AccountIdentifier{ - secondaryAccountId, - }, - ), - ), - ) - require.NoError(t, err) - - err = client.Connections.CreateReplicated(ctx, sdk.NewCreateReplicatedConnectionRequest(id, sdk.NewExternalObjectIdentifier(accountId, primaryConn.ID()))) - require.NoError(t, err) - t.Cleanup(testClientHelper().Connection.DropFunc(t, id)) - - assertions.AssertThatObject(t, objectassert.Connection(t, id). - HasSnowflakeRegion(region). - HasAccountName(accountName). - HasName(id.Name()). - HasNoComment(). - HasIsPrimary(false). - HasPrimary(fmt.Sprintf("%s.%s.%s", orgName, accountName, primaryConn.ID().Name())). - HasFailoverAllowedToAccounts( - []string{ - fmt.Sprintf("%s.%s", orgName, accountName), - fmt.Sprintf("%s.%s", orgName, secondaryAccountId.Name()), - }, - ). - HasOrganizationName(orgName). - HasAccountLocator(client.GetAccountLocator()), - ) - }) - */ - - t.Run("AlterConnection", func(t *testing.T) { + t.Run("Alter enable failover", func(t *testing.T) { + id := testClientHelper().Ids.RandomAccountObjectIdentifier() + secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() + + _, connectionCleanup := testClientHelper().Connection.Create(t, id) + t.Cleanup(connectionCleanup) + + err := client.Connections.Alter(ctx, sdk.NewAlterConnectionRequest(id). + WithEnableConnectionFailover( + *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( + []sdk.AccountIdentifier{ + secondaryAccountId, + }, + ), + ), + ) + require.ErrorContains(t, err, ConnectionFailoverToAccountInSameRegionErrorMessage) + + // TODO: [SNOW-1763442] + /* + require.NoError(t, err) + assertions.AssertThatObject(t, objectassert.Connection(t, id). + HasSnowflakeRegion(sessionDetails.Region). + HasAccountName(sessionDetails.AccountName). + HasName(id.Name()). + HasNoComment(). + HasIsPrimary(true). + HasPrimary(fmt.Sprintf("%s.%s.%s", sessionDetails.OrganizationName, sessionDetails.AccountName, id.Name())). + HasFailoverAllowedToAccounts( + []string{ + fmt.Sprintf("%s.%s", sessionDetails.OrganizationName, sessionDetails.AccountName), + fmt.Sprintf("%s.%s", sessionDetails.OrganizationName, secondaryAccountId.Name()), + }, + ). + HasOrganizationName(sessionDetails.OrganizationName). + HasAccountLocator(client.GetAccountLocator()), + HasConnectionUrl( + strings.ToLower( + fmt.Sprintf("%s-%s.snowflakecomputing.com", sessionDetails.OrganizationName, id.Name()), + ), + ), + ) + */ + }) + + t.Run("Create as replica of", func(t *testing.T) { + id := testClientHelper().Ids.RandomAccountObjectIdentifier() + accountId := testClientHelper().Ids.AccountIdentifierWithLocator() + _ = id + _ = accountId + secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() + + primaryConn, connectionCleanup := testClientHelper().Connection.Create(t, testClientHelper().Ids.RandomAccountObjectIdentifier()) + t.Cleanup(connectionCleanup) + + err := client.Connections.Alter(ctx, sdk.NewAlterConnectionRequest(primaryConn.ID()). + WithEnableConnectionFailover( + *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( + []sdk.AccountIdentifier{ + secondaryAccountId, + }, + ), + ), + ) + require.ErrorContains(t, err, ConnectionFailoverToAccountInSameRegionErrorMessage) + // TODO: [SNOW-1763442] + // + // require.NoError(t, err) + + /* + // create replica on secondary account + err = secondaryClient.Connections.Create(ctx, sdk.NewCreateConnectionRequest(id). + WithAsReplicaOf(sdk.AsReplicaOfRequest{ + AsReplicaOf: sdk.NewExternalObjectIdentifier(accountId, id.Name()), + })) + t.Cleanup(testClientHelper().Connection.DropFunc(t, id)) + require.NoError(t, err) + + assertions.AssertThatObject(t, objectassert.Connection(t, id). + HasSnowflakeRegion(sessionDetails.Region). + HasAccountName(sessionDetails.AccountName). + HasName(id.Name()). + HasNoComment(). + HasIsPrimary(false). + HasPrimary(fmt.Sprintf("%s.%s.%s", sessionDetails.OrganizationName, sessionDetails.AccountName, id.Name())). + HasFailoverAllowedToAccounts( + []string{ + fmt.Sprintf("%s.%s", sessionDetails.OrganizationName, sessionDetails.AccountName), + fmt.Sprintf("%s.%s", sessionDetails.OrganizationName, secondaryAccountId.Name()), + }, + ). + HasOrganizationName(sessionDetails.OrganizationName). + HasAccountLocator(client.GetAccountLocator()). + HasConnectionUrl( + strings.ToLower( + fmt.Sprintf("%s-%s.snowflakecomputing.com", sessionDetails.OrganizationName, id.Name()), + ), + ), + ) + */ + }) + + t.Run("Alter disable failover", func(t *testing.T) { + id := testClientHelper().Ids.RandomAccountObjectIdentifier() + accountId := testClientHelper().Ids.AccountIdentifierWithLocator() + secondaryAccountId := secondaryTestClientHelper().Account.GetAccountIdentifier(t) + + primaryConn, connectionCleanup := testClientHelper().Connection.Create(t, id) + t.Cleanup(connectionCleanup) + + // Add secondary account to failover list + err := client.Connections.Alter(ctx, sdk.NewAlterConnectionRequest(id). + WithEnableConnectionFailover( + *sdk.NewEnableConnectionFailoverRequest().WithToAccounts( + []sdk.AccountIdentifier{ + secondaryAccountId, + }, + ), + ), + ) + require.ErrorContains(t, err, ConnectionFailoverToAccountInSameRegionErrorMessage) + // TODO: [SNOW-1763442] + // + // require.NoError(t, err) + + // Disable promotion of this connection + err = client.Connections.Alter(ctx, sdk.NewAlterConnectionRequest(id). + WithDisableConnectionFailover(*sdk.NewDisableConnectionFailoverRequest())) + require.NoError(t, err) + + // Assert that promotion for other account has been disabled + assertions.AssertThatObject(t, objectassert.Connection(t, primaryConn.ID()). + HasPrimary(fmt.Sprintf("%s.%s.%s", sessionDetails.OrganizationName, sessionDetails.AccountName, id.Name())). + HasFailoverAllowedToAccounts( + []string{ + fmt.Sprintf("%s.%s", sessionDetails.OrganizationName, sessionDetails.AccountName), + }, + ), + ) + + // Try to create repllication on secondary account + err = secondaryClient.Connections.Create(ctx, sdk.NewCreateConnectionRequest(id). + WithAsReplicaOf(sdk.AsReplicaOfRequest{ + AsReplicaOf: sdk.NewExternalObjectIdentifier(accountId, id), + })) + require.ErrorContains(t, err, "This account is not authorized to create a secondary connection of this primary connection") + }) + + t.Run("Alter", func(t *testing.T) { id := testClientHelper().Ids.RandomAccountObjectIdentifier() _, connectionCleanup := testClientHelper().Connection.Create(t, id) t.Cleanup(connectionCleanup) From fd52498d5dad6d15de233118ab9dd49485f7ec40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Mon, 28 Oct 2024 11:21:36 +0100 Subject: [PATCH 10/13] added drop with if exists and HasPrimaryIdentifier extended function --- .../objectassert/connection_snowflake_ext.go | 13 +++ .../connections_gen_integration_test.go | 95 +++++++++++-------- 2 files changed, 66 insertions(+), 42 deletions(-) diff --git a/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_ext.go b/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_ext.go index 5eb76cb0fe..4a585ed82c 100644 --- a/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_ext.go +++ b/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_ext.go @@ -3,6 +3,7 @@ package objectassert import ( "fmt" "slices" + "strings" "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" @@ -41,3 +42,15 @@ func (c *ConnectionAssert) HasConnectionUrlNotEmpty() *ConnectionAssert { 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 +} diff --git a/pkg/sdk/testint/connections_gen_integration_test.go b/pkg/sdk/testint/connections_gen_integration_test.go index f26e2ed798..9eacefc123 100644 --- a/pkg/sdk/testint/connections_gen_integration_test.go +++ b/pkg/sdk/testint/connections_gen_integration_test.go @@ -21,6 +21,7 @@ func TestInt_Connections(t *testing.T) { sessionDetails, err := client.ContextFunctions.CurrentSessionDetails(ctx) require.NoError(t, err) + accountId := testClientHelper().Account.GetAccountIdentifier(t) t.Run("Create minimal", func(t *testing.T) { id := testClientHelper().Ids.RandomAccountObjectIdentifier() @@ -30,16 +31,17 @@ func TestInt_Connections(t *testing.T) { require.NoError(t, err) t.Cleanup(testClientHelper().Connection.DropFunc(t, id)) + externalObjectIdentifier := sdk.NewExternalObjectIdentifier(accountId, id) assertions.AssertThatObject(t, objectassert.Connection(t, id). HasSnowflakeRegion(sessionDetails.Region). HasAccountName(sessionDetails.AccountName). HasName(id.Name()). HasNoComment(). HasIsPrimary(true). - HasPrimary(fmt.Sprintf("%s.%s.%s", sessionDetails.OrganizationName, sessionDetails.AccountName, id.Name())). + HasPrimaryIdentifier(externalObjectIdentifier.FullyQualifiedName()). HasFailoverAllowedToAccounts( []string{ - fmt.Sprintf("%s.%s", sessionDetails.OrganizationName, sessionDetails.AccountName), + accountId.Name(), }, ). HasOrganizationName(sessionDetails.OrganizationName). @@ -60,16 +62,17 @@ func TestInt_Connections(t *testing.T) { require.NoError(t, err) t.Cleanup(testClientHelper().Connection.DropFunc(t, id)) + externalObjectIdentifier := sdk.NewExternalObjectIdentifier(accountId, id) assertions.AssertThatObject(t, objectassert.Connection(t, id). HasSnowflakeRegion(sessionDetails.Region). HasAccountName(sessionDetails.AccountName). HasName(id.Name()). HasComment("test comment for connection"). HasIsPrimary(true). - HasPrimary(fmt.Sprintf("%s.%s.%s", sessionDetails.OrganizationName, sessionDetails.AccountName, id.Name())). + HasPrimaryIdentifier(externalObjectIdentifier.FullyQualifiedName()). HasFailoverAllowedToAccounts( []string{ - fmt.Sprintf("%s.%s", sessionDetails.OrganizationName, sessionDetails.AccountName), + accountId.Name(), }, ). HasOrganizationName(sessionDetails.OrganizationName). @@ -103,17 +106,18 @@ func TestInt_Connections(t *testing.T) { // TODO: [SNOW-1763442] /* require.NoError(t, err) + externalObjectIdentifier := sdk.NewExternalObjectIdentifier(accountId, id) assertions.AssertThatObject(t, objectassert.Connection(t, id). HasSnowflakeRegion(sessionDetails.Region). HasAccountName(sessionDetails.AccountName). HasName(id.Name()). HasNoComment(). HasIsPrimary(true). - HasPrimary(fmt.Sprintf("%s.%s.%s", sessionDetails.OrganizationName, sessionDetails.AccountName, id.Name())). + HasPrimaryIdentifier(externalObjectIdentifier.FullyQualifiedName()). HasFailoverAllowedToAccounts( []string{ - fmt.Sprintf("%s.%s", sessionDetails.OrganizationName, sessionDetails.AccountName), - fmt.Sprintf("%s.%s", sessionDetails.OrganizationName, secondaryAccountId.Name()), + accountId.Name(), + secondaryAccountId.Name(), }, ). HasOrganizationName(sessionDetails.OrganizationName). @@ -129,9 +133,7 @@ func TestInt_Connections(t *testing.T) { t.Run("Create as replica of", func(t *testing.T) { id := testClientHelper().Ids.RandomAccountObjectIdentifier() - accountId := testClientHelper().Ids.AccountIdentifierWithLocator() _ = id - _ = accountId secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() primaryConn, connectionCleanup := testClientHelper().Connection.Create(t, testClientHelper().Ids.RandomAccountObjectIdentifier()) @@ -151,42 +153,42 @@ func TestInt_Connections(t *testing.T) { // // require.NoError(t, err) + // create replica on secondary account /* - // create replica on secondary account - err = secondaryClient.Connections.Create(ctx, sdk.NewCreateConnectionRequest(id). - WithAsReplicaOf(sdk.AsReplicaOfRequest{ - AsReplicaOf: sdk.NewExternalObjectIdentifier(accountId, id.Name()), - })) - t.Cleanup(testClientHelper().Connection.DropFunc(t, id)) - require.NoError(t, err) - - assertions.AssertThatObject(t, objectassert.Connection(t, id). - HasSnowflakeRegion(sessionDetails.Region). - HasAccountName(sessionDetails.AccountName). - HasName(id.Name()). - HasNoComment(). - HasIsPrimary(false). - HasPrimary(fmt.Sprintf("%s.%s.%s", sessionDetails.OrganizationName, sessionDetails.AccountName, id.Name())). - HasFailoverAllowedToAccounts( - []string{ - fmt.Sprintf("%s.%s", sessionDetails.OrganizationName, sessionDetails.AccountName), - fmt.Sprintf("%s.%s", sessionDetails.OrganizationName, secondaryAccountId.Name()), - }, - ). - HasOrganizationName(sessionDetails.OrganizationName). - HasAccountLocator(client.GetAccountLocator()). - HasConnectionUrl( - strings.ToLower( - fmt.Sprintf("%s-%s.snowflakecomputing.com", sessionDetails.OrganizationName, id.Name()), - ), - ), - ) + externalObjectIdentifier := sdk.NewExternalObjectIdentifier(accountId, id) + err = secondaryClient.Connections.Create(ctx, sdk.NewCreateConnectionRequest(id). + WithAsReplicaOf(sdk.AsReplicaOfRequest{ + AsReplicaOf: externalObjectIdentifier, + })) + t.Cleanup(testClientHelper().Connection.DropFunc(t, id)) + require.NoError(t, err) + + assertions.AssertThatObject(t, objectassert.Connection(t, id). + HasSnowflakeRegion(sessionDetails.Region). + HasAccountName(sessionDetails.AccountName). + HasName(id.Name()). + HasNoComment(). + HasIsPrimary(false). + HasPrimaryIdentifier(externalObjectIdentifier.FullyQualifiedName()). + HasFailoverAllowedToAccounts( + []string{ + accountId.Name(), + secondaryAccountId.Name(), + }, + ). + HasOrganizationName(sessionDetails.OrganizationName). + HasAccountLocator(client.GetAccountLocator()). + HasConnectionUrl( + strings.ToLower( + fmt.Sprintf("%s-%s.snowflakecomputing.com", sessionDetails.OrganizationName, id.Name()), + ), + ), + ) */ }) t.Run("Alter disable failover", func(t *testing.T) { id := testClientHelper().Ids.RandomAccountObjectIdentifier() - accountId := testClientHelper().Ids.AccountIdentifierWithLocator() secondaryAccountId := secondaryTestClientHelper().Account.GetAccountIdentifier(t) primaryConn, connectionCleanup := testClientHelper().Connection.Create(t, id) @@ -213,11 +215,12 @@ func TestInt_Connections(t *testing.T) { require.NoError(t, err) // Assert that promotion for other account has been disabled + externalObjectIdentifier := sdk.NewExternalObjectIdentifier(accountId, id) assertions.AssertThatObject(t, objectassert.Connection(t, primaryConn.ID()). - HasPrimary(fmt.Sprintf("%s.%s.%s", sessionDetails.OrganizationName, sessionDetails.AccountName, id.Name())). + HasPrimaryIdentifier(externalObjectIdentifier.FullyQualifiedName()). HasFailoverAllowedToAccounts( []string{ - fmt.Sprintf("%s.%s", sessionDetails.OrganizationName, sessionDetails.AccountName), + accountId.Name(), }, ), ) @@ -225,7 +228,7 @@ func TestInt_Connections(t *testing.T) { // Try to create repllication on secondary account err = secondaryClient.Connections.Create(ctx, sdk.NewCreateConnectionRequest(id). WithAsReplicaOf(sdk.AsReplicaOfRequest{ - AsReplicaOf: sdk.NewExternalObjectIdentifier(accountId, id), + AsReplicaOf: externalObjectIdentifier, })) require.ErrorContains(t, err, "This account is not authorized to create a secondary connection of this primary connection") }) @@ -275,6 +278,14 @@ func TestInt_Connections(t *testing.T) { require.Error(t, err) }) + t.Run("Drop with if exists", func(t *testing.T) { + err = client.Connections.Drop(ctx, sdk.NewDropConnectionRequest(NonExistingAccountObjectIdentifier)) + require.ErrorIs(t, err, sdk.ErrObjectNotExistOrAuthorized) + + err = client.Connections.Drop(ctx, sdk.NewDropConnectionRequest(NonExistingAccountObjectIdentifier).WithIfExists(true)) + require.NoError(t, err) + }) + t.Run("Show", func(t *testing.T) { id1 := testClientHelper().Ids.RandomAccountObjectIdentifier() id2 := testClientHelper().Ids.RandomAccountObjectIdentifier() From b739e999758271a2295dd8b89ecfb3f9e38b7884 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Mon, 28 Oct 2024 11:24:48 +0100 Subject: [PATCH 11/13] pre-push adjustments --- pkg/sdk/connections_impl_gen.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pkg/sdk/connections_impl_gen.go b/pkg/sdk/connections_impl_gen.go index 1d42ae96ae..f70f37aa77 100644 --- a/pkg/sdk/connections_impl_gen.go +++ b/pkg/sdk/connections_impl_gen.go @@ -70,8 +70,7 @@ func (r *AlterConnectionRequest) toOpts() *AlterConnectionOptions { opts := &AlterConnectionOptions{ IfExists: r.IfExists, name: r.name, - - Primary: r.Primary, + Primary: r.Primary, } if r.EnableConnectionFailover != nil { @@ -81,7 +80,6 @@ func (r *AlterConnectionRequest) toOpts() *AlterConnectionOptions { } if r.DisableConnectionFailover != nil { - opts.DisableConnectionFailover = &DisableConnectionFailover{} if r.DisableConnectionFailover.ToAccounts != nil { @@ -89,7 +87,6 @@ func (r *AlterConnectionRequest) toOpts() *AlterConnectionOptions { Accounts: r.DisableConnectionFailover.ToAccounts.Accounts, } } - } if r.Set != nil { From d7b13de9a4e0f6739220aa8b8381dbe9149c232f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Mon, 28 Oct 2024 11:44:06 +0100 Subject: [PATCH 12/13] added skips for tests --- pkg/sdk/testint/connections_gen_integration_test.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/sdk/testint/connections_gen_integration_test.go b/pkg/sdk/testint/connections_gen_integration_test.go index 9eacefc123..77ca241312 100644 --- a/pkg/sdk/testint/connections_gen_integration_test.go +++ b/pkg/sdk/testint/connections_gen_integration_test.go @@ -10,6 +10,7 @@ import ( assertions "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert/objectassert" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs" ) const ConnectionFailoverToAccountInSameRegionErrorMessage = "The connection cannot be failed over to an account in the same region" @@ -86,6 +87,9 @@ func TestInt_Connections(t *testing.T) { }) t.Run("Alter enable failover", func(t *testing.T) { + // TODO: [SNOW-1763442]: Unskip; Business Critical Snowflake Edition needed + _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) + id := testClientHelper().Ids.RandomAccountObjectIdentifier() secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() @@ -132,6 +136,9 @@ func TestInt_Connections(t *testing.T) { }) t.Run("Create as replica of", func(t *testing.T) { + // TODO: [SNOW-1763442]: Unskip; Business Critical Snowflake Edition needed + _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) + id := testClientHelper().Ids.RandomAccountObjectIdentifier() _ = id secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() @@ -188,6 +195,9 @@ func TestInt_Connections(t *testing.T) { }) t.Run("Alter disable failover", func(t *testing.T) { + // TODO: [SNOW-1763442]: Unskip; Business Critical Snowflake Edition needed + _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) + id := testClientHelper().Ids.RandomAccountObjectIdentifier() secondaryAccountId := secondaryTestClientHelper().Account.GetAccountIdentifier(t) @@ -233,7 +243,7 @@ func TestInt_Connections(t *testing.T) { require.ErrorContains(t, err, "This account is not authorized to create a secondary connection of this primary connection") }) - t.Run("Alter", func(t *testing.T) { + t.Run("Alter comment", func(t *testing.T) { id := testClientHelper().Ids.RandomAccountObjectIdentifier() _, connectionCleanup := testClientHelper().Connection.Create(t, id) t.Cleanup(connectionCleanup) From f9c4f6a932073b3066a82519dc6633ca99566bb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Budzy=C5=84ski?= Date: Tue, 29 Oct 2024 09:18:53 +0100 Subject: [PATCH 13/13] resolved comments --- .../objectassert/connection_snowflake_ext.go | 8 ++-- pkg/acceptance/helpers/connection_client.go | 2 +- pkg/sdk/connections_def.go | 12 +++-- pkg/sdk/connections_dto_builders_gen.go | 10 +---- pkg/sdk/connections_dto_gen.go | 6 +-- pkg/sdk/connections_gen.go | 15 +++---- pkg/sdk/connections_gen_test.go | 6 +-- pkg/sdk/connections_impl_gen.go | 4 +- pkg/sdk/connections_validations_gen.go | 6 +-- .../connections_gen_integration_test.go | 45 +++++++++++++------ 10 files changed, 56 insertions(+), 58 deletions(-) diff --git a/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_ext.go b/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_ext.go index 4a585ed82c..4e237798ef 100644 --- a/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_ext.go +++ b/pkg/acceptance/bettertestspoc/assert/objectassert/connection_snowflake_ext.go @@ -43,12 +43,12 @@ func (c *ConnectionAssert) HasConnectionUrlNotEmpty() *ConnectionAssert { return c } -func (c *ConnectionAssert) HasPrimaryIdentifier(expected string) *ConnectionAssert { +func (c *ConnectionAssert) HasPrimaryIdentifier(expected sdk.ExternalObjectIdentifier) *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) + expectedString := strings.ReplaceAll(expected.FullyQualifiedName(), `"`, "") + if o.Primary != expectedString { + return fmt.Errorf("expected primary identifier: %v; got: %v", expectedString, o.Primary) } return nil }) diff --git a/pkg/acceptance/helpers/connection_client.go b/pkg/acceptance/helpers/connection_client.go index 5ea271f064..24fdf9384a 100644 --- a/pkg/acceptance/helpers/connection_client.go +++ b/pkg/acceptance/helpers/connection_client.go @@ -38,7 +38,7 @@ 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.NewCreateConnectionRequest(id).WithAsReplicaOf(sdk.AsReplicaOfRequest{AsReplicaOf: replicaOf}) + request := sdk.NewCreateConnectionRequest(id).WithAsReplicaOf(replicaOf) err := c.client().Create(ctx, request) require.NoError(t, err) connection, err := c.client().ShowByID(ctx, id) diff --git a/pkg/sdk/connections_def.go b/pkg/sdk/connections_def.go index 45a87feec4..39f583aff2 100644 --- a/pkg/sdk/connections_def.go +++ b/pkg/sdk/connections_def.go @@ -17,15 +17,13 @@ var ConnectionDef = g.NewInterface( SQL("CONNECTION"). IfNotExists(). Name(). - OptionalQueryStructField( + OptionalIdentifier( "AsReplicaOf", - g.NewQueryStruct("AsReplicaOf"). - Identifier("AsReplicaOf", g.KindOfT[ExternalObjectIdentifier](), g.IdentifierOptions().Required().SQL("AS REPLICA OF")). - WithValidation(g.ValidIdentifier, "AsReplicaOf"), - g.IdentifierOptions(), - ). + g.KindOfT[ExternalObjectIdentifier](), + g.IdentifierOptions().Required().SQL("AS REPLICA OF")). OptionalComment(). - WithValidation(g.ValidIdentifier, "name"), + WithValidation(g.ValidIdentifier, "name"). + WithValidation(g.ValidIdentifierIfSet, "AsReplicaOf"), ).AlterOperation( "https://docs.snowflake.com/en/sql-reference/sql/alter-connection", g.NewQueryStruct("Alter"). diff --git a/pkg/sdk/connections_dto_builders_gen.go b/pkg/sdk/connections_dto_builders_gen.go index 30d75649c0..eb226b05a2 100644 --- a/pkg/sdk/connections_dto_builders_gen.go +++ b/pkg/sdk/connections_dto_builders_gen.go @@ -15,7 +15,7 @@ func (s *CreateConnectionRequest) WithIfNotExists(IfNotExists bool) *CreateConne return s } -func (s *CreateConnectionRequest) WithAsReplicaOf(AsReplicaOf AsReplicaOfRequest) *CreateConnectionRequest { +func (s *CreateConnectionRequest) WithAsReplicaOf(AsReplicaOf ExternalObjectIdentifier) *CreateConnectionRequest { s.AsReplicaOf = &AsReplicaOf return s } @@ -25,14 +25,6 @@ func (s *CreateConnectionRequest) WithComment(Comment string) *CreateConnectionR return s } -func NewAsReplicaOfRequest( - AsReplicaOf ExternalObjectIdentifier, -) *AsReplicaOfRequest { - s := AsReplicaOfRequest{} - s.AsReplicaOf = AsReplicaOf - return &s -} - func NewAlterConnectionRequest( name AccountObjectIdentifier, ) *AlterConnectionRequest { diff --git a/pkg/sdk/connections_dto_gen.go b/pkg/sdk/connections_dto_gen.go index 2225137072..1323bf0af3 100644 --- a/pkg/sdk/connections_dto_gen.go +++ b/pkg/sdk/connections_dto_gen.go @@ -12,14 +12,10 @@ var ( type CreateConnectionRequest struct { IfNotExists *bool name AccountObjectIdentifier // required - AsReplicaOf *AsReplicaOfRequest + AsReplicaOf *ExternalObjectIdentifier Comment *string } -type AsReplicaOfRequest struct { - AsReplicaOf ExternalObjectIdentifier // required -} - type AlterConnectionRequest struct { IfExists *bool name AccountObjectIdentifier // required diff --git a/pkg/sdk/connections_gen.go b/pkg/sdk/connections_gen.go index 9006c9c2a4..80885df87b 100644 --- a/pkg/sdk/connections_gen.go +++ b/pkg/sdk/connections_gen.go @@ -16,15 +16,12 @@ type Connections interface { // CreateConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-connection. type CreateConnectionOptions struct { - create bool `ddl:"static" sql:"CREATE"` - connection bool `ddl:"static" sql:"CONNECTION"` - IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` - name AccountObjectIdentifier `ddl:"identifier"` - AsReplicaOf *AsReplicaOf `ddl:"identifier"` - Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` -} -type AsReplicaOf struct { - AsReplicaOf ExternalObjectIdentifier `ddl:"identifier" sql:"AS REPLICA OF"` + create bool `ddl:"static" sql:"CREATE"` + connection bool `ddl:"static" sql:"CONNECTION"` + IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` + name AccountObjectIdentifier `ddl:"identifier"` + AsReplicaOf *ExternalObjectIdentifier `ddl:"identifier" sql:"AS REPLICA OF"` + Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` } // AlterConnectionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/alter-connection. diff --git a/pkg/sdk/connections_gen_test.go b/pkg/sdk/connections_gen_test.go index 797beb8ee2..8ee6f7441a 100644 --- a/pkg/sdk/connections_gen_test.go +++ b/pkg/sdk/connections_gen_test.go @@ -24,7 +24,7 @@ func TestConnections_Create(t *testing.T) { t.Run("validation: valid identifier for [opts.ReplicaOf]", func(t *testing.T) { opts := defaultOpts() opts.name = id - opts.AsReplicaOf = &AsReplicaOf{emptyExternalObjectIdentifier} + opts.AsReplicaOf = &emptyExternalObjectIdentifier assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) @@ -46,7 +46,7 @@ func TestConnections_Create(t *testing.T) { externalId := randomExternalObjectIdentifier() opts := defaultOpts() opts.name = id - opts.AsReplicaOf = &AsReplicaOf{externalId} + opts.AsReplicaOf = &externalId assertOptsValidAndSQLEquals(t, opts, "CREATE CONNECTION %s AS REPLICA OF %s", id.FullyQualifiedName(), externalId.FullyQualifiedName()) }) @@ -55,7 +55,7 @@ func TestConnections_Create(t *testing.T) { opts := defaultOpts() opts.name = id opts.IfNotExists = Bool(true) - opts.AsReplicaOf = &AsReplicaOf{externalId} + opts.AsReplicaOf = &externalId opts.Comment = String("comment") assertOptsValidAndSQLEquals(t, opts, "CREATE CONNECTION IF NOT EXISTS %s AS REPLICA OF %s COMMENT = 'comment'", id.FullyQualifiedName(), externalId.FullyQualifiedName()) }) diff --git a/pkg/sdk/connections_impl_gen.go b/pkg/sdk/connections_impl_gen.go index f70f37aa77..325141116e 100644 --- a/pkg/sdk/connections_impl_gen.go +++ b/pkg/sdk/connections_impl_gen.go @@ -58,9 +58,7 @@ func (r *CreateConnectionRequest) toOpts() *CreateConnectionOptions { } if r.AsReplicaOf != nil { - opts.AsReplicaOf = &AsReplicaOf{ - AsReplicaOf: r.AsReplicaOf.AsReplicaOf, - } + opts.AsReplicaOf = r.AsReplicaOf } return opts diff --git a/pkg/sdk/connections_validations_gen.go b/pkg/sdk/connections_validations_gen.go index 52ecb38c1c..df6b4e1d0f 100644 --- a/pkg/sdk/connections_validations_gen.go +++ b/pkg/sdk/connections_validations_gen.go @@ -15,10 +15,8 @@ func (opts *CreateConnectionOptions) validate() error { if !ValidObjectIdentifier(opts.name) { errs = append(errs, ErrInvalidObjectIdentifier) } - if valueSet(opts.AsReplicaOf) { - if !ValidObjectIdentifier(opts.AsReplicaOf.AsReplicaOf) { - errs = append(errs, ErrInvalidObjectIdentifier) - } + if opts.AsReplicaOf != nil && !ValidObjectIdentifier(opts.AsReplicaOf) { + errs = append(errs, ErrInvalidObjectIdentifier) } return JoinErrors(errs...) } diff --git a/pkg/sdk/testint/connections_gen_integration_test.go b/pkg/sdk/testint/connections_gen_integration_test.go index 77ca241312..79573d1b6d 100644 --- a/pkg/sdk/testint/connections_gen_integration_test.go +++ b/pkg/sdk/testint/connections_gen_integration_test.go @@ -25,6 +25,9 @@ func TestInt_Connections(t *testing.T) { accountId := testClientHelper().Account.GetAccountIdentifier(t) t.Run("Create minimal", func(t *testing.T) { + // TODO: [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed + _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) + id := testClientHelper().Ids.RandomAccountObjectIdentifier() require.NoError(t, err) @@ -39,7 +42,7 @@ func TestInt_Connections(t *testing.T) { HasName(id.Name()). HasNoComment(). HasIsPrimary(true). - HasPrimaryIdentifier(externalObjectIdentifier.FullyQualifiedName()). + HasPrimaryIdentifier(externalObjectIdentifier). HasFailoverAllowedToAccounts( []string{ accountId.Name(), @@ -56,6 +59,9 @@ func TestInt_Connections(t *testing.T) { }) t.Run("Create all options", func(t *testing.T) { + // TODO: [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed + _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) + id := testClientHelper().Ids.RandomAccountObjectIdentifier() err := client.Connections.Create(ctx, sdk.NewCreateConnectionRequest(id). WithIfNotExists(true). @@ -70,7 +76,7 @@ func TestInt_Connections(t *testing.T) { HasName(id.Name()). HasComment("test comment for connection"). HasIsPrimary(true). - HasPrimaryIdentifier(externalObjectIdentifier.FullyQualifiedName()). + HasPrimaryIdentifier(externalObjectIdentifier). HasFailoverAllowedToAccounts( []string{ accountId.Name(), @@ -87,7 +93,7 @@ func TestInt_Connections(t *testing.T) { }) t.Run("Alter enable failover", func(t *testing.T) { - // TODO: [SNOW-1763442]: Unskip; Business Critical Snowflake Edition needed + // TODO: [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) id := testClientHelper().Ids.RandomAccountObjectIdentifier() @@ -117,7 +123,7 @@ func TestInt_Connections(t *testing.T) { HasName(id.Name()). HasNoComment(). HasIsPrimary(true). - HasPrimaryIdentifier(externalObjectIdentifier.FullyQualifiedName()). + HasPrimaryIdentifier(externalObjectIdentifier). HasFailoverAllowedToAccounts( []string{ accountId.Name(), @@ -136,11 +142,9 @@ func TestInt_Connections(t *testing.T) { }) t.Run("Create as replica of", func(t *testing.T) { - // TODO: [SNOW-1763442]: Unskip; Business Critical Snowflake Edition needed + // TODO: [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) - id := testClientHelper().Ids.RandomAccountObjectIdentifier() - _ = id secondaryAccountId := secondaryTestClientHelper().Ids.AccountIdentifierWithLocator() primaryConn, connectionCleanup := testClientHelper().Connection.Create(t, testClientHelper().Ids.RandomAccountObjectIdentifier()) @@ -176,7 +180,7 @@ func TestInt_Connections(t *testing.T) { HasName(id.Name()). HasNoComment(). HasIsPrimary(false). - HasPrimaryIdentifier(externalObjectIdentifier.FullyQualifiedName()). + HasPrimaryIdentifier(externalObjectIdentifier). HasFailoverAllowedToAccounts( []string{ accountId.Name(), @@ -227,7 +231,7 @@ func TestInt_Connections(t *testing.T) { // Assert that promotion for other account has been disabled externalObjectIdentifier := sdk.NewExternalObjectIdentifier(accountId, id) assertions.AssertThatObject(t, objectassert.Connection(t, primaryConn.ID()). - HasPrimaryIdentifier(externalObjectIdentifier.FullyQualifiedName()). + HasPrimaryIdentifier(externalObjectIdentifier). HasFailoverAllowedToAccounts( []string{ accountId.Name(), @@ -236,14 +240,14 @@ func TestInt_Connections(t *testing.T) { ) // Try to create repllication on secondary account - err = secondaryClient.Connections.Create(ctx, sdk.NewCreateConnectionRequest(id). - WithAsReplicaOf(sdk.AsReplicaOfRequest{ - AsReplicaOf: externalObjectIdentifier, - })) + err = secondaryClient.Connections.Create(ctx, sdk.NewCreateConnectionRequest(id).WithAsReplicaOf(externalObjectIdentifier)) require.ErrorContains(t, err, "This account is not authorized to create a secondary connection of this primary connection") }) t.Run("Alter comment", func(t *testing.T) { + // TODO: [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed + _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) + id := testClientHelper().Ids.RandomAccountObjectIdentifier() _, connectionCleanup := testClientHelper().Connection.Create(t, id) t.Cleanup(connectionCleanup) @@ -272,6 +276,9 @@ func TestInt_Connections(t *testing.T) { }) t.Run("Drop", func(t *testing.T) { + // TODO: [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed + _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) + id := testClientHelper().Ids.RandomAccountObjectIdentifier() _, connectionCleanup := testClientHelper().Connection.Create(t, id) t.Cleanup(connectionCleanup) @@ -289,6 +296,9 @@ func TestInt_Connections(t *testing.T) { }) t.Run("Drop with if exists", func(t *testing.T) { + // TODO: [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed + _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) + err = client.Connections.Drop(ctx, sdk.NewDropConnectionRequest(NonExistingAccountObjectIdentifier)) require.ErrorIs(t, err, sdk.ErrObjectNotExistOrAuthorized) @@ -297,6 +307,9 @@ func TestInt_Connections(t *testing.T) { }) t.Run("Show", func(t *testing.T) { + // TODO: [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed + _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) + id1 := testClientHelper().Ids.RandomAccountObjectIdentifier() id2 := testClientHelper().Ids.RandomAccountObjectIdentifier() @@ -313,6 +326,9 @@ func TestInt_Connections(t *testing.T) { }) t.Run("Show with Like", func(t *testing.T) { + // TODO: [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed + _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) + id1 := testClientHelper().Ids.RandomAccountObjectIdentifier() id2 := testClientHelper().Ids.RandomAccountObjectIdentifier() @@ -332,6 +348,9 @@ func TestInt_Connections(t *testing.T) { }) t.Run("ShowByID", func(t *testing.T) { + // TODO: [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed + _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) + id := testClientHelper().Ids.RandomAccountObjectIdentifier() _, connectionCleanup := testClientHelper().Connection.Create(t, id)