From ecba5262f113a75948c250015f59b088fcb21cf5 Mon Sep 17 00:00:00 2001 From: Venelin Date: Thu, 1 Feb 2024 16:24:59 +0000 Subject: [PATCH] validate resources only during build time --- helper/schema/provider.go | 16 +++++++++------- helper/schema/runtime.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 helper/schema/runtime.go diff --git a/helper/schema/provider.go b/helper/schema/provider.go index 75e1a7c4..40956371 100644 --- a/helper/schema/provider.go +++ b/helper/schema/provider.go @@ -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)) + } } } diff --git a/helper/schema/runtime.go b/helper/schema/runtime.go new file mode 100644 index 00000000..39117e96 --- /dev/null +++ b/helper/schema/runtime.go @@ -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 +}