Skip to content

Commit

Permalink
Try using a global
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeffryes committed Oct 4, 2023
1 parent 62ced78 commit 08c845f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 21 deletions.
49 changes: 28 additions & 21 deletions pkg/tfbridge/auto_aliasing.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ package tfbridge

import (
"github.com/Masterminds/semver"
"runtime/debug"
"strings"

shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
md "github.com/pulumi/pulumi-terraform-bridge/v3/unstable/metadata"
Expand Down Expand Up @@ -125,9 +123,6 @@ func (info *ProviderInfo) ApplyAutoAliases() error {
return err
}

buildInfo, _ := debug.ReadBuildInfo()
isTfgen := buildInfo != nil && strings.Contains(buildInfo.Path, "pulumi-tfgen")

var currentVersion int
// If version is missing, we assume the current version is the most recent major
// version in mentioned in history.
Expand Down Expand Up @@ -167,7 +162,7 @@ func (info *ProviderInfo) ApplyAutoAliases() error {
for tfToken, computed := range info.Resources {
r, _ := rMap.GetOk(tfToken)
aliasResource(info, r, &applyAliases, hist.Resources,
computed, tfToken, currentVersion, isTfgen)
computed, tfToken, currentVersion)
}

for tfToken, computed := range info.DataSources {
Expand All @@ -180,7 +175,7 @@ func (info *ProviderInfo) ApplyAutoAliases() error {
f()
}

if isTfgen {
if isTfgen() {
if err := md.Set(artifact, aliasMetadataKey, hist); err != nil {
// Set fails only when `hist` is not serializable. Because `hist` is
// composed of marshallable, non-cyclic types, this is impossible.
Expand Down Expand Up @@ -212,11 +207,10 @@ func aliasResource(
applyResourceAliases *[]func(),
hist map[string]*tokenHistory[tokens.Type], computed *ResourceInfo,
tfToken string, version int,
isTfgen bool,
) {
prev, hasPrev := hist[tfToken]
if !hasPrev {
if isTfgen {
if isTfgen() {
// It's not in the history, so it must be new. Stick it in the history for
// next time.
hist[tfToken] = &tokenHistory[tokens.Type]{
Expand Down Expand Up @@ -271,7 +265,7 @@ func applyResourceMaxItemsOneAliasing(
hasH = hasH || fieldHasHist
hasI = hasI || fieldHasInfo

if !hasH {
if !hasH && isTfgen() {
delete(*hist, k)
}
if !hasI {
Expand Down Expand Up @@ -330,13 +324,15 @@ func applyMaxItemsOneAliasing(schema shim.Schema, h *fieldHistory, info *SchemaI
// MaxItemsOne does not apply, so do nothing
} else if info.MaxItemsOne != nil {
// The user has overwritten the value, so we will just record that.
h.MaxItemsOne = info.MaxItemsOne
hasH = true
if isTfgen() {
h.MaxItemsOne = info.MaxItemsOne
hasH = true
}
} else if h.MaxItemsOne != nil {
// If we have a previous value in the history, we keep it as is.
info.MaxItemsOne = h.MaxItemsOne
hasI = true
} else {
} else if isTfgen() {
// There is no history for this value, so we bake it into the
// alias history.
h.MaxItemsOne = BoolRef(IsMaxItemsOne(schema, info))
Expand All @@ -349,10 +345,12 @@ func applyMaxItemsOneAliasing(schema shim.Schema, h *fieldHistory, info *SchemaI
// If the .Elem existed before this function, we mark it as unsafe to cleanup.
var hasElemH, hasElemI bool
populateElem := func() {
if h.Elem == nil {
h.Elem = &fieldHistory{}
} else {
hasElemH = true
if isTfgen() {
if h.Elem == nil {
h.Elem = &fieldHistory{}
} else {
hasElemH = true
}
}
if info.Elem == nil {
info.Elem = &SchemaInfo{}
Expand All @@ -367,7 +365,7 @@ func applyMaxItemsOneAliasing(schema shim.Schema, h *fieldHistory, info *SchemaI
cleanupElem := func(elemHist, elemInfo bool) {
hasElemH = hasElemH || elemHist
hasElemI = hasElemI || elemInfo
if !hasElemH {
if !hasElemH && isTfgen() {
h.Elem = nil
}
if !hasElemI {
Expand All @@ -379,11 +377,17 @@ func applyMaxItemsOneAliasing(schema shim.Schema, h *fieldHistory, info *SchemaI
switch e := e.(type) {
case shim.Resource:
populateElem()
eHasH, eHasI := applyResourceMaxItemsOneAliasing(e, &h.Elem.Fields, &info.Elem.Fields)
var eHasH, eHasI bool
if h.Elem != nil {
eHasH, eHasI = applyResourceMaxItemsOneAliasing(e, &h.Elem.Fields, &info.Elem.Fields)
}
cleanupElem(eHasH, eHasI)
case shim.Schema:
populateElem()
eHasH, eHasI := applyMaxItemsOneAliasing(e, h.Elem, info.Elem)
var eHasH, eHasI bool
if h.Elem != nil {
eHasH, eHasI = applyMaxItemsOneAliasing(e, h.Elem, info.Elem)
}
cleanupElem(eHasH, eHasI)
}

Expand Down Expand Up @@ -477,7 +481,6 @@ func aliasDataSource(
}

applyResourceMaxItemsOneAliasing(ds, &hist[tfToken].Fields, &computed.Fields)

}

func aliasOrRenameDataSource(
Expand Down Expand Up @@ -518,3 +521,7 @@ func aliasOrRenameDataSource(
}

}

func isTfgen() bool {
return GetRuntimeStage() != ResourceStage
}
48 changes: 48 additions & 0 deletions pkg/tfbridge/runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package tfbridge

import (
"runtime/debug"
"strings"
)

type RuntimeStage int

const (
UnknownStage RuntimeStage = iota
TfgenStage
ResourceStage
)

// Holds runtime flags
type RuntimeInfo struct {
Stage RuntimeStage
}

var runtime = initRuntimeInfo()

func initRuntimeInfo() RuntimeInfo {
buildInfo, _ := debug.ReadBuildInfo()
stage := UnknownStage
if buildInfo != nil {
if strings.Contains(buildInfo.Path, "pulumi-tfgen") {
stage = TfgenStage
} else if strings.Contains(buildInfo.Path, "pulumi-resource") {
stage = ResourceStage
}
}
return RuntimeInfo{
Stage: stage,
}
}

func ReadRuntimeInfo() RuntimeInfo {
return runtime
}

func GetRuntimeStage() RuntimeStage {
return runtime.Stage
}

func SetRuntimeStage(s RuntimeStage) {
runtime.Stage = s
}

0 comments on commit 08c845f

Please sign in to comment.