Skip to content

Commit

Permalink
feat: Tasks v1 readiness - SDK part (#3086)
Browse files Browse the repository at this point in the history
## Changes
- Generated assertions for task
- Added a few missing fields in Create, Alter, and outputs
- Added CreateOrAlter support
- Adjusted unit and integration tests
- Adjusted validation generator a bit (It was panic-ing before for that
case; it was never used before)
- Adjusted resource and datasource to use new SDK

## References
* [CREATE
TASK](https://docs.snowflake.com/en/sql-reference/sql/create-task)
  • Loading branch information
sfc-gh-jcieslak committed Sep 23, 2024
1 parent a32f118 commit 0a77383
Show file tree
Hide file tree
Showing 25 changed files with 3,241 additions and 858 deletions.
2 changes: 0 additions & 2 deletions examples/resources/snowflake_shared_database/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ resource "snowflake_shared_database" "test" {
from_share = "<primary_account_organization_name>.<primary_account_name>.${snowflake_share.test.name}"
comment = "A shared database"

data_retention_time_in_days = 10
max_data_extension_time_in_days = 20
external_volume = "<external_volume_name>"
catalog = "<catalog_name>"
replace_invalid_characters = false
Expand Down
7 changes: 7 additions & 0 deletions pkg/acceptance/bettertestspoc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,10 @@ func (w *WarehouseDatasourceShowOutputAssert) IsEmpty() {
- support the rest of attribute types in config model builders (TODO left in `config/model/gen/model.go`)
- parametrize test client helper used - integration versus acceptance tests - this has to be changed in the generator too (TODO left in `assert/objectassert/user_snowflake_ext.go`)
- Omit computed fields in the model (like FullyQualifiedName), because it doesn't make sense to set them
- There's an error when generating models, steps to reproduce:
- Go to view resource code and change `data_metric_function` field to `testing` and make it required
- During the generation, the following error appears: mixed named and unnamed parameters.
It's a golang error indicating that the parameter has both unnamed and named parameters in function (e.g. `func(abc string, int)`).
The error is a result of both things:
1. Lists of objects are partially generated, and only parameter name is generated in some functions (the type has to be added manually).
2. `testing` is a package name that makes Go think that we want to have unnamed parameter there, but we just didn't generate the type for that field in the function argument.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ var allStructs = []SdkObjectDef{
ObjectType: sdk.ObjectTypeAuthenticationPolicy,
ObjectStruct: sdk.AuthenticationPolicy{},
},
{
IdType: "sdk.SchemaObjectIdentifier",
ObjectType: sdk.ObjectTypeTask,
ObjectStruct: sdk.Task{},
},
}

func GetSdkObjectDetails() []genhelpers.SdkObjectDetails {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package objectassert

import (
"errors"
"fmt"
"reflect"
"slices"
"testing"

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

func (t *TaskAssert) HasNotEmptyCreatedOn() *TaskAssert {
t.AddAssertion(func(t *testing.T, o *sdk.Task) error {
t.Helper()
if o.CreatedOn == "" {
return fmt.Errorf("expected created on not empty; got: %v", o.CreatedOn)
}
return nil
})
return t
}

func (t *TaskAssert) HasNotEmptyId() *TaskAssert {
t.AddAssertion(func(t *testing.T, o *sdk.Task) error {
t.Helper()
if o.Id == "" {
return fmt.Errorf("expected id not empty; got: %v", o.CreatedOn)
}
return nil
})
return t
}

func (t *TaskAssert) HasPredecessors(ids ...sdk.SchemaObjectIdentifier) *TaskAssert {
t.AddAssertion(func(t *testing.T, o *sdk.Task) error {
t.Helper()
if len(o.Predecessors) != len(ids) {
return fmt.Errorf("expected %d (%v) predecessors, got %d (%v)", len(ids), ids, len(o.Predecessors), o.Predecessors)
}
var errs []error
for _, id := range ids {
if !slices.ContainsFunc(o.Predecessors, func(predecessorId sdk.SchemaObjectIdentifier) bool {
return predecessorId.FullyQualifiedName() == id.FullyQualifiedName()
}) {
errs = append(errs, fmt.Errorf("expected id: %s, to be in the list of predecessors: %v", id.FullyQualifiedName(), o.Predecessors))
}
}
return errors.Join(errs...)
})
return t
}

func (t *TaskAssert) HasTaskRelations(expected sdk.TaskRelations) *TaskAssert {
t.AddAssertion(func(t *testing.T, o *sdk.Task) error {
t.Helper()
if len(o.TaskRelations.Predecessors) != len(expected.Predecessors) {
return fmt.Errorf("expected %d (%v) predecessors in task relations, got %d (%v)", len(expected.Predecessors), expected.Predecessors, len(o.TaskRelations.Predecessors), o.TaskRelations.Predecessors)
}
var errs []error
for _, id := range expected.Predecessors {
if !slices.ContainsFunc(o.TaskRelations.Predecessors, func(predecessorId sdk.SchemaObjectIdentifier) bool {
return predecessorId.FullyQualifiedName() == id.FullyQualifiedName()
}) {
errs = append(errs, fmt.Errorf("expected id: %s, to be in the list of predecessors in task relations: %v", id.FullyQualifiedName(), o.TaskRelations.Predecessors))
}
}
if !reflect.DeepEqual(expected.FinalizerTask, o.TaskRelations.FinalizerTask) {
errs = append(errs, fmt.Errorf("expected finalizer task: %v; got: %v", expected.FinalizerTask, o.TaskRelations.FinalizerTask))
}
return errors.Join(errs...)
})
return t
}

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

Loading

0 comments on commit 0a77383

Please sign in to comment.