Skip to content

Commit

Permalink
feat: Introduce assertions generators part2 (#2956)
Browse files Browse the repository at this point in the history
Continuation to
#2952.

Main features:
- missing generators (4) added
- definitions of generateable objects extracted to allow an easier reuse
- common extractor for schemas added
  • Loading branch information
sfc-gh-asawicki authored Jul 25, 2024
1 parent f70e40e commit f715e8a
Show file tree
Hide file tree
Showing 67 changed files with 3,331 additions and 1,139 deletions.
28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,32 @@ generate-snowflake-object-parameters-assertions: ## Generate snowflake object pa
clean-snowflake-object-parameters-assertions: ## Clean snowflake object parameters assertions
rm -f ./pkg/acceptance/bettertestspoc/assert/objectparametersassert/*_gen.go

generate-resource-assertions: ## Generate resource assertions
go generate ./pkg/acceptance/bettertestspoc/assert/resourceassert/generate.go

clean-resource-assertions: ## Clean resource assertions
rm -f ./pkg/acceptance/bettertestspoc/assert/resourceassert/*_gen.go

generate-resource-parameters-assertions: ## Generate resource parameters assertions
go generate ./pkg/acceptance/bettertestspoc/assert/resourceparametersassert/generate.go

clean-resource-parameters-assertions: ## Clean resource parameters assertions
rm -f ./pkg/acceptance/bettertestspoc/assert/resourceparametersassert/*_gen.go

generate-resource-show-output-assertions: ## Generate resource parameters assertions
go generate ./pkg/acceptance/bettertestspoc/assert/resourceshowoutputassert/generate.go

clean-resource-show-output-assertions: ## Clean resource parameters assertions
rm -f ./pkg/acceptance/bettertestspoc/assert/resourceshowoutputassert/*_gen.go

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

clean-resource-model-builder: ## 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-builder ## 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 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
526 changes: 198 additions & 328 deletions pkg/acceptance/bettertestspoc/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,18 @@ package main
import (
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert/objectassert/gen"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/genhelpers"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
)

func main() {
genhelpers.NewGenerator(
getSdkObjectDetails,
gen.GetSdkObjectDetails,
gen.ModelFromSdkObjectDetails,
getFilename,
gen.AllTemplates,
).
RunAndHandleOsReturn()
}

type SdkObjectDef struct {
idType string
objectType sdk.ObjectType
objectStruct any
}

func getSdkObjectDetails() []genhelpers.SdkObjectDetails {
allSdkObjectsDetails := make([]genhelpers.SdkObjectDetails, len(allStructs))
for idx, d := range allStructs {
structDetails := genhelpers.ExtractStructDetails(d.objectStruct)
allSdkObjectsDetails[idx] = genhelpers.SdkObjectDetails{
IdType: d.idType,
ObjectType: d.objectType,
StructDetails: structDetails,
}
}
return allSdkObjectsDetails
}

func getFilename(_ genhelpers.SdkObjectDetails, model gen.SnowflakeObjectAssertionsModel) string {
return genhelpers.ToSnakeCase(model.Name) + "_snowflake" + "_gen.go"
}

var allStructs = []SdkObjectDef{
{
idType: "sdk.AccountObjectIdentifier",
objectType: sdk.ObjectTypeUser,
objectStruct: sdk.User{},
},
{
idType: "sdk.AccountObjectIdentifier",
objectType: sdk.ObjectTypeWarehouse,
objectStruct: sdk.Warehouse{},
},
}
14 changes: 1 addition & 13 deletions pkg/acceptance/bettertestspoc/assert/objectassert/gen/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gen

import (
"os"
"slices"
"strings"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/genhelpers"
Expand Down Expand Up @@ -35,19 +34,8 @@ type SnowflakeObjectFieldAssertion struct {
func ModelFromSdkObjectDetails(sdkObject genhelpers.SdkObjectDetails) SnowflakeObjectAssertionsModel {
name, _ := strings.CutPrefix(sdkObject.Name, "sdk.")
fields := make([]SnowflakeObjectFieldAssertion, len(sdkObject.Fields))
imports := make(map[string]struct{})
for idx, field := range sdkObject.Fields {
fields[idx] = MapToSnowflakeObjectFieldAssertion(field)
additionalImport, isImportedType := field.GetImportedType()
if isImportedType {
imports[additionalImport] = struct{}{}
}
}
additionalImports := make([]string, 0)
for k := range imports {
if !slices.Contains([]string{"sdk"}, k) {
additionalImports = append(additionalImports, k)
}
}

packageWithGenerateDirective := os.Getenv("GOPACKAGE")
Expand All @@ -58,7 +46,7 @@ func ModelFromSdkObjectDetails(sdkObject genhelpers.SdkObjectDetails) SnowflakeO
Fields: fields,
PreambleModel: PreambleModel{
PackageName: packageWithGenerateDirective,
AdditionalStandardImports: additionalImports,
AdditionalStandardImports: genhelpers.AdditionalStandardImports(sdkObject.Fields),
},
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package gen

import (
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/genhelpers"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
)

type SdkObjectDef struct {
IdType string
ObjectType sdk.ObjectType
ObjectStruct any
}

var allStructs = []SdkObjectDef{
{
IdType: "sdk.AccountObjectIdentifier",
ObjectType: sdk.ObjectTypeUser,
ObjectStruct: sdk.User{},
},
{
IdType: "sdk.AccountObjectIdentifier",
ObjectType: sdk.ObjectTypeWarehouse,
ObjectStruct: sdk.Warehouse{},
},
}

func GetSdkObjectDetails() []genhelpers.SdkObjectDetails {
allSdkObjectsDetails := make([]genhelpers.SdkObjectDetails, len(allStructs))
for idx, d := range allStructs {
structDetails := genhelpers.ExtractStructDetails(d.ObjectStruct)
allSdkObjectsDetails[idx] = genhelpers.SdkObjectDetails{
IdType: d.IdType,
ObjectType: d.ObjectType,
StructDetails: structDetails,
}
}
return allSdkObjectsDetails
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert/objectassert/gen.PreambleModel*/ -}}

// Code generated by sdk-to-schema generator; DO NOT EDIT.
// Code generated by assertions generator; DO NOT EDIT.

package {{ .PackageName }}

Expand Down

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

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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,105 +5,18 @@ package main
import (
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert/objectparametersassert/gen"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/genhelpers"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
)

func main() {
genhelpers.NewGenerator(
getAllSnowflakeObjectParameters,
gen.GetAllSnowflakeObjectParameters,
gen.ModelFromSnowflakeObjectParameters,
getFilename,
gen.AllTemplates,
).
RunAndHandleOsReturn()
}

func getAllSnowflakeObjectParameters() []gen.SnowflakeObjectParameters {
return allObjectsParameters
}

func getFilename(_ gen.SnowflakeObjectParameters, model gen.SnowflakeObjectParametersAssertionsModel) string {
return genhelpers.ToSnakeCase(model.Name) + "_parameters_snowflake" + "_gen.go"
}

// TODO [SNOW-1501905]: use SDK definition after parameters rework (+ preprocessing here)
var allObjectsParameters = []gen.SnowflakeObjectParameters{
{
Name: "User",
IdType: "sdk.AccountObjectIdentifier",
Level: sdk.ParameterTypeUser,
Parameters: []gen.SnowflakeParameter{
{ParameterName: string(sdk.UserParameterEnableUnredactedQuerySyntaxError), ParameterType: "bool", DefaultValue: "false", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterNetworkPolicy), ParameterType: "string", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterPreventUnloadToInternalStages), ParameterType: "bool", DefaultValue: "false", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterAbortDetachedQuery), ParameterType: "bool", DefaultValue: "false", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterAutocommit), ParameterType: "bool", DefaultValue: "true", DefaultLevel: "sdk.ParameterTypeAccount"},
{ParameterName: string(sdk.UserParameterBinaryInputFormat), ParameterType: "sdk.BinaryInputFormat", DefaultValue: "sdk.BinaryInputFormatHex", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterBinaryOutputFormat), ParameterType: "sdk.BinaryOutputFormat", DefaultValue: "sdk.BinaryOutputFormatHex", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterClientMemoryLimit), ParameterType: "int", DefaultValue: "1536", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterClientMetadataRequestUseConnectionCtx), ParameterType: "bool", DefaultValue: "false", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterClientPrefetchThreads), ParameterType: "int", DefaultValue: "4", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterClientResultChunkSize), ParameterType: "int", DefaultValue: "160", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterClientResultColumnCaseInsensitive), ParameterType: "bool", DefaultValue: "false", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterClientSessionKeepAlive), ParameterType: "bool", DefaultValue: "false", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterClientSessionKeepAliveHeartbeatFrequency), ParameterType: "int", DefaultValue: "3600", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterClientTimestampTypeMapping), ParameterType: "sdk.ClientTimestampTypeMapping", DefaultValue: "sdk.ClientTimestampTypeMappingLtz", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterDateInputFormat), ParameterType: "string", DefaultValue: "AUTO", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterDateOutputFormat), ParameterType: "string", DefaultValue: "YYYY-MM-DD", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterEnableUnloadPhysicalTypeOptimization), ParameterType: "bool", DefaultValue: "true", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterErrorOnNondeterministicMerge), ParameterType: "bool", DefaultValue: "true", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterErrorOnNondeterministicUpdate), ParameterType: "bool", DefaultValue: "false", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterGeographyOutputFormat), ParameterType: "sdk.GeographyOutputFormat", DefaultValue: "sdk.GeographyOutputFormatGeoJSON", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterGeometryOutputFormat), ParameterType: "sdk.GeometryOutputFormat", DefaultValue: "sdk.GeometryOutputFormatGeoJSON", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterJdbcTreatDecimalAsInt), ParameterType: "bool", DefaultValue: "true", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterJdbcTreatTimestampNtzAsUtc), ParameterType: "bool", DefaultValue: "false", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterJdbcUseSessionTimezone), ParameterType: "bool", DefaultValue: "true", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterJsonIndent), ParameterType: "int", DefaultValue: "2", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterLockTimeout), ParameterType: "int", DefaultValue: "43200", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterLogLevel), ParameterType: "sdk.LogLevel", DefaultValue: "sdk.LogLevelOff", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterMultiStatementCount), ParameterType: "int", DefaultValue: "1", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterNoorderSequenceAsDefault), ParameterType: "bool", DefaultValue: "true", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterOdbcTreatDecimalAsInt), ParameterType: "bool", DefaultValue: "false", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterQueryTag), ParameterType: "string", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterQuotedIdentifiersIgnoreCase), ParameterType: "bool", DefaultValue: "false", DefaultLevel: "sdk.ParameterTypeAccount"},
{ParameterName: string(sdk.UserParameterRowsPerResultset), ParameterType: "int", DefaultValue: "0", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterS3StageVpceDnsName), ParameterType: "string", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterSearchPath), ParameterType: "string", DefaultValue: "$current, $public", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterSimulatedDataSharingConsumer), ParameterType: "string", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterStatementQueuedTimeoutInSeconds), ParameterType: "int", DefaultValue: "0", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterStatementTimeoutInSeconds), ParameterType: "int", DefaultValue: "172800", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterStrictJsonOutput), ParameterType: "bool", DefaultValue: "false", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterTimestampDayIsAlways24h), ParameterType: "bool", DefaultValue: "false", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterTimestampInputFormat), ParameterType: "string", DefaultValue: "AUTO", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterTimestampLtzOutputFormat), ParameterType: "string", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterTimestampNtzOutputFormat), ParameterType: "string", DefaultValue: "YYYY-MM-DD HH24:MI:SS.FF3", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterTimestampOutputFormat), ParameterType: "string", DefaultValue: "YYYY-MM-DD HH24:MI:SS.FF3 TZHTZM", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterTimestampTypeMapping), ParameterType: "sdk.TimestampTypeMapping", DefaultValue: "sdk.TimestampTypeMappingNtz", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterTimestampTzOutputFormat), ParameterType: "string", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterTimezone), ParameterType: "string", DefaultValue: "America/Los_Angeles", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterTimeInputFormat), ParameterType: "string", DefaultValue: "AUTO", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterTimeOutputFormat), ParameterType: "string", DefaultValue: "HH24:MI:SS", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterTraceLevel), ParameterType: "sdk.TraceLevel", DefaultValue: "sdk.TraceLevelOff", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterTransactionAbortOnError), ParameterType: "bool", DefaultValue: "false", DefaultLevel: "sdk.ParameterTypeAccount"},
{ParameterName: string(sdk.UserParameterTransactionDefaultIsolationLevel), ParameterType: "sdk.TransactionDefaultIsolationLevel", DefaultValue: "sdk.TransactionDefaultIsolationLevelReadCommitted", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterTwoDigitCenturyStart), ParameterType: "int", DefaultValue: "1970", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
// TODO: quick workaround for now: lowercase for ignore in snowflake by default but uppercase for FAIL
{ParameterName: string(sdk.UserParameterUnsupportedDdlAction), ParameterType: "sdk.UnsupportedDDLAction", DefaultValue: "sdk.UnsupportedDDLAction(strings.ToLower(string(sdk.UnsupportedDDLActionIgnore)))", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterUseCachedResult), ParameterType: "bool", DefaultValue: "true", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterWeekOfYearPolicy), ParameterType: "int", DefaultValue: "0", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.UserParameterWeekStart), ParameterType: "int", DefaultValue: "0", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
},
// for the quickfix above
AdditionalImports: []string{"strings"},
},
{
Name: "Warehouse",
IdType: "sdk.AccountObjectIdentifier",
Level: sdk.ParameterTypeWarehouse,
Parameters: []gen.SnowflakeParameter{
{ParameterName: string(sdk.WarehouseParameterMaxConcurrencyLevel), ParameterType: "int", DefaultValue: "8", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.WarehouseParameterStatementQueuedTimeoutInSeconds), ParameterType: "int", DefaultValue: "0", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
{ParameterName: string(sdk.WarehouseParameterStatementTimeoutInSeconds), ParameterType: "int", DefaultValue: "172800", DefaultLevel: "sdk.ParameterTypeSnowflakeDefault"},
},
},
}
Loading

0 comments on commit f715e8a

Please sign in to comment.