Skip to content

Commit

Permalink
Toward bridging with PlanResourceChange/ApplyResourceChange
Browse files Browse the repository at this point in the history
  • Loading branch information
t0yv0 committed Jan 25, 2024
1 parent 1b7d17b commit 220a70e
Show file tree
Hide file tree
Showing 8 changed files with 751 additions and 12 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ require (
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-plugin-go v0.19.0 // indirect
github.com/hashicorp/terraform-plugin-go v0.19.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-registry-address v0.2.2 // indirect
github.com/hashicorp/vault/api v1.8.2 // indirect
Expand Down Expand Up @@ -248,4 +248,4 @@ require (
lukechampine.com/frand v1.4.2 // indirect
)

replace github.com/hashicorp/terraform-plugin-sdk/v2 => github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20230912190043-e6d96b3b8f7e
replace github.com/hashicorp/terraform-plugin-sdk/v2 => github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240124180637-ce7315bc0145
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2181,8 +2181,8 @@ github.com/pulumi/schema-tools v0.1.2 h1:Fd9xvUjgck4NA+7/jSk7InqCUT4Kj940+EcnbQK
github.com/pulumi/schema-tools v0.1.2/go.mod h1:62lgj52Tzq11eqWTIaKd+EVyYAu5dEcDJxMhTjvMO/k=
github.com/pulumi/terraform-diff-reader v0.0.2 h1:kTE4nEXU3/SYXESvAIem+wyHMI3abqkI3OhJ0G04LLI=
github.com/pulumi/terraform-diff-reader v0.0.2/go.mod h1:sZ9FUzGO+yM41hsQHs/yIcj/Y993qMdBxBU5mpDmAfQ=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20230912190043-e6d96b3b8f7e h1:blSirnXqvm8JXLxwxelsBroUNRhOHakDO7cgJUYTdpQ=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20230912190043-e6d96b3b8f7e/go.mod h1:qH/34G25Ugdj5FcM95cSoXzUgIbgfhVLXCcEcYaMwq8=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240124180637-ce7315bc0145 h1:5zU+hIoj2r/pffT8IxNHf4EZN+rGGIp0Y/SvqQf1SOY=
github.com/pulumi/terraform-plugin-sdk/v2 v2.0.0-20240124180637-ce7315bc0145/go.mod h1:qH/34G25Ugdj5FcM95cSoXzUgIbgfhVLXCcEcYaMwq8=
github.com/rakyll/embedmd v0.0.0-20171029212350-c8060a0752a2/go.mod h1:7jOTMgqac46PZcF54q6l2hkLEG8op93fZu61KmxWDV4=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
Expand Down
13 changes: 11 additions & 2 deletions pkg/tfshim/sdk-v2/cty.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,20 @@ func makeResourceRawConfigClassic(config *terraform.ResourceConfig, resource *sc
// One possibility to refactor this code is to have MakeTerraformInput return cty.Value directly
// instead of implicit any encoding.
func recoverAndCoerceCtyValue(resource *schema.Resource, value any) (cty.Value, error) {
c, err := recoverCtyValue(resource.CoreConfigSchema().ImpliedType(), value)
return recoverAndCoerceCtyValueWithSchema(resource.CoreConfigSchema(), value)
}

type blockLike interface {
ImpliedType() cty.Type
CoerceValue(cty.Value) (cty.Value, error)
}

func recoverAndCoerceCtyValueWithSchema(schema blockLike, value any) (cty.Value, error) {
c, err := recoverCtyValue(schema.ImpliedType(), value)
if err != nil {
return cty.NilVal, fmt.Errorf("recoverCtyValue failed: %w", err)
}
cv, err := resource.CoreConfigSchema().CoerceValue(c)
cv, err := schema.CoerceValue(c)
if err != nil {
return cty.NilVal, fmt.Errorf("CoerceValue failed: %w", err)
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/tfshim/sdk-v2/instance_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,15 @@ func (s v2InstanceState) objectViaCty(sch shim.SchemaMap) (map[string]interface{
contract.AssertNoErrorf(err, "schema.ApplyDiff failed")
}

return objectFromCtyValue(v), nil
}

func objectFromCtyValue(v cty.Value) map[string]interface{} {
// Now we need to translate cty.Value to a JSON-like form. This could have been avoided if surrounding Pulumi
// code accepted a cty.Value and translated that to resource.PropertyValue, but that is currently not the case.
//
// An additional complication is that unknown values cannot serialize, so first replace them with sentinels.
v, err = cty.Transform(v, func(_ cty.Path, v cty.Value) (cty.Value, error) {
v, err := cty.Transform(v, func(_ cty.Path, v cty.Value) (cty.Value, error) {
if !v.IsKnown() {
return cty.StringVal(UnknownVariableValue), nil
}
Expand All @@ -100,7 +104,7 @@ func (s v2InstanceState) objectViaCty(sch shim.SchemaMap) (map[string]interface{
obj, err := schema.StateValueToJSONMap(v, v.Type())
contract.AssertNoErrorf(err, "schema.StateValueToJSONMap failed")

return obj, nil
return obj
}

// The legacy version of Object used custom Pulumi code forked from TF sources.
Expand Down
6 changes: 5 additions & 1 deletion pkg/tfshim/sdk-v2/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ type v2Provider struct {
var _ shim.Provider = (*v2Provider)(nil)

func NewProvider(p *schema.Provider, opts ...providerOption) shim.Provider {
return v2Provider{
prov := v2Provider{
tf: p,
opts: opts,
}
if opts, err := getProviderOptions(opts); err == nil && opts.planResourceChangeFilter != nil {
return newProviderWithPlanResourceChange(p, prov, opts.planResourceChangeFilter)
}
return prov
}

func (p v2Provider) Schema() shim.SchemaMap {
Expand Down
Loading

0 comments on commit 220a70e

Please sign in to comment.