diff --git a/go.mod b/go.mod index ea5c7e0680..659fe3f099 100644 --- a/go.mod +++ b/go.mod @@ -119,7 +119,7 @@ require ( github.com/zclconf/go-cty v1.14.0 // indirect github.com/zeebo/xxh3 v1.0.2 // indirect golang.org/x/mod v0.13.0 // indirect - golang.org/x/net v0.16.0 // indirect + golang.org/x/net v0.17.0 // indirect golang.org/x/sync v0.4.0 // indirect golang.org/x/sys v0.13.0 // indirect golang.org/x/term v0.13.0 // indirect diff --git a/go.sum b/go.sum index a6855135cc..9010ac8935 100644 --- a/go.sum +++ b/go.sum @@ -342,8 +342,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= -golang.org/x/net v0.16.0 h1:7eBu7KsSvFDtSXUIDbh3aqlK4DPsZ1rByC8PFfBThos= -golang.org/x/net v0.16.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= +golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= +golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= diff --git a/pkg/sdk/accounts.go b/pkg/sdk/accounts.go index fc556cd7e9..f1af243dad 100644 --- a/pkg/sdk/accounts.go +++ b/pkg/sdk/accounts.go @@ -237,10 +237,10 @@ type AccountRename struct { } func (opts *AccountRename) validate() error { - if !validObjectidentifier(opts.Name) { + if !ValidObjectIdentifier(opts.Name) { return fmt.Errorf("Name must be set") } - if !validObjectidentifier(opts.NewName) { + if !ValidObjectIdentifier(opts.NewName) { return fmt.Errorf("NewName must be set") } return nil @@ -252,7 +252,7 @@ type AccountDrop struct { } func (opts *AccountDrop) validate() error { - if !validObjectidentifier(opts.Name) { + if !ValidObjectIdentifier(opts.Name) { return fmt.Errorf("Name must be set") } if valueSet(opts.OldURL) { @@ -386,7 +386,7 @@ func (c *accounts) ShowByID(ctx context.Context, id AccountObjectIdentifier) (*A return &account, nil } } - return nil, errObjectNotExistOrAuthorized + return nil, ErrObjectNotExistOrAuthorized } // DropAccountOptions is based on https://docs.snowflake.com/en/sql-reference/sql/drop-account. @@ -399,7 +399,7 @@ type DropAccountOptions struct { } func (opts *DropAccountOptions) validate() error { - if !validObjectidentifier(opts.name) { + if !ValidObjectIdentifier(opts.name) { return fmt.Errorf("Name must be set") } if !validateIntGreaterThanOrEqual(opts.gracePeriodInDays, 3) { diff --git a/pkg/sdk/alerts.go b/pkg/sdk/alerts.go index 9ffd555b2b..9ba1f3dd48 100644 --- a/pkg/sdk/alerts.go +++ b/pkg/sdk/alerts.go @@ -55,7 +55,7 @@ type AlertCondition struct { } func (opts *CreateAlertOptions) validate() error { - if !validObjectidentifier(opts.name) { + if !ValidObjectIdentifier(opts.name) { return errors.New("invalid object identifier") } @@ -115,7 +115,7 @@ type AlterAlertOptions struct { } func (opts *AlterAlertOptions) validate() error { - if !validObjectidentifier(opts.name) { + if !ValidObjectIdentifier(opts.name) { return errors.New("invalid object identifier") } @@ -175,8 +175,8 @@ type dropAlertOptions struct { } func (opts *dropAlertOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } return nil } @@ -297,7 +297,7 @@ func (v *alerts) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Aler return &alert, nil } } - return nil, errObjectNotExistOrAuthorized + return nil, ErrObjectNotExistOrAuthorized } // describeAlertOptions is based on https://docs.snowflake.com/en/sql-reference/sql/desc-alert. @@ -308,8 +308,8 @@ type describeAlertOptions struct { } func (v *describeAlertOptions) validate() error { - if !validObjectidentifier(v.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(v.name) { + return ErrInvalidObjectIdentifier } return nil } diff --git a/pkg/sdk/alerts_test.go b/pkg/sdk/alerts_test.go index e51fccc5a7..d70f726d39 100644 --- a/pkg/sdk/alerts_test.go +++ b/pkg/sdk/alerts_test.go @@ -4,16 +4,16 @@ import ( "fmt" "testing" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestAlertCreate(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() t.Run("with complete options", func(t *testing.T) { - newComment := randomString(t) + newComment := random.String() warehouse := AccountObjectIdentifier{"warehouse"} existsCondition := "SELECT 1" condition := AlertCondition{[]string{existsCondition}} @@ -39,7 +39,7 @@ func TestAlertCreate(t *testing.T) { } func TestAlertAlter(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() t.Run("fail without alter action specified", func(t *testing.T) { opts := &AlterAlertOptions{ @@ -50,7 +50,7 @@ func TestAlertAlter(t *testing.T) { }) t.Run("fail when 2 alter actions specified", func(t *testing.T) { - newComment := randomString(t) + newComment := random.String() opts := &AlterAlertOptions{ name: id, Action: &AlertActionResume, @@ -91,7 +91,7 @@ func TestAlertAlter(t *testing.T) { }) t.Run("with set", func(t *testing.T) { - newComment := randomString(t) + newComment := random.String() opts := &AlterAlertOptions{ name: id, Set: &AlertSet{ @@ -150,7 +150,7 @@ func TestAlertAlter(t *testing.T) { } func TestAlertDrop(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &dropAlertOptions{} @@ -172,7 +172,7 @@ func TestAlertDrop(t *testing.T) { } func TestAlertShow(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &ShowAlertOptions{} @@ -271,7 +271,7 @@ func TestAlertShow(t *testing.T) { } func TestAlertDescribe(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &describeAlertOptions{} diff --git a/pkg/sdk/client.go b/pkg/sdk/client.go index 941eba4f4e..6c535c086b 100644 --- a/pkg/sdk/client.go +++ b/pkg/sdk/client.go @@ -52,6 +52,14 @@ type Client struct { Warehouses Warehouses } +func (c *Client) GetAccountLocator() string { + return c.accountLocator +} + +func (c *Client) GetConfig() *gosnowflake.Config { + return c.config +} + func NewDefaultClient() (*Client, error) { return NewClient(nil) } diff --git a/pkg/sdk/collection_helpers.go b/pkg/sdk/collection_helpers.go deleted file mode 100644 index 1d753981ab..0000000000 --- a/pkg/sdk/collection_helpers.go +++ /dev/null @@ -1,10 +0,0 @@ -package sdk - -func findOne[T any](collection []T, condition func(T) bool) (*T, error) { - for _, o := range collection { - if condition(o) { - return &o, nil - } - } - return nil, errObjectNotExistOrAuthorized -} diff --git a/pkg/sdk/database_role_impl.go b/pkg/sdk/database_role_impl.go index d6b627b5cf..7876bc63bf 100644 --- a/pkg/sdk/database_role_impl.go +++ b/pkg/sdk/database_role_impl.go @@ -1,6 +1,10 @@ package sdk -import "context" +import ( + "context" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/collections" +) var _ DatabaseRoles = (*databaseRoles)(nil) @@ -42,7 +46,7 @@ func (v *databaseRoles) ShowByID(ctx context.Context, id DatabaseObjectIdentifie return nil, err } - return findOne(databaseRoles, func(r DatabaseRole) bool { return r.Name == id.Name() }) + return collections.FindOne(databaseRoles, func(r DatabaseRole) bool { return r.Name == id.Name() }) } func (v *databaseRoles) Grant(ctx context.Context, request *GrantDatabaseRoleRequest) error { diff --git a/pkg/sdk/database_role_test.go b/pkg/sdk/database_role_test.go index d26d734789..f40c2514be 100644 --- a/pkg/sdk/database_role_test.go +++ b/pkg/sdk/database_role_test.go @@ -2,10 +2,12 @@ package sdk import ( "testing" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" ) func TestDatabaseRoleCreate(t *testing.T) { - id := randomDatabaseObjectIdentifier(t) + id := RandomDatabaseObjectIdentifier() defaultOpts := func() *createDatabaseRoleOptions { return &createDatabaseRoleOptions{ @@ -15,13 +17,13 @@ func TestDatabaseRoleCreate(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *createDatabaseRoleOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: incorrect identifier", func(t *testing.T) { opts := defaultOpts() opts.name = NewDatabaseObjectIdentifier("", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: both ifNotExists and orReplace present", func(t *testing.T) { @@ -36,7 +38,7 @@ func TestDatabaseRoleCreate(t *testing.T) { opts.name = NewDatabaseObjectIdentifier("", "") opts.IfNotExists = Bool(true) opts.OrReplace = Bool(true) - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier, errOneOf("OrReplace", "IfNotExists")) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier, errOneOf("OrReplace", "IfNotExists")) }) t.Run("basic", func(t *testing.T) { @@ -54,7 +56,7 @@ func TestDatabaseRoleCreate(t *testing.T) { } func TestDatabaseRoleAlter(t *testing.T) { - id := randomDatabaseObjectIdentifier(t) + id := RandomDatabaseObjectIdentifier() defaultOpts := func() *alterDatabaseRoleOptions { return &alterDatabaseRoleOptions{ @@ -64,13 +66,13 @@ func TestDatabaseRoleAlter(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *alterDatabaseRoleOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: incorrect identifier", func(t *testing.T) { opts := defaultOpts() opts.name = NewDatabaseObjectIdentifier("", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: no alter action", func(t *testing.T) { @@ -94,17 +96,17 @@ func TestDatabaseRoleAlter(t *testing.T) { opts.Rename = &DatabaseRoleRename{ Name: NewDatabaseObjectIdentifier("", ""), } - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: new name from different db", func(t *testing.T) { - newId := NewDatabaseObjectIdentifier(id.DatabaseName()+randomStringN(t, 1), randomStringN(t, 12)) + newId := NewDatabaseObjectIdentifier(id.DatabaseName()+random.StringN(1), random.StringN(12)) opts := defaultOpts() opts.Rename = &DatabaseRoleRename{ Name: newId, } - assertOptsInvalidJoinedErrors(t, opts, errDifferentDatabase) + assertOptsInvalidJoinedErrors(t, opts, ErrDifferentDatabase) }) t.Run("validation: no property to unset", func(t *testing.T) { @@ -116,7 +118,7 @@ func TestDatabaseRoleAlter(t *testing.T) { }) t.Run("rename", func(t *testing.T) { - newId := NewDatabaseObjectIdentifier(id.DatabaseName(), randomStringN(t, 12)) + newId := NewDatabaseObjectIdentifier(id.DatabaseName(), random.StringN(12)) opts := defaultOpts() opts.Rename = &DatabaseRoleRename{ @@ -154,7 +156,7 @@ func TestDatabaseRoleAlter(t *testing.T) { } func TestDatabaseRoleDrop(t *testing.T) { - id := randomDatabaseObjectIdentifier(t) + id := RandomDatabaseObjectIdentifier() defaultOpts := func() *dropDatabaseRoleOptions { return &dropDatabaseRoleOptions{ @@ -164,13 +166,13 @@ func TestDatabaseRoleDrop(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *dropDatabaseRoleOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: incorrect identifier", func(t *testing.T) { opts := defaultOpts() opts.name = NewDatabaseObjectIdentifier("", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("empty options", func(t *testing.T) { @@ -186,7 +188,7 @@ func TestDatabaseRoleDrop(t *testing.T) { } func TestDatabaseRolesShow(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := RandomAccountObjectIdentifier() defaultOpts := func() *showDatabaseRoleOptions { return &showDatabaseRoleOptions{ @@ -196,19 +198,19 @@ func TestDatabaseRolesShow(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *showDatabaseRoleOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: incorrect identifier", func(t *testing.T) { opts := defaultOpts() opts.Database = NewAccountObjectIdentifier("") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: empty like", func(t *testing.T) { opts := defaultOpts() opts.Like = &Like{} - assertOptsInvalidJoinedErrors(t, opts, errPatternRequiredForLikeKeyword) + assertOptsInvalidJoinedErrors(t, opts, ErrPatternRequiredForLikeKeyword) }) t.Run("show", func(t *testing.T) { @@ -226,9 +228,9 @@ func TestDatabaseRolesShow(t *testing.T) { } func TestDatabaseRoles_Grant(t *testing.T) { - id := randomDatabaseObjectIdentifier(t) - databaseRoleId := randomDatabaseObjectIdentifier(t) - accountRoleId := randomAccountObjectIdentifier(t) + id := RandomDatabaseObjectIdentifier() + databaseRoleId := RandomDatabaseObjectIdentifier() + accountRoleId := RandomAccountObjectIdentifier() setUpOpts := func() *grantDatabaseRoleOptions { return &grantDatabaseRoleOptions{ @@ -238,13 +240,13 @@ func TestDatabaseRoles_Grant(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *grantDatabaseRoleOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: invalid identifier", func(t *testing.T) { opts := setUpOpts() opts.name = NewDatabaseObjectIdentifier("", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: no role", func(t *testing.T) { @@ -275,9 +277,9 @@ func TestDatabaseRoles_Grant(t *testing.T) { } func TestDatabaseRoles_Revoke(t *testing.T) { - id := randomDatabaseObjectIdentifier(t) - databaseRoleId := randomDatabaseObjectIdentifier(t) - accountRoleId := randomAccountObjectIdentifier(t) + id := RandomDatabaseObjectIdentifier() + databaseRoleId := RandomDatabaseObjectIdentifier() + accountRoleId := RandomAccountObjectIdentifier() setUpOpts := func() *revokeDatabaseRoleOptions { return &revokeDatabaseRoleOptions{ @@ -287,13 +289,13 @@ func TestDatabaseRoles_Revoke(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *revokeDatabaseRoleOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: invalid identifier", func(t *testing.T) { opts := setUpOpts() opts.name = NewDatabaseObjectIdentifier("", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: no role", func(t *testing.T) { @@ -324,8 +326,8 @@ func TestDatabaseRoles_Revoke(t *testing.T) { } func TestDatabaseRoles_GrantToShare(t *testing.T) { - id := randomDatabaseObjectIdentifier(t) - share := randomAccountObjectIdentifier(t) + id := RandomDatabaseObjectIdentifier() + share := RandomAccountObjectIdentifier() setUpOpts := func() *grantDatabaseRoleToShareOptions { return &grantDatabaseRoleToShareOptions{ @@ -336,19 +338,19 @@ func TestDatabaseRoles_GrantToShare(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *grantDatabaseRoleToShareOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: invalid identifier", func(t *testing.T) { opts := setUpOpts() opts.name = NewDatabaseObjectIdentifier("", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: invalid share identifier", func(t *testing.T) { opts := setUpOpts() opts.Share = NewAccountObjectIdentifier("") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("grant to share", func(t *testing.T) { @@ -359,8 +361,8 @@ func TestDatabaseRoles_GrantToShare(t *testing.T) { } func TestDatabaseRoles_RevokeFromShare(t *testing.T) { - id := randomDatabaseObjectIdentifier(t) - share := randomAccountObjectIdentifier(t) + id := RandomDatabaseObjectIdentifier() + share := RandomAccountObjectIdentifier() setUpOpts := func() *revokeDatabaseRoleFromShareOptions { return &revokeDatabaseRoleFromShareOptions{ @@ -371,19 +373,19 @@ func TestDatabaseRoles_RevokeFromShare(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *revokeDatabaseRoleFromShareOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: invalid identifier", func(t *testing.T) { opts := setUpOpts() opts.name = NewDatabaseObjectIdentifier("", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: invalid share identifier", func(t *testing.T) { opts := setUpOpts() opts.Share = NewAccountObjectIdentifier("") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("revoke from share", func(t *testing.T) { diff --git a/pkg/sdk/database_role_validations.go b/pkg/sdk/database_role_validations.go index 312036232b..c62162e130 100644 --- a/pkg/sdk/database_role_validations.go +++ b/pkg/sdk/database_role_validations.go @@ -13,15 +13,13 @@ var ( _ validatable = new(revokeDatabaseRoleFromShareOptions) ) -var errDifferentDatabase = errors.New("database must be the same") - func (opts *createDatabaseRoleOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if everyValueSet(opts.OrReplace, opts.IfNotExists) && *opts.OrReplace && *opts.IfNotExists { errs = append(errs, errOneOf("OrReplace", "IfNotExists")) @@ -31,11 +29,11 @@ func (opts *createDatabaseRoleOptions) validate() error { func (opts *alterDatabaseRoleOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if ok := exactlyOneValueSet( opts.Rename, @@ -45,11 +43,11 @@ func (opts *alterDatabaseRoleOptions) validate() error { errs = append(errs, errAlterNeedsExactlyOneAction) } if rename := opts.Rename; valueSet(rename) { - if !validObjectidentifier(rename.Name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(rename.Name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if opts.name.DatabaseName() != rename.Name.DatabaseName() { - errs = append(errs, errDifferentDatabase) + errs = append(errs, ErrDifferentDatabase) } } if unset := opts.Unset; valueSet(unset) { @@ -62,36 +60,36 @@ func (opts *alterDatabaseRoleOptions) validate() error { func (opts *dropDatabaseRoleOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } return errors.Join(errs...) } func (opts *showDatabaseRoleOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.Database) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.Database) { + errs = append(errs, ErrInvalidObjectIdentifier) } if valueSet(opts.Like) && !valueSet(opts.Like.Pattern) { - errs = append(errs, errPatternRequiredForLikeKeyword) + errs = append(errs, ErrPatternRequiredForLikeKeyword) } return errors.Join(errs...) } func (opts *grantDatabaseRoleOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if ok := exactlyOneValueSet(opts.ParentRole.DatabaseRoleName, opts.ParentRole.AccountRoleName); !ok { errs = append(errs, errOneOf("DatabaseRoleName", "AccountRoleName")) @@ -101,11 +99,11 @@ func (opts *grantDatabaseRoleOptions) validate() error { func (opts *revokeDatabaseRoleOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if ok := exactlyOneValueSet(opts.ParentRole.DatabaseRoleName, opts.ParentRole.AccountRoleName); !ok { errs = append(errs, errOneOf("DatabaseRoleName", "AccountRoleName")) @@ -115,28 +113,28 @@ func (opts *revokeDatabaseRoleOptions) validate() error { func (opts *grantDatabaseRoleToShareOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } - if !validObjectidentifier(opts.Share) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.Share) { + errs = append(errs, ErrInvalidObjectIdentifier) } return errors.Join(errs...) } func (opts *revokeDatabaseRoleFromShareOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } - if !validObjectidentifier(opts.Share) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.Share) { + errs = append(errs, ErrInvalidObjectIdentifier) } return errors.Join(errs...) } diff --git a/pkg/sdk/databases.go b/pkg/sdk/databases.go index 4891d30649..9674e9561c 100644 --- a/pkg/sdk/databases.go +++ b/pkg/sdk/databases.go @@ -185,11 +185,11 @@ type CreateSharedDatabaseOptions struct { } func (opts *CreateSharedDatabaseOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } - if !validObjectidentifier(opts.fromShare) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.fromShare) { + return ErrInvalidObjectIdentifier } return nil } @@ -223,11 +223,11 @@ type CreateSecondaryDatabaseOptions struct { } func (opts *CreateSecondaryDatabaseOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } - if !validObjectidentifier(opts.primaryDatabase) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.primaryDatabase) { + return ErrInvalidObjectIdentifier } return nil } @@ -262,14 +262,14 @@ type AlterDatabaseOptions struct { } func (opts *AlterDatabaseOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } - if validObjectidentifier(opts.NewName) && anyValueSet(opts.Set, opts.Unset, opts.SwapWith) { + if ValidObjectIdentifier(opts.NewName) && anyValueSet(opts.Set, opts.Unset, opts.SwapWith) { return errors.New("RENAME TO cannot be set with other options") } - if validObjectidentifier(opts.SwapWith) && anyValueSet(opts.Set, opts.Unset, opts.NewName) { + if ValidObjectIdentifier(opts.SwapWith) && anyValueSet(opts.Set, opts.Unset, opts.NewName) { return errors.New("SWAP WITH cannot be set with other options") } @@ -344,8 +344,8 @@ type AlterDatabaseReplicationOptions struct { } func (opts *AlterDatabaseReplicationOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } if everyValueNil(opts.EnableReplication, opts.DisableReplication, opts.Refresh) { return errors.New("one of ENABLE REPLICATION, DISABLE REPLICATION or REFRESH must be set") @@ -410,8 +410,8 @@ type AlterDatabaseFailoverOptions struct { } func (opts *AlterDatabaseFailoverOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } if everyValueNil(opts.EnableFailover, opts.DisableFailover, opts.Primary) { return errors.New("one of ENABLE FAILOVER, DISABLE FAILOVER or PRIMARY must be set") @@ -473,8 +473,8 @@ type DropDatabaseOptions struct { } func (opts *DropDatabaseOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } return nil } @@ -503,8 +503,8 @@ type undropDatabaseOptions struct { } func (opts *undropDatabaseOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } return nil } @@ -563,7 +563,7 @@ func (v *databases) ShowByID(ctx context.Context, id AccountObjectIdentifier) (* return &database, nil } } - return nil, errObjectNotExistOrAuthorized + return nil, ErrObjectNotExistOrAuthorized } type DatabaseDetails struct { @@ -584,8 +584,8 @@ type describeDatabaseOptions struct { } func (opts *describeDatabaseOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } return nil } diff --git a/pkg/sdk/dynamic_table_impl.go b/pkg/sdk/dynamic_table_impl.go index 3743c06898..9010aa257b 100644 --- a/pkg/sdk/dynamic_table_impl.go +++ b/pkg/sdk/dynamic_table_impl.go @@ -2,6 +2,8 @@ package sdk import ( "context" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/collections" ) var _ DynamicTables = (*dynamicTables)(nil) @@ -50,7 +52,7 @@ func (v *dynamicTables) ShowByID(ctx context.Context, id AccountObjectIdentifier if err != nil { return nil, err } - return findOne(dynamicTables, func(r DynamicTable) bool { return r.Name == id.Name() }) + return collections.FindOne(dynamicTables, func(r DynamicTable) bool { return r.Name == id.Name() }) } func (s *CreateDynamicTableRequest) toOpts() *createDynamicTableOptions { diff --git a/pkg/sdk/dynamic_table_test.go b/pkg/sdk/dynamic_table_test.go index b8cd043397..cf1536fe31 100644 --- a/pkg/sdk/dynamic_table_test.go +++ b/pkg/sdk/dynamic_table_test.go @@ -5,7 +5,7 @@ import ( ) func TestDynamicTableCreate(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() defaultOpts := func() *createDynamicTableOptions { return &createDynamicTableOptions{ name: id, @@ -20,13 +20,13 @@ func TestDynamicTableCreate(t *testing.T) { } t.Run("validation: nil options", func(t *testing.T) { var opts *createDynamicTableOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: incorrect identifier", func(t *testing.T) { opts := defaultOpts() opts.name = NewSchemaObjectIdentifier("", "", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("basic", func(t *testing.T) { @@ -44,7 +44,7 @@ func TestDynamicTableCreate(t *testing.T) { } func TestDynamicTableAlter(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() defaultOpts := func() *alterDynamicTableOptions { return &alterDynamicTableOptions{ name: id, @@ -53,13 +53,13 @@ func TestDynamicTableAlter(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *alterDynamicTableOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: incorrect identifier", func(t *testing.T) { opts := defaultOpts() opts.name = NewSchemaObjectIdentifier("", "", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: no alter action", func(t *testing.T) { @@ -106,7 +106,7 @@ func TestDynamicTableAlter(t *testing.T) { } func TestDynamicTableDrop(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() defaultOpts := func() *dropDynamicTableOptions { return &dropDynamicTableOptions{ name: id, @@ -115,13 +115,13 @@ func TestDynamicTableDrop(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *dropDynamicTableOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: incorrect identifier", func(t *testing.T) { opts := defaultOpts() opts.name = NewSchemaObjectIdentifier("", "", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("empty options", func(t *testing.T) { @@ -131,20 +131,20 @@ func TestDynamicTableDrop(t *testing.T) { } func TestDynamicTableShow(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() defaultOpts := func() *showDynamicTableOptions { return &showDynamicTableOptions{} } t.Run("validation: nil options", func(t *testing.T) { var opts *showDynamicTableOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: empty like", func(t *testing.T) { opts := defaultOpts() opts.Like = &Like{} - assertOptsInvalidJoinedErrors(t, opts, errPatternRequiredForLikeKeyword) + assertOptsInvalidJoinedErrors(t, opts, ErrPatternRequiredForLikeKeyword) }) t.Run("show with in", func(t *testing.T) { @@ -176,7 +176,7 @@ func TestDynamicTableShow(t *testing.T) { } func TestDynamicTableDescribe(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() defaultOpts := func() *describeDynamicTableOptions { return &describeDynamicTableOptions{ name: id, @@ -185,13 +185,13 @@ func TestDynamicTableDescribe(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *describeDynamicTableOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: incorrect identifier", func(t *testing.T) { opts := defaultOpts() opts.name = NewSchemaObjectIdentifier("", "", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("describe", func(t *testing.T) { diff --git a/pkg/sdk/dynamic_table_validations.go b/pkg/sdk/dynamic_table_validations.go index 80fd2e0e36..5845b7590e 100644 --- a/pkg/sdk/dynamic_table_validations.go +++ b/pkg/sdk/dynamic_table_validations.go @@ -15,7 +15,7 @@ var ( func (tl *TargetLag) validate() error { if tl == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error if everyValueSet(tl.Lagtime, tl.Downstream) { @@ -26,14 +26,14 @@ func (tl *TargetLag) validate() error { func (opts *createDynamicTableOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } - if !validObjectidentifier(opts.warehouse) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.warehouse) { + errs = append(errs, ErrInvalidObjectIdentifier) } return errors.Join(errs...) } @@ -45,8 +45,8 @@ func (dts *DynamicTableSet) validate() error { } if valueSet(dts.Warehouse) { - if !validObjectidentifier(*dts.Warehouse) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(*dts.Warehouse) { + errs = append(errs, ErrInvalidObjectIdentifier) } } return errors.Join(errs...) @@ -54,11 +54,11 @@ func (dts *DynamicTableSet) validate() error { func (opts *alterDynamicTableOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if ok := exactlyOneValueSet( opts.Suspend, @@ -79,11 +79,11 @@ func (opts *alterDynamicTableOptions) validate() error { func (opts *showDynamicTableOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error if valueSet(opts.Like) && !valueSet(opts.Like.Pattern) { - errs = append(errs, errPatternRequiredForLikeKeyword) + errs = append(errs, ErrPatternRequiredForLikeKeyword) } if valueSet(opts.In) && !exactlyOneValueSet(opts.In.Account, opts.In.Database, opts.In.Schema) { errs = append(errs, errScopeRequiredForInKeyword) @@ -93,23 +93,23 @@ func (opts *showDynamicTableOptions) validate() error { func (opts *dropDynamicTableOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } return errors.Join(errs...) } func (opts *describeDynamicTableOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } return errors.Join(errs...) } diff --git a/pkg/sdk/errors.go b/pkg/sdk/errors.go index 60acd8e449..e90d643889 100644 --- a/pkg/sdk/errors.go +++ b/pkg/sdk/errors.go @@ -8,15 +8,16 @@ import ( ) var ( - errNilOptions = errors.New("options cannot be nil") - errPatternRequiredForLikeKeyword = errors.New("pattern must be specified for like keyword") + ErrNilOptions = errors.New("options cannot be nil") + ErrPatternRequiredForLikeKeyword = errors.New("pattern must be specified for like keyword") // go-snowflake errors. - errObjectNotExistOrAuthorized = errors.New("object does not exist or not authorized") - errAccountIsEmpty = errors.New("account is empty") + ErrObjectNotExistOrAuthorized = errors.New("object does not exist or not authorized") + ErrAccountIsEmpty = errors.New("account is empty") // snowflake-sdk errors. - errInvalidObjectIdentifier = errors.New("invalid object identifier") + ErrInvalidObjectIdentifier = errors.New("invalid object identifier") + ErrDifferentDatabase = errors.New("database must be the same") ) func errOneOf(structName string, fieldNames ...string) error { @@ -41,8 +42,8 @@ func decodeDriverError(err error) error { } log.Printf("[DEBUG] err: %v\n", err) m := map[string]error{ - "does not exist or not authorized": errObjectNotExistOrAuthorized, - "account is empty": errAccountIsEmpty, + "does not exist or not authorized": ErrObjectNotExistOrAuthorized, + "account is empty": ErrAccountIsEmpty, } for k, v := range m { if strings.Contains(err.Error(), k) { diff --git a/pkg/sdk/external_tables_dto.go b/pkg/sdk/external_tables_dto.go index 7ae04de24f..3bcc01103f 100644 --- a/pkg/sdk/external_tables_dto.go +++ b/pkg/sdk/external_tables_dto.go @@ -34,6 +34,10 @@ type CreateExternalTableRequest struct { tag []*TagAssociationRequest } +func (s *CreateExternalTableRequest) GetColumns() []*ExternalTableColumnRequest { + return s.columns +} + type ExternalTableColumnRequest struct { name string // required dataType DataType // required diff --git a/pkg/sdk/external_tables_impl.go b/pkg/sdk/external_tables_impl.go index 54d6c2833e..b799040bf0 100644 --- a/pkg/sdk/external_tables_impl.go +++ b/pkg/sdk/external_tables_impl.go @@ -1,6 +1,10 @@ package sdk -import "context" +import ( + "context" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/collections" +) var _ ExternalTables = (*externalTables)(nil) @@ -46,8 +50,8 @@ func (v *externalTables) Show(ctx context.Context, req *ShowExternalTableRequest } func (v *externalTables) ShowByID(ctx context.Context, req *ShowExternalTableByIDRequest) (*ExternalTable, error) { - if !validObjectidentifier(req.id) { - return nil, errInvalidObjectIdentifier + if !ValidObjectIdentifier(req.id) { + return nil, ErrInvalidObjectIdentifier } externalTables, err := v.client.ExternalTables.Show(ctx, NewShowExternalTableRequest().WithLike(String(req.id.Name()))) @@ -55,7 +59,7 @@ func (v *externalTables) ShowByID(ctx context.Context, req *ShowExternalTableByI return nil, err } - return findOne(externalTables, func(t ExternalTable) bool { return t.ID().FullyQualifiedName() == req.id.FullyQualifiedName() }) + return collections.FindOne(externalTables, func(t ExternalTable) bool { return t.ID().FullyQualifiedName() == req.id.FullyQualifiedName() }) } func (v *externalTables) DescribeColumns(ctx context.Context, req *DescribeExternalTableColumnsRequest) ([]ExternalTableColumnDetails, error) { diff --git a/pkg/sdk/external_tables_test.go b/pkg/sdk/external_tables_test.go index 987d37191f..71ea7b83e7 100644 --- a/pkg/sdk/external_tables_test.go +++ b/pkg/sdk/external_tables_test.go @@ -89,7 +89,7 @@ func TestExternalTablesCreate(t *testing.T) { assertOptsInvalidJoinedErrors( t, opts, errOneOf("CreateExternalTableOptions", "OrReplace", "IfNotExists"), - errInvalidObjectIdentifier, + ErrInvalidObjectIdentifier, errNotSet("CreateExternalTableOptions", "Location"), errNotSet("CreateExternalTableOptions", "FileFormat"), ) @@ -151,7 +151,7 @@ func TestExternalTablesCreateWithManualPartitioning(t *testing.T) { assertOptsInvalidJoinedErrors( t, opts, errOneOf("CreateWithManualPartitioningExternalTableOptions", "OrReplace", "IfNotExists"), - errInvalidObjectIdentifier, + ErrInvalidObjectIdentifier, errNotSet("CreateWithManualPartitioningExternalTableOptions", "Location"), errNotSet("CreateWithManualPartitioningExternalTableOptions", "FileFormat"), ) @@ -211,7 +211,7 @@ func TestExternalTablesCreateDeltaLake(t *testing.T) { assertOptsInvalidJoinedErrors( t, opts, errOneOf("CreateDeltaLakeExternalTableOptions", "OrReplace", "IfNotExists"), - errInvalidObjectIdentifier, + ErrInvalidObjectIdentifier, errNotSet("CreateDeltaLakeExternalTableOptions", "Location"), errNotSet("CreateDeltaLakeExternalTableOptions", "FileFormat"), ) @@ -260,7 +260,7 @@ func TestExternalTableUsingTemplateOpts(t *testing.T) { } assertOptsInvalidJoinedErrors( t, opts, - errInvalidObjectIdentifier, + ErrInvalidObjectIdentifier, errNotSet("CreateExternalTableUsingTemplateOptions", "Query"), errNotSet("CreateExternalTableUsingTemplateOptions", "Location"), errNotSet("CreateExternalTableUsingTemplateOptions", "FileFormat"), @@ -355,7 +355,7 @@ func TestExternalTablesAlter(t *testing.T) { } assertOptsInvalidJoinedErrors( t, opts, - errInvalidObjectIdentifier, + ErrInvalidObjectIdentifier, errOneOf("AlterExternalTableOptions", "Refresh", "AddFiles", "RemoveFiles", "AutoRefresh", "SetTag", "UnsetTag"), ) }) @@ -399,7 +399,7 @@ func TestExternalTablesAlterPartitions(t *testing.T) { } assertOptsInvalidJoinedErrors( t, opts, - errInvalidObjectIdentifier, + ErrInvalidObjectIdentifier, errOneOf("AlterExternalTablePartitionOptions", "AddPartitions", "DropPartition"), ) }) @@ -439,7 +439,7 @@ func TestExternalTablesDrop(t *testing.T) { assertOptsInvalidJoinedErrors( t, opts, - errInvalidObjectIdentifier, + ErrInvalidObjectIdentifier, errOneOf("ExternalTableDropOption", "Restrict", "Cascade"), ) }) @@ -495,7 +495,7 @@ func TestExternalTablesShow(t *testing.T) { assertOptsInvalidJoinedErrors( t, opts, - errInvalidObjectIdentifier, + ErrInvalidObjectIdentifier, errOneOf("ExternalTableDropOption", "Restrict", "Cascade"), ) }) diff --git a/pkg/sdk/external_tables_validations.go b/pkg/sdk/external_tables_validations.go index 11abd40787..7d56812a59 100644 --- a/pkg/sdk/external_tables_validations.go +++ b/pkg/sdk/external_tables_validations.go @@ -23,8 +23,8 @@ func (opts *CreateExternalTableOptions) validate() error { if everyValueSet(opts.OrReplace, opts.IfNotExists) { errs = append(errs, errOneOf("CreateExternalTableOptions", "OrReplace", "IfNotExists")) } - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if !valueSet(opts.Location) { errs = append(errs, errNotSet("CreateExternalTableOptions", "Location")) @@ -49,8 +49,8 @@ func (opts *CreateWithManualPartitioningExternalTableOptions) validate() error { if everyValueSet(opts.OrReplace, opts.IfNotExists) { errs = append(errs, errOneOf("CreateWithManualPartitioningExternalTableOptions", "OrReplace", "IfNotExists")) } - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if !valueSet(opts.Location) { errs = append(errs, errNotSet("CreateWithManualPartitioningExternalTableOptions", "Location")) @@ -75,8 +75,8 @@ func (opts *CreateDeltaLakeExternalTableOptions) validate() error { if everyValueSet(opts.OrReplace, opts.IfNotExists) { errs = append(errs, errOneOf("CreateDeltaLakeExternalTableOptions", "OrReplace", "IfNotExists")) } - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if !valueSet(opts.Location) { errs = append(errs, errNotSet("CreateDeltaLakeExternalTableOptions", "Location")) @@ -98,8 +98,8 @@ func (opts *CreateDeltaLakeExternalTableOptions) validate() error { func (opts *CreateExternalTableUsingTemplateOptions) validate() error { var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if !valueSet(opts.Query) { errs = append(errs, errNotSet("CreateExternalTableUsingTemplateOptions", "Query")) @@ -124,8 +124,8 @@ func (opts *CreateExternalTableUsingTemplateOptions) validate() error { func (opts *AlterExternalTableOptions) validate() error { var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if anyValueSet(opts.Refresh, opts.AddFiles, opts.RemoveFiles, opts.AutoRefresh, opts.SetTag, opts.UnsetTag) && !exactlyOneValueSet(opts.Refresh, opts.AddFiles, opts.RemoveFiles, opts.AutoRefresh, opts.SetTag, opts.UnsetTag) { @@ -136,8 +136,8 @@ func (opts *AlterExternalTableOptions) validate() error { func (opts *AlterExternalTablePartitionOptions) validate() error { var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if everyValueSet(opts.AddPartitions, opts.DropPartition) { errs = append(errs, errOneOf("AlterExternalTablePartitionOptions", "AddPartitions", "DropPartition")) @@ -147,8 +147,8 @@ func (opts *AlterExternalTablePartitionOptions) validate() error { func (opts *DropExternalTableOptions) validate() error { var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if valueSet(opts.DropOption) { if err := opts.DropOption.validate(); err != nil { @@ -163,15 +163,15 @@ func (opts *ShowExternalTableOptions) validate() error { } func (v *describeExternalTableColumnsOptions) validate() error { - if !validObjectidentifier(v.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(v.name) { + return ErrInvalidObjectIdentifier } return nil } func (v *describeExternalTableStageOptions) validate() error { - if !validObjectidentifier(v.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(v.name) { + return ErrInvalidObjectIdentifier } return nil } diff --git a/pkg/sdk/failover_groups.go b/pkg/sdk/failover_groups.go index a285905800..f97163693a 100644 --- a/pkg/sdk/failover_groups.go +++ b/pkg/sdk/failover_groups.go @@ -68,8 +68,8 @@ type CreateFailoverGroupOptions struct { } func (opts *CreateFailoverGroupOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } return nil } @@ -102,11 +102,11 @@ type CreateSecondaryReplicationGroupOptions struct { } func (opts *CreateSecondaryReplicationGroupOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } - if !validObjectidentifier(opts.primaryFailoverGroup) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.primaryFailoverGroup) { + return ErrInvalidObjectIdentifier } return nil } @@ -142,8 +142,8 @@ type AlterSourceFailoverGroupOptions struct { } func (opts *AlterSourceFailoverGroupOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } if !exactlyOneValueSet(opts.Set, opts.Add, opts.Move, opts.Remove, opts.NewName) { return errors.New("exactly one of SET, ADD, MOVE, REMOVE, or NewName must be specified") @@ -248,8 +248,8 @@ type AlterTargetFailoverGroupOptions struct { } func (opts *AlterTargetFailoverGroupOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } if !exactlyOneValueSet(opts.Refresh, opts.Primary, opts.Suspend, opts.Resume) { return errors.New("must set one of [Refresh, Primary, Suspend, Resume]") @@ -282,8 +282,8 @@ type DropFailoverGroupOptions struct { } func (opts *DropFailoverGroupOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } return nil } @@ -475,7 +475,7 @@ func (v *failoverGroups) ShowByID(ctx context.Context, id AccountObjectIdentifie return &failoverGroup, nil } } - return nil, errObjectNotExistOrAuthorized + return nil, ErrObjectNotExistOrAuthorized } // showFailoverGroupDatabasesOptions is based on https://docs.snowflake.com/en/sql-reference/sql/show-databases-in-failover-group. @@ -486,8 +486,8 @@ type showFailoverGroupDatabasesOptions struct { } func (opts *showFailoverGroupDatabasesOptions) validate() error { - if !validObjectidentifier(opts.in) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.in) { + return ErrInvalidObjectIdentifier } return nil } @@ -525,8 +525,8 @@ type showFailoverGroupSharesOptions struct { } func (opts *showFailoverGroupSharesOptions) validate() error { - if !validObjectidentifier(opts.in) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.in) { + return ErrInvalidObjectIdentifier } return nil } diff --git a/pkg/sdk/file_format.go b/pkg/sdk/file_format.go index c0319a2fed..fa0171fcef 100644 --- a/pkg/sdk/file_format.go +++ b/pkg/sdk/file_format.go @@ -663,7 +663,7 @@ func (v *fileFormats) ShowByID(ctx context.Context, id SchemaObjectIdentifier) ( return &f, nil } } - return nil, errObjectNotExistOrAuthorized + return nil, ErrObjectNotExistOrAuthorized } type FileFormatDetails struct { diff --git a/pkg/sdk/grants_test.go b/pkg/sdk/grants_test.go index 733a59d44c..f478978562 100644 --- a/pkg/sdk/grants_test.go +++ b/pkg/sdk/grants_test.go @@ -655,9 +655,9 @@ func TestGrants_RevokePrivilegesFromDatabaseRoleRole(t *testing.T) { } func TestGrantPrivilegeToShare(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := RandomAccountObjectIdentifier() t.Run("on database", func(t *testing.T) { - otherID := randomAccountObjectIdentifier(t) + otherID := RandomAccountObjectIdentifier() opts := &grantPrivilegeToShareOptions{ privilege: ObjectPrivilegeUsage, On: &GrantPrivilegeToShareOn{ @@ -669,7 +669,7 @@ func TestGrantPrivilegeToShare(t *testing.T) { }) t.Run("on schema", func(t *testing.T) { - otherID := randomDatabaseObjectIdentifier(t) + otherID := RandomDatabaseObjectIdentifier() opts := &grantPrivilegeToShareOptions{ privilege: ObjectPrivilegeUsage, On: &GrantPrivilegeToShareOn{ @@ -681,7 +681,7 @@ func TestGrantPrivilegeToShare(t *testing.T) { }) t.Run("on table", func(t *testing.T) { - otherID := randomSchemaObjectIdentifier(t) + otherID := RandomSchemaObjectIdentifier() opts := &grantPrivilegeToShareOptions{ privilege: ObjectPrivilegeUsage, On: &GrantPrivilegeToShareOn{ @@ -695,7 +695,7 @@ func TestGrantPrivilegeToShare(t *testing.T) { }) t.Run("on all tables", func(t *testing.T) { - otherID := randomDatabaseObjectIdentifier(t) + otherID := RandomDatabaseObjectIdentifier() opts := &grantPrivilegeToShareOptions{ privilege: ObjectPrivilegeUsage, On: &GrantPrivilegeToShareOn{ @@ -709,7 +709,7 @@ func TestGrantPrivilegeToShare(t *testing.T) { }) t.Run("on view", func(t *testing.T) { - otherID := randomSchemaObjectIdentifier(t) + otherID := RandomSchemaObjectIdentifier() opts := &grantPrivilegeToShareOptions{ privilege: ObjectPrivilegeUsage, On: &GrantPrivilegeToShareOn{ @@ -722,9 +722,9 @@ func TestGrantPrivilegeToShare(t *testing.T) { } func TestRevokePrivilegeFromShare(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := RandomAccountObjectIdentifier() t.Run("on database", func(t *testing.T) { - otherID := randomAccountObjectIdentifier(t) + otherID := RandomAccountObjectIdentifier() opts := &revokePrivilegeFromShareOptions{ privilege: ObjectPrivilegeUsage, On: &RevokePrivilegeFromShareOn{ @@ -736,7 +736,7 @@ func TestRevokePrivilegeFromShare(t *testing.T) { }) t.Run("on schema", func(t *testing.T) { - otherID := randomDatabaseObjectIdentifier(t) + otherID := RandomDatabaseObjectIdentifier() opts := &revokePrivilegeFromShareOptions{ privilege: ObjectPrivilegeUsage, On: &RevokePrivilegeFromShareOn{ @@ -748,7 +748,7 @@ func TestRevokePrivilegeFromShare(t *testing.T) { }) t.Run("on table", func(t *testing.T) { - otherID := randomSchemaObjectIdentifier(t) + otherID := RandomSchemaObjectIdentifier() opts := &revokePrivilegeFromShareOptions{ privilege: ObjectPrivilegeUsage, On: &RevokePrivilegeFromShareOn{ @@ -762,7 +762,7 @@ func TestRevokePrivilegeFromShare(t *testing.T) { }) t.Run("on all tables", func(t *testing.T) { - otherID := randomDatabaseObjectIdentifier(t) + otherID := RandomDatabaseObjectIdentifier() opts := &revokePrivilegeFromShareOptions{ privilege: ObjectPrivilegeUsage, On: &RevokePrivilegeFromShareOn{ @@ -776,7 +776,7 @@ func TestRevokePrivilegeFromShare(t *testing.T) { }) t.Run("on view", func(t *testing.T) { - otherID := randomSchemaObjectIdentifier(t) + otherID := RandomSchemaObjectIdentifier() opts := &revokePrivilegeFromShareOptions{ privilege: ObjectPrivilegeUsage, On: &RevokePrivilegeFromShareOn{ @@ -790,7 +790,7 @@ func TestRevokePrivilegeFromShare(t *testing.T) { }) t.Run("on all views", func(t *testing.T) { - otherID := randomDatabaseObjectIdentifier(t) + otherID := RandomDatabaseObjectIdentifier() opts := &revokePrivilegeFromShareOptions{ privilege: ObjectPrivilegeUsage, On: &RevokePrivilegeFromShareOn{ @@ -941,7 +941,7 @@ func TestGrantShow(t *testing.T) { }) t.Run("on database", func(t *testing.T) { - dbID := randomAccountObjectIdentifier(t) + dbID := RandomAccountObjectIdentifier() opts := &ShowGrantOptions{ On: &ShowGrantsOn{ Object: &Object{ @@ -954,7 +954,7 @@ func TestGrantShow(t *testing.T) { }) t.Run("to role", func(t *testing.T) { - roleID := randomAccountObjectIdentifier(t) + roleID := RandomAccountObjectIdentifier() opts := &ShowGrantOptions{ To: &ShowGrantsTo{ Role: roleID, @@ -964,7 +964,7 @@ func TestGrantShow(t *testing.T) { }) t.Run("to user", func(t *testing.T) { - userID := randomAccountObjectIdentifier(t) + userID := RandomAccountObjectIdentifier() opts := &ShowGrantOptions{ To: &ShowGrantsTo{ User: userID, @@ -974,7 +974,7 @@ func TestGrantShow(t *testing.T) { }) t.Run("to share", func(t *testing.T) { - shareID := randomAccountObjectIdentifier(t) + shareID := RandomAccountObjectIdentifier() opts := &ShowGrantOptions{ To: &ShowGrantsTo{ Share: shareID, @@ -984,7 +984,7 @@ func TestGrantShow(t *testing.T) { }) t.Run("of role", func(t *testing.T) { - roleID := randomAccountObjectIdentifier(t) + roleID := RandomAccountObjectIdentifier() opts := &ShowGrantOptions{ Of: &ShowGrantsOf{ Role: roleID, @@ -994,7 +994,7 @@ func TestGrantShow(t *testing.T) { }) t.Run("of share", func(t *testing.T) { - shareID := randomAccountObjectIdentifier(t) + shareID := RandomAccountObjectIdentifier() opts := &ShowGrantOptions{ Of: &ShowGrantsOf{ Share: shareID, diff --git a/pkg/sdk/grants_validations.go b/pkg/sdk/grants_validations.go index 6ef10df215..05f6a598bb 100644 --- a/pkg/sdk/grants_validations.go +++ b/pkg/sdk/grants_validations.go @@ -114,8 +114,8 @@ func (opts *RevokePrivilegesFromAccountRoleOptions) validate() error { if err := opts.on.validate(); err != nil { return err } - if !validObjectidentifier(opts.accountRole) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.accountRole) { + return ErrInvalidObjectIdentifier } if everyValueSet(opts.Restrict, opts.Cascade) { return fmt.Errorf("either Restrict or Cascade can be set, or neither but not both") @@ -189,8 +189,8 @@ func (opts *RevokePrivilegesFromDatabaseRoleOptions) validate() error { if err := opts.on.validate(); err != nil { return err } - if !validObjectidentifier(opts.databaseRole) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.databaseRole) { + return ErrInvalidObjectIdentifier } if everyValueSet(opts.Restrict, opts.Cascade) { return fmt.Errorf("either Restrict or Cascade can be set, or neither but not both") @@ -199,8 +199,8 @@ func (opts *RevokePrivilegesFromDatabaseRoleOptions) validate() error { } func (opts *grantPrivilegeToShareOptions) validate() error { - if !validObjectidentifier(opts.to) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.to) { + return ErrInvalidObjectIdentifier } if !valueSet(opts.On) || opts.privilege == "" { return fmt.Errorf("on and privilege are required") @@ -231,8 +231,8 @@ func (v *OnTable) validate() error { } func (opts *revokePrivilegeFromShareOptions) validate() error { - if !validObjectidentifier(opts.from) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.from) { + return ErrInvalidObjectIdentifier } if !valueSet(opts.On) || opts.privilege == "" { return fmt.Errorf("on and privilege are required") diff --git a/pkg/sdk/integration_test_imports.go b/pkg/sdk/integration_test_imports.go index a996de087a..ea3398351c 100644 --- a/pkg/sdk/integration_test_imports.go +++ b/pkg/sdk/integration_test_imports.go @@ -3,73 +3,15 @@ package sdk import ( "context" "database/sql" - - "github.com/snowflakedb/gosnowflake" ) // All the contents of this file were added to be able to use them outside the sdk package (i.e. integration tests package). // It was easier to do it that way, so that we do not include big rename changes in the first moving PR. -// For each of them we will have to decide what do we do: -// - do we expose the field/method (e.g. errors or ValidObjectIdentifier) -// - do we keep the workaround -// - do we copy the code (e.g. ExecForTests) -// - do we move the code to other place and use it from both places (e.g. findOne) -// - something else. -// This will be handled in subsequent PRs, so that the main difficulty (moving) is already merged. - -var ( - ErrObjectNotExistOrAuthorized = errObjectNotExistOrAuthorized - ErrDifferentDatabase = errDifferentDatabase -) // ExecForTests is an exact copy of exec (that is unexported), that some integration tests/helpers were using +// TODO: remove after we have all usages covered by SDK (for now it means implementing stages, tables, and tags) func (c *Client) ExecForTests(ctx context.Context, sql string) (sql.Result, error) { ctx = context.WithValue(ctx, snowflakeAccountLocatorContextKey, c.accountLocator) result, err := c.db.ExecContext(ctx, sql) return result, decodeDriverError(err) } - -// ValidObjectIdentifier is just a delegate to existing unexported validObjectidentifier -func ValidObjectIdentifier(objectIdentifier ObjectIdentifier) bool { - return validObjectidentifier(objectIdentifier) -} - -// GetName is just an accessor to unexported name field -func (r *CreateNetworkPolicyRequest) GetName() AccountObjectIdentifier { - return r.name -} - -// GetName is just an accessor to unexported name field -func (s *CreateRoleRequest) GetName() AccountObjectIdentifier { - return s.name -} - -// GetName is just an accessor to unexported name field -func (r *CreateTaskRequest) GetName() SchemaObjectIdentifier { - return r.name -} - -// GetName is just an accessor to unexported name field -func (r *CloneTaskRequest) GetName() SchemaObjectIdentifier { - return r.name -} - -// GetColumns is just an accessor to unexported name field -func (s *CreateExternalTableRequest) GetColumns() []*ExternalTableColumnRequest { - return s.columns -} - -// GetAccountLocator is an accessor to unexported accountLocator, which is needed in some tests -func (c *Client) GetAccountLocator() string { - return c.accountLocator -} - -// GetConfig is an accessor to unexported config, which is needed in some tests -func (c *Client) GetConfig() *gosnowflake.Config { - return c.config -} - -// FindOne just delegates to our util findOne from SDK -func FindOne[T any](collection []T, condition func(T) bool) (*T, error) { - return findOne(collection, condition) -} diff --git a/pkg/sdk/internal/collections/collection_helpers.go b/pkg/sdk/internal/collections/collection_helpers.go new file mode 100644 index 0000000000..79cd729aad --- /dev/null +++ b/pkg/sdk/internal/collections/collection_helpers.go @@ -0,0 +1,16 @@ +package collections + +import ( + "errors" +) + +var ErrObjectNotFound = errors.New("object does not exist") + +func FindOne[T any](collection []T, condition func(T) bool) (*T, error) { + for _, o := range collection { + if condition(o) { + return &o, nil + } + } + return nil, ErrObjectNotFound +} diff --git a/pkg/sdk/internal/random/random_helpers.go b/pkg/sdk/internal/random/random_helpers.go new file mode 100644 index 0000000000..d9ddac43c0 --- /dev/null +++ b/pkg/sdk/internal/random/random_helpers.go @@ -0,0 +1,39 @@ +package random + +import ( + "github.com/brianvoe/gofakeit/v6" + "github.com/hashicorp/go-uuid" +) + +func UUID() string { + v, _ := uuid.GenerateUUID() + return v +} + +func Comment() string { + return gofakeit.Sentence(10) +} + +func Bool() bool { + return gofakeit.Bool() +} + +func String() string { + return gofakeit.Password(true, true, true, true, false, 28) +} + +func StringN(num int) string { + return gofakeit.Password(true, true, true, true, false, num) +} + +func AlphanumericN(num int) string { + return gofakeit.Password(true, true, true, false, false, num) +} + +func StringRange(min, max int) string { + return gofakeit.Password(true, true, true, true, false, IntRange(min, max)) +} + +func IntRange(min, max int) int { + return gofakeit.IntRange(min, max) +} diff --git a/pkg/sdk/masking_policy.go b/pkg/sdk/masking_policy.go index be4e799928..aa65e64f08 100644 --- a/pkg/sdk/masking_policy.go +++ b/pkg/sdk/masking_policy.go @@ -52,7 +52,7 @@ type CreateMaskingPolicyOptions struct { } func (opts *CreateMaskingPolicyOptions) validate() error { - if !validObjectidentifier(opts.name) { + if !ValidObjectIdentifier(opts.name) { return errors.New("invalid object identifier") } @@ -90,13 +90,13 @@ type AlterMaskingPolicyOptions struct { } func (opts *AlterMaskingPolicyOptions) validate() error { - if !validObjectidentifier(opts.name) { + if !ValidObjectIdentifier(opts.name) { return errors.New("invalid object identifier") } if everyValueNil(opts.Set, opts.Unset) { - if !validObjectidentifier(opts.NewName) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.NewName) { + return ErrInvalidObjectIdentifier } } @@ -168,8 +168,8 @@ type DropMaskingPolicyOptions struct { } func (opts *DropMaskingPolicyOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } return nil } @@ -285,7 +285,7 @@ func (v *maskingPolicies) ShowByID(ctx context.Context, id SchemaObjectIdentifie return &maskingPolicy, nil } } - return nil, errObjectNotExistOrAuthorized + return nil, ErrObjectNotExistOrAuthorized } // describeMaskingPolicyOptions is based on https://docs.snowflake.com/en/sql-reference/sql/desc-masking-policy. @@ -296,8 +296,8 @@ type describeMaskingPolicyOptions struct { } func (v *describeMaskingPolicyOptions) validate() error { - if !validObjectidentifier(v.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(v.name) { + return ErrInvalidObjectIdentifier } return nil } diff --git a/pkg/sdk/masking_policy_test.go b/pkg/sdk/masking_policy_test.go index cb5b0dd102..92f2ac6235 100644 --- a/pkg/sdk/masking_policy_test.go +++ b/pkg/sdk/masking_policy_test.go @@ -4,13 +4,13 @@ import ( "fmt" "testing" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestMaskingPolicyCreate(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &CreateMaskingPolicyOptions{} @@ -32,7 +32,7 @@ func TestMaskingPolicyCreate(t *testing.T) { }, } expression := "REPLACE('X', 1, 2)" - comment := randomString(t) + comment := random.String() opts := &CreateMaskingPolicyOptions{ OrReplace: Bool(true), @@ -53,7 +53,7 @@ func TestMaskingPolicyCreate(t *testing.T) { } func TestMaskingPolicyAlter(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &AlterMaskingPolicyOptions{} @@ -74,7 +74,7 @@ func TestMaskingPolicyAlter(t *testing.T) { }) t.Run("with set", func(t *testing.T) { - newComment := randomString(t) + newComment := random.String() opts := &AlterMaskingPolicyOptions{ name: id, Set: &MaskingPolicySet{ @@ -101,7 +101,7 @@ func TestMaskingPolicyAlter(t *testing.T) { }) t.Run("rename", func(t *testing.T) { - newID := NewSchemaObjectIdentifier(id.databaseName, id.schemaName, randomUUID(t)) + newID := NewSchemaObjectIdentifier(id.databaseName, id.schemaName, random.UUID()) opts := &AlterMaskingPolicyOptions{ name: id, NewName: newID, @@ -114,7 +114,7 @@ func TestMaskingPolicyAlter(t *testing.T) { } func TestMaskingPolicyDrop(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &DropMaskingPolicyOptions{} @@ -136,7 +136,7 @@ func TestMaskingPolicyDrop(t *testing.T) { } func TestMaskingPolicyShow(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &ShowMaskingPolicyOptions{} @@ -217,7 +217,7 @@ func TestMaskingPolicyShow(t *testing.T) { } func TestMaskingPolicyDescribe(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &describeMaskingPolicyOptions{} diff --git a/pkg/sdk/network_policies_dto_gen.go b/pkg/sdk/network_policies_dto_gen.go index 266ceb0e81..8902f05440 100644 --- a/pkg/sdk/network_policies_dto_gen.go +++ b/pkg/sdk/network_policies_dto_gen.go @@ -18,6 +18,10 @@ type CreateNetworkPolicyRequest struct { Comment *string } +func (r *CreateNetworkPolicyRequest) GetName() AccountObjectIdentifier { + return r.name +} + type IPRequest struct { IP string // required } diff --git a/pkg/sdk/network_policies_gen_test.go b/pkg/sdk/network_policies_gen_test.go index 12c2084d65..f03fd49bb3 100644 --- a/pkg/sdk/network_policies_gen_test.go +++ b/pkg/sdk/network_policies_gen_test.go @@ -5,7 +5,7 @@ import ( ) func TestNetworkPolicies_Create(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := RandomAccountObjectIdentifier() // Minimal valid CreateNetworkPolicyOptions defaultOpts := func() *CreateNetworkPolicyOptions { @@ -20,13 +20,13 @@ func TestNetworkPolicies_Create(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *CreateNetworkPolicyOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { opts := defaultOpts() opts.name = NewAccountObjectIdentifier("") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("all options", func(t *testing.T) { @@ -36,7 +36,7 @@ func TestNetworkPolicies_Create(t *testing.T) { } func TestNetworkPolicies_Alter(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := RandomAccountObjectIdentifier() // Minimal valid AlterNetworkPolicyOptions defaultOpts := func() *AlterNetworkPolicyOptions { @@ -48,14 +48,14 @@ func TestNetworkPolicies_Alter(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *AlterNetworkPolicyOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { opts := defaultOpts() opts.name = NewAccountObjectIdentifier("") opts.UnsetComment = Bool(true) - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: exactly one field from [opts.Set opts.UnsetComment opts.RenameTo] should be present", func(t *testing.T) { @@ -101,14 +101,14 @@ func TestNetworkPolicies_Alter(t *testing.T) { t.Run("rename to", func(t *testing.T) { opts := defaultOpts() - newName := randomAccountObjectIdentifier(t) + newName := RandomAccountObjectIdentifier() opts.RenameTo = &newName assertOptsValidAndSQLEquals(t, opts, "ALTER NETWORK POLICY IF EXISTS %s RENAME TO %s", id.FullyQualifiedName(), newName.FullyQualifiedName()) }) } func TestNetworkPolicies_Drop(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := RandomAccountObjectIdentifier() // Minimal valid DropNetworkPolicyOptions defaultOpts := func() *DropNetworkPolicyOptions { @@ -119,13 +119,13 @@ func TestNetworkPolicies_Drop(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *DropNetworkPolicyOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { opts := defaultOpts() opts.name = NewAccountObjectIdentifier("") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("all options", func(t *testing.T) { @@ -143,7 +143,7 @@ func TestNetworkPolicies_Show(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *ShowNetworkPolicyOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("all options", func(t *testing.T) { @@ -153,7 +153,7 @@ func TestNetworkPolicies_Show(t *testing.T) { } func TestNetworkPolicies_Describe(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := RandomAccountObjectIdentifier() // Minimal valid DescribeNetworkPolicyOptions defaultOpts := func() *DescribeNetworkPolicyOptions { @@ -164,13 +164,13 @@ func TestNetworkPolicies_Describe(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *DescribeNetworkPolicyOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { opts := defaultOpts() opts.name = NewAccountObjectIdentifier("") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("all options", func(t *testing.T) { diff --git a/pkg/sdk/network_policies_impl_gen.go b/pkg/sdk/network_policies_impl_gen.go index c1e5b61a40..b8b787c225 100644 --- a/pkg/sdk/network_policies_impl_gen.go +++ b/pkg/sdk/network_policies_impl_gen.go @@ -1,6 +1,10 @@ package sdk -import "context" +import ( + "context" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/collections" +) var _ NetworkPolicies = (*networkPolicies)(nil) @@ -39,7 +43,7 @@ func (v *networkPolicies) ShowByID(ctx context.Context, id AccountObjectIdentifi return nil, err } - return findOne(networkPolicies, func(r NetworkPolicy) bool { return r.Name == id.Name() }) + return collections.FindOne(networkPolicies, func(r NetworkPolicy) bool { return r.Name == id.Name() }) } func (v *networkPolicies) Describe(ctx context.Context, id AccountObjectIdentifier) ([]NetworkPolicyDescription, error) { diff --git a/pkg/sdk/network_policies_validations_gen.go b/pkg/sdk/network_policies_validations_gen.go index b0bb6c026f..6a7e5aff61 100644 --- a/pkg/sdk/network_policies_validations_gen.go +++ b/pkg/sdk/network_policies_validations_gen.go @@ -12,28 +12,28 @@ var ( func (opts *CreateNetworkPolicyOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } return errors.Join(errs...) } func (opts *AlterNetworkPolicyOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if ok := exactlyOneValueSet(opts.Set, opts.UnsetComment, opts.RenameTo); !ok { errs = append(errs, errExactlyOneOf("Set", "UnsetComment", "RenameTo")) } - if valueSet(opts.RenameTo) && !validObjectidentifier(opts.RenameTo) { - errs = append(errs, errInvalidObjectIdentifier) + if valueSet(opts.RenameTo) && !ValidObjectIdentifier(opts.RenameTo) { + errs = append(errs, ErrInvalidObjectIdentifier) } if valueSet(opts.Set) { if ok := anyValueSet(opts.Set.AllowedIpList, opts.Set.BlockedIpList, opts.Set.Comment); !ok { @@ -45,18 +45,18 @@ func (opts *AlterNetworkPolicyOptions) validate() error { func (opts *DropNetworkPolicyOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } return errors.Join(errs...) } func (opts *ShowNetworkPolicyOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error return errors.Join(errs...) @@ -64,11 +64,11 @@ func (opts *ShowNetworkPolicyOptions) validate() error { func (opts *DescribeNetworkPolicyOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } return errors.Join(errs...) } diff --git a/pkg/sdk/parameters_test.go b/pkg/sdk/parameters_test.go index 9f80a27dea..389d0087c0 100644 --- a/pkg/sdk/parameters_test.go +++ b/pkg/sdk/parameters_test.go @@ -8,7 +8,7 @@ import ( ) func TestSetObjectParameterOnObject(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := RandomAccountObjectIdentifier() defaultOpts := func() *setParameterOnObject { return &setParameterOnObject{ diff --git a/pkg/sdk/password_policy.go b/pkg/sdk/password_policy.go index 613f79f593..96c227ac71 100644 --- a/pkg/sdk/password_policy.go +++ b/pkg/sdk/password_policy.go @@ -53,8 +53,8 @@ type CreatePasswordPolicyOptions struct { } func (opts *CreatePasswordPolicyOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } return nil @@ -88,13 +88,13 @@ type AlterPasswordPolicyOptions struct { } func (opts *AlterPasswordPolicyOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } if everyValueNil(opts.Set, opts.Unset) { - if !validObjectidentifier(opts.NewName) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.NewName) { + return ErrInvalidObjectIdentifier } } @@ -214,8 +214,8 @@ type DropPasswordPolicyOptions struct { } func (opts *DropPasswordPolicyOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } return nil } @@ -334,7 +334,7 @@ func (v *passwordPolicies) ShowByID(ctx context.Context, id SchemaObjectIdentifi return &passwordPolicy, nil } } - return nil, errObjectNotExistOrAuthorized + return nil, ErrObjectNotExistOrAuthorized } // describePasswordPolicyOptions is based on https://docs.snowflake.com/en/sql-reference/sql/desc-password-policy. @@ -345,8 +345,8 @@ type describePasswordPolicyOptions struct { } func (v *describePasswordPolicyOptions) validate() error { - if !validObjectidentifier(v.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(v.name) { + return ErrInvalidObjectIdentifier } return nil } diff --git a/pkg/sdk/password_policy_test.go b/pkg/sdk/password_policy_test.go index 35221a5976..687886a006 100644 --- a/pkg/sdk/password_policy_test.go +++ b/pkg/sdk/password_policy_test.go @@ -4,13 +4,13 @@ import ( "fmt" "testing" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestPasswordPolicyCreate(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &CreatePasswordPolicyOptions{} @@ -54,7 +54,7 @@ func TestPasswordPolicyCreate(t *testing.T) { } func TestPasswordPolicyAlter(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &AlterPasswordPolicyOptions{} @@ -103,7 +103,7 @@ func TestPasswordPolicyAlter(t *testing.T) { }) t.Run("rename", func(t *testing.T) { - newID := NewSchemaObjectIdentifier(id.databaseName, id.schemaName, randomUUID(t)) + newID := NewSchemaObjectIdentifier(id.databaseName, id.schemaName, random.UUID()) opts := &AlterPasswordPolicyOptions{ name: id, NewName: newID, @@ -116,7 +116,7 @@ func TestPasswordPolicyAlter(t *testing.T) { } func TestPasswordPolicyDrop(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &DropPasswordPolicyOptions{} @@ -149,7 +149,7 @@ func TestPasswordPolicyDrop(t *testing.T) { } func TestPasswordPolicyShow(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &ShowPasswordPolicyOptions{} @@ -230,7 +230,7 @@ func TestPasswordPolicyShow(t *testing.T) { } func TestPasswordPolicyDescribe(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &describePasswordPolicyOptions{} diff --git a/pkg/sdk/pipes_impl.go b/pkg/sdk/pipes_impl.go index dbcf3a7de0..86bc728d7a 100644 --- a/pkg/sdk/pipes_impl.go +++ b/pkg/sdk/pipes_impl.go @@ -1,6 +1,10 @@ package sdk -import "context" +import ( + "context" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/collections" +) var _ Pipes = (*pipes)(nil) @@ -52,7 +56,7 @@ func (v *pipes) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Pipe, return nil, err } - return findOne(pipes, func(p Pipe) bool { return p.ID().name == id.Name() }) + return collections.FindOne(pipes, func(p Pipe) bool { return p.ID().name == id.Name() }) } func (v *pipes) Describe(ctx context.Context, id SchemaObjectIdentifier) (*Pipe, error) { diff --git a/pkg/sdk/pipes_test.go b/pkg/sdk/pipes_test.go index 9daceb138e..9c2047f1ff 100644 --- a/pkg/sdk/pipes_test.go +++ b/pkg/sdk/pipes_test.go @@ -5,7 +5,7 @@ import ( ) func TestPipesCreate(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() defaultOpts := func() *CreatePipeOptions { return &CreatePipeOptions{ @@ -16,13 +16,13 @@ func TestPipesCreate(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *CreatePipeOptions = nil - assertOptsInvalid(t, opts, errNilOptions) + assertOptsInvalid(t, opts, ErrNilOptions) }) t.Run("validation: incorrect identifier", func(t *testing.T) { opts := defaultOpts() opts.name = NewSchemaObjectIdentifier("", "", "") - assertOptsInvalid(t, opts, errInvalidObjectIdentifier) + assertOptsInvalid(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: copy statement required", func(t *testing.T) { @@ -49,7 +49,7 @@ func TestPipesCreate(t *testing.T) { } func TestPipesAlter(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() defaultOpts := func() *AlterPipeOptions { return &AlterPipeOptions{ @@ -59,13 +59,13 @@ func TestPipesAlter(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *AlterPipeOptions = nil - assertOptsInvalid(t, opts, errNilOptions) + assertOptsInvalid(t, opts, ErrNilOptions) }) t.Run("validation: incorrect identifier", func(t *testing.T) { opts := defaultOpts() opts.name = NewSchemaObjectIdentifier("", "", "") - assertOptsInvalid(t, opts, errInvalidObjectIdentifier) + assertOptsInvalid(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: no alter action", func(t *testing.T) { @@ -202,7 +202,7 @@ func TestPipesAlter(t *testing.T) { } func TestPipesDrop(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() defaultOpts := func() *DropPipeOptions { return &DropPipeOptions{ @@ -212,13 +212,13 @@ func TestPipesDrop(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *DropPipeOptions = nil - assertOptsInvalid(t, opts, errNilOptions) + assertOptsInvalid(t, opts, ErrNilOptions) }) t.Run("validation: incorrect identifier", func(t *testing.T) { opts := defaultOpts() opts.name = NewSchemaObjectIdentifier("", "", "") - assertOptsInvalid(t, opts, errInvalidObjectIdentifier) + assertOptsInvalid(t, opts, ErrInvalidObjectIdentifier) }) t.Run("empty options", func(t *testing.T) { @@ -234,7 +234,7 @@ func TestPipesDrop(t *testing.T) { } func TestPipesShow(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() databaseIdentifier := NewAccountObjectIdentifier(id.DatabaseName()) schemaIdentifier := NewDatabaseObjectIdentifier(id.DatabaseName(), id.SchemaName()) @@ -244,13 +244,13 @@ func TestPipesShow(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *ShowPipeOptions = nil - assertOptsInvalid(t, opts, errNilOptions) + assertOptsInvalid(t, opts, ErrNilOptions) }) t.Run("validation: empty like", func(t *testing.T) { opts := defaultOpts() opts.Like = &Like{} - assertOptsInvalid(t, opts, errPatternRequiredForLikeKeyword) + assertOptsInvalid(t, opts, ErrPatternRequiredForLikeKeyword) }) t.Run("validation: empty in", func(t *testing.T) { @@ -340,7 +340,7 @@ func TestPipesShow(t *testing.T) { } func TestPipesDescribe(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() defaultOpts := func() *describePipeOptions { return &describePipeOptions{ @@ -350,13 +350,13 @@ func TestPipesDescribe(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *describePipeOptions = nil - assertOptsInvalid(t, opts, errNilOptions) + assertOptsInvalid(t, opts, ErrNilOptions) }) t.Run("validation: incorrect identifier", func(t *testing.T) { opts := defaultOpts() opts.name = NewSchemaObjectIdentifier("", "", "") - assertOptsInvalid(t, opts, errInvalidObjectIdentifier) + assertOptsInvalid(t, opts, ErrInvalidObjectIdentifier) }) t.Run("with name", func(t *testing.T) { diff --git a/pkg/sdk/pipes_validations.go b/pkg/sdk/pipes_validations.go index b7f8f96a9c..593dd5f5e7 100644 --- a/pkg/sdk/pipes_validations.go +++ b/pkg/sdk/pipes_validations.go @@ -14,10 +14,10 @@ var ( func (opts *CreatePipeOptions) validate() error { if opts == nil { - return errNilOptions + return ErrNilOptions } - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } if opts.copyStatement == "" { return errCopyStatementRequired @@ -27,10 +27,10 @@ func (opts *CreatePipeOptions) validate() error { func (opts *AlterPipeOptions) validate() error { if opts == nil { - return errNilOptions + return ErrNilOptions } - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } if ok := exactlyOneValueSet( opts.Set, @@ -66,20 +66,20 @@ func (opts *AlterPipeOptions) validate() error { func (opts *DropPipeOptions) validate() error { if opts == nil { - return errNilOptions + return ErrNilOptions } - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } return nil } func (opts *ShowPipeOptions) validate() error { if opts == nil { - return errNilOptions + return ErrNilOptions } if valueSet(opts.Like) && !valueSet(opts.Like.Pattern) { - return errPatternRequiredForLikeKeyword + return ErrPatternRequiredForLikeKeyword } if valueSet(opts.In) && !exactlyOneValueSet(opts.In.Account, opts.In.Database, opts.In.Schema) { return errScopeRequiredForInKeyword @@ -89,10 +89,10 @@ func (opts *ShowPipeOptions) validate() error { func (opts *describePipeOptions) validate() error { if opts == nil { - return errNilOptions + return ErrNilOptions } - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } return nil } diff --git a/pkg/sdk/poc/README.md b/pkg/sdk/poc/README.md index 2c690f8c80..73b011d9de 100644 --- a/pkg/sdk/poc/README.md +++ b/pkg/sdk/poc/README.md @@ -97,6 +97,8 @@ find a better solution to solve the issue (add more logic to the templates ?) - generate full tests for common types (e.g. setting/unsetting tags) - generate common resources for integration tests - cleanup the design of builders in DSL (e.g. why transformer has to be always added?) +- generate getters for requests, at least for identifier/name +- generate integration tests in child package (because now we keep them in `testint` package) ##### Known issues - generating two converts when Show and Desc use the same data structure diff --git a/pkg/sdk/poc/example/database_role_gen_test.go b/pkg/sdk/poc/example/database_role_gen_test.go index eacde5e7a2..989e22d57c 100644 --- a/pkg/sdk/poc/example/database_role_gen_test.go +++ b/pkg/sdk/poc/example/database_role_gen_test.go @@ -3,7 +3,7 @@ package example import "testing" func TestDatabaseRoles_Create(t *testing.T) { - id := randomDatabaseObjectIdentifier(t) + id := RandomDatabaseObjectIdentifier(t) // Minimal valid CreateDatabaseRoleOptions defaultOpts := func() *CreateDatabaseRoleOptions { @@ -14,13 +14,13 @@ func TestDatabaseRoles_Create(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *CreateDatabaseRoleOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { opts := defaultOpts() // TODO: fill me - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: conflicting fields for [opts.OrReplace opts.IfNotExists]", func(t *testing.T) { @@ -43,7 +43,7 @@ func TestDatabaseRoles_Create(t *testing.T) { } func TestDatabaseRoles_Alter(t *testing.T) { - id := randomDatabaseObjectIdentifier(t) + id := RandomDatabaseObjectIdentifier(t) // Minimal valid AlterDatabaseRoleOptions defaultOpts := func() *AlterDatabaseRoleOptions { @@ -54,13 +54,13 @@ func TestDatabaseRoles_Alter(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *AlterDatabaseRoleOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { opts := defaultOpts() // TODO: fill me - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: exactly one field from [opts.Rename opts.Set opts.Unset] should be present", func(t *testing.T) { @@ -72,7 +72,7 @@ func TestDatabaseRoles_Alter(t *testing.T) { t.Run("validation: valid identifier for [opts.Rename.Name]", func(t *testing.T) { opts := defaultOpts() // TODO: fill me - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: at least one of the fields [opts.Set.NestedThirdLevel.Field] should be set", func(t *testing.T) { diff --git a/pkg/sdk/poc/example/database_role_validations_gen.go b/pkg/sdk/poc/example/database_role_validations_gen.go index 5b41131f6b..23da9af2c9 100644 --- a/pkg/sdk/poc/example/database_role_validations_gen.go +++ b/pkg/sdk/poc/example/database_role_validations_gen.go @@ -9,11 +9,11 @@ var ( func (opts *CreateDatabaseRoleOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if everyValueSet(opts.OrReplace, opts.IfNotExists) { errs = append(errs, errOneOf("CreateDatabaseRoleOptions", "OrReplace", "IfNotExists")) @@ -23,18 +23,18 @@ func (opts *CreateDatabaseRoleOptions) validate() error { func (opts *AlterDatabaseRoleOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if ok := exactlyOneValueSet(opts.Rename, opts.Set, opts.Unset); !ok { errs = append(errs, errExactlyOneOf("Rename", "Set", "Unset")) } if valueSet(opts.Rename) { - if !validObjectidentifier(opts.Rename.Name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.Rename.Name) { + errs = append(errs, ErrInvalidObjectIdentifier) } } if valueSet(opts.Set) { diff --git a/pkg/sdk/poc/example/sdk_definitions.go b/pkg/sdk/poc/example/sdk_definitions.go index c8c1c9487c..7ba8672e28 100644 --- a/pkg/sdk/poc/example/sdk_definitions.go +++ b/pkg/sdk/poc/example/sdk_definitions.go @@ -29,25 +29,25 @@ type ( TableColumnIdentifier struct{} ) -func randomAccountObjectIdentifier(t *testing.T) AccountObjectIdentifier { +func RandomAccountObjectIdentifier(t *testing.T) AccountObjectIdentifier { t.Helper() _ = t return AccountObjectIdentifier{} } -func randomDatabaseObjectIdentifier(t *testing.T) DatabaseObjectIdentifier { +func RandomDatabaseObjectIdentifier(t *testing.T) DatabaseObjectIdentifier { t.Helper() _ = t return DatabaseObjectIdentifier{} } -func randomSchemaObjectIdentifier(t *testing.T) SchemaObjectIdentifier { +func RandomSchemaObjectIdentifier(t *testing.T) SchemaObjectIdentifier { t.Helper() _ = t return SchemaObjectIdentifier{} } -func validObjectidentifier(objectIdentifier ObjectIdentifier) bool { +func ValidObjectIdentifier(objectIdentifier ObjectIdentifier) bool { _ = objectIdentifier return true } @@ -85,8 +85,8 @@ func errAtLeastOneOf(fieldNames ...string) error { } var ( - errNilOptions = errors.New("options cannot be nil") - errInvalidObjectIdentifier = errors.New("invalid object identifier") + ErrNilOptions = errors.New("options cannot be nil") + ErrInvalidObjectIdentifier = errors.New("invalid object identifier") ) func validateAndExec(client *Client, ctx context.Context, opts validatable) error { diff --git a/pkg/sdk/poc/generator/templates.go b/pkg/sdk/poc/generator/templates.go index f19b690b27..919b8e7fa3 100644 --- a/pkg/sdk/poc/generator/templates.go +++ b/pkg/sdk/poc/generator/templates.go @@ -115,7 +115,11 @@ var ImplementationTemplate, _ = template.New("implementationTemplate"). return &{{ .To.KindNoPtr }}{} } {{ end }} -import "context" +import ( +"context" + +"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/collections" +) {{ $impl := .NameLowerCased }} var _ {{ .Name }} = (*{{ $impl }})(nil) @@ -141,7 +145,7 @@ type {{ $impl }} struct { if err != nil { return nil, err } - return findOne({{ $impl }}, func(r {{ .ObjectInterface.NameSingular }}) bool { return r.Name == id.Name() }) + return collections.FindOne({{ $impl }}, func(r {{ .ObjectInterface.NameSingular }}) bool { return r.Name == id.Name() }) } {{ else if and (eq .Name "Describe") .DescribeMapping }} {{ if .DescribeKind }} @@ -219,7 +223,7 @@ import "testing" {{ range .Operations }} {{- if .OptsField }} func Test{{ .ObjectInterface.Name }}_{{ .Name }}(t *testing.T) { - id := random{{ .ObjectInterface.IdentifierKind }}(t) + id := Random{{ .ObjectInterface.IdentifierKind }}() // Minimal valid {{ .OptsField.KindNoPtr }} defaultOpts := func() *{{ .OptsField.KindNoPtr }} { @@ -230,7 +234,7 @@ import "testing" t.Run("validation: nil options", func(t *testing.T) { var opts *{{ .OptsField.KindNoPtr }} = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) {{- template "VALIDATIONS" .OptsField }} @@ -281,7 +285,7 @@ var ( {{- if .OptsField }} func (opts *{{ .OptsField.KindNoPtr }}) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error {{- template "VALIDATIONS" .OptsField }} diff --git a/pkg/sdk/poc/generator/validation.go b/pkg/sdk/poc/generator/validation.go index a21275ae8f..39729f3a47 100644 --- a/pkg/sdk/poc/generator/validation.go +++ b/pkg/sdk/poc/generator/validation.go @@ -55,9 +55,9 @@ func (v *Validation) fieldsWithPath(field *Field) []string { func (v *Validation) Condition(field *Field) string { switch v.Type { case ValidIdentifier: - return fmt.Sprintf("!validObjectidentifier(%s)", strings.Join(v.fieldsWithPath(field), ",")) + return fmt.Sprintf("!ValidObjectIdentifier(%s)", strings.Join(v.fieldsWithPath(field), ",")) case ValidIdentifierIfSet: - return fmt.Sprintf("valueSet(%s) && !validObjectidentifier(%s)", strings.Join(v.fieldsWithPath(field), ","), strings.Join(v.fieldsWithPath(field), ",")) + return fmt.Sprintf("valueSet(%s) && !ValidObjectIdentifier(%s)", strings.Join(v.fieldsWithPath(field), ","), strings.Join(v.fieldsWithPath(field), ",")) case ConflictingFields: return fmt.Sprintf("everyValueSet(%s)", strings.Join(v.fieldsWithPath(field), ",")) case ExactlyOneValueSet: @@ -73,9 +73,9 @@ func (v *Validation) Condition(field *Field) string { func (v *Validation) ReturnedError(field *Field) string { switch v.Type { case ValidIdentifier: - return "errInvalidObjectIdentifier" + return "ErrInvalidObjectIdentifier" case ValidIdentifierIfSet: - return "errInvalidObjectIdentifier" + return "ErrInvalidObjectIdentifier" case ConflictingFields: return fmt.Sprintf(`errOneOf("%s", %s)`, field.Name, strings.Join(v.paramsQuoted(), ",")) case ExactlyOneValueSet: diff --git a/pkg/sdk/random.go b/pkg/sdk/random.go index 2bb6205228..a28b474858 100644 --- a/pkg/sdk/random.go +++ b/pkg/sdk/random.go @@ -1,77 +1,43 @@ package sdk import ( - "testing" - - "github.com/brianvoe/gofakeit/v6" - "github.com/hashicorp/go-uuid" - "github.com/stretchr/testify/require" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" ) -func randomUUID(t *testing.T) string { - t.Helper() - v, err := uuid.GenerateUUID() - require.NoError(t, err) - return v -} - -func randomComment(t *testing.T) string { - t.Helper() - return gofakeit.Sentence(10) -} - -func randomBool(t *testing.T) bool { - t.Helper() - return gofakeit.Bool() -} - -func randomString(t *testing.T) string { - t.Helper() - return gofakeit.Password(true, true, true, true, false, 28) -} - -func randomStringN(t *testing.T, num int) string { - t.Helper() - return gofakeit.Password(true, true, true, true, false, num) -} - -func randomAlphanumericN(t *testing.T, num int) string { - t.Helper() - return gofakeit.Password(true, true, true, false, false, num) -} - -func randomStringRange(t *testing.T, min, max int) string { - t.Helper() - if min > max { - t.Errorf("min %d is greater than max %d", min, max) - } - return gofakeit.Password(true, true, true, true, false, randomIntRange(t, min, max)) -} - -func randomIntRange(t *testing.T, min, max int) int { - t.Helper() - if min > max { - t.Errorf("min %d is greater than max %d", min, max) - } - return gofakeit.IntRange(min, max) -} - -func randomSchemaObjectIdentifier(t *testing.T) SchemaObjectIdentifier { - t.Helper() - return NewSchemaObjectIdentifier(randomStringN(t, 12), randomStringN(t, 12), randomStringN(t, 12)) -} - -func randomDatabaseObjectIdentifier(t *testing.T) DatabaseObjectIdentifier { - t.Helper() - return NewDatabaseObjectIdentifier(randomStringN(t, 12), randomStringN(t, 12)) -} - -func alphanumericDatabaseObjectIdentifier(t *testing.T) DatabaseObjectIdentifier { - t.Helper() - return NewDatabaseObjectIdentifier(randomAlphanumericN(t, 12), randomAlphanumericN(t, 12)) -} - -func randomAccountObjectIdentifier(t *testing.T) AccountObjectIdentifier { - t.Helper() - return NewAccountObjectIdentifier(randomStringN(t, 12)) +// Helper methods in this file are used both in SDK tests and also in integration tests. +// These methods are not supposed to be used in production code, so it would be better to not export them. +// Simply moving them to internal/random along other random helper methods is not an easy option, because it would create import cycle: +// sdk needed here -> sdk imports this to use the methods for tests. +// +// To move it there, we have to break a cycle. We have a few, slightly different options: +// 1. SDK reorganization +// We can extract separate package for identifiers only, it could be imported both by sdk and by internal/random packages. +// While this is an elegant solution, we are keeping all sdk files in one package now, and this would be exception. +// 2. Changing the package for unit tests in sdk package +// This would be different from extracting integration tests because it would require only changing package to sdk_test without moving the file. +// The reason for that is that go allows keeping x_test package together with x package inside one directory. +// Then one downside (or not a downside?) would be, that we would be running our unit tests as a black box; +// we would be restricted only to exported objects. +// 3. Using go:linkname compiler directive +// This is a hacky solution which allows linking without importing a package. +// We could e.g. replace the XxxIdentifier structs and NewXxxIdentifier methods with our types with such directive. +// That way we would be able to use them without importing them directly. +// As stated in an official documentation this is not recommended, unsafe method. +// 4. Do nothing +// Just leave these helpers exported. + +func RandomSchemaObjectIdentifier() SchemaObjectIdentifier { + return NewSchemaObjectIdentifier(random.StringN(12), random.StringN(12), random.StringN(12)) +} + +func RandomDatabaseObjectIdentifier() DatabaseObjectIdentifier { + return NewDatabaseObjectIdentifier(random.StringN(12), random.StringN(12)) +} + +func RandomAccountObjectIdentifier() AccountObjectIdentifier { + return NewAccountObjectIdentifier(random.StringN(12)) +} + +func AlphanumericDatabaseObjectIdentifier() DatabaseObjectIdentifier { + return NewDatabaseObjectIdentifier(random.AlphanumericN(12), random.AlphanumericN(12)) } diff --git a/pkg/sdk/resource_monitors.go b/pkg/sdk/resource_monitors.go index 71b3709efc..ce9cea34ca 100644 --- a/pkg/sdk/resource_monitors.go +++ b/pkg/sdk/resource_monitors.go @@ -196,8 +196,8 @@ type ResourceMonitorWith struct { } func (opts *CreateResourceMonitorOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } return nil } @@ -281,8 +281,8 @@ type AlterResourceMonitorOptions struct { } func (opts *AlterResourceMonitorOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } if opts.Set == nil { return nil @@ -327,8 +327,8 @@ type dropResourceMonitorOptions struct { } func (opts *dropResourceMonitorOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } return nil } @@ -398,5 +398,5 @@ func (v *resourceMonitors) ShowByID(ctx context.Context, id AccountObjectIdentif return &resourceMonitor, nil } } - return nil, errObjectNotExistOrAuthorized + return nil, ErrObjectNotExistOrAuthorized } diff --git a/pkg/sdk/resource_monitors_test.go b/pkg/sdk/resource_monitors_test.go index cb7dcaad16..7a717ccafe 100644 --- a/pkg/sdk/resource_monitors_test.go +++ b/pkg/sdk/resource_monitors_test.go @@ -11,7 +11,7 @@ import ( ) func TestResourceMonitorCreate(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := RandomAccountObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &CreateResourceMonitorOptions{} @@ -63,7 +63,7 @@ func TestResourceMonitorCreate(t *testing.T) { } func TestResourceMonitorAlter(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := RandomAccountObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &AlterResourceMonitorOptions{} @@ -117,7 +117,7 @@ func TestResourceMonitorAlter(t *testing.T) { } func TestResourceMonitorDrop(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := RandomAccountObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &dropResourceMonitorOptions{} @@ -139,7 +139,7 @@ func TestResourceMonitorDrop(t *testing.T) { } func TestResourceMonitorShow(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &ShowResourceMonitorOptions{} diff --git a/pkg/sdk/roles_dto.go b/pkg/sdk/roles_dto.go index 4b71cbfc8f..0300d36d6c 100644 --- a/pkg/sdk/roles_dto.go +++ b/pkg/sdk/roles_dto.go @@ -17,6 +17,10 @@ type CreateRoleRequest struct { Tag []TagAssociation } +func (s *CreateRoleRequest) GetName() AccountObjectIdentifier { + return s.name +} + func NewCreateRoleRequest(name AccountObjectIdentifier) *CreateRoleRequest { return &CreateRoleRequest{ name: name, diff --git a/pkg/sdk/roles_impl.go b/pkg/sdk/roles_impl.go index 189379c180..c6ce1dbabb 100644 --- a/pkg/sdk/roles_impl.go +++ b/pkg/sdk/roles_impl.go @@ -2,6 +2,8 @@ package sdk import ( "context" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/collections" ) var ( @@ -39,7 +41,7 @@ func (v *roles) ShowByID(ctx context.Context, req *ShowRoleByIdRequest) (*Role, if err != nil { return nil, err } - return findOne(roleList, func(r Role) bool { return r.ID().name == req.id.Name() }) + return collections.FindOne(roleList, func(r Role) bool { return r.ID().name == req.id.Name() }) } func (v *roles) Grant(ctx context.Context, req *GrantRoleRequest) error { diff --git a/pkg/sdk/roles_test.go b/pkg/sdk/roles_test.go index 7e2ebd7b52..96b219092d 100644 --- a/pkg/sdk/roles_test.go +++ b/pkg/sdk/roles_test.go @@ -33,12 +33,12 @@ func TestRolesCreate(t *testing.T) { opts := &CreateRoleOptions{ name: NewAccountObjectIdentifier(""), } - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: one of OrReplace, IfNotExists", func(t *testing.T) { opts := &CreateRoleOptions{ - name: randomAccountObjectIdentifier(t), + name: RandomAccountObjectIdentifier(), IfNotExists: Bool(true), OrReplace: Bool(true), } @@ -66,7 +66,7 @@ func TestRolesDrop(t *testing.T) { opts := &DropRoleOptions{ name: NewAccountObjectIdentifier(""), } - assertOptsInvalid(t, opts, errInvalidObjectIdentifier) + assertOptsInvalid(t, opts, ErrInvalidObjectIdentifier) }) } @@ -129,19 +129,19 @@ func TestRolesAlter(t *testing.T) { name: NewAccountObjectIdentifier(""), UnsetComment: Bool(true), } - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: no alter action specified", func(t *testing.T) { opts := &AlterRoleOptions{ - name: randomAccountObjectIdentifier(t), + name: RandomAccountObjectIdentifier(), } assertOptsInvalidJoinedErrors(t, opts, errors.New("no alter action specified")) }) t.Run("validation: more than one alter action specified", func(t *testing.T) { opts := &AlterRoleOptions{ - name: randomAccountObjectIdentifier(t), + name: RandomAccountObjectIdentifier(), SetComment: String("comment"), UnsetComment: Bool(true), } @@ -177,7 +177,7 @@ func TestRolesShow(t *testing.T) { opts := &ShowRoleOptions{ Like: &Like{}, } - assertOptsInvalidJoinedErrors(t, opts, errPatternRequiredForLikeKeyword) + assertOptsInvalidJoinedErrors(t, opts, ErrPatternRequiredForLikeKeyword) }) t.Run("validation: invalid class name", func(t *testing.T) { @@ -187,7 +187,7 @@ func TestRolesShow(t *testing.T) { Class: &class, }, } - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) } @@ -216,13 +216,13 @@ func TestRolesGrant(t *testing.T) { opts := &GrantRoleOptions{ name: NewAccountObjectIdentifier(""), } - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier, errors.New("only one grant option can be set [TO ROLE or TO USER]")) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier, errors.New("only one grant option can be set [TO ROLE or TO USER]")) }) t.Run("validation: invalid object identifier for granted role", func(t *testing.T) { id := NewAccountObjectIdentifier("") opts := &GrantRoleOptions{ - name: randomAccountObjectIdentifier(t), + name: RandomAccountObjectIdentifier(), Grant: GrantRole{ Role: &id, }, @@ -233,7 +233,7 @@ func TestRolesGrant(t *testing.T) { t.Run("validation: invalid object identifier for granted user", func(t *testing.T) { id := NewAccountObjectIdentifier("") opts := &GrantRoleOptions{ - name: randomAccountObjectIdentifier(t), + name: RandomAccountObjectIdentifier(), Grant: GrantRole{ User: &id, }, @@ -267,6 +267,6 @@ func TestRolesRevoke(t *testing.T) { opts := &RevokeRoleOptions{ name: NewAccountObjectIdentifier(""), } - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier, errors.New("only one revoke option can be set [FROM ROLE or FROM USER]")) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier, errors.New("only one revoke option can be set [FROM ROLE or FROM USER]")) }) } diff --git a/pkg/sdk/roles_validations.go b/pkg/sdk/roles_validations.go index 761f690ca7..a231928b59 100644 --- a/pkg/sdk/roles_validations.go +++ b/pkg/sdk/roles_validations.go @@ -13,11 +13,11 @@ var ( func (opts *CreateRoleOptions) validate() error { if opts == nil { - return errNilOptions + return ErrNilOptions } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if everyValueSet(opts.OrReplace, opts.IfNotExists) { errs = append(errs, errOneOf("OrReplace", "IfNotExists")) @@ -27,11 +27,11 @@ func (opts *CreateRoleOptions) validate() error { func (opts *AlterRoleOptions) validate() error { if opts == nil { - return errNilOptions + return ErrNilOptions } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if everyValueNil(opts.RenameTo, opts.SetComment, opts.UnsetComment, opts.SetTags, opts.UnsetTags) { errs = append(errs, errors.New("no alter action specified")) @@ -45,43 +45,43 @@ func (opts *AlterRoleOptions) validate() error { func (opts *DropRoleOptions) validate() error { if opts == nil { - return errNilOptions + return ErrNilOptions } - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } return nil } func (opts *ShowRoleOptions) validate() error { if opts == nil { - return errNilOptions + return ErrNilOptions } var errs []error if valueSet(opts.Like) && !valueSet(opts.Like.Pattern) { - errs = append(errs, errPatternRequiredForLikeKeyword) + errs = append(errs, ErrPatternRequiredForLikeKeyword) } - if valueSet(opts.InClass) && !validObjectidentifier(opts.InClass.Class) { - errs = append(errs, errInvalidObjectIdentifier) + if valueSet(opts.InClass) && !ValidObjectIdentifier(opts.InClass.Class) { + errs = append(errs, ErrInvalidObjectIdentifier) } return errors.Join(errs...) } func (opts *GrantRoleOptions) validate() error { if opts == nil { - return errNilOptions + return ErrNilOptions } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if (opts.Grant.Role != nil && opts.Grant.User != nil) || (opts.Grant.Role == nil && opts.Grant.User == nil) { errs = append(errs, errors.New("only one grant option can be set [TO ROLE or TO USER]")) } - if opts.Grant.Role != nil && !validObjectidentifier(opts.Grant.Role) { + if opts.Grant.Role != nil && !ValidObjectIdentifier(opts.Grant.Role) { errs = append(errs, errors.New("invalid object identifier for granted role")) } - if opts.Grant.User != nil && !validObjectidentifier(opts.Grant.User) { + if opts.Grant.User != nil && !ValidObjectIdentifier(opts.Grant.User) { errs = append(errs, errors.New("invalid object identifier for granted user")) } return errors.Join(errs...) @@ -89,11 +89,11 @@ func (opts *GrantRoleOptions) validate() error { func (opts *RevokeRoleOptions) validate() error { if opts == nil { - return errNilOptions + return ErrNilOptions } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if (opts.Revoke.Role != nil && opts.Revoke.User != nil) || (opts.Revoke.Role == nil && opts.Revoke.User == nil) { errs = append(errs, errors.New("only one revoke option can be set [FROM ROLE or FROM USER]")) diff --git a/pkg/sdk/schemas.go b/pkg/sdk/schemas.go index 39bfa626d3..101405d57a 100644 --- a/pkg/sdk/schemas.go +++ b/pkg/sdk/schemas.go @@ -107,8 +107,8 @@ type CreateSchemaOptions struct { func (opts *CreateSchemaOptions) validate() error { var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if valueSet(opts.Clone) { if err := opts.Clone.validate(); err != nil { @@ -154,8 +154,8 @@ type AlterSchemaOptions struct { func (opts *AlterSchemaOptions) validate() error { var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if !exactlyOneValueSet(opts.NewName, opts.SwapWith, opts.Set, opts.Unset, opts.EnableManagedAccess, opts.DisableManagedAccess) { errs = append(errs, errOneOf("NewName", "SwapWith", "Set", "Unset", "EnableManagedAccess", "DisableManagedAccess")) @@ -232,8 +232,8 @@ type DropSchemaOptions struct { func (opts *DropSchemaOptions) validate() error { var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if everyValueSet(opts.Cascade, opts.Restrict) { errs = append(errs, errors.New("only one of the fields [ Cascade | Restrict ] can be set at once")) @@ -265,8 +265,8 @@ type undropSchemaOptions struct { } func (opts *undropSchemaOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } return nil } @@ -294,8 +294,8 @@ type describeSchemaOptions struct { } func (opts *describeSchemaOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } return nil } @@ -381,7 +381,7 @@ func (v *schemas) ShowByID(ctx context.Context, id DatabaseObjectIdentifier) (*S return &s, nil } } - return nil, errObjectNotExistOrAuthorized + return nil, ErrObjectNotExistOrAuthorized } func (v *schemas) Use(ctx context.Context, id DatabaseObjectIdentifier) error { diff --git a/pkg/sdk/session_policies_gen_test.go b/pkg/sdk/session_policies_gen_test.go index 68254ef7f1..0c68390c53 100644 --- a/pkg/sdk/session_policies_gen_test.go +++ b/pkg/sdk/session_policies_gen_test.go @@ -3,7 +3,7 @@ package sdk import "testing" func TestSessionPolicies_Create(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() // Minimal valid CreateSessionPolicyOptions defaultOpts := func() *CreateSessionPolicyOptions { @@ -14,13 +14,13 @@ func TestSessionPolicies_Create(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *CreateSessionPolicyOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { opts := defaultOpts() opts.name = NewSchemaObjectIdentifier("", "", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: conflicting fields for [opts.OrReplace opts.IfNotExists]", func(t *testing.T) { @@ -46,7 +46,7 @@ func TestSessionPolicies_Create(t *testing.T) { } func TestSessionPolicies_Alter(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() // Minimal valid AlterSessionPolicyOptions defaultOpts := func() *AlterSessionPolicyOptions { @@ -57,13 +57,13 @@ func TestSessionPolicies_Alter(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *AlterSessionPolicyOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { opts := defaultOpts() opts.name = NewSchemaObjectIdentifier("", "", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: exactly one field from [opts.RenameTo opts.Set opts.SetTags opts.UnsetTags opts.Unset] should be present - none present", func(t *testing.T) { @@ -112,7 +112,7 @@ func TestSessionPolicies_Alter(t *testing.T) { t.Run("alter rename", func(t *testing.T) { opts := defaultOpts() - newId := randomSchemaObjectIdentifier(t) + newId := RandomSchemaObjectIdentifier() opts.RenameTo = &newId assertOptsValidAndSQLEquals(t, opts, "ALTER SESSION POLICY %s RENAME TO %s", id.FullyQualifiedName(), newId.FullyQualifiedName()) }) @@ -143,7 +143,7 @@ func TestSessionPolicies_Alter(t *testing.T) { } func TestSessionPolicies_Drop(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() // Minimal valid DropSessionPolicyOptions defaultOpts := func() *DropSessionPolicyOptions { @@ -154,13 +154,13 @@ func TestSessionPolicies_Drop(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *DropSessionPolicyOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { opts := defaultOpts() opts.name = NewSchemaObjectIdentifier("", "", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("all options", func(t *testing.T) { @@ -177,7 +177,7 @@ func TestSessionPolicies_Show(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *ShowSessionPolicyOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("all options", func(t *testing.T) { @@ -187,7 +187,7 @@ func TestSessionPolicies_Show(t *testing.T) { } func TestSessionPolicies_Describe(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() // Minimal valid DescribeSessionPolicyOptions defaultOpts := func() *DescribeSessionPolicyOptions { @@ -198,13 +198,13 @@ func TestSessionPolicies_Describe(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *DescribeSessionPolicyOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { opts := defaultOpts() opts.name = NewSchemaObjectIdentifier("", "", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("all options", func(t *testing.T) { diff --git a/pkg/sdk/session_policies_impl_gen.go b/pkg/sdk/session_policies_impl_gen.go index ce2287c4d3..1d48dadff7 100644 --- a/pkg/sdk/session_policies_impl_gen.go +++ b/pkg/sdk/session_policies_impl_gen.go @@ -1,6 +1,10 @@ package sdk -import "context" +import ( + "context" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/collections" +) var _ SessionPolicies = (*sessionPolicies)(nil) @@ -39,7 +43,7 @@ func (v *sessionPolicies) ShowByID(ctx context.Context, id SchemaObjectIdentifie return nil, err } - return findOne(sessionPolicies, func(r SessionPolicy) bool { return r.Name == id.Name() }) + return collections.FindOne(sessionPolicies, func(r SessionPolicy) bool { return r.Name == id.Name() }) } func (v *sessionPolicies) Describe(ctx context.Context, id SchemaObjectIdentifier) (*SessionPolicyDescription, error) { diff --git a/pkg/sdk/session_policies_validations_gen.go b/pkg/sdk/session_policies_validations_gen.go index cd6afcc79b..f14894942c 100644 --- a/pkg/sdk/session_policies_validations_gen.go +++ b/pkg/sdk/session_policies_validations_gen.go @@ -12,11 +12,11 @@ var ( func (opts *CreateSessionPolicyOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if everyValueSet(opts.OrReplace, opts.IfNotExists) { errs = append(errs, errOneOf("CreateSessionPolicyOptions", "OrReplace", "IfNotExists")) @@ -26,11 +26,11 @@ func (opts *CreateSessionPolicyOptions) validate() error { func (opts *AlterSessionPolicyOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if ok := exactlyOneValueSet(opts.RenameTo, opts.Set, opts.SetTags, opts.UnsetTags, opts.Unset); !ok { errs = append(errs, errExactlyOneOf("RenameTo", "Set", "SetTags", "UnsetTags", "Unset")) @@ -50,18 +50,18 @@ func (opts *AlterSessionPolicyOptions) validate() error { func (opts *DropSessionPolicyOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } return errors.Join(errs...) } func (opts *ShowSessionPolicyOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error return errors.Join(errs...) @@ -69,11 +69,11 @@ func (opts *ShowSessionPolicyOptions) validate() error { func (opts *DescribeSessionPolicyOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } return errors.Join(errs...) } diff --git a/pkg/sdk/shares.go b/pkg/sdk/shares.go index 6cdfabff47..ede1e2dcc7 100644 --- a/pkg/sdk/shares.go +++ b/pkg/sdk/shares.go @@ -111,7 +111,7 @@ type CreateShareOptions struct { } func (opts *CreateShareOptions) validate() error { - if !validObjectidentifier(opts.name) { + if !ValidObjectIdentifier(opts.name) { return fmt.Errorf("not a valid object identifier: %s", opts.name) } return nil @@ -172,7 +172,7 @@ type AlterShareOptions struct { } func (opts *AlterShareOptions) validate() error { - if !validObjectidentifier(opts.name) { + if !ValidObjectIdentifier(opts.name) { return fmt.Errorf("not a valid object identifier: %s", opts.name) } if ok := exactlyOneValueSet(opts.Add, opts.Remove, opts.Set, opts.Unset); !ok { @@ -302,7 +302,7 @@ func (s *shares) ShowByID(ctx context.Context, id AccountObjectIdentifier) (*Sha return &share, nil } } - return nil, errObjectNotExistOrAuthorized + return nil, ErrObjectNotExistOrAuthorized } type ShareDetails struct { @@ -348,8 +348,8 @@ type describeShareOptions struct { } func (opts *describeShareOptions) validate() error { - if ok := validObjectidentifier(opts.name); !ok { - return errInvalidObjectIdentifier + if ok := ValidObjectIdentifier(opts.name); !ok { + return ErrInvalidObjectIdentifier } return nil } diff --git a/pkg/sdk/shares_test.go b/pkg/sdk/shares_test.go index 0a19a0c42d..4409a24f75 100644 --- a/pkg/sdk/shares_test.go +++ b/pkg/sdk/shares_test.go @@ -3,6 +3,7 @@ package sdk import ( "testing" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -19,7 +20,7 @@ func TestSharesCreate(t *testing.T) { }) t.Run("with complete options", func(t *testing.T) { - comment := randomComment(t) + comment := random.Comment() opts := &CreateShareOptions{ OrReplace: Bool(true), name: NewAccountObjectIdentifier("complete_share"), @@ -76,7 +77,7 @@ func TestShareAlter(t *testing.T) { t.Run("with set", func(t *testing.T) { accounts := []AccountIdentifier{NewAccountIdentifier("my-org", "myaccount")} - comment := randomComment(t) + comment := random.Comment() opts := &AlterShareOptions{ IfExists: Bool(true), name: NewAccountObjectIdentifier("myshare"), diff --git a/pkg/sdk/sql_builder_test.go b/pkg/sdk/sql_builder_test.go index 0a9e7ab12b..f6c0a895de 100644 --- a/pkg/sdk/sql_builder_test.go +++ b/pkg/sdk/sql_builder_test.go @@ -364,7 +364,7 @@ func TestBuilder_parseStruct(t *testing.T) { t.Run("test struct with all fields", func(t *testing.T) { s := &structTestHelper{ static: true, - name: randomAccountObjectIdentifier(t), + name: RandomAccountObjectIdentifier(), Param: String("example"), } clauses, err := builder.parseStruct(s) diff --git a/pkg/sdk/tasks_dto_gen.go b/pkg/sdk/tasks_dto_gen.go index 7ca010806a..6307db4c7c 100644 --- a/pkg/sdk/tasks_dto_gen.go +++ b/pkg/sdk/tasks_dto_gen.go @@ -32,6 +32,10 @@ type CreateTaskRequest struct { sql string // required } +func (r *CreateTaskRequest) GetName() SchemaObjectIdentifier { + return r.name +} + type CreateTaskWarehouseRequest struct { Warehouse *AccountObjectIdentifier UserTaskManagedInitialWarehouseSize *WarehouseSize @@ -44,6 +48,10 @@ type CloneTaskRequest struct { CopyGrants *bool } +func (r *CloneTaskRequest) GetName() SchemaObjectIdentifier { + return r.name +} + type AlterTaskRequest struct { IfExists *bool name SchemaObjectIdentifier // required diff --git a/pkg/sdk/tasks_gen_test.go b/pkg/sdk/tasks_gen_test.go index fc32173c65..12b7eeabb8 100644 --- a/pkg/sdk/tasks_gen_test.go +++ b/pkg/sdk/tasks_gen_test.go @@ -6,7 +6,7 @@ import ( ) func TestTasks_Create(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() sql := "SELECT CURRENT_TIMESTAMP" // Minimal valid CreateTaskOptions @@ -19,13 +19,13 @@ func TestTasks_Create(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *CreateTaskOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { opts := defaultOpts() opts.name = NewSchemaObjectIdentifier("", "", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: conflicting fields for [opts.OrReplace opts.IfNotExists]", func(t *testing.T) { @@ -61,9 +61,9 @@ func TestTasks_Create(t *testing.T) { }) t.Run("all options", func(t *testing.T) { - warehouseId := randomAccountObjectIdentifier(t) - otherTaskId := randomSchemaObjectIdentifier(t) - tagId := randomSchemaObjectIdentifier(t) + warehouseId := RandomAccountObjectIdentifier() + otherTaskId := RandomSchemaObjectIdentifier() + tagId := RandomSchemaObjectIdentifier() req := NewCreateTaskRequest(id, sql). WithOrReplace(Bool(true)). @@ -91,8 +91,8 @@ func TestTasks_Create(t *testing.T) { } func TestTasks_Clone(t *testing.T) { - id := randomSchemaObjectIdentifier(t) - sourceId := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() + sourceId := RandomSchemaObjectIdentifier() // Minimal valid CloneTaskOptions defaultOpts := func() *CloneTaskOptions { @@ -104,19 +104,19 @@ func TestTasks_Clone(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *CloneTaskOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { opts := defaultOpts() opts.name = NewSchemaObjectIdentifier("", "", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: valid identifier for [opts.sourceTask]", func(t *testing.T) { opts := defaultOpts() opts.sourceTask = NewSchemaObjectIdentifier("", "", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("basic", func(t *testing.T) { @@ -133,8 +133,8 @@ func TestTasks_Clone(t *testing.T) { } func TestTasks_Alter(t *testing.T) { - id := randomSchemaObjectIdentifier(t) - otherTaskId := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() + otherTaskId := RandomSchemaObjectIdentifier() // Minimal valid AlterTaskOptions defaultOpts := func() *AlterTaskOptions { @@ -145,13 +145,13 @@ func TestTasks_Alter(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *AlterTaskOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { opts := defaultOpts() opts.name = NewSchemaObjectIdentifier("", "", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("validation: exactly one field from [opts.Resume opts.Suspend opts.RemoveAfter opts.AddAfter opts.Set opts.Unset opts.SetTags opts.UnsetTags opts.ModifyAs opts.ModifyWhen] should be present", func(t *testing.T) { @@ -282,7 +282,7 @@ func TestTasks_Alter(t *testing.T) { } func TestTasks_Drop(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() // Minimal valid DropTaskOptions defaultOpts := func() *DropTaskOptions { @@ -293,13 +293,13 @@ func TestTasks_Drop(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *DropTaskOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { opts := defaultOpts() opts.name = NewSchemaObjectIdentifier("", "", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("basic", func(t *testing.T) { @@ -316,7 +316,7 @@ func TestTasks_Show(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *ShowTaskOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("basic", func(t *testing.T) { @@ -341,7 +341,7 @@ func TestTasks_Show(t *testing.T) { } func TestTasks_Describe(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() // Minimal valid DescribeTaskOptions defaultOpts := func() *DescribeTaskOptions { @@ -352,13 +352,13 @@ func TestTasks_Describe(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *DescribeTaskOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { opts := defaultOpts() opts.name = NewSchemaObjectIdentifier("", "", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("basic", func(t *testing.T) { @@ -368,7 +368,7 @@ func TestTasks_Describe(t *testing.T) { } func TestTasks_Execute(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() // Minimal valid ExecuteTaskOptions defaultOpts := func() *ExecuteTaskOptions { @@ -379,13 +379,13 @@ func TestTasks_Execute(t *testing.T) { t.Run("validation: nil options", func(t *testing.T) { var opts *ExecuteTaskOptions = nil - assertOptsInvalidJoinedErrors(t, opts, errNilOptions) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) t.Run("validation: valid identifier for [opts.name]", func(t *testing.T) { opts := defaultOpts() opts.name = NewSchemaObjectIdentifier("", "", "") - assertOptsInvalidJoinedErrors(t, opts, errInvalidObjectIdentifier) + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) t.Run("basic", func(t *testing.T) { diff --git a/pkg/sdk/tasks_validations_gen.go b/pkg/sdk/tasks_validations_gen.go index 084209eb44..4148d375a3 100644 --- a/pkg/sdk/tasks_validations_gen.go +++ b/pkg/sdk/tasks_validations_gen.go @@ -14,11 +14,11 @@ var ( func (opts *CreateTaskOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if everyValueSet(opts.OrReplace, opts.IfNotExists) { errs = append(errs, errOneOf("CreateTaskOptions", "OrReplace", "IfNotExists")) @@ -38,25 +38,25 @@ func (opts *CreateTaskOptions) validate() error { func (opts *CloneTaskOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } - if !validObjectidentifier(opts.sourceTask) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.sourceTask) { + errs = append(errs, ErrInvalidObjectIdentifier) } return errors.Join(errs...) } func (opts *AlterTaskOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } if ok := exactlyOneValueSet(opts.Resume, opts.Suspend, opts.RemoveAfter, opts.AddAfter, opts.Set, opts.Unset, opts.SetTags, opts.UnsetTags, opts.ModifyAs, opts.ModifyWhen); !ok { errs = append(errs, errExactlyOneOf("Resume", "Suspend", "RemoveAfter", "AddAfter", "Set", "Unset", "SetTags", "UnsetTags", "ModifyAs", "ModifyWhen")) @@ -86,18 +86,18 @@ func (opts *AlterTaskOptions) validate() error { func (opts *DropTaskOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } return errors.Join(errs...) } func (opts *ShowTaskOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error return errors.Join(errs...) @@ -105,22 +105,22 @@ func (opts *ShowTaskOptions) validate() error { func (opts *DescribeTaskOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } return errors.Join(errs...) } func (opts *ExecuteTaskOptions) validate() error { if opts == nil { - return errors.Join(errNilOptions) + return errors.Join(ErrNilOptions) } var errs []error - if !validObjectidentifier(opts.name) { - errs = append(errs, errInvalidObjectIdentifier) + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) } return errors.Join(errs...) } diff --git a/pkg/sdk/testint/accounts_integration_test.go b/pkg/sdk/testint/accounts_integration_test.go index dfed16a8ba..566418e5f7 100644 --- a/pkg/sdk/testint/accounts_integration_test.go +++ b/pkg/sdk/testint/accounts_integration_test.go @@ -8,6 +8,7 @@ import ( "time" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/avast/retry-go" "github.com/brianvoe/gofakeit/v6" "github.com/stretchr/testify/assert" @@ -58,13 +59,13 @@ func TestInt_AccountCreate(t *testing.T) { t.Skip("ORGADMIN role is not in current session") } t.Run("complete case", func(t *testing.T) { - accountID := sdk.NewAccountObjectIdentifier("TF_" + strings.ToUpper(gofakeit.Fruit()) + "_" + fmt.Sprintf("%d", randomIntRange(t, 100, 999))) + accountID := sdk.NewAccountObjectIdentifier("TF_" + strings.ToUpper(gofakeit.Fruit()) + "_" + fmt.Sprintf("%d", random.IntRange(100, 999))) region, err := client.ContextFunctions.CurrentRegion(ctx) require.NoError(t, err) opts := &sdk.CreateAccountOptions{ AdminName: "someadmin", - AdminPassword: sdk.String(randomStringN(t, 12)), + AdminPassword: sdk.String(random.StringN(12)), FirstName: sdk.String("Ad"), LastName: sdk.String("Min"), Email: "admin@example.com", @@ -95,7 +96,7 @@ func TestInt_AccountCreate(t *testing.T) { assert.Equal(t, region, account.SnowflakeRegion) // rename - newAccountID := sdk.NewAccountObjectIdentifier("TF_" + strings.ToUpper(gofakeit.Animal()) + "_" + fmt.Sprintf("%d", randomIntRange(t, 100, 999))) + newAccountID := sdk.NewAccountObjectIdentifier("TF_" + strings.ToUpper(gofakeit.Animal()) + "_" + fmt.Sprintf("%d", random.IntRange(100, 999))) alterOpts := &sdk.AlterAccountOptions{ Rename: &sdk.AccountRename{ Name: accountID, diff --git a/pkg/sdk/testint/alerts_integration_test.go b/pkg/sdk/testint/alerts_integration_test.go index a696d03cb5..c200109baa 100644 --- a/pkg/sdk/testint/alerts_integration_test.go +++ b/pkg/sdk/testint/alerts_integration_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -99,11 +100,11 @@ func TestInt_AlertCreate(t *testing.T) { t.Cleanup(warehouseCleanup) t.Run("test complete case", func(t *testing.T) { - name := randomString(t) + name := random.String() schedule := "USING CRON * * * * TUE,THU UTC" condition := "SELECT 1" action := "SELECT 1" - comment := randomComment(t) + comment := random.Comment() id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) err := client.Alerts.Create(ctx, id, testWarehouse.ID(), schedule, condition, action, &sdk.CreateAlertOptions{ OrReplace: sdk.Bool(true), @@ -135,11 +136,11 @@ func TestInt_AlertCreate(t *testing.T) { }) t.Run("test if_not_exists", func(t *testing.T) { - name := randomString(t) + name := random.String() schedule := "USING CRON * * * * TUE,THU UTC" condition := "SELECT 1" action := "SELECT 1" - comment := randomComment(t) + comment := random.Comment() id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) err := client.Alerts.Create(ctx, id, testWarehouse.ID(), schedule, condition, action, &sdk.CreateAlertOptions{ OrReplace: sdk.Bool(false), @@ -171,7 +172,7 @@ func TestInt_AlertCreate(t *testing.T) { }) t.Run("test no options", func(t *testing.T) { - name := randomString(t) + name := random.String() schedule := "USING CRON * * * * TUE,THU UTC" condition := "SELECT 1" action := "SELECT 1" @@ -201,7 +202,7 @@ func TestInt_AlertCreate(t *testing.T) { }) t.Run("test multiline action", func(t *testing.T) { - name := randomString(t) + name := random.String() schedule := "USING CRON * * * * TUE,THU UTC" condition := "SELECT 1" action := ` diff --git a/pkg/sdk/testint/comments_integration_test.go b/pkg/sdk/testint/comments_integration_test.go index 9f85718a68..6c5b29d0b5 100644 --- a/pkg/sdk/testint/comments_integration_test.go +++ b/pkg/sdk/testint/comments_integration_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -16,7 +17,7 @@ func TestInt_Comment(t *testing.T) { t.Cleanup(warehouseCleanup) t.Run("set", func(t *testing.T) { - comment := randomComment(t) + comment := random.Comment() err := client.Comments.Set(ctx, &sdk.SetCommentOptions{ ObjectType: sdk.ObjectTypeWarehouse, ObjectName: testWarehouse.ID(), diff --git a/pkg/sdk/testint/database_role_integration_test.go b/pkg/sdk/testint/database_role_integration_test.go index 449dce8eff..f8a4eddb5e 100644 --- a/pkg/sdk/testint/database_role_integration_test.go +++ b/pkg/sdk/testint/database_role_integration_test.go @@ -4,6 +4,8 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/collections" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -35,7 +37,7 @@ func TestInt_DatabaseRoles(t *testing.T) { createDatabaseRole := func(t *testing.T) *sdk.DatabaseRole { t.Helper() - name := randomString(t) + name := random.String() id := sdk.NewDatabaseObjectIdentifier(database.Name, name) err := client.DatabaseRoles.Create(ctx, sdk.NewCreateDatabaseRoleRequest(id)) @@ -49,9 +51,9 @@ func TestInt_DatabaseRoles(t *testing.T) { } t.Run("create database_role: complete case", func(t *testing.T) { - name := randomString(t) + name := random.String() id := sdk.NewDatabaseObjectIdentifier(database.Name, name) - comment := randomComment(t) + comment := random.Comment() request := sdk.NewCreateDatabaseRoleRequest(id).WithComment(&comment).WithIfNotExists(true) err := client.DatabaseRoles.Create(ctx, request) @@ -65,7 +67,7 @@ func TestInt_DatabaseRoles(t *testing.T) { }) t.Run("create database_role: no optionals", func(t *testing.T) { - name := randomString(t) + name := random.String() id := sdk.NewDatabaseObjectIdentifier(database.Name, name) err := client.DatabaseRoles.Create(ctx, sdk.NewCreateDatabaseRoleRequest(id)) @@ -79,7 +81,7 @@ func TestInt_DatabaseRoles(t *testing.T) { }) t.Run("drop database_role: existing", func(t *testing.T) { - name := randomString(t) + name := random.String() id := sdk.NewDatabaseObjectIdentifier(database.Name, name) err := client.DatabaseRoles.Create(ctx, sdk.NewCreateDatabaseRoleRequest(id)) @@ -89,7 +91,7 @@ func TestInt_DatabaseRoles(t *testing.T) { require.NoError(t, err) _, err = client.DatabaseRoles.ShowByID(ctx, id) - assert.ErrorIs(t, err, sdk.ErrObjectNotExistOrAuthorized) + assert.ErrorIs(t, err, collections.ErrObjectNotFound) }) t.Run("drop database_role: non-existing", func(t *testing.T) { @@ -100,7 +102,7 @@ func TestInt_DatabaseRoles(t *testing.T) { }) t.Run("alter database_role: set value and unset value", func(t *testing.T) { - name := randomString(t) + name := random.String() id := sdk.NewDatabaseObjectIdentifier(database.Name, name) err := client.DatabaseRoles.Create(ctx, sdk.NewCreateDatabaseRoleRequest(id)) @@ -127,13 +129,13 @@ func TestInt_DatabaseRoles(t *testing.T) { }) t.Run("alter database_role: rename", func(t *testing.T) { - name := randomString(t) + name := random.String() id := sdk.NewDatabaseObjectIdentifier(database.Name, name) err := client.DatabaseRoles.Create(ctx, sdk.NewCreateDatabaseRoleRequest(id)) require.NoError(t, err) - newName := randomString(t) + newName := random.String() newId := sdk.NewDatabaseObjectIdentifier(database.Name, newName) alterRequest := sdk.NewAlterDatabaseRoleRequest(id).WithRename(newId) @@ -146,7 +148,7 @@ func TestInt_DatabaseRoles(t *testing.T) { require.NoError(t, err) _, err = client.DatabaseRoles.ShowByID(ctx, id) - assert.ErrorIs(t, err, sdk.ErrObjectNotExistOrAuthorized) + assert.ErrorIs(t, err, collections.ErrObjectNotFound) databaseRole, err := client.DatabaseRoles.ShowByID(ctx, newId) require.NoError(t, err) @@ -158,14 +160,14 @@ func TestInt_DatabaseRoles(t *testing.T) { secondDatabase, secondDatabaseCleanup := createDatabase(t, client) t.Cleanup(secondDatabaseCleanup) - name := randomString(t) + name := random.String() id := sdk.NewDatabaseObjectIdentifier(database.Name, name) err := client.DatabaseRoles.Create(ctx, sdk.NewCreateDatabaseRoleRequest(id)) require.NoError(t, err) t.Cleanup(cleanupDatabaseRoleProvider(id)) - newName := randomString(t) + newName := random.String() newId := sdk.NewDatabaseObjectIdentifier(secondDatabase.Name, newName) alterRequest := sdk.NewAlterDatabaseRoleRequest(id).WithRename(newId) diff --git a/pkg/sdk/testint/databases_integration_test.go b/pkg/sdk/testint/databases_integration_test.go index 24aa53cc37..48d3832e93 100644 --- a/pkg/sdk/testint/databases_integration_test.go +++ b/pkg/sdk/testint/databases_integration_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -14,7 +15,7 @@ func TestInt_DatabasesCreate(t *testing.T) { ctx := testContext(t) t.Run("minimal", func(t *testing.T) { - databaseID := randomAccountObjectIdentifier(t) + databaseID := sdk.RandomAccountObjectIdentifier() err := client.Databases.Create(ctx, databaseID, nil) require.NoError(t, err) database, err := client.Databases.ShowByID(ctx, databaseID) @@ -29,7 +30,7 @@ func TestInt_DatabasesCreate(t *testing.T) { t.Run("as clone", func(t *testing.T) { cloneDatabase, cloneDatabaseCleanup := createDatabase(t, client) t.Cleanup(cloneDatabaseCleanup) - databaseID := randomAccountObjectIdentifier(t) + databaseID := sdk.RandomAccountObjectIdentifier() opts := &sdk.CreateDatabaseOptions{ Clone: &sdk.Clone{ SourceObject: cloneDatabase.ID(), @@ -50,7 +51,7 @@ func TestInt_DatabasesCreate(t *testing.T) { }) t.Run("complete", func(t *testing.T) { - databaseID := randomAccountObjectIdentifier(t) + databaseID := sdk.RandomAccountObjectIdentifier() databaseTest, databaseCleanup := createDatabase(t, client) t.Cleanup(databaseCleanup) @@ -61,7 +62,7 @@ func TestInt_DatabasesCreate(t *testing.T) { tag2Test, tag2Cleanup := createTag(t, client, databaseTest, schemaTest) t.Cleanup(tag2Cleanup) - comment := randomComment(t) + comment := random.Comment() opts := &sdk.CreateDatabaseOptions{ OrReplace: sdk.Bool(true), Transient: sdk.Bool(true), @@ -135,7 +136,7 @@ func TestInt_CreateShared(t *testing.T) { }, }) - databaseID := randomAccountObjectIdentifier(t) + databaseID := sdk.RandomAccountObjectIdentifier() err = secondaryClient.Databases.CreateShared(ctx, databaseID, shareTest.ExternalID(), nil) require.NoError(t, err) database, err := secondaryClient.Databases.ShowByID(ctx, databaseID) @@ -207,7 +208,7 @@ func TestInt_DatabasesAlter(t *testing.T) { t.Run("renaming", func(t *testing.T) { databaseTest, _ := createDatabase(t, client) - newName := randomAccountObjectIdentifier(t) + newName := sdk.RandomAccountObjectIdentifier() err := client.Databases.Alter(ctx, databaseTest.ID(), &sdk.AlterDatabaseOptions{ NewName: newName, }) diff --git a/pkg/sdk/testint/dynamic_table_integration_test.go b/pkg/sdk/testint/dynamic_table_integration_test.go index d7279196f6..764844c857 100644 --- a/pkg/sdk/testint/dynamic_table_integration_test.go +++ b/pkg/sdk/testint/dynamic_table_integration_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -22,12 +23,12 @@ func TestInt_DynamicTableCreateAndDrop(t *testing.T) { ctx := testContext(t) t.Run("test complete", func(t *testing.T) { - name := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, randomString(t)) + name := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, random.String()) targetLag := sdk.TargetLag{ Lagtime: sdk.String("2 minutes"), } query := "select id from " + tableTest.ID().FullyQualifiedName() - comment := randomComment(t) + comment := random.Comment() err := client.DynamicTables.Create(ctx, sdk.NewCreateDynamicTableRequest(name, warehouseTest.ID(), targetLag, query).WithOrReplace(true).WithComment(&comment)) require.NoError(t, err) t.Cleanup(func() { @@ -45,12 +46,12 @@ func TestInt_DynamicTableCreateAndDrop(t *testing.T) { }) t.Run("test complete with target lag", func(t *testing.T) { - name := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, randomString(t)) + name := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, random.String()) targetLag := sdk.TargetLag{ Downstream: sdk.Bool(true), } query := "select id from " + tableTest.ID().FullyQualifiedName() - comment := randomComment(t) + comment := random.Comment() err := client.DynamicTables.Create(ctx, sdk.NewCreateDynamicTableRequest(name, warehouseTest.ID(), targetLag, query).WithOrReplace(true).WithComment(&comment)) require.NoError(t, err) t.Cleanup(func() { diff --git a/pkg/sdk/testint/external_tables_integration_test.go b/pkg/sdk/testint/external_tables_integration_test.go index 005c6b58d2..e7db17e6cc 100644 --- a/pkg/sdk/testint/external_tables_integration_test.go +++ b/pkg/sdk/testint/external_tables_integration_test.go @@ -5,6 +5,8 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/collections" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -63,7 +65,7 @@ func TestInt_ExternalTables(t *testing.T) { } t.Run("Create: minimal", func(t *testing.T) { - name := randomAlphanumericN(t, 32) + name := random.AlphanumericN(32) externalTableID := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, name) err := client.ExternalTables.Create(ctx, minimalCreateExternalTableReq(name)) require.NoError(t, err) @@ -74,7 +76,7 @@ func TestInt_ExternalTables(t *testing.T) { }) t.Run("Create: complete", func(t *testing.T) { - name := randomAlphanumericN(t, 32) + name := random.AlphanumericN(32) externalTableID := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, name) err := client.ExternalTables.Create( ctx, @@ -108,7 +110,7 @@ func TestInt_ExternalTables(t *testing.T) { err := client.Sessions.UseWarehouse(ctx, warehouse.ID()) require.NoError(t, err) - name := randomAlphanumericN(t, 32) + name := random.AlphanumericN(32) id := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, name) query := fmt.Sprintf(`SELECT ARRAY_AGG(OBJECT_CONSTRUCT(*)) WITHIN GROUP (ORDER BY order_id) FROM TABLE (INFER_SCHEMA(location => '%s', FILE_FORMAT=>'%s', ignore_case => true))`, stageLocation, fileFormat.ID().FullyQualifiedName()) err = client.ExternalTables.CreateUsingTemplate( @@ -127,7 +129,7 @@ func TestInt_ExternalTables(t *testing.T) { }) t.Run("Create with manual partitioning: complete", func(t *testing.T) { - name := randomAlphanumericN(t, 32) + name := random.AlphanumericN(32) externalTableID := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, name) err := client.ExternalTables.CreateWithManualPartitioning(ctx, createExternalTableWithManualPartitioningReq(name)) require.NoError(t, err) @@ -138,7 +140,7 @@ func TestInt_ExternalTables(t *testing.T) { }) t.Run("Create delta lake: complete", func(t *testing.T) { - name := randomAlphanumericN(t, 32) + name := random.AlphanumericN(32) externalTableID := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, name) err := client.ExternalTables.CreateDeltaLake( ctx, @@ -165,7 +167,7 @@ func TestInt_ExternalTables(t *testing.T) { }) t.Run("Alter: refresh", func(t *testing.T) { - name := randomAlphanumericN(t, 32) + name := random.AlphanumericN(32) externalTableID := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, name) err := client.ExternalTables.Create(ctx, minimalCreateExternalTableReq(name)) require.NoError(t, err) @@ -180,7 +182,7 @@ func TestInt_ExternalTables(t *testing.T) { }) t.Run("Alter: add files", func(t *testing.T) { - name := randomAlphanumericN(t, 32) + name := random.AlphanumericN(32) externalTableID := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, name) err := client.ExternalTables.Create( ctx, @@ -199,7 +201,7 @@ func TestInt_ExternalTables(t *testing.T) { }) t.Run("Alter: remove files", func(t *testing.T) { - name := randomAlphanumericN(t, 32) + name := random.AlphanumericN(32) externalTableID := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, name) err := client.ExternalTables.Create( ctx, @@ -226,7 +228,7 @@ func TestInt_ExternalTables(t *testing.T) { }) t.Run("Alter: set auto refresh", func(t *testing.T) { - name := randomAlphanumericN(t, 32) + name := random.AlphanumericN(32) externalTableID := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, name) err := client.ExternalTables.Create(ctx, minimalCreateExternalTableReq(name)) require.NoError(t, err) @@ -286,7 +288,7 @@ func TestInt_ExternalTables(t *testing.T) { // }) t.Run("Alter: add partitions", func(t *testing.T) { - name := randomAlphanumericN(t, 32) + name := random.AlphanumericN(32) externalTableID := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, name) err := client.ExternalTables.CreateWithManualPartitioning(ctx, createExternalTableWithManualPartitioningReq(name)) require.NoError(t, err) @@ -302,7 +304,7 @@ func TestInt_ExternalTables(t *testing.T) { }) t.Run("Alter: drop partitions", func(t *testing.T) { - name := randomAlphanumericN(t, 32) + name := random.AlphanumericN(32) externalTableID := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, name) err := client.ExternalTables.CreateWithManualPartitioning(ctx, createExternalTableWithManualPartitioningReq(name)) require.NoError(t, err) @@ -327,7 +329,7 @@ func TestInt_ExternalTables(t *testing.T) { }) t.Run("Drop", func(t *testing.T) { - name := randomAlphanumericN(t, 32) + name := random.AlphanumericN(32) externalTableID := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, name) err := client.ExternalTables.Create(ctx, minimalCreateExternalTableReq(name)) require.NoError(t, err) @@ -341,11 +343,11 @@ func TestInt_ExternalTables(t *testing.T) { require.NoError(t, err) _, err = client.ExternalTables.ShowByID(ctx, sdk.NewShowExternalTableByIDRequest(externalTableID)) - require.ErrorIs(t, err, sdk.ErrObjectNotExistOrAuthorized) + require.ErrorIs(t, err, collections.ErrObjectNotFound) }) t.Run("Show", func(t *testing.T) { - name := randomAlphanumericN(t, 32) + name := random.AlphanumericN(32) externalTableID := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, name) err := client.ExternalTables.Create(ctx, minimalCreateExternalTableReq(name)) require.NoError(t, err) @@ -365,7 +367,7 @@ func TestInt_ExternalTables(t *testing.T) { }) t.Run("Describe: columns", func(t *testing.T) { - name := randomAlphanumericN(t, 32) + name := random.AlphanumericN(32) externalTableID := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, name) req := minimalCreateExternalTableReq(name) err := client.ExternalTables.Create(ctx, req) @@ -391,7 +393,7 @@ func TestInt_ExternalTables(t *testing.T) { }) t.Run("Describe: stage", func(t *testing.T) { - name := randomAlphanumericN(t, 32) + name := random.AlphanumericN(32) externalTableID := sdk.NewSchemaObjectIdentifier(db.Name, schema.Name, name) err := client.ExternalTables.Create(ctx, minimalCreateExternalTableReq(name)) require.NoError(t, err) diff --git a/pkg/sdk/testint/failover_groups_integration_test.go b/pkg/sdk/testint/failover_groups_integration_test.go index 609e669853..c957620028 100644 --- a/pkg/sdk/testint/failover_groups_integration_test.go +++ b/pkg/sdk/testint/failover_groups_integration_test.go @@ -25,7 +25,7 @@ func TestInt_FailoverGroupsCreate(t *testing.T) { t.Cleanup(shareCleanup) t.Run("test complete", func(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := sdk.RandomAccountObjectIdentifier() objectTypes := []sdk.PluralObjectType{ sdk.PluralObjectTypeShares, sdk.PluralObjectTypeDatabases, @@ -77,7 +77,7 @@ func TestInt_FailoverGroupsCreate(t *testing.T) { }) t.Run("test with allowed integration types", func(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := sdk.RandomAccountObjectIdentifier() objectTypes := []sdk.PluralObjectType{ sdk.PluralObjectTypeIntegrations, } @@ -121,7 +121,7 @@ func TestInt_CreateSecondaryReplicationGroup(t *testing.T) { t.Cleanup(cleanupDatabase) // create a failover group in primary account and share with target account - id := randomAccountObjectIdentifier(t) + id := sdk.RandomAccountObjectIdentifier() opts := &sdk.CreateFailoverGroupOptions{ AllowedShares: []sdk.AccountObjectIdentifier{ @@ -191,7 +191,7 @@ func TestInt_FailoverGroupsAlterSource(t *testing.T) { t.Run("rename the failover group", func(t *testing.T) { failoverGroup, _ := createFailoverGroup(t, client) oldID := failoverGroup.ID() - newID := randomAccountObjectIdentifier(t) + newID := sdk.RandomAccountObjectIdentifier() opts := &sdk.AlterSourceFailoverGroupOptions{ NewName: newID, } @@ -556,7 +556,7 @@ func TestInt_FailoverGroupsAlterTarget(t *testing.T) { t.Cleanup(cleanupDatabase) // create a failover group in primary account and share with target account - id := randomAccountObjectIdentifier(t) + id := sdk.RandomAccountObjectIdentifier() opts := &sdk.CreateFailoverGroupOptions{ AllowedDatabases: []sdk.AccountObjectIdentifier{ diff --git a/pkg/sdk/testint/file_format_integration_test.go b/pkg/sdk/testint/file_format_integration_test.go index 25e03ed01e..443201f35f 100644 --- a/pkg/sdk/testint/file_format_integration_test.go +++ b/pkg/sdk/testint/file_format_integration_test.go @@ -5,6 +5,7 @@ import ( "time" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -18,7 +19,7 @@ func TestInt_FileFormatsCreateAndRead(t *testing.T) { t.Cleanup(schemaCleanup) t.Run("CSV", func(t *testing.T) { - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schema.Name, randomString(t)) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schema.Name, random.String()) err := client.FileFormats.Create(ctx, id, &sdk.CreateFileFormatOptions{ Type: sdk.FileFormatTypeCSV, FileFormatTypeOptions: sdk.FileFormatTypeOptions{ @@ -107,7 +108,7 @@ func TestInt_FileFormatsCreateAndRead(t *testing.T) { assert.Equal(t, &sdk.CSVEncodingGB18030, describeResult.Options.CSVEncoding) }) t.Run("JSON", func(t *testing.T) { - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schema.Name, randomString(t)) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schema.Name, random.String()) err := client.FileFormats.Create(ctx, id, &sdk.CreateFileFormatOptions{ Type: sdk.FileFormatTypeJSON, FileFormatTypeOptions: sdk.FileFormatTypeOptions{ @@ -179,7 +180,7 @@ func TestInt_FileFormatsCreateAndRead(t *testing.T) { assert.Equal(t, true, *describeResult.Options.JSONSkipByteOrderMark) }) t.Run("AVRO", func(t *testing.T) { - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schema.Name, randomString(t)) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schema.Name, random.String()) err := client.FileFormats.Create(ctx, id, &sdk.CreateFileFormatOptions{ Type: sdk.FileFormatTypeAvro, FileFormatTypeOptions: sdk.FileFormatTypeOptions{ @@ -221,7 +222,7 @@ func TestInt_FileFormatsCreateAndRead(t *testing.T) { assert.Equal(t, []sdk.NullString{{S: "a"}, {S: "b"}}, *describeResult.Options.AvroNullIf) }) t.Run("ORC", func(t *testing.T) { - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schema.Name, randomString(t)) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schema.Name, random.String()) err := client.FileFormats.Create(ctx, id, &sdk.CreateFileFormatOptions{ Type: sdk.FileFormatTypeORC, FileFormatTypeOptions: sdk.FileFormatTypeOptions{ @@ -260,7 +261,7 @@ func TestInt_FileFormatsCreateAndRead(t *testing.T) { assert.Equal(t, []sdk.NullString{{S: "a"}, {S: "b"}}, *describeResult.Options.ORCNullIf) }) t.Run("PARQUET", func(t *testing.T) { - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schema.Name, randomString(t)) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schema.Name, random.String()) err := client.FileFormats.Create(ctx, id, &sdk.CreateFileFormatOptions{ Type: sdk.FileFormatTypeParquet, FileFormatTypeOptions: sdk.FileFormatTypeOptions{ @@ -305,7 +306,7 @@ func TestInt_FileFormatsCreateAndRead(t *testing.T) { assert.Equal(t, []sdk.NullString{{S: "a"}, {S: "b"}}, *describeResult.Options.ParquetNullIf) }) t.Run("XML", func(t *testing.T) { - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schema.Name, randomString(t)) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schema.Name, random.String()) err := client.FileFormats.Create(ctx, id, &sdk.CreateFileFormatOptions{ Type: sdk.FileFormatTypeXML, FileFormatTypeOptions: sdk.FileFormatTypeOptions{ @@ -370,7 +371,7 @@ func TestInt_FileFormatsAlter(t *testing.T) { fileFormat, fileFormatCleanup := createFileFormat(t, client, schemaTest.ID()) t.Cleanup(fileFormatCleanup) oldId := fileFormat.ID() - newId := sdk.NewSchemaObjectIdentifier(oldId.DatabaseName(), oldId.SchemaName(), randomString(t)) + newId := sdk.NewSchemaObjectIdentifier(oldId.DatabaseName(), oldId.SchemaName(), random.String()) err := client.FileFormats.Alter(ctx, oldId, &sdk.AlterFileFormatOptions{ Rename: &sdk.AlterFileFormatRenameOptions{ diff --git a/pkg/sdk/testint/grants_integration_test.go b/pkg/sdk/testint/grants_integration_test.go index 67d1b3f7f3..8ebfc9be52 100644 --- a/pkg/sdk/testint/grants_integration_test.go +++ b/pkg/sdk/testint/grants_integration_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/collections" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -246,11 +247,11 @@ func TestInt_GrantAndRevokePrivilegesToDatabaseRole(t *testing.T) { // Expecting two grants because database role has usage on database by default require.Equal(t, 2, len(returnedGrants)) - usagePrivilege, err := sdk.FindOne[sdk.Grant](returnedGrants, func(g sdk.Grant) bool { return g.Privilege == sdk.AccountObjectPrivilegeUsage.String() }) + usagePrivilege, err := collections.FindOne[sdk.Grant](returnedGrants, func(g sdk.Grant) bool { return g.Privilege == sdk.AccountObjectPrivilegeUsage.String() }) require.NoError(t, err) assert.Equal(t, sdk.ObjectTypeDatabaseRole, usagePrivilege.GrantedTo) - createSchemaPrivilege, err := sdk.FindOne[sdk.Grant](returnedGrants, func(g sdk.Grant) bool { return g.Privilege == sdk.AccountObjectPrivilegeCreateSchema.String() }) + createSchemaPrivilege, err := collections.FindOne[sdk.Grant](returnedGrants, func(g sdk.Grant) bool { return g.Privilege == sdk.AccountObjectPrivilegeCreateSchema.String() }) require.NoError(t, err) assert.Equal(t, sdk.ObjectTypeDatabase, createSchemaPrivilege.GrantedOn) assert.Equal(t, sdk.ObjectTypeDatabaseRole, createSchemaPrivilege.GrantedTo) @@ -295,11 +296,11 @@ func TestInt_GrantAndRevokePrivilegesToDatabaseRole(t *testing.T) { // Expecting two grants because database role has usage on database by default require.Equal(t, 2, len(returnedGrants)) - usagePrivilege, err := sdk.FindOne[sdk.Grant](returnedGrants, func(g sdk.Grant) bool { return g.Privilege == sdk.AccountObjectPrivilegeUsage.String() }) + usagePrivilege, err := collections.FindOne[sdk.Grant](returnedGrants, func(g sdk.Grant) bool { return g.Privilege == sdk.AccountObjectPrivilegeUsage.String() }) require.NoError(t, err) assert.Equal(t, sdk.ObjectTypeDatabaseRole, usagePrivilege.GrantedTo) - createAlertPrivilege, err := sdk.FindOne[sdk.Grant](returnedGrants, func(g sdk.Grant) bool { return g.Privilege == sdk.SchemaPrivilegeCreateAlert.String() }) + createAlertPrivilege, err := collections.FindOne[sdk.Grant](returnedGrants, func(g sdk.Grant) bool { return g.Privilege == sdk.SchemaPrivilegeCreateAlert.String() }) require.NoError(t, err) assert.Equal(t, sdk.ObjectTypeSchema, createAlertPrivilege.GrantedOn) assert.Equal(t, sdk.ObjectTypeDatabaseRole, createAlertPrivilege.GrantedTo) @@ -348,11 +349,11 @@ func TestInt_GrantAndRevokePrivilegesToDatabaseRole(t *testing.T) { // Expecting two grants because database role has usage on database by default require.Equal(t, 2, len(returnedGrants)) - usagePrivilege, err := sdk.FindOne[sdk.Grant](returnedGrants, func(g sdk.Grant) bool { return g.Privilege == sdk.AccountObjectPrivilegeUsage.String() }) + usagePrivilege, err := collections.FindOne[sdk.Grant](returnedGrants, func(g sdk.Grant) bool { return g.Privilege == sdk.AccountObjectPrivilegeUsage.String() }) require.NoError(t, err) assert.Equal(t, sdk.ObjectTypeDatabaseRole, usagePrivilege.GrantedTo) - selectPrivilege, err := sdk.FindOne[sdk.Grant](returnedGrants, func(g sdk.Grant) bool { return g.Privilege == sdk.SchemaObjectPrivilegeSelect.String() }) + selectPrivilege, err := collections.FindOne[sdk.Grant](returnedGrants, func(g sdk.Grant) bool { return g.Privilege == sdk.SchemaObjectPrivilegeSelect.String() }) require.NoError(t, err) assert.Equal(t, sdk.ObjectTypeTable, selectPrivilege.GrantedOn) assert.Equal(t, sdk.ObjectTypeDatabaseRole, selectPrivilege.GrantedTo) @@ -521,11 +522,11 @@ func TestInt_GrantOwnership(t *testing.T) { // Expecting two grants because database role has usage on database by default require.Equal(t, 2, len(returnedGrants)) - usagePrivilege, err := sdk.FindOne[sdk.Grant](returnedGrants, func(g sdk.Grant) bool { return g.Privilege == sdk.AccountObjectPrivilegeUsage.String() }) + usagePrivilege, err := collections.FindOne[sdk.Grant](returnedGrants, func(g sdk.Grant) bool { return g.Privilege == sdk.AccountObjectPrivilegeUsage.String() }) require.NoError(t, err) assert.Equal(t, sdk.ObjectTypeDatabaseRole, usagePrivilege.GrantedTo) - ownership, err := sdk.FindOne[sdk.Grant](returnedGrants, func(g sdk.Grant) bool { return g.Privilege == sdk.SchemaObjectOwnership.String() }) + ownership, err := collections.FindOne[sdk.Grant](returnedGrants, func(g sdk.Grant) bool { return g.Privilege == sdk.SchemaObjectOwnership.String() }) require.NoError(t, err) assert.Equal(t, sdk.ObjectTypeTable, ownership.GrantedOn) assert.Equal(t, sdk.ObjectTypeDatabaseRole, ownership.GrantedTo) diff --git a/pkg/sdk/testint/helpers.go b/pkg/sdk/testint/helpers.go index f3c41ad47f..9455139599 100644 --- a/pkg/sdk/testint/helpers.go +++ b/pkg/sdk/testint/helpers.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/require" ) @@ -101,7 +102,7 @@ func useWarehouse(t *testing.T, client *sdk.Client, warehouseID sdk.AccountObjec func createDatabase(t *testing.T, client *sdk.Client) (*sdk.Database, func()) { t.Helper() - return createDatabaseWithOptions(t, client, randomAccountObjectIdentifier(t), &sdk.CreateDatabaseOptions{}) + return createDatabaseWithOptions(t, client, sdk.RandomAccountObjectIdentifier(), &sdk.CreateDatabaseOptions{}) } func createDatabaseWithIdentifier(t *testing.T, client *sdk.Client, id sdk.AccountObjectIdentifier) (*sdk.Database, func()) { @@ -124,7 +125,7 @@ func createDatabaseWithOptions(t *testing.T, client *sdk.Client, id sdk.AccountO func createSchema(t *testing.T, client *sdk.Client, database *sdk.Database) (*sdk.Schema, func()) { t.Helper() - return createSchemaWithIdentifier(t, client, database, randomStringRange(t, 8, 28)) + return createSchemaWithIdentifier(t, client, database, random.StringRange(8, 28)) } func createSchemaWithIdentifier(t *testing.T, client *sdk.Client, database *sdk.Database, name string) (*sdk.Schema, func()) { @@ -151,7 +152,7 @@ func createWarehouse(t *testing.T, client *sdk.Client) (*sdk.Warehouse, func()) func createWarehouseWithOptions(t *testing.T, client *sdk.Client, opts *sdk.CreateWarehouseOptions) (*sdk.Warehouse, func()) { t.Helper() - name := randomStringRange(t, 8, 28) + name := random.StringRange(8, 28) id := sdk.NewAccountObjectIdentifier(name) ctx := context.Background() err := client.Warehouses.Create(ctx, id, opts) @@ -166,7 +167,7 @@ func createWarehouseWithOptions(t *testing.T, client *sdk.Client, opts *sdk.Crea func createUser(t *testing.T, client *sdk.Client) (*sdk.User, func()) { t.Helper() - name := randomStringRange(t, 8, 28) + name := random.StringRange(8, 28) id := sdk.NewAccountObjectIdentifier(name) return createUserWithOptions(t, client, id, &sdk.CreateUserOptions{}) } @@ -192,7 +193,7 @@ func createUserWithOptions(t *testing.T, client *sdk.Client, id sdk.AccountObjec func createTable(t *testing.T, client *sdk.Client, database *sdk.Database, schema *sdk.Schema) (*sdk.Table, func()) { t.Helper() - name := randomStringRange(t, 8, 28) + name := random.StringRange(8, 28) ctx := context.Background() _, err := client.ExecForTests(ctx, fmt.Sprintf("CREATE TABLE \"%s\".\"%s\".\"%s\" (id NUMBER)", database.Name, schema.Name, name)) require.NoError(t, err) @@ -229,12 +230,12 @@ func createDynamicTableWithOptions(t *testing.T, client *sdk.Client, warehouse * if table == nil { table, tableCleanup = createTable(t, client, database, schema) } - name := sdk.NewSchemaObjectIdentifier(schema.DatabaseName, schema.Name, randomString(t)) + name := sdk.NewSchemaObjectIdentifier(schema.DatabaseName, schema.Name, random.String()) targetLag := sdk.TargetLag{ Lagtime: sdk.String("2 minutes"), } query := "select id from " + table.ID().FullyQualifiedName() - comment := randomComment(t) + comment := random.Comment() ctx := context.Background() err := client.DynamicTables.Create(ctx, sdk.NewCreateDynamicTableRequest(name, warehouse.ID(), targetLag, query).WithOrReplace(true).WithComment(&comment)) require.NoError(t, err) @@ -265,7 +266,7 @@ func createTag(t *testing.T, client *sdk.Client, database *sdk.Database, schema func createTagWithOptions(t *testing.T, client *sdk.Client, database *sdk.Database, schema *sdk.Schema, _ *sdk.CreateTagOptions) (*sdk.Tag, func()) { t.Helper() - name := randomStringRange(t, 8, 28) + name := random.StringRange(8, 28) ctx := context.Background() _, err := client.ExecForTests(ctx, fmt.Sprintf("CREATE TAG \"%s\".\"%s\".\"%s\"", database.Name, schema.Name, name)) require.NoError(t, err) @@ -375,7 +376,7 @@ func createPasswordPolicyWithOptions(t *testing.T, client *sdk.Client, database if schema == nil { schema, schemaCleanup = createSchema(t, client, database) } - name := randomUUID(t) + name := random.UUID() id := sdk.NewSchemaObjectIdentifier(schema.DatabaseName, schema.Name, name) ctx := context.Background() err := client.PasswordPolicies.Create(ctx, id, options) @@ -416,7 +417,7 @@ func createNetworkPolicy(t *testing.T, client *sdk.Client, req *sdk.CreateNetwor func createSessionPolicy(t *testing.T, client *sdk.Client, database *sdk.Database, schema *sdk.Schema) (*sdk.SessionPolicy, func()) { t.Helper() - id := sdk.NewSchemaObjectIdentifier(database.Name, schema.Name, randomStringN(t, 12)) + id := sdk.NewSchemaObjectIdentifier(database.Name, schema.Name, random.StringN(12)) return createSessionPolicyWithOptions(t, client, id, sdk.NewCreateSessionPolicyRequest(id)) } @@ -458,7 +459,7 @@ func createResourceMonitor(t *testing.T, client *sdk.Client) (*sdk.ResourceMonit func createResourceMonitorWithOptions(t *testing.T, client *sdk.Client, opts *sdk.CreateResourceMonitorOptions) (*sdk.ResourceMonitor, func()) { t.Helper() - id := randomAccountObjectIdentifier(t) + id := sdk.RandomAccountObjectIdentifier() ctx := context.Background() err := client.ResourceMonitors.Create(ctx, id, opts) require.NoError(t, err) @@ -474,14 +475,14 @@ func createMaskingPolicy(t *testing.T, client *sdk.Client, database *sdk.Databas t.Helper() signature := []sdk.TableColumnSignature{ { - Name: randomString(t), + Name: random.String(), Type: sdk.DataTypeVARCHAR, }, } - n := randomIntRange(t, 0, 5) + n := random.IntRange(0, 5) for i := 0; i < n; i++ { signature = append(signature, sdk.TableColumnSignature{ - Name: randomString(t), + Name: random.String(), Type: sdk.DataTypeVARCHAR, }) } @@ -499,7 +500,7 @@ func createMaskingPolicyWithOptions(t *testing.T, client *sdk.Client, database * if schema == nil { schema, schemaCleanup = createSchema(t, client, database) } - name := randomString(t) + name := random.String() id := sdk.NewSchemaObjectIdentifier(schema.DatabaseName, schema.Name, name) ctx := context.Background() err := client.MaskingPolicies.Create(ctx, id, signature, returns, expression, options) @@ -551,7 +552,7 @@ func createAlertWithOptions(t *testing.T, client *sdk.Client, database *sdk.Data warehouse, warehouseCleanup = createWarehouse(t, client) } - name := randomString(t) + name := random.String() id := sdk.NewSchemaObjectIdentifier(schema.DatabaseName, schema.Name, name) ctx := context.Background() err := client.Alerts.Create(ctx, id, warehouse.ID(), schedule, condition, action, opts) @@ -585,7 +586,7 @@ func createAlertWithOptions(t *testing.T, client *sdk.Client, database *sdk.Data func createRole(t *testing.T, client *sdk.Client) (*sdk.Role, func()) { t.Helper() - return createRoleWithRequest(t, client, sdk.NewCreateRoleRequest(randomAccountObjectIdentifier(t))) + return createRoleWithRequest(t, client, sdk.NewCreateRoleRequest(sdk.RandomAccountObjectIdentifier())) } func createRoleWithRequest(t *testing.T, client *sdk.Client, req *sdk.CreateRoleRequest) (*sdk.Role, func()) { @@ -604,7 +605,7 @@ func createRoleWithRequest(t *testing.T, client *sdk.Client, req *sdk.CreateRole func createDatabaseRole(t *testing.T, client *sdk.Client, database *sdk.Database) (*sdk.DatabaseRole, func()) { t.Helper() - name := randomString(t) + name := random.String() id := sdk.NewDatabaseObjectIdentifier(database.Name, name) ctx := context.Background() @@ -638,7 +639,7 @@ func createFailoverGroup(t *testing.T, client *sdk.Client) (*sdk.FailoverGroup, func createFailoverGroupWithOptions(t *testing.T, client *sdk.Client, objectTypes []sdk.PluralObjectType, allowedAccounts []sdk.AccountIdentifier, opts *sdk.CreateFailoverGroupOptions) (*sdk.FailoverGroup, func()) { t.Helper() - id := randomAccountObjectIdentifier(t) + id := sdk.RandomAccountObjectIdentifier() ctx := context.Background() err := client.FailoverGroups.Create(ctx, id, objectTypes, allowedAccounts, opts) require.NoError(t, err) @@ -657,7 +658,7 @@ func createShare(t *testing.T, client *sdk.Client) (*sdk.Share, func()) { func createShareWithOptions(t *testing.T, client *sdk.Client, opts *sdk.CreateShareOptions) (*sdk.Share, func()) { t.Helper() - id := randomAccountObjectIdentifier(t) + id := sdk.RandomAccountObjectIdentifier() ctx := context.Background() err := client.Shares.Create(ctx, id, opts) require.NoError(t, err) @@ -678,7 +679,7 @@ func createFileFormat(t *testing.T, client *sdk.Client, schema sdk.DatabaseObjec func createFileFormatWithOptions(t *testing.T, client *sdk.Client, schema sdk.DatabaseObjectIdentifier, opts *sdk.CreateFileFormatOptions) (*sdk.FileFormat, func()) { t.Helper() - id := sdk.NewSchemaObjectIdentifier(schema.DatabaseName(), schema.Name(), randomString(t)) + id := sdk.NewSchemaObjectIdentifier(schema.DatabaseName(), schema.Name(), random.String()) ctx := context.Background() err := client.FileFormats.Create(ctx, id, opts) require.NoError(t, err) diff --git a/pkg/sdk/testint/masking_policy_integration_test.go b/pkg/sdk/testint/masking_policy_integration_test.go index 1faba366a8..ffd33cdb6e 100644 --- a/pkg/sdk/testint/masking_policy_integration_test.go +++ b/pkg/sdk/testint/masking_policy_integration_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -100,7 +101,7 @@ func TestInt_MaskingPolicyCreate(t *testing.T) { t.Cleanup(schemaCleanup) t.Run("test complete case", func(t *testing.T) { - name := randomString(t) + name := random.String() id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) signature := []sdk.TableColumnSignature{ { @@ -113,8 +114,8 @@ func TestInt_MaskingPolicyCreate(t *testing.T) { }, } expression := "REPLACE('X', 1, 2)" - comment := randomComment(t) - exemptOtherPolicies := randomBool(t) + comment := random.Comment() + exemptOtherPolicies := random.Bool() err := client.MaskingPolicies.Create(ctx, id, signature, sdk.DataTypeVARCHAR, expression, &sdk.CreateMaskingPolicyOptions{ OrReplace: sdk.Bool(true), IfNotExists: sdk.Bool(false), @@ -145,7 +146,7 @@ func TestInt_MaskingPolicyCreate(t *testing.T) { }) t.Run("test if_not_exists", func(t *testing.T) { - name := randomString(t) + name := random.String() id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) signature := []sdk.TableColumnSignature{ { @@ -158,7 +159,7 @@ func TestInt_MaskingPolicyCreate(t *testing.T) { }, } expression := "REPLACE('X', 1, 2)" - comment := randomComment(t) + comment := random.Comment() err := client.MaskingPolicies.Create(ctx, id, signature, sdk.DataTypeVARCHAR, expression, &sdk.CreateMaskingPolicyOptions{ OrReplace: sdk.Bool(false), IfNotExists: sdk.Bool(true), @@ -189,7 +190,7 @@ func TestInt_MaskingPolicyCreate(t *testing.T) { }) t.Run("test no options", func(t *testing.T) { - name := randomString(t) + name := random.String() id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) signature := []sdk.TableColumnSignature{ { @@ -223,7 +224,7 @@ func TestInt_MaskingPolicyCreate(t *testing.T) { }) t.Run("test multiline expression", func(t *testing.T) { - name := randomString(t) + name := random.String() id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) signature := []sdk.TableColumnSignature{ { @@ -291,7 +292,7 @@ func TestInt_MaskingPolicyAlter(t *testing.T) { t.Run("when setting and unsetting a value", func(t *testing.T) { maskingPolicy, maskingPolicyCleanup := createMaskingPolicy(t, client, databaseTest, schemaTest) t.Cleanup(maskingPolicyCleanup) - comment := randomComment(t) + comment := random.Comment() alterOptions := &sdk.AlterMaskingPolicyOptions{ Set: &sdk.MaskingPolicySet{ Comment: sdk.String(comment), @@ -337,7 +338,7 @@ func TestInt_MaskingPolicyAlter(t *testing.T) { maskingPolicy, maskingPolicyCleanup := createMaskingPolicy(t, client, databaseTest, schemaTest) oldID := maskingPolicy.ID() t.Cleanup(maskingPolicyCleanup) - newName := randomString(t) + newName := random.String() newID := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, newName) alterOptions := &sdk.AlterMaskingPolicyOptions{ NewName: newID, diff --git a/pkg/sdk/testint/network_policies_gen_integration_test.go b/pkg/sdk/testint/network_policies_gen_integration_test.go index fdd2f1602b..a0d8d219fa 100644 --- a/pkg/sdk/testint/network_policies_gen_integration_test.go +++ b/pkg/sdk/testint/network_policies_gen_integration_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/collections" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -17,7 +18,7 @@ func TestInt_NetworkPolicies(t *testing.T) { blockedIP := sdk.NewIPRequest("125.0.0.1") blockedIP2 := sdk.NewIPRequest("124.0.0.1") defaultCreateRequest := func() *sdk.CreateNetworkPolicyRequest { - id := randomAccountObjectIdentifier(t) + id := sdk.RandomAccountObjectIdentifier() comment := "some_comment" return sdk.NewCreateNetworkPolicyRequest(id). WithOrReplace(sdk.Bool(true)). @@ -27,7 +28,7 @@ func TestInt_NetworkPolicies(t *testing.T) { } findNetworkPolicy := func(nps []sdk.NetworkPolicy, name string) (*sdk.NetworkPolicy, error) { - return sdk.FindOne[sdk.NetworkPolicy](nps, func(t sdk.NetworkPolicy) bool { + return collections.FindOne[sdk.NetworkPolicy](nps, func(t sdk.NetworkPolicy) bool { return t.Name == name }) } @@ -132,7 +133,7 @@ func TestInt_NetworkPolicies(t *testing.T) { } }) - newID := randomAccountObjectIdentifier(t) + newID := sdk.RandomAccountObjectIdentifier() err = client.NetworkPolicies.Alter(ctx, sdk.NewAlterNetworkPolicyRequest(req.GetName()).WithRenameTo(&newID)) require.NoError(t, err) altered = true diff --git a/pkg/sdk/testint/password_policy_integration_test.go b/pkg/sdk/testint/password_policy_integration_test.go index 466687a829..af642de14a 100644 --- a/pkg/sdk/testint/password_policy_integration_test.go +++ b/pkg/sdk/testint/password_policy_integration_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -91,7 +92,7 @@ func TestInt_PasswordPolicyCreate(t *testing.T) { schemaTest, schemaCleanup := createSchema(t, client, databaseTest) t.Cleanup(schemaCleanup) t.Run("test complete", func(t *testing.T) { - name := randomUUID(t) + name := random.UUID() id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) err := client.PasswordPolicies.Create(ctx, id, &sdk.CreatePasswordPolicyOptions{ OrReplace: sdk.Bool(true), @@ -125,7 +126,7 @@ func TestInt_PasswordPolicyCreate(t *testing.T) { }) t.Run("test if_not_exists", func(t *testing.T) { - name := randomUUID(t) + name := random.UUID() id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) err := client.PasswordPolicies.Create(ctx, id, &sdk.CreatePasswordPolicyOptions{ OrReplace: sdk.Bool(false), @@ -148,7 +149,7 @@ func TestInt_PasswordPolicyCreate(t *testing.T) { }) t.Run("test no options", func(t *testing.T) { - name := randomUUID(t) + name := random.UUID() id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) err := client.PasswordPolicies.Create(ctx, id, nil) require.NoError(t, err) @@ -229,7 +230,7 @@ func TestInt_PasswordPolicyAlter(t *testing.T) { passwordPolicy, passwordPolicyCleanup := createPasswordPolicy(t, client, databaseTest, schemaTest) oldID := passwordPolicy.ID() t.Cleanup(passwordPolicyCleanup) - newName := randomUUID(t) + newName := random.UUID() newID := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, newName) alterOptions := &sdk.AlterPasswordPolicyOptions{ NewName: newID, diff --git a/pkg/sdk/testint/pipes_integration_test.go b/pkg/sdk/testint/pipes_integration_test.go index cecd729e9b..648100a68d 100644 --- a/pkg/sdk/testint/pipes_integration_test.go +++ b/pkg/sdk/testint/pipes_integration_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -27,14 +28,14 @@ func TestInt_IncorrectCreatePipeBehaviour(t *testing.T) { table, tableCleanup := createTable(t, itc.client, database, schema) t.Cleanup(tableCleanup) - stageName := randomAlphanumericN(t, 20) + stageName := random.AlphanumericN(20) stage, stageCleanup := createStage(t, itc.client, database, schema, stageName) t.Cleanup(stageCleanup) t.Run("if we have special characters in db or schema name, create pipe returns error in copy <> from <> section", func(t *testing.T) { err := itc.client.Pipes.Create( itc.ctx, - sdk.NewSchemaObjectIdentifier(database.Name, schema.Name, randomAlphanumericN(t, 20)), + sdk.NewSchemaObjectIdentifier(database.Name, schema.Name, random.AlphanumericN(20)), createPipeCopyStatement(t, table, stage), &sdk.CreatePipeOptions{}, ) @@ -59,7 +60,7 @@ func TestInt_IncorrectCreatePipeBehaviour(t *testing.T) { err := itc.client.Pipes.Create( itc.ctx, - sdk.NewSchemaObjectIdentifier(database.Name, schema.Name, randomAlphanumericN(t, 20)), + sdk.NewSchemaObjectIdentifier(database.Name, schema.Name, random.AlphanumericN(20)), createCopyStatementWithoutQualifiersForStage(t, table, stage), &sdk.CreatePipeOptions{}, ) @@ -69,7 +70,7 @@ func TestInt_IncorrectCreatePipeBehaviour(t *testing.T) { } func TestInt_PipesShowAndDescribe(t *testing.T) { - schemaIdentifier := alphanumericDatabaseObjectIdentifier(t) + schemaIdentifier := sdk.AlphanumericDatabaseObjectIdentifier() database, databaseCleanup := createDatabaseWithIdentifier(t, itc.client, sdk.NewAccountObjectIdentifier(schemaIdentifier.DatabaseName())) t.Cleanup(databaseCleanup) @@ -82,16 +83,16 @@ func TestInt_PipesShowAndDescribe(t *testing.T) { table2, table2Cleanup := createTable(t, itc.client, database, schema) t.Cleanup(table2Cleanup) - stageName := randomAlphanumericN(t, 20) + stageName := random.AlphanumericN(20) stage, stageCleanup := createStage(t, itc.client, database, schema, stageName) t.Cleanup(stageCleanup) - pipe1Name := randomAlphanumericN(t, 20) + pipe1Name := random.AlphanumericN(20) pipe1CopyStatement := createPipeCopyStatement(t, table1, stage) pipe1, pipe1Cleanup := createPipe(t, itc.client, database, schema, pipe1Name, pipe1CopyStatement) t.Cleanup(pipe1Cleanup) - pipe2Name := randomAlphanumericN(t, 20) + pipe2Name := random.AlphanumericN(20) pipe2CopyStatement := createPipeCopyStatement(t, table2, stage) pipe2, pipe2Cleanup := createPipe(t, itc.client, database, schema, pipe2Name, pipe2CopyStatement) t.Cleanup(pipe2Cleanup) @@ -160,7 +161,7 @@ func TestInt_PipesShowAndDescribe(t *testing.T) { } func TestInt_PipeCreate(t *testing.T) { - schemaIdentifier := alphanumericDatabaseObjectIdentifier(t) + schemaIdentifier := sdk.AlphanumericDatabaseObjectIdentifier() database, databaseCleanup := createDatabaseWithIdentifier(t, itc.client, sdk.NewAccountObjectIdentifier(schemaIdentifier.DatabaseName())) t.Cleanup(databaseCleanup) @@ -170,7 +171,7 @@ func TestInt_PipeCreate(t *testing.T) { table, tableCleanup := createTable(t, itc.client, database, schema) t.Cleanup(tableCleanup) - stageName := randomAlphanumericN(t, 20) + stageName := random.AlphanumericN(20) stage, stageCleanup := createStage(t, itc.client, database, schema, stageName) t.Cleanup(stageCleanup) @@ -195,9 +196,9 @@ func TestInt_PipeCreate(t *testing.T) { // TODO: test error integration, aws sns topic and integration when we have them in project t.Run("test complete case", func(t *testing.T) { - name := randomString(t) + name := random.String() id := sdk.NewSchemaObjectIdentifier(database.Name, schema.Name, name) - comment := randomComment(t) + comment := random.Comment() err := itc.client.Pipes.Create(itc.ctx, id, copyStatement, &sdk.CreatePipeOptions{ OrReplace: sdk.Bool(false), @@ -214,7 +215,7 @@ func TestInt_PipeCreate(t *testing.T) { }) t.Run("test if not exists and or replace are incompatible", func(t *testing.T) { - name := randomString(t) + name := random.String() id := sdk.NewSchemaObjectIdentifier(database.Name, schema.Name, name) err := itc.client.Pipes.Create(itc.ctx, id, copyStatement, &sdk.CreatePipeOptions{ @@ -225,7 +226,7 @@ func TestInt_PipeCreate(t *testing.T) { }) t.Run("test no options", func(t *testing.T) { - name := randomString(t) + name := random.String() id := sdk.NewSchemaObjectIdentifier(database.Name, schema.Name, name) err := itc.client.Pipes.Create(itc.ctx, id, copyStatement, nil) @@ -239,7 +240,7 @@ func TestInt_PipeCreate(t *testing.T) { } func TestInt_PipeDrop(t *testing.T) { - schemaIdentifier := alphanumericDatabaseObjectIdentifier(t) + schemaIdentifier := sdk.AlphanumericDatabaseObjectIdentifier() database, databaseCleanup := createDatabaseWithIdentifier(t, itc.client, sdk.NewAccountObjectIdentifier(schemaIdentifier.DatabaseName())) t.Cleanup(databaseCleanup) @@ -249,12 +250,12 @@ func TestInt_PipeDrop(t *testing.T) { table, tableCleanup := createTable(t, itc.client, database, schema) t.Cleanup(tableCleanup) - stageName := randomAlphanumericN(t, 20) + stageName := random.AlphanumericN(20) stage, stageCleanup := createStage(t, itc.client, database, schema, stageName) t.Cleanup(stageCleanup) t.Run("pipe exists", func(t *testing.T) { - pipeName := randomAlphanumericN(t, 20) + pipeName := random.AlphanumericN(20) pipeCopyStatement := createPipeCopyStatement(t, table, stage) pipe, _ := createPipe(t, itc.client, database, schema, pipeName, pipeCopyStatement) @@ -274,7 +275,7 @@ func TestInt_PipeDrop(t *testing.T) { } func TestInt_PipeAlter(t *testing.T) { - schemaIdentifier := alphanumericDatabaseObjectIdentifier(t) + schemaIdentifier := sdk.AlphanumericDatabaseObjectIdentifier() database, databaseCleanup := createDatabaseWithIdentifier(t, itc.client, sdk.NewAccountObjectIdentifier(schemaIdentifier.DatabaseName())) t.Cleanup(databaseCleanup) @@ -284,7 +285,7 @@ func TestInt_PipeAlter(t *testing.T) { table, tableCleanup := createTable(t, itc.client, database, schema) t.Cleanup(tableCleanup) - stageName := randomAlphanumericN(t, 20) + stageName := random.AlphanumericN(20) stage, stageCleanup := createStage(t, itc.client, database, schema, stageName) t.Cleanup(stageCleanup) @@ -292,7 +293,7 @@ func TestInt_PipeAlter(t *testing.T) { // TODO: test error integration when we have them in project t.Run("set value and unset value", func(t *testing.T) { - pipeName := randomAlphanumericN(t, 20) + pipeName := random.AlphanumericN(20) pipe, pipeCleanup := createPipe(t, itc.client, database, schema, pipeName, pipeCopyStatement) t.Cleanup(pipeCleanup) @@ -331,7 +332,7 @@ func TestInt_PipeAlter(t *testing.T) { tag, tagCleanup := createTag(t, itc.client, database, schema) t.Cleanup(tagCleanup) - pipeName := randomAlphanumericN(t, 20) + pipeName := random.AlphanumericN(20) pipe, pipeCleanup := createPipe(t, itc.client, database, schema, pipeName, pipeCopyStatement) t.Cleanup(pipeCleanup) @@ -371,7 +372,7 @@ func TestInt_PipeAlter(t *testing.T) { }) t.Run("refresh with all", func(t *testing.T) { - pipeName := randomAlphanumericN(t, 20) + pipeName := random.AlphanumericN(20) pipe, pipeCleanup := createPipe(t, itc.client, database, schema, pipeName, pipeCopyStatement) t.Cleanup(pipeCleanup) diff --git a/pkg/sdk/testint/random.go b/pkg/sdk/testint/random.go deleted file mode 100644 index 52a9f08862..0000000000 --- a/pkg/sdk/testint/random.go +++ /dev/null @@ -1,82 +0,0 @@ -package testint - -import ( - "testing" - - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" - "github.com/brianvoe/gofakeit/v6" - "github.com/hashicorp/go-uuid" - "github.com/stretchr/testify/require" -) - -// All functions in this file exist also in SDK (they are used also there). -// They were copied to allow easier extraction of all integration tests to separate package. -// This will be dealt with in subsequent PRs. - -func randomUUID(t *testing.T) string { - t.Helper() - v, err := uuid.GenerateUUID() - require.NoError(t, err) - return v -} - -func randomComment(t *testing.T) string { - t.Helper() - return gofakeit.Sentence(10) -} - -func randomBool(t *testing.T) bool { - t.Helper() - return gofakeit.Bool() -} - -func randomString(t *testing.T) string { - t.Helper() - return gofakeit.Password(true, true, true, true, false, 28) -} - -func randomStringN(t *testing.T, num int) string { - t.Helper() - return gofakeit.Password(true, true, true, true, false, num) -} - -func randomAlphanumericN(t *testing.T, num int) string { - t.Helper() - return gofakeit.Password(true, true, true, false, false, num) -} - -func randomStringRange(t *testing.T, min, max int) string { - t.Helper() - if min > max { - t.Errorf("min %d is greater than max %d", min, max) - } - return gofakeit.Password(true, true, true, true, false, randomIntRange(t, min, max)) -} - -func randomIntRange(t *testing.T, min, max int) int { - t.Helper() - if min > max { - t.Errorf("min %d is greater than max %d", min, max) - } - return gofakeit.IntRange(min, max) -} - -func randomSchemaObjectIdentifier(t *testing.T) sdk.SchemaObjectIdentifier { - t.Helper() - return sdk.NewSchemaObjectIdentifier(randomStringN(t, 12), randomStringN(t, 12), randomStringN(t, 12)) -} - -func randomDatabaseObjectIdentifier(t *testing.T) sdk.DatabaseObjectIdentifier { - t.Helper() - return sdk.NewDatabaseObjectIdentifier(randomStringN(t, 12), randomStringN(t, 12)) -} - -func alphanumericDatabaseObjectIdentifier(t *testing.T) sdk.DatabaseObjectIdentifier { - t.Helper() - return sdk.NewDatabaseObjectIdentifier(randomAlphanumericN(t, 12), randomAlphanumericN(t, 12)) -} - -func randomAccountObjectIdentifier(t *testing.T) sdk.AccountObjectIdentifier { - t.Helper() - return sdk.NewAccountObjectIdentifier(randomStringN(t, 12)) -} diff --git a/pkg/sdk/testint/resource_monitors_integration_test.go b/pkg/sdk/testint/resource_monitors_integration_test.go index b28584db01..86e19aa5fc 100644 --- a/pkg/sdk/testint/resource_monitors_integration_test.go +++ b/pkg/sdk/testint/resource_monitors_integration_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -44,7 +45,7 @@ func TestInt_ResourceMonitorCreate(t *testing.T) { ctx := testContext(t) t.Run("test complete case", func(t *testing.T) { - name := randomString(t) + name := random.String() id := sdk.NewAccountObjectIdentifier(name) frequency, err := sdk.FrequencyFromString("Monthly") require.NoError(t, err) @@ -112,7 +113,7 @@ func TestInt_ResourceMonitorCreate(t *testing.T) { }) t.Run("test no options", func(t *testing.T) { - name := randomString(t) + name := random.String() id := sdk.NewAccountObjectIdentifier(name) err := client.ResourceMonitors.Create(ctx, id, nil) diff --git a/pkg/sdk/testint/roles_integration_test.go b/pkg/sdk/testint/roles_integration_test.go index 5d20460685..9d644a4d41 100644 --- a/pkg/sdk/testint/roles_integration_test.go +++ b/pkg/sdk/testint/roles_integration_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -19,7 +20,7 @@ func TestInt_Roles(t *testing.T) { tag2, _ := createTag(t, client, database, schema) t.Run("create no options", func(t *testing.T) { - roleID := randomAccountObjectIdentifier(t) + roleID := sdk.RandomAccountObjectIdentifier() err := client.Roles.Create(ctx, sdk.NewCreateRoleRequest(roleID)) require.NoError(t, err) t.Cleanup(func() { @@ -34,7 +35,7 @@ func TestInt_Roles(t *testing.T) { }) t.Run("create if not exists", func(t *testing.T) { - roleID := randomAccountObjectIdentifier(t) + roleID := sdk.RandomAccountObjectIdentifier() err := client.Roles.Create(ctx, sdk.NewCreateRoleRequest(roleID).WithIfNotExists(true)) require.NoError(t, err) t.Cleanup(func() { @@ -48,8 +49,8 @@ func TestInt_Roles(t *testing.T) { }) t.Run("create complete", func(t *testing.T) { - roleID := randomAccountObjectIdentifier(t) - comment := randomComment(t) + roleID := sdk.RandomAccountObjectIdentifier() + comment := random.Comment() createReq := sdk.NewCreateRoleRequest(roleID). WithOrReplace(true). WithTag([]sdk.TagAssociation{ @@ -87,7 +88,7 @@ func TestInt_Roles(t *testing.T) { t.Run("alter rename to", func(t *testing.T) { role, _ := createRole(t, client) - newName := randomAccountObjectIdentifier(t) + newName := sdk.RandomAccountObjectIdentifier() t.Cleanup(func() { err := client.Roles.Drop(ctx, sdk.NewDropRoleRequest(newName)) if err != nil { @@ -127,7 +128,7 @@ func TestInt_Roles(t *testing.T) { t.Run("alter unset tags", func(t *testing.T) { tagValue := "tag-value" - id := randomAccountObjectIdentifier(t) + id := sdk.RandomAccountObjectIdentifier() role, cleanup := createRoleWithRequest(t, client, sdk.NewCreateRoleRequest(id). WithTag([]sdk.TagAssociation{ { @@ -152,7 +153,7 @@ func TestInt_Roles(t *testing.T) { role, cleanupRole := createRole(t, client) t.Cleanup(cleanupRole) - comment := randomComment(t) + comment := random.Comment() err := client.Roles.Alter(ctx, sdk.NewAlterRoleRequest(role.ID()).WithSetComment(comment)) require.NoError(t, err) @@ -162,8 +163,8 @@ func TestInt_Roles(t *testing.T) { }) t.Run("alter unset comment", func(t *testing.T) { - comment := randomComment(t) - id := randomAccountObjectIdentifier(t) + comment := random.Comment() + id := sdk.RandomAccountObjectIdentifier() role, cleanup := createRoleWithRequest(t, client, sdk.NewCreateRoleRequest(id).WithComment(comment)) t.Cleanup(cleanup) diff --git a/pkg/sdk/testint/schemas_integration_test.go b/pkg/sdk/testint/schemas_integration_test.go index dc3b20fcc7..f6a65c36e4 100644 --- a/pkg/sdk/testint/schemas_integration_test.go +++ b/pkg/sdk/testint/schemas_integration_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -51,13 +52,13 @@ func TestInt_SchemasCreate(t *testing.T) { t.Run("clone", func(t *testing.T) { comment := "some_comment" - schemaID := sdk.NewDatabaseObjectIdentifier(db.Name, randomAccountObjectIdentifier(t).Name()) + schemaID := sdk.NewDatabaseObjectIdentifier(db.Name, sdk.RandomAccountObjectIdentifier().Name()) err := client.Schemas.Create(ctx, schemaID, &sdk.CreateSchemaOptions{ Comment: sdk.String(comment), }) require.NoError(t, err) - clonedSchemaID := sdk.NewDatabaseObjectIdentifier(db.Name, randomAccountObjectIdentifier(t).Name()) + clonedSchemaID := sdk.NewDatabaseObjectIdentifier(db.Name, sdk.RandomAccountObjectIdentifier().Name()) err = client.Schemas.Create(ctx, clonedSchemaID, &sdk.CreateSchemaOptions{ Comment: sdk.String(comment), Clone: &sdk.Clone{ @@ -82,7 +83,7 @@ func TestInt_SchemasCreate(t *testing.T) { }) t.Run("with tags", func(t *testing.T) { - tagName := randomString(t) + tagName := random.String() tagID := sdk.NewAccountObjectIdentifier(tagName) _, err := client.ExecForTests(ctx, fmt.Sprintf(`CREATE TAG "%s"`, tagName)) require.NoError(t, err) @@ -91,8 +92,8 @@ func TestInt_SchemasCreate(t *testing.T) { require.NoError(t, err) }) - schemaID := sdk.NewDatabaseObjectIdentifier(db.Name, randomAccountObjectIdentifier(t).Name()) - tagValue := randomString(t) + schemaID := sdk.NewDatabaseObjectIdentifier(db.Name, sdk.RandomAccountObjectIdentifier().Name()) + tagValue := random.String() err = client.Schemas.Create(ctx, schemaID, &sdk.CreateSchemaOptions{ Tag: []sdk.TagAssociation{ { @@ -122,7 +123,7 @@ func TestInt_SchemasAlter(t *testing.T) { t.Run("rename to", func(t *testing.T) { schema, _ := createSchema(t, client, db) - newID := sdk.NewDatabaseObjectIdentifier(db.Name, randomString(t)) + newID := sdk.NewDatabaseObjectIdentifier(db.Name, random.String()) err := client.Schemas.Alter(ctx, schema.ID(), &sdk.AlterSchemaOptions{ NewName: newID, }) @@ -165,7 +166,7 @@ func TestInt_SchemasAlter(t *testing.T) { schema, cleanupSchema := createSchema(t, client, db) t.Cleanup(cleanupSchema) - comment := randomComment(t) + comment := random.Comment() err := client.Schemas.Alter(ctx, schema.ID(), &sdk.AlterSchemaOptions{ Set: &sdk.SchemaSet{ DataRetentionTimeInDays: sdk.Int(3), @@ -182,8 +183,8 @@ func TestInt_SchemasAlter(t *testing.T) { }) t.Run("unset", func(t *testing.T) { - schemaID := sdk.NewDatabaseObjectIdentifier(db.Name, randomString(t)) - comment := randomComment(t) + schemaID := sdk.NewDatabaseObjectIdentifier(db.Name, random.String()) + comment := random.Comment() err := client.Schemas.Create(ctx, schemaID, &sdk.CreateSchemaOptions{ Comment: sdk.String(comment), }) @@ -207,7 +208,7 @@ func TestInt_SchemasAlter(t *testing.T) { }) t.Run("set tags", func(t *testing.T) { - schemaID := sdk.NewDatabaseObjectIdentifier(db.Name, randomString(t)) + schemaID := sdk.NewDatabaseObjectIdentifier(db.Name, random.String()) err := client.Schemas.Create(ctx, schemaID, nil) require.NoError(t, err) t.Cleanup(func() { @@ -240,7 +241,7 @@ func TestInt_SchemasAlter(t *testing.T) { }) t.Run("unset tags", func(t *testing.T) { - tagName := randomString(t) + tagName := random.String() tagID := sdk.NewAccountObjectIdentifier(tagName) _, err := client.ExecForTests(ctx, fmt.Sprintf(`CREATE TAG "%s"`, tagName)) require.NoError(t, err) @@ -249,8 +250,8 @@ func TestInt_SchemasAlter(t *testing.T) { require.NoError(t, err) }) - schemaID := sdk.NewDatabaseObjectIdentifier(db.Name, randomAccountObjectIdentifier(t).Name()) - tagValue := randomString(t) + schemaID := sdk.NewDatabaseObjectIdentifier(db.Name, sdk.RandomAccountObjectIdentifier().Name()) + tagValue := random.String() err = client.Schemas.Create(ctx, schemaID, &sdk.CreateSchemaOptions{ Tag: []sdk.TagAssociation{ { diff --git a/pkg/sdk/testint/session_policies_gen_integration_test.go b/pkg/sdk/testint/session_policies_gen_integration_test.go index d054df48dd..706a5f6291 100644 --- a/pkg/sdk/testint/session_policies_gen_integration_test.go +++ b/pkg/sdk/testint/session_policies_gen_integration_test.go @@ -4,6 +4,8 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/collections" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -52,7 +54,7 @@ func TestInt_SessionPolicies(t *testing.T) { createSessionPolicy := func(t *testing.T) *sdk.SessionPolicy { t.Helper() - name := randomString(t) + name := random.String() id := sdk.NewSchemaObjectIdentifier(database.Name, schema.Name, name) err := client.SessionPolicies.Create(ctx, sdk.NewCreateSessionPolicyRequest(id)) @@ -66,9 +68,9 @@ func TestInt_SessionPolicies(t *testing.T) { } t.Run("create session_policy: complete case", func(t *testing.T) { - name := randomString(t) + name := random.String() id := sdk.NewSchemaObjectIdentifier(database.Name, schema.Name, name) - comment := randomComment(t) + comment := random.Comment() request := sdk.NewCreateSessionPolicyRequest(id). WithSessionIdleTimeoutMins(sdk.Int(5)). @@ -87,7 +89,7 @@ func TestInt_SessionPolicies(t *testing.T) { }) t.Run("create session_policy: no optionals", func(t *testing.T) { - name := randomString(t) + name := random.String() id := sdk.NewSchemaObjectIdentifier(database.Name, schema.Name, name) request := sdk.NewCreateSessionPolicyRequest(id) @@ -103,7 +105,7 @@ func TestInt_SessionPolicies(t *testing.T) { }) t.Run("drop session_policy: existing", func(t *testing.T) { - name := randomString(t) + name := random.String() id := sdk.NewSchemaObjectIdentifier(database.Name, schema.Name, name) err := client.SessionPolicies.Create(ctx, sdk.NewCreateSessionPolicyRequest(id)) @@ -113,7 +115,7 @@ func TestInt_SessionPolicies(t *testing.T) { require.NoError(t, err) _, err = client.SessionPolicies.ShowByID(ctx, id) - assert.ErrorIs(t, err, sdk.ErrObjectNotExistOrAuthorized) + assert.ErrorIs(t, err, collections.ErrObjectNotFound) }) t.Run("drop session_policy: non-existing", func(t *testing.T) { @@ -124,7 +126,7 @@ func TestInt_SessionPolicies(t *testing.T) { }) t.Run("alter session_policy: set value and unset value", func(t *testing.T) { - name := randomString(t) + name := random.String() id := sdk.NewSchemaObjectIdentifier(database.Name, schema.Name, name) err := client.SessionPolicies.Create(ctx, sdk.NewCreateSessionPolicyRequest(id)) @@ -154,7 +156,7 @@ func TestInt_SessionPolicies(t *testing.T) { tag, tagCleanup := createTag(t, client, database, schema) t.Cleanup(tagCleanup) - name := randomString(t) + name := random.String() id := sdk.NewSchemaObjectIdentifier(database.Name, schema.Name, name) err := client.SessionPolicies.Create(ctx, sdk.NewCreateSessionPolicyRequest(id)) @@ -191,13 +193,13 @@ func TestInt_SessionPolicies(t *testing.T) { }) t.Run("alter session_policy: rename", func(t *testing.T) { - name := randomString(t) + name := random.String() id := sdk.NewSchemaObjectIdentifier(database.Name, schema.Name, name) err := client.SessionPolicies.Create(ctx, sdk.NewCreateSessionPolicyRequest(id)) require.NoError(t, err) - newName := randomString(t) + newName := random.String() newId := sdk.NewSchemaObjectIdentifier(database.Name, schema.Name, newName) alterRequest := sdk.NewAlterSessionPolicyRequest(id).WithRenameTo(&newId) @@ -210,7 +212,7 @@ func TestInt_SessionPolicies(t *testing.T) { require.NoError(t, err) _, err = client.SessionPolicies.ShowByID(ctx, id) - assert.ErrorIs(t, err, sdk.ErrObjectNotExistOrAuthorized) + assert.ErrorIs(t, err, collections.ErrObjectNotFound) sessionPolicy, err := client.SessionPolicies.ShowByID(ctx, newId) require.NoError(t, err) diff --git a/pkg/sdk/testint/shares_integration_test.go b/pkg/sdk/testint/shares_integration_test.go index 5c31d8fa0c..de9b340859 100644 --- a/pkg/sdk/testint/shares_integration_test.go +++ b/pkg/sdk/testint/shares_integration_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -63,7 +64,7 @@ func TestInt_SharesCreate(t *testing.T) { ctx := testContext(t) t.Run("test complete", func(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := sdk.RandomAccountObjectIdentifier() err := client.Shares.Create(ctx, id, &sdk.CreateShareOptions{ OrReplace: sdk.Bool(true), Comment: sdk.String("test comment"), @@ -89,7 +90,7 @@ func TestInt_SharesCreate(t *testing.T) { }) t.Run("test no options", func(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := sdk.RandomAccountObjectIdentifier() err := client.Shares.Create(ctx, id, &sdk.CreateShareOptions{ OrReplace: sdk.Bool(true), Comment: sdk.String("test comment"), @@ -234,7 +235,7 @@ func TestInt_SharesAlter(t *testing.T) { require.NoError(t, err) }) - comment := randomComment(t) + comment := random.Comment() err = client.Shares.Alter(ctx, shareTest.ID(), &sdk.AlterShareOptions{ IfExists: sdk.Bool(true), Set: &sdk.ShareSet{ @@ -294,11 +295,11 @@ func TestInt_SharesAlter(t *testing.T) { tagAssociations := []sdk.TagAssociation{ { Name: tagTest.ID(), - Value: randomString(t), + Value: random.String(), }, { Name: tagTest2.ID(), - Value: randomString(t), + Value: random.String(), }, } err = client.Shares.Alter(ctx, shareTest.ID(), &sdk.AlterShareOptions{ diff --git a/pkg/sdk/testint/system_functions_integration_test.go b/pkg/sdk/testint/system_functions_integration_test.go index de1e7b7dd7..3bc7762c85 100644 --- a/pkg/sdk/testint/system_functions_integration_test.go +++ b/pkg/sdk/testint/system_functions_integration_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -24,7 +25,7 @@ func TestInt_GetTag(t *testing.T) { maskingPolicyTest, maskingPolicyCleanup := createMaskingPolicy(t, client, databaseTest, schemaTest) t.Cleanup(maskingPolicyCleanup) - tagValue := randomString(t) + tagValue := random.String() err := client.MaskingPolicies.Alter(ctx, maskingPolicyTest.ID(), &sdk.AlterMaskingPolicyOptions{ Set: &sdk.MaskingPolicySet{ Tag: []sdk.TagAssociation{ diff --git a/pkg/sdk/testint/tasks_gen_integration_test.go b/pkg/sdk/testint/tasks_gen_integration_test.go index 8e06191460..6d467a6d74 100644 --- a/pkg/sdk/testint/tasks_gen_integration_test.go +++ b/pkg/sdk/testint/tasks_gen_integration_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -115,7 +116,7 @@ func TestInt_Tasks(t *testing.T) { createTaskBasicRequest := func(t *testing.T) *sdk.CreateTaskRequest { t.Helper() - name := randomString(t) + name := random.String() id := sdk.NewSchemaObjectIdentifier(database.Name, schema.Name, name) return sdk.NewCreateTaskRequest(id, sql) @@ -182,7 +183,7 @@ func TestInt_Tasks(t *testing.T) { }) t.Run("create task: with after", func(t *testing.T) { - otherName := randomString(t) + otherName := random.String() otherId := sdk.NewSchemaObjectIdentifier(database.Name, schema.Name, otherName) request := sdk.NewCreateTaskRequest(otherId, sql).WithSchedule(sdk.String("10 MINUTE")) @@ -238,7 +239,7 @@ func TestInt_Tasks(t *testing.T) { t.Run("clone task: default", func(t *testing.T) { sourceTask := createTask(t) - name := randomString(t) + name := random.String() id := sdk.NewSchemaObjectIdentifier(database.Name, schema.Name, name) request := sdk.NewCloneTaskRequest(id, sourceTask.ID()) diff --git a/pkg/sdk/testint/users_integration_test.go b/pkg/sdk/testint/users_integration_test.go index 3eb6fb6fe9..98cf902805 100644 --- a/pkg/sdk/testint/users_integration_test.go +++ b/pkg/sdk/testint/users_integration_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -88,16 +89,16 @@ func TestInt_UserCreate(t *testing.T) { t.Cleanup(tagCleanup) t.Run("test complete case", func(t *testing.T) { - id := randomAccountObjectIdentifier(t) - tagValue := randomString(t) + id := sdk.RandomAccountObjectIdentifier() + tagValue := random.String() tags := []sdk.TagAssociation{ { Name: tag.ID(), Value: tagValue, }, } - password := randomString(t) - loginName := randomString(t) + password := random.String() + loginName := random.String() opts := &sdk.CreateUserOptions{ OrReplace: sdk.Bool(true), @@ -132,16 +133,16 @@ func TestInt_UserCreate(t *testing.T) { }) t.Run("test if not exists", func(t *testing.T) { - id := randomAccountObjectIdentifier(t) - tagValue := randomString(t) + id := sdk.RandomAccountObjectIdentifier() + tagValue := random.String() tags := []sdk.TagAssociation{ { Name: sdk.NewAccountObjectIdentifier(tag.Name), Value: tagValue, }, } - password := randomString(t) - loginName := randomString(t) + password := random.String() + loginName := random.String() opts := &sdk.CreateUserOptions{ IfNotExists: sdk.Bool(true), @@ -176,7 +177,7 @@ func TestInt_UserCreate(t *testing.T) { }) t.Run("test no options", func(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := sdk.RandomAccountObjectIdentifier() err := client.Users.Create(ctx, id, nil) require.NoError(t, err) diff --git a/pkg/sdk/testint/warehouses_integration_test.go b/pkg/sdk/testint/warehouses_integration_test.go index 5fb78b0104..abdcbda695 100644 --- a/pkg/sdk/testint/warehouses_integration_test.go +++ b/pkg/sdk/testint/warehouses_integration_test.go @@ -64,7 +64,7 @@ func TestInt_WarehouseCreate(t *testing.T) { t.Cleanup(tag2Cleanup) t.Run("test complete", func(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := sdk.RandomAccountObjectIdentifier() err := client.Warehouses.Create(ctx, id, &sdk.CreateWarehouseOptions{ OrReplace: sdk.Bool(true), WarehouseType: &sdk.WarehouseTypeStandard, @@ -129,7 +129,7 @@ func TestInt_WarehouseCreate(t *testing.T) { }) t.Run("test no options", func(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := sdk.RandomAccountObjectIdentifier() err := client.Warehouses.Create(ctx, id, nil) require.NoError(t, err) t.Cleanup(func() { @@ -197,7 +197,7 @@ func TestInt_WarehouseAlter(t *testing.T) { t.Cleanup(tagCleanup2) t.Run("terraform acc test", func(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := sdk.RandomAccountObjectIdentifier() opts := &sdk.CreateWarehouseOptions{ Comment: sdk.String("test comment"), WarehouseSize: &sdk.WarehouseSizeXSmall, @@ -228,7 +228,7 @@ func TestInt_WarehouseAlter(t *testing.T) { assert.Equal(t, sdk.WarehouseSizeXSmall, warehouse.Size) // rename - newID := randomAccountObjectIdentifier(t) + newID := sdk.RandomAccountObjectIdentifier() alterOptions := &sdk.AlterWarehouseOptions{ NewName: newID, } @@ -284,7 +284,7 @@ func TestInt_WarehouseAlter(t *testing.T) { oldID := warehouse.ID() t.Cleanup(warehouseCleanup) - newID := randomAccountObjectIdentifier(t) + newID := sdk.RandomAccountObjectIdentifier() alterOptions := &sdk.AlterWarehouseOptions{ NewName: newID, } diff --git a/pkg/sdk/users.go b/pkg/sdk/users.go index d2aa0da62e..c0d495735e 100644 --- a/pkg/sdk/users.go +++ b/pkg/sdk/users.go @@ -170,7 +170,7 @@ type UserTag struct { } func (opts *CreateUserOptions) validate() error { - if !validObjectidentifier(opts.name) { + if !ValidObjectIdentifier(opts.name) { return errors.New("invalid object identifier") } return nil @@ -273,7 +273,7 @@ type AlterUserOptions struct { } func (opts *AlterUserOptions) validate() error { - if !validObjectidentifier(opts.name) { + if !ValidObjectIdentifier(opts.name) { return errors.New("invalid object identifier") } if ok := exactlyOneValueSet( @@ -392,8 +392,8 @@ type DropUserOptions struct { } func (opts *DropUserOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } return nil } @@ -527,8 +527,8 @@ type describeUserOptions struct { } func (v *describeUserOptions) validate() error { - if !validObjectidentifier(v.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(v.name) { + return ErrInvalidObjectIdentifier } return nil } @@ -594,5 +594,5 @@ func (v *users) ShowByID(ctx context.Context, id AccountObjectIdentifier) (*User return &user, nil } } - return nil, errObjectNotExistOrAuthorized + return nil, ErrObjectNotExistOrAuthorized } diff --git a/pkg/sdk/users_test.go b/pkg/sdk/users_test.go index 8de24d2e4e..fed1a447e9 100644 --- a/pkg/sdk/users_test.go +++ b/pkg/sdk/users_test.go @@ -4,13 +4,13 @@ import ( "fmt" "testing" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestUserCreate(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := RandomAccountObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &CreateUserOptions{} @@ -27,8 +27,8 @@ func TestUserCreate(t *testing.T) { Value: "v1", }, } - password := randomString(t) - loginName := randomString(t) + password := random.String() + loginName := random.String() opts := &CreateUserOptions{ OrReplace: Bool(true), @@ -56,7 +56,7 @@ func TestUserCreate(t *testing.T) { } func TestUserAlter(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := RandomAccountObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &AlterUserOptions{} @@ -113,7 +113,7 @@ func TestUserAlter(t *testing.T) { assert.Equal(t, expected, actual) }) t.Run("with setting properties and parameters", func(t *testing.T) { - password := randomString(t) + password := random.String() objectProperties := UserObjectProperties{ Password: &password, DefaultSeconaryRoles: &SecondaryRoles{Roles: []SecondaryRole{{Value: "ALL"}}}, @@ -160,7 +160,7 @@ func TestUserAlter(t *testing.T) { }) t.Run("reset password", func(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := RandomAccountObjectIdentifier() opts := &AlterUserOptions{ name: id, ResetPassword: Bool(true), @@ -171,7 +171,7 @@ func TestUserAlter(t *testing.T) { assert.Equal(t, expected, actual) }) t.Run("abort all queries", func(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := RandomAccountObjectIdentifier() opts := &AlterUserOptions{ name: id, AbortAllQueries: Bool(true), @@ -182,7 +182,7 @@ func TestUserAlter(t *testing.T) { assert.Equal(t, expected, actual) }) t.Run("rename", func(t *testing.T) { - newID := NewAccountObjectIdentifier(randomString(t)) + newID := NewAccountObjectIdentifier(random.String()) opts := &AlterUserOptions{ name: id, NewName: newID, @@ -270,7 +270,7 @@ func TestUserAlter(t *testing.T) { } func TestUserDrop(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := RandomAccountObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &DropUserOptions{} @@ -292,7 +292,7 @@ func TestUserDrop(t *testing.T) { } func TestUserShow(t *testing.T) { - id := randomSchemaObjectIdentifier(t) + id := RandomSchemaObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &ShowUserOptions{} @@ -315,7 +315,7 @@ func TestUserShow(t *testing.T) { }) t.Run("with like and from", func(t *testing.T) { - fromPatern := randomString(t) + fromPatern := random.String() opts := &ShowUserOptions{ Like: &Like{ Pattern: String(id.Name()), @@ -343,8 +343,8 @@ func TestUserShow(t *testing.T) { }) t.Run("with starts with and from", func(t *testing.T) { - fromPattern := randomString(t) - startsWithPattern := randomString(t) + fromPattern := random.String() + startsWithPattern := random.String() opts := &ShowUserOptions{ StartsWith: &startsWithPattern, @@ -358,7 +358,7 @@ func TestUserShow(t *testing.T) { } func TestUserDescribe(t *testing.T) { - id := randomAccountObjectIdentifier(t) + id := RandomAccountObjectIdentifier() t.Run("empty options", func(t *testing.T) { opts := &describeUserOptions{} diff --git a/pkg/sdk/validations.go b/pkg/sdk/validations.go index e3aa3dce28..546f7ac15f 100644 --- a/pkg/sdk/validations.go +++ b/pkg/sdk/validations.go @@ -14,7 +14,7 @@ func IsValidWarehouseSize(v string) bool { return err == nil } -func validObjectidentifier(objectIdentifier ObjectIdentifier) bool { +func ValidObjectIdentifier(objectIdentifier ObjectIdentifier) bool { // https://docs.snowflake.com/en/sql-reference/identifiers-syntax#double-quoted-identifiers l := len(objectIdentifier.Name()) if l == 0 || l > 255 { @@ -85,7 +85,7 @@ func valueSet(value interface{}) bool { return false case reflect.Struct: if _, ok := reflectedValue.Interface().(ObjectIdentifier); ok { - return validObjectidentifier(reflectedValue.Interface().(ObjectIdentifier)) + return ValidObjectIdentifier(reflectedValue.Interface().(ObjectIdentifier)) } return reflectedValue.Interface() != nil } diff --git a/pkg/sdk/validations_test.go b/pkg/sdk/validations_test.go index 00646b998b..3725c52661 100644 --- a/pkg/sdk/validations_test.go +++ b/pkg/sdk/validations_test.go @@ -3,6 +3,7 @@ package sdk import ( "testing" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" "github.com/stretchr/testify/assert" ) @@ -30,24 +31,24 @@ func TestIsValidWarehouseSize(t *testing.T) { }) } -func TestValidObjectidentifier(t *testing.T) { +func TestValidObjectIdentifier(t *testing.T) { t.Run("with valid object identifier", func(t *testing.T) { - ok := validObjectidentifier(randomAccountObjectIdentifier(t)) + ok := ValidObjectIdentifier(RandomAccountObjectIdentifier()) assert.Equal(t, ok, true) }) t.Run("with invalid object identifier", func(t *testing.T) { - ok := validObjectidentifier(NewAccountObjectIdentifier("")) + ok := ValidObjectIdentifier(NewAccountObjectIdentifier("")) assert.Equal(t, ok, false) }) t.Run("over 255 characters", func(t *testing.T) { - ok := validObjectidentifier(NewAccountObjectIdentifier(randomStringN(t, 256))) + ok := ValidObjectIdentifier(NewAccountObjectIdentifier(random.StringN(256))) assert.Equal(t, ok, false) }) t.Run("with 255 charcters in each of db, schema and name", func(t *testing.T) { - ok := validObjectidentifier(NewSchemaObjectIdentifier(randomStringN(t, 255), randomStringN(t, 255), randomStringN(t, 255))) + ok := ValidObjectIdentifier(NewSchemaObjectIdentifier(random.StringN(255), random.StringN(255), random.StringN(255))) assert.Equal(t, ok, true) }) } diff --git a/pkg/sdk/warehouses.go b/pkg/sdk/warehouses.go index 8b3701381a..1e18be915e 100644 --- a/pkg/sdk/warehouses.go +++ b/pkg/sdk/warehouses.go @@ -119,8 +119,8 @@ type CreateWarehouseOptions struct { } func (opts *CreateWarehouseOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } if valueSet(opts.MinClusterCount) && valueSet(opts.MaxClusterCount) && !validateIntGreaterThanOrEqual(*opts.MaxClusterCount, *opts.MinClusterCount) { return fmt.Errorf("MinClusterCount must be less than or equal to MaxClusterCount") @@ -165,8 +165,8 @@ type AlterWarehouseOptions struct { } func (opts *AlterWarehouseOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } if ok := exactlyOneValueSet( opts.Suspend, @@ -298,8 +298,8 @@ type DropWarehouseOptions struct { } func (opts *DropWarehouseOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } return nil } @@ -476,7 +476,7 @@ func (c *warehouses) ShowByID(ctx context.Context, id AccountObjectIdentifier) ( return &warehouse, nil } } - return nil, errObjectNotExistOrAuthorized + return nil, ErrObjectNotExistOrAuthorized } // describeWarehouseOptions is based on https://docs.snowflake.com/en/sql-reference/sql/desc-warehouse. @@ -487,8 +487,8 @@ type describeWarehouseOptions struct { } func (opts *describeWarehouseOptions) validate() error { - if !validObjectidentifier(opts.name) { - return errInvalidObjectIdentifier + if !ValidObjectIdentifier(opts.name) { + return ErrInvalidObjectIdentifier } return nil }