Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into tag-association-v1
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jmichalak committed Nov 29, 2024
2 parents 4e5d39a + 425787c commit b53d7c5
Show file tree
Hide file tree
Showing 71 changed files with 3,765 additions and 1,142 deletions.
18 changes: 15 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ sweep: ## destroy the whole architecture; USE ONLY FOR DEVELOPMENT ACCOUNTS
@echo "Are you sure? [y/n]" >&2
@read -r REPLY; \
if echo "$$REPLY" | grep -qG "^[yY]$$"; then \
TEST_SF_TF_ENABLE_SWEEP=1 go test -timeout 300s -run "^(TestSweepAll|Test_Sweeper_NukeStaleObjects)" ./pkg/sdk -v; \
TEST_SF_TF_ENABLE_SWEEP=1 go test -timeout=10m -run "^(TestSweepAll|Test_Sweeper_NukeStaleObjects)" ./pkg/sdk -v; \
else echo "Aborting..."; \
fi;

Expand Down Expand Up @@ -162,8 +162,20 @@ generate-resource-model-builders: ## Generate resource model builders
clean-resource-model-builders: ## Clean resource model builders
rm -f ./pkg/acceptance/bettertestspoc/config/model/*_gen.go

clean-all-assertions-and-config-models: clean-snowflake-object-assertions clean-snowflake-object-parameters-assertions clean-resource-assertions clean-resource-parameters-assertions clean-resource-show-output-assertions clean-resource-model-builders ## clean all generated assertions and config models
generate-provider-model-builders: ## Generate provider model builders
go generate ./pkg/acceptance/bettertestspoc/config/providermodel/generate.go

generate-all-assertions-and-config-models: generate-snowflake-object-assertions generate-snowflake-object-parameters-assertions generate-resource-assertions generate-resource-parameters-assertions generate-resource-show-output-assertions generate-resource-model-builders ## generate all assertions and config models
clean-provider-model-builders: ## Clean provider model builders
rm -f ./pkg/acceptance/bettertestspoc/config/providermodel/*_gen.go

generate-datasource-model-builders: ## Generate datasource model builders
go generate ./pkg/acceptance/bettertestspoc/config/datasourcemodel/generate.go

clean-datasource-model-builders: ## Clean datasource model builders
rm -f ./pkg/acceptance/bettertestspoc/config/datasourcemodel/*_gen.go

clean-all-assertions-and-config-models: clean-snowflake-object-assertions clean-snowflake-object-parameters-assertions clean-resource-assertions clean-resource-parameters-assertions clean-resource-show-output-assertions clean-resource-model-builders clean-provider-model-builders clean-datasource-model-builders ## clean all generated assertions and config models

generate-all-assertions-and-config-models: generate-snowflake-object-assertions generate-snowflake-object-parameters-assertions generate-resource-assertions generate-resource-parameters-assertions generate-resource-show-output-assertions generate-resource-model-builders generate-provider-model-builders generate-datasource-model-builders ## generate all assertions and config models

.PHONY: build-local clean-generator-poc dev-setup dev-cleanup docs docs-check fmt fmt-check fumpt help install lint lint-fix mod mod-check pre-push pre-push-check sweep test test-acceptance uninstall-tf
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/gookit/color v1.5.4
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/hcl v1.0.0
github.com/hashicorp/terraform-json v0.21.0
github.com/hashicorp/terraform-plugin-framework v1.8.0
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mO
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/hc-install v0.6.3 h1:yE/r1yJvWbtrJ0STwScgEnCanb0U9v7zp0Gbkmcoxqs=
github.com/hashicorp/hc-install v0.6.3/go.mod h1:KamGdbodYzlufbWh4r9NRo8y6GLHWZP2GBtdnms1Ln0=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/hcl/v2 v2.19.1 h1://i05Jqznmb2EXqa39Nsvyan2o5XyMowW5fnCKW5RPI=
github.com/hashicorp/hcl/v2 v2.19.1/go.mod h1:ThLC89FV4p9MPW804KVbe/cEXoQ8NZEh+JtMeeGErHE=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
Expand Down
23 changes: 0 additions & 23 deletions pkg/acceptance/asserts.go

This file was deleted.

73 changes: 0 additions & 73 deletions pkg/acceptance/asserts_test.go

This file was deleted.

82 changes: 75 additions & 7 deletions pkg/acceptance/bettertestspoc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ It contains the following packages:
- resource parameters assertions (generated in subpackage `resourceparametersassert`)
- show output assertions (generated in subpackage `resourceshowoutputassert`)

- `config` - the new `ResourceModel` abstraction resides here. It provides models for objects and the builder methods allowing better config preparation in the acceptance tests.
- `config` - the new model abstractions (`ResourceModel`, `DatasourceModel`, and `ProviderModel`) reside here. They provide models for objects and the builder methods allowing better config preparation in the acceptance tests.
It aims to be more readable than using `Config:` with hardcoded string or `ConfigFile:` for file that is not directly reachable from the test body. Also, it should be easier to reuse the models and prepare convenience extension methods. The models are already generated.

## How it works
Expand Down Expand Up @@ -97,18 +97,58 @@ Resource config model builders can be generated automatically. For object `abc`
- add object you want to generate to `allResourceSchemaDefs` slice in the `assert/resourceassert/gen/resource_schema_def.go`
- to add custom (not generated) config builder methods create file `warehouse_model_ext` in the `config/model` package. Example would be:
```go
func BasicWarehouseModel(
func BasicAbcModel(
name string,
comment string,
) *WarehouseModel {
return WarehouseWithDefaultMeta(name).WithComment(comment)
) *AbcModel {
return AbcWithDefaultMeta(name).WithComment(comment)
}

func (w *WarehouseModel) WithWarehouseSizeEnum(warehouseSize sdk.WarehouseSize) *WarehouseModel {
func (w *AbcModel) WithWarehouseSizeEnum(warehouseSize sdk.WarehouseSize) *AbcModel {
return w.WithWarehouseSize(string(warehouseSize))
}
```

### Adding new datasource config model builders
Data source config model builders can be generated automatically. For object `abc` do the following:
- add object you want to generate to `allDatasourcesSchemaDefs` slice in the `config/datasourcemodel/gen/datasource_schema_def.go`
- to add custom (not generated) config builder methods create file `abc_model_ext` in the `config/datasourcemodel` package. Example would be:
```go
func BasicAbcModel(
name string,
comment string,
) *AbcModel {
return AbcWithDefaultMeta(name).WithComment(comment)
}

func (d *AbcModel) WithLimit(rows int) *AbcModel {
return d.WithLimitValue(
tfconfig.ObjectVariable(map[string]tfconfig.Variable{
"rows": tfconfig.IntegerVariable(rows),
}),
)
}
```

### Adding new provider config model builders
Provider config model builders can be generated automatically. For object `abc` do the following:
- add object you want to generate to `allProviderSchemaDefs` slice in the `config/providermodel/gen/provider_schema_def.go`
- to add custom (not generated) config builder methods create file `abc_model_ext` in the `config/providermodel` package. Example would be:
```go
func BasicAbcModel(
name string,
comment string,
) *AbcModel {
return AbcWithDefaultMeta(name).WithComment(comment)
}

func (w *AbcModel) WithWarehouseSizeEnum(warehouseSize sdk.WarehouseSize) *AbcModel {
return w.WithWarehouseSize(string(warehouseSize))
}
```

*Note*: our provider's config is already generated, so there should not be a need to generate any more providers (the regeneration or adding custom methods are still expected).

### Running the generators
Each of the above assertion types/config models has its own generator and cleanup entry in our Makefile.
You can generate config models with:
Expand Down Expand Up @@ -310,12 +350,12 @@ it will result in:
Test: TestInt_Warehouses/create:_complete
```

## Known limitations/planned improvements
## Planned improvements
- Test all the utilities for assertion/model construction (public interfaces, methods, functions).
- Verify if all the config types are supported.
- Consider a better implementation for the model conversion to config (TODO left in `config/config.go`).
- Support additional methods for references in models (TODO left in `config/config.go`).
- Support depends_on in models so that it can be chained like other resource fields (TODO left in `config/config.go`).
- Generate depends_on for all compatible models. Consider exporting it in meta (discussion: https://github.com/Snowflake-Labs/terraform-provider-snowflake/pull/3207#discussion_r1850053618).
- Add a convenience function to concatenate multiple models (TODO left in `config/config.go`).
- Add function to support using `ConfigFile:` in the acceptance tests (TODO left in `config/config.go`).
- Replace `acceptance/snowflakechecks` with the new proposed Snowflake objects assertions.
Expand Down Expand Up @@ -354,4 +394,32 @@ func (w *WarehouseDatasourceShowOutputAssert) IsEmpty() {
- utilize `ContainsExactlyInAnyOrder` function in `pkg/acceptance/bettertestspoc/assert/commons.go` to create asserts on collections that are order independent
- Additional asserts for sets and lists that wouldn't rely on the order of items saved to the state (SNOW-1706544)
- support generating provider config and use generated configs in `pkg/provider/provider_acceptance_test.go`
- add config builders for other block types (Variable, Output, Locals, Module, Terraform)
- add provider to resource/datasource models (use in the grant_ownership_acceptance_test)
- explore HCL v2 in more detail (especially struct tags generation; probably with migration to plugin framework because of schema models); ref: https://github.com/hashicorp/hcl/blob/bee2dc2e75f7528ad85777b7a013c13796426bd6/gohcl/encode_test.go#L48
- introduce some common interface for all three existing models (ResourceModel, DatasourceModel, and ProviderModel)
- rename ResourceSchemaDetails (because it is used for the datasources and provider too)
- consider duplicating the builders template from resource (currently same template used for datasources and provider which limits the customization possibilities for just one block type)
- consider merging ResourceModel with DatasourceModel (currently the implementation is really similar)
- remove schema.TypeMap workaround or make it wiser (e.g. during generation we could programmatically gather all schema.TypeMap and use this workaround only for them)
- support asserting resource id in `assert/resourceassert/*_gen.go`

## Known limitations
- generating provider config may misbehave when used only with one object/map paramter (like `params`), e.g.:
```json
{
"provider": {
"snowflake": {
"params": {
"statement_timeout_in_seconds": 31337
}
}
}
}
```
will be converted to HCL:
```hcl
provider "snowflake" "params" {
statement_timeout_in_seconds = 31337
}
```
Loading

0 comments on commit b53d7c5

Please sign in to comment.