From df6f314170020c360181d2f87ca9b78742dda359 Mon Sep 17 00:00:00 2001 From: guineveresaenger Date: Mon, 9 Dec 2024 11:37:06 -0800 Subject: [PATCH] --no-edit --- dynamic/info.go | 3 --- dynamic/main.go | 19 ++++++++-------- dynamic/parameterize/args.go | 17 +++++++++++--- dynamic/parameterize/args_test.go | 37 ++++++++++++++++++++++++++----- dynamic/provider_test.go | 7 +++--- pkg/tfgen/docs.go | 3 ++- 6 files changed, 62 insertions(+), 24 deletions(-) diff --git a/dynamic/info.go b/dynamic/info.go index da337b604..1fa33e357 100644 --- a/dynamic/info.go +++ b/dynamic/info.go @@ -42,9 +42,6 @@ func providerInfo(ctx context.Context, p run.Provider, value parameterize.Value) Publisher: "Pulumi", ResourcePrefix: inferResourcePrefix(provider), - // To avoid bogging down schema generation speed, we skip all examples. - SkipExamples: func(tfbridge.SkipExamplesArgs) bool { return false }, - MetadataInfo: &tfbridge.MetadataInfo{ Path: "", Data: tfbridge.ProviderMetadata(nil), }, diff --git a/dynamic/main.go b/dynamic/main.go index 61e1750de..0a97b970f 100644 --- a/dynamic/main.go +++ b/dynamic/main.go @@ -69,7 +69,7 @@ func initialSetup() (info.Provider, pfbridge.ProviderMetadata, func() error) { var fullDocs bool metadata = pfbridge.ProviderMetadata{ XGetSchema: func(ctx context.Context, req plugin.GetSchemaRequest) ([]byte, error) { - // Create a custom generator for schema and, of fullDocs is set, examples + // Create a custom generator for schema. Examples will only be generated if `fullDocs` is set. g, err := tfgen.NewGenerator(tfgen.GeneratorOptions{ Package: info.Name, Version: info.Version, @@ -93,6 +93,7 @@ func initialSetup() (info.Provider, pfbridge.ProviderMetadata, func() error) { if info.SchemaPostProcessor != nil { info.SchemaPostProcessor(&packageSchema.PackageSpec) } + return json.Marshal(packageSchema.PackageSpec) }, XParamaterize: func(ctx context.Context, req plugin.ParameterizeRequest) (plugin.ParameterizeResponse, error) { @@ -161,7 +162,14 @@ func initialSetup() (info.Provider, pfbridge.ProviderMetadata, func() error) { return plugin.ParameterizeResponse{}, err } - if args.Remote != nil { + switch args.Remote { + case nil: + // We're using local args. + if args.Local.UpstreamRepoPath != "" { + info.UpstreamRepoPath = args.Local.UpstreamRepoPath + fullDocs = true + } + default: fullDocs = args.Remote.Docs if fullDocs { // Write the upstream files at this version to a temporary directory @@ -181,13 +189,6 @@ func initialSetup() (info.Provider, pfbridge.ProviderMetadata, func() error) { } } - if args.Local != nil { - if args.Local.UpstreamRepoPath != "" { - info.UpstreamRepoPath = args.Local.UpstreamRepoPath - fullDocs = true - } - } - return plugin.ParameterizeResponse{ Name: p.Name(), Version: v, diff --git a/dynamic/parameterize/args.go b/dynamic/parameterize/args.go index b1444f91a..a0f22a3af 100644 --- a/dynamic/parameterize/args.go +++ b/dynamic/parameterize/args.go @@ -76,11 +76,22 @@ func ParseArgs(args []string) (Args, error) { // The third argument, if any, is the full docs option for when we need to generate docs case 3: docsArg := args[2] - if docsArg == "fullDocs" { + errMsg := "expected third parameterized argument to be 'fullDocs=true/false' or be empty" + + fullDocs, found := strings.CutPrefix(docsArg, "fullDocs=") + if !found { + return Args{}, fmt.Errorf("%s", errMsg) + } + + switch fullDocs { + case "true": remote.Docs = true - } else { - return Args{}, fmt.Errorf("expected third parameterized argument to be 'fullDocs' or empty") + case "false": + // Do nothing + default: + return Args{}, fmt.Errorf("%s", errMsg) } + fallthrough // The second argument, if any is the version case 2: diff --git a/dynamic/parameterize/args_test.go b/dynamic/parameterize/args_test.go index 409855a8c..fcd679cf9 100644 --- a/dynamic/parameterize/args_test.go +++ b/dynamic/parameterize/args_test.go @@ -83,19 +83,46 @@ func TestParseArgs(t *testing.T) { errMsg: autogold.Expect("expected to be parameterized by 1-3 arguments: [version] [fullDocs]"), }, { - name: "invalid third arg", - args: []string{"arg1", "arg2", "arg3"}, - errMsg: autogold.Expect("expected third parameterized argument to be 'fullDocs' or empty"), + name: "invalid third arg", + args: []string{"arg1", "arg2", "arg3"}, + errMsg: autogold.Expect( + "expected third parameterized argument to be 'fullDocs=true/false' or be empty", + ), + }, + { + name: "empty third arg", + args: []string{"arg1", "arg2"}, + expect: Args{Remote: &RemoteArgs{ + Name: "arg1", + Version: "arg2", + Docs: false, + }}, }, { - name: "valid third arg", - args: []string{"my-registry.io/typ", "1.2.3", "fullDocs"}, + name: "valid third arg true", + args: []string{"my-registry.io/typ", "1.2.3", "fullDocs=true"}, expect: Args{Remote: &RemoteArgs{ Name: "my-registry.io/typ", Version: "1.2.3", Docs: true, }}, }, + { + name: "valid third arg false", + args: []string{"my-registry.io/typ", "1.2.3", "fullDocs=false"}, + expect: Args{Remote: &RemoteArgs{ + Name: "my-registry.io/typ", + Version: "1.2.3", + Docs: false, + }}, + }, + { + name: "third arg invalid input", + args: []string{"my-registry.io/typ", "1.2.3", "fullDocs=invalid-input"}, + errMsg: autogold.Expect( + "expected third parameterized argument to be 'fullDocs=true/false' or be empty", + ), + }, } for _, tt := range tests { diff --git a/dynamic/provider_test.go b/dynamic/provider_test.go index e276f0b48..41753566c 100644 --- a/dynamic/provider_test.go +++ b/dynamic/provider_test.go @@ -462,14 +462,15 @@ func TestSchemaGenerationFullDocs(t *testing.T) { //nolint:paralleltest type testCase struct { name string version string - fullDocs bool + fullDocs string } tc := testCase{ name: "hashicorp/random", version: "3.6.3", - fullDocs: true, + fullDocs: "fullDocs=true", } + t.Run(strings.Join([]string{tc.name, tc.version}, "-"), func(t *testing.T) { helper.Integration(t) ctx := context.Background() @@ -479,7 +480,7 @@ func TestSchemaGenerationFullDocs(t *testing.T) { //nolint:paralleltest result, err := server.Parameterize(ctx, &pulumirpc.ParameterizeRequest{ Parameters: &pulumirpc.ParameterizeRequest_Args{ Args: &pulumirpc.ParameterizeRequest_ParametersArgs{ - Args: []string{tc.name, tc.version, "fullDocs"}, + Args: []string{tc.name, tc.version, tc.fullDocs}, }, }, }) diff --git a/pkg/tfgen/docs.go b/pkg/tfgen/docs.go index b2166a1a7..2977e06f4 100644 --- a/pkg/tfgen/docs.go +++ b/pkg/tfgen/docs.go @@ -186,7 +186,6 @@ func getDocsForResource(g *Generator, source DocsSource, kind DocKind, if g.skipDocs { return entityDocs{}, nil } - var docInfo *tfbridge.DocInfo if info != nil { docInfo = info.GetDocs() @@ -209,6 +208,7 @@ func getDocsForResource(g *Generator, source DocsSource, kind DocKind, docFile = nil } } + if err != nil { return entityDocs{}, fmt.Errorf("get docs for token %s: %w", rawname, err) } @@ -1457,6 +1457,7 @@ func (g *Generator) convertExamples(docs string, path examplePath) string { if docs == "" { return "" } + if g.info.SkipExamples != nil { if g.info.SkipExamples(tfbridge.SkipExamplesArgs{ Token: path.Token(),