Skip to content

Commit

Permalink
--no-edit
Browse files Browse the repository at this point in the history
  • Loading branch information
guineveresaenger committed Dec 9, 2024
1 parent 97a7b57 commit df6f314
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 24 deletions.
3 changes: 0 additions & 3 deletions dynamic/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
Expand Down
19 changes: 10 additions & 9 deletions dynamic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
17 changes: 14 additions & 3 deletions dynamic/parameterize/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
37 changes: 32 additions & 5 deletions dynamic/parameterize/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,46 @@ func TestParseArgs(t *testing.T) {
errMsg: autogold.Expect("expected to be parameterized by 1-3 arguments: <name> [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 {
Expand Down
7 changes: 4 additions & 3 deletions dynamic/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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},
},
},
})
Expand Down
3 changes: 2 additions & 1 deletion pkg/tfgen/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit df6f314

Please sign in to comment.