Skip to content

Commit

Permalink
diff tests for sdkv2
Browse files Browse the repository at this point in the history
  • Loading branch information
VenelinMartinov committed Jul 3, 2024
1 parent 03104a4 commit 725ea59
Show file tree
Hide file tree
Showing 6 changed files with 521 additions and 396 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ go 1.21

replace github.com/pulumi/pulumi-terraform-bridge/x/muxer => ./x/muxer

replace github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tests => ./pkg/tests

require (
github.com/apparentlymart/go-cidr v1.1.0
github.com/blang/semver v3.5.1+incompatible
Expand Down Expand Up @@ -36,8 +38,9 @@ require (
github.com/mitchellh/reflectwalk v1.0.2
github.com/pkg/errors v0.9.1
github.com/pulumi/inflector v0.1.1
github.com/pulumi/providertest v0.0.11
github.com/pulumi/providertest v0.0.13
github.com/pulumi/pulumi-java/pkg v0.11.0
github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tests v0.0.0-00010101000000-000000000000
github.com/pulumi/pulumi-terraform-bridge/x/muxer v0.0.8
github.com/pulumi/pulumi-yaml v1.8.0
github.com/pulumi/schema-tools v0.1.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1901,8 +1901,8 @@ github.com/pulumi/esc v0.9.1 h1:HH5eEv8sgyxSpY5a8yePyqFXzA8cvBvapfH8457+mIs=
github.com/pulumi/esc v0.9.1/go.mod h1:oEJ6bOsjYlQUpjf70GiX+CXn3VBmpwFDxUTlmtUN84c=
github.com/pulumi/inflector v0.1.1 h1:dvlxlWtXwOJTUUtcYDvwnl6Mpg33prhK+7mzeF+SobA=
github.com/pulumi/inflector v0.1.1/go.mod h1:HUFCjcPTz96YtTuUlwG3i3EZG4WlniBvR9bd+iJxCUY=
github.com/pulumi/providertest v0.0.11 h1:mg8MQ7Cq7+9XlHIkBD+aCqQO4mwAJEISngZgVdnQUe8=
github.com/pulumi/providertest v0.0.11/go.mod h1:HsxjVsytcMIuNj19w1lT2W0QXY0oReXl1+h6eD2JXP8=
github.com/pulumi/providertest v0.0.13 h1:9CAaoviOTuCVHDI15h3znXa5JsKYtXLYHIIdxOCzo3Y=
github.com/pulumi/providertest v0.0.13/go.mod h1:REAoaN+hGOtdWJGirfWYqcSjCejlbGfzyVTUuemJTuE=
github.com/pulumi/pulumi-java/pkg v0.11.0 h1:Jw9gBvyfmfOMq/EkYDm9+zGPxsDAA8jfeMpHmtZ+1oA=
github.com/pulumi/pulumi-java/pkg v0.11.0/go.mod h1:sXAk25P47AQVQL6ilAbFmRNgZykC7og/+87ihnqzFTc=
github.com/pulumi/pulumi-yaml v1.8.0 h1:bhmidiCMMuzsJao5FE0UR69iF3WVKPCFrRkzjotFNn4=
Expand Down
2 changes: 2 additions & 0 deletions pkg/tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ replace (
)

require (
github.com/hashicorp/terraform-plugin-sdk v1.7.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0
github.com/hexops/autogold/v2 v2.2.1
github.com/hexops/valast v1.4.4
Expand Down Expand Up @@ -91,6 +92,7 @@ require (
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/zclconf/go-cty-yaml v1.0.1 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions pkg/tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,7 @@ github.com/hashicorp/hil v0.0.0-20190212132231-97b3a9cdfa93 h1:T1Q6ag9tCwun16AW+
github.com/hashicorp/hil v0.0.0-20190212132231-97b3a9cdfa93/go.mod h1:n2TSygSNwsLJ76m8qFXTSc7beTb+auJxYdqrnoqwZWE=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/terraform-config-inspect v0.0.0-20191115094559-17f92b0546e8 h1:+RyjwU+Gnd/aTJBPZVDNm903eXVjjqhbaR4Ypx3xYyY=
github.com/hashicorp/terraform-config-inspect v0.0.0-20191115094559-17f92b0546e8/go.mod h1:p+ivJws3dpqbp1iP84+npOyAmTTOLMgCzrXd3GSdn/A=
github.com/hashicorp/terraform-exec v0.20.0/go.mod h1:ckKGkJWbsNqFKV1itgMnE0hY9IYf1HoiekpuN0eWoDw=
github.com/hashicorp/terraform-json v0.4.0/go.mod h1:eAbqb4w0pSlRmdvl8fOyHAi/+8jnkVYN28gJkSJrLhU=
Expand Down
130 changes: 130 additions & 0 deletions pkg/tests/schemaconvert/schemaconvert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package schemaconvert

import (
v1Schema "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
v2Schema "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
)

func Sdkv2ToV1Type(t v2Schema.ValueType) v1Schema.ValueType {
return v1Schema.ValueType(t)
}

func Sdkv2ToV1SchemaOrResource(elem interface{}) interface{} {
switch elem := elem.(type) {
case nil:
return nil
case *v2Schema.Schema:
return Sdkv2ToV1Schema(elem)
case *v2Schema.Resource:
return Sdkv2ToV1Resource(elem)
default:
contract.Failf("unexpected type %T", elem)
return nil
}
}

func Sdkv2ToV1Resource(sch *v2Schema.Resource) *v1Schema.Resource {
if sch.MigrateState != nil {
contract.Failf("MigrateState is not supported in conversion")
}
if sch.StateUpgraders != nil {
contract.Failf("StateUpgraders is not supported in conversion")
}
if sch.Create != nil || sch.Read != nil || sch.Update != nil || sch.Delete != nil || sch.Exists != nil ||
sch.CreateContext != nil || sch.ReadContext != nil || sch.UpdateContext != nil || sch.DeleteContext != nil || sch.Importer != nil {
contract.Failf("runtime methods not supported in conversion")
}

if sch.CustomizeDiff != nil {
contract.Failf("CustomizeDiff is not supported in conversion")
}

timeouts := v1Schema.ResourceTimeout{}
if sch.Timeouts != nil {
timeouts = v1Schema.ResourceTimeout{
Create: sch.Timeouts.Create,
Read: sch.Timeouts.Read,
Update: sch.Timeouts.Update,
Delete: sch.Timeouts.Delete,
Default: sch.Timeouts.Default,
}
}
timoutsPtr := &timeouts
if sch.Timeouts == nil {
timoutsPtr = nil
}

return &v1Schema.Resource{
Schema: Sdkv2ToV1SchemaMap(sch.Schema),
SchemaVersion: sch.SchemaVersion,
DeprecationMessage: sch.DeprecationMessage,
Timeouts: timoutsPtr,
}
}

func Sdkv2ToV1Schema(sch *v2Schema.Schema) *v1Schema.Schema {
if sch.DiffSuppressFunc != nil {
contract.Failf("DiffSuppressFunc is not supported in conversion")
}

defaultFunc := v1Schema.SchemaDefaultFunc(nil)
if sch.DefaultFunc != nil {
defaultFunc = func() (interface{}, error) {
return sch.DefaultFunc()
}
}

stateFunc := v1Schema.SchemaStateFunc(nil)
if sch.StateFunc != nil {
stateFunc = func(i interface{}) string {
return sch.StateFunc(i)
}
}

set := v1Schema.SchemaSetFunc(nil)
if sch.Set != nil {
set = func(i interface{}) int {
return sch.Set(i)
}
}

validateFunc := v1Schema.SchemaValidateFunc(nil)
if sch.ValidateFunc != nil {
validateFunc = func(i interface{}, s string) ([]string, []error) {
return sch.ValidateFunc(i, s)
}
}

return &v1Schema.Schema{
Type: Sdkv2ToV1Type(sch.Type),
Optional: sch.Optional,
Required: sch.Required,
Default: sch.Default,
DefaultFunc: defaultFunc,
Description: sch.Description,
InputDefault: sch.InputDefault,
Computed: sch.Computed,
ForceNew: sch.ForceNew,
StateFunc: stateFunc,
Elem: Sdkv2ToV1SchemaOrResource(sch.Elem),
MaxItems: sch.MaxItems,
MinItems: sch.MinItems,
Set: set,
ComputedWhen: sch.ComputedWhen,
ConflictsWith: sch.ConflictsWith,
ExactlyOneOf: sch.ExactlyOneOf,
AtLeastOneOf: sch.AtLeastOneOf,
Deprecated: sch.Deprecated,
ValidateFunc: validateFunc,
Sensitive: sch.Sensitive,
}
}

func Sdkv2ToV1SchemaMap(sch map[string]*v2Schema.Schema) map[string]*v1Schema.Schema {
res := make(map[string]*v1Schema.Schema)
for k, v := range sch {
res[k] = Sdkv2ToV1Schema(v)
}
return res
}
Loading

0 comments on commit 725ea59

Please sign in to comment.