Skip to content

Commit

Permalink
feat: Database SDK upgrade (#2814)
Browse files Browse the repository at this point in the history
## Summary of changes
- Added catalog and external volume test clients
- Added missing fields in the database operations + validations
- Moved LogLevel and TraceLevel to common types

## Test Plan
* [x] Added / modified unit tests
* [x] Added / modified integration tests

## References

* [CREATE
DATABASE](https://docs.snowflake.com/en/sql-reference/sql/create-database)
  • Loading branch information
sfc-gh-jcieslak authored May 22, 2024
1 parent ce0fbad commit 750fe37
Show file tree
Hide file tree
Showing 10 changed files with 1,419 additions and 434 deletions.
52 changes: 52 additions & 0 deletions pkg/acceptance/helpers/catalog_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package helpers

import (
"context"
"fmt"
"testing"

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

type CatalogIntegrationClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewCatalogIntegrationClient(context *TestClientContext, idsGenerator *IdsGenerator) *CatalogIntegrationClient {
return &CatalogIntegrationClient{
context: context,
ids: idsGenerator,
}
}

func (c *CatalogIntegrationClient) exec(sql string) error {
ctx := context.Background()
_, err := c.context.client.ExecForTests(ctx, sql)
return err
}

// TODO(SNOW-999142): Use SDK implementation for Catalog once it's available
func (c *CatalogIntegrationClient) Create(t *testing.T) (sdk.AccountObjectIdentifier, func()) {
t.Helper()
id := c.ids.RandomAccountObjectIdentifier()
err := c.exec(fmt.Sprintf(`
create catalog integration %s
catalog_source=object_store
table_format=iceberg
enabled=true
`, id.FullyQualifiedName()))
require.NoError(t, err)

return id, c.DropFunc(t, id)
}

func (c *CatalogIntegrationClient) DropFunc(t *testing.T, id sdk.AccountObjectIdentifier) func() {
t.Helper()

return func() {
err := c.exec(fmt.Sprintf(`drop catalog integration if exists %s`, id.FullyQualifiedName()))
require.NoError(t, err)
}
}
59 changes: 59 additions & 0 deletions pkg/acceptance/helpers/external_volume_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package helpers

import (
"context"
"fmt"
"testing"

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

type ExternalVolumeClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewExternalVolumeClient(context *TestClientContext, idsGenerator *IdsGenerator) *ExternalVolumeClient {
return &ExternalVolumeClient{
context: context,
ids: idsGenerator,
}
}

func (c *ExternalVolumeClient) exec(sql string) error {
ctx := context.Background()
_, err := c.context.client.ExecForTests(ctx, sql)
return err
}

// TODO(SNOW-999142): Use SDK implementation for External Volume once it's available
func (c *ExternalVolumeClient) Create(t *testing.T) (sdk.AccountObjectIdentifier, func()) {
t.Helper()
id := c.ids.RandomAccountObjectIdentifier()
err := c.exec(fmt.Sprintf(`
create external volume %s
storage_locations =
(
(
name = 'my-s3-us-west-2'
storage_provider = 's3'
storage_base_url = 's3://my_example_bucket/'
storage_aws_role_arn = 'arn:aws:iam::123456789012:role/myrole'
encryption=(type='aws_sse_kms' kms_key_id='1234abcd-12ab-34cd-56ef-1234567890ab')
)
);
`, id.FullyQualifiedName()))
require.NoError(t, err)

return id, c.DropFunc(t, id)
}

func (c *ExternalVolumeClient) DropFunc(t *testing.T, id sdk.AccountObjectIdentifier) func() {
t.Helper()

return func() {
err := c.exec(fmt.Sprintf(`drop external volume if exists %s`, id.FullyQualifiedName()))
require.NoError(t, err)
}
}
4 changes: 4 additions & 0 deletions pkg/acceptance/helpers/test_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ type TestClient struct {
Application *ApplicationClient
ApplicationPackage *ApplicationPackageClient
Context *ContextClient
CatalogIntegration *CatalogIntegrationClient
Database *DatabaseClient
DatabaseRole *DatabaseRoleClient
DynamicTable *DynamicTableClient
ExternalVolume *ExternalVolumeClient
FailoverGroup *FailoverGroupClient
FileFormat *FileFormatClient
MaskingPolicy *MaskingPolicyClient
Expand Down Expand Up @@ -62,9 +64,11 @@ func NewTestClient(c *sdk.Client, database string, schema string, warehouse stri
Application: NewApplicationClient(context, idsGenerator),
ApplicationPackage: NewApplicationPackageClient(context, idsGenerator),
Context: NewContextClient(context),
CatalogIntegration: NewCatalogIntegrationClient(context, idsGenerator),
Database: NewDatabaseClient(context, idsGenerator),
DatabaseRole: NewDatabaseRoleClient(context, idsGenerator),
DynamicTable: NewDynamicTableClient(context, idsGenerator),
ExternalVolume: NewExternalVolumeClient(context, idsGenerator),
FailoverGroup: NewFailoverGroupClient(context, idsGenerator),
FileFormat: NewFileFormatClient(context, idsGenerator),
MaskingPolicy: NewMaskingPolicyClient(context, idsGenerator),
Expand Down
2 changes: 1 addition & 1 deletion pkg/resources/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func UpdateDatabase(d *schema.ResourceData, meta interface{}) error {
newName := d.Get("name").(string)
newId := sdk.NewAccountObjectIdentifier(newName)
opts := &sdk.AlterDatabaseOptions{
NewName: newId,
NewName: &newId,
}
err := client.Databases.Alter(ctx, id, opts)
if err != nil {
Expand Down
29 changes: 25 additions & 4 deletions pkg/sdk/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ type Clone struct {
}

func (v *Clone) validate() error {
var errs []error
if everyValueSet(v.At, v.Before) {
return errors.New("only one of AT or BEFORE can be set")
errs = append(errs, errors.New("only one of AT or BEFORE can be set"))
}
if valueSet(v.At) {
return v.At.validate()
errs = append(errs, v.At.validate())
}
if valueSet(v.Before) {
return v.Before.validate()
errs = append(errs, v.Before.validate())
}
return nil
return errors.Join(errs...)
}

type LimitFrom struct {
Expand Down Expand Up @@ -220,3 +221,23 @@ var (
func DistributionPointer(v Distribution) *Distribution {
return &v
}

type LogLevel string

const (
LogLevelTrace LogLevel = "TRACE"
LogLevelDebug LogLevel = "DEBUG"
LogLevelInfo LogLevel = "INFO"
LogLevelWarn LogLevel = "WARN"
LogLevelError LogLevel = "ERROR"
LogLevelFatal LogLevel = "FATAL"
LogLevelOff LogLevel = "OFF"
)

type TraceLevel string

const (
TraceLevelAlways TraceLevel = "ALWAYS"
TraceLevelOnEvent TraceLevel = "ON_EVENT"
TraceLevelOff TraceLevel = "OFF"
)
Loading

0 comments on commit 750fe37

Please sign in to comment.