Skip to content

Commit

Permalink
validate resources only during build time
Browse files Browse the repository at this point in the history
  • Loading branch information
VenelinMartinov committed Feb 1, 2024
1 parent 74776a5 commit ecba526
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
16 changes: 9 additions & 7 deletions helper/schema/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,17 @@ func (p *Provider) InternalValidate() error {
}
}

for k, r := range p.ResourcesMap {
if err := r.InternalValidate(nil, true); err != nil {
validationErrors = multierror.Append(validationErrors, fmt.Errorf("resource %s: %s", k, err))
if currentRuntimeStage == buildingProviderStage {
for k, r := range p.ResourcesMap {
if err := r.InternalValidate(nil, true); err != nil {
validationErrors = multierror.Append(validationErrors, fmt.Errorf("resource %s: %s", k, err))
}
}
}

for k, r := range p.DataSourcesMap {
if err := r.InternalValidate(nil, false); err != nil {
validationErrors = multierror.Append(validationErrors, fmt.Errorf("data source %s: %s", k, err))
for k, r := range p.DataSourcesMap {
if err := r.InternalValidate(nil, false); err != nil {
validationErrors = multierror.Append(validationErrors, fmt.Errorf("data source %s: %s", k, err))
}
}
}

Expand Down
31 changes: 31 additions & 0 deletions helper/schema/runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copied from https://github.com/pulumi/pulumi-terraform-bridge/blob/7c411fc807e709a85170cab0add5cb3a856c0c25/pkg/tfbridge/runtime.go#L1
package schema

import (
"runtime/debug"
"strings"
)

// Used internally for the code to distinguish if it is running at build-time or at runtime.
type runtimeStage int

const (
unknownStage runtimeStage = iota
buildingProviderStage
runningProviderStage
)

var currentRuntimeStage = guessRuntimeStage()

func guessRuntimeStage() runtimeStage {
buildInfo, _ := debug.ReadBuildInfo()
stage := unknownStage
if buildInfo != nil {
if strings.Contains(buildInfo.Path, "pulumi-tfgen") {
stage = buildingProviderStage
} else if strings.Contains(buildInfo.Path, "pulumi-resource") {
stage = runningProviderStage
}
}
return stage
}

0 comments on commit ecba526

Please sign in to comment.