diff --git a/pkg/tfbridge/info.go b/pkg/tfbridge/info.go index 037779c8f4..1e99e86777 100644 --- a/pkg/tfbridge/info.go +++ b/pkg/tfbridge/info.go @@ -133,6 +133,12 @@ type ProviderInfo struct { // // See https://github.com/pulumi/pulumi-terraform-bridge/issues/1501 XSkipDetailedDiffForChanges bool + + // Enables generation of a trimmed, runtime-only metadata file + // to help reduce resource plugin start time + // + // See also pulumi/pulumi-terraform-bridge#1524 + GenerateRuntimeMetadata bool } // Send logs or status logs to the user. diff --git a/pkg/tfbridge/metadata_test.go b/pkg/tfbridge/metadata_test.go new file mode 100644 index 0000000000..69284d0a41 --- /dev/null +++ b/pkg/tfbridge/metadata_test.go @@ -0,0 +1,34 @@ +package tfbridge + +import ( + "testing" + + md "github.com/pulumi/pulumi-terraform-bridge/v3/unstable/metadata" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestMetadataInfo(t *testing.T) { + data, err := md.New(nil) + require.NoError(t, err) + + err = md.Set(data, "hi", []string{"hello", "world"}) + require.NoError(t, err) + err = md.Set(data, "auto-aliasing", []string{"1", "2"}) + require.NoError(t, err) + err = md.Set(data, "mux", []string{"a", "b"}) + require.NoError(t, err) + + marshalled := data.Marshal() + require.Equal(t, `{"auto-aliasing":["1","2"],"hi":["hello","world"],"mux":["a","b"]}`, string(marshalled)) + + info := NewProviderMetadata(marshalled) + assert.Equal(t, "bridge-metadata.json", info.Path) + marshalledInfo := (*md.Data)(info.Data).Marshal() + assert.Equal(t, `{"auto-aliasing":["1","2"],"hi":["hello","world"],"mux":["a","b"]}`, string(marshalledInfo)) + + runtimeMetadata := info.ExtractRuntimeMetadata() + assert.Equal(t, "runtime-bridge-metadata.json", runtimeMetadata.Path) + runtimeMarshalled := (*md.Data)(runtimeMetadata.Data).Marshal() + assert.Equal(t, `{"auto-aliasing":["1","2"],"mux":["a","b"]}`, string(runtimeMarshalled)) +} diff --git a/pkg/tfgen/generate.go b/pkg/tfgen/generate.go index e15fbac99e..bcfb0fb8c8 100644 --- a/pkg/tfgen/generate.go +++ b/pkg/tfgen/generate.go @@ -947,7 +947,7 @@ func (g *Generator) UnstableGenerateFromSchema(genSchemaResult *GenerateSchemaRe if info := g.info.MetadataInfo; info != nil { files[info.Path] = (*metadata.Data)(info.Data).MarshalIndent() - if true { + if g.info.GenerateRuntimeMetadata { runtimeInfo := info.ExtractRuntimeMetadata() files[runtimeInfo.Path] = (*metadata.Data)(runtimeInfo.Data).Marshal() } diff --git a/unstable/metadata/metadata_test.go b/unstable/metadata/metadata_test.go index 02e1ac2cf6..b332e67dde 100644 --- a/unstable/metadata/metadata_test.go +++ b/unstable/metadata/metadata_test.go @@ -28,10 +28,34 @@ func TestMarshal(t *testing.T) { err = Set(data, "hi", []string{"hello", "world"}) assert.NoError(t, err) + marshalled := data.MarshalIndent() assert.Equal(t, `{ "hi": [ "hello", "world" ] -}`, string(data.MarshalIndent())) +}`, string(marshalled)) + + parsed, err := New(marshalled) + assert.NoError(t, err) + read, _, err := Get[[]string](parsed, "hi") + assert.NoError(t, err) + assert.Equal(t, []string{"hello", "world"}, read) +} + +func TestMarshalIndent(t *testing.T) { + data, err := New(nil) + require.NoError(t, err) + + err = Set(data, "hi", []string{"hello", "world"}) + assert.NoError(t, err) + + marshalled := data.Marshal() + assert.Equal(t, `{"hi":["hello","world"]}`, string(marshalled)) + + parsed, err := New(marshalled) + assert.NoError(t, err) + read, _, err := Get[[]string](parsed, "hi") + assert.NoError(t, err) + assert.Equal(t, []string{"hello", "world"}, read) }