Skip to content

Commit

Permalink
Pass a local docs path to the local provider gen
Browse files Browse the repository at this point in the history
  • Loading branch information
guineveresaenger committed Nov 28, 2024
1 parent a637a52 commit 064910c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 30 deletions.
2 changes: 1 addition & 1 deletion dynamic/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func providerInfo(ctx context.Context, p run.Provider, value parameterize.Value)
},
}
// Add presumed best-effort GitHub org to the provider info.
// We do not yet handle full docsgen for a local dynamic provider so we do not set the GitHubOrg field in that case.
// We do not set the GitHubOrg field for a local dynamic provider.
if value.Remote != nil {
// https://github.com/opentofu/registry/issues/1337:
// Due to discrepancies in the registry protocol/implementation,
Expand Down
62 changes: 34 additions & 28 deletions dynamic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/blang/semver"
"github.com/opentofu/opentofu/shim/run"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag/colors"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"os"
"os/exec"

"github.com/blang/semver"
"github.com/opentofu/opentofu/shim/run"
"github.com/pulumi/pulumi-terraform-bridge/dynamic/parameterize"
"github.com/pulumi/pulumi-terraform-bridge/dynamic/version"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/proto"
pfbridge "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/tfbridge"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge/info"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfgen"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag/colors"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
)

const (
Expand Down Expand Up @@ -65,19 +65,12 @@ func initialSetup() (info.Provider, pfbridge.ProviderMetadata, func() error) {
var fullDocs bool
metadata = pfbridge.ProviderMetadata{
XGetSchema: func(ctx context.Context, req plugin.GetSchemaRequest) ([]byte, error) {
// By default, only read in docs from TF schema
var schemaDocsOnly = true
// If full docs is expected set schemaDocsOnly to false
if fullDocs {
schemaDocsOnly = false
}

packageSchema, err := tfgen.GenerateSchemaWithOptions(tfgen.GenerateSchemaOptions{
ProviderInfo: info,
DiagnosticsSink: diag.DefaultSink(os.Stdout, os.Stderr, diag.FormatOptions{
Color: colors.Always,
}),
XInMemoryDocs: schemaDocsOnly,
XInMemoryDocs: !fullDocs,
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -154,23 +147,36 @@ func initialSetup() (info.Provider, pfbridge.ProviderMetadata, func() error) {
if err != nil {
return plugin.ParameterizeResponse{}, err
}
fullDocs = args.Remote.Docs
if fullDocs {
// Write the upstream files at this version to a temporary directory
tmpDir, err := os.MkdirTemp("", "upstreamRepoDir")
if err != nil {
return plugin.ParameterizeResponse{}, err

if args.Remote != nil {
fullDocs = args.Remote.Docs
if fullDocs {
// Write the upstream files at this version to a temporary directory
tmpDir, err := os.MkdirTemp("", "upstreamRepoDir")
if err != nil {
return plugin.ParameterizeResponse{}, err
}
versionWithPrefix := "v" + info.Version
ghRepo := "https://github.com/" + info.GitHubOrg + "/terraform-provider-" + info.Name

cmd := exec.Command(
"git", "clone", "--depth", "1", "-b", versionWithPrefix, ghRepo, tmpDir,
)
err = cmd.Run()
if err != nil {
return plugin.ParameterizeResponse{}, err
}
info.UpstreamRepoPath = tmpDir
}
versionWithPrefix := "v" + info.Version
ghRepo := "https://github.com/" + info.GitHubOrg + "/terraform-provider-" + info.Name
}

cmd := exec.Command("git", "clone", "--depth", "1", "-b", versionWithPrefix, ghRepo, tmpDir)
err = cmd.Run()
if err != nil {
return plugin.ParameterizeResponse{}, err
if args.Local != nil {
if args.Local.DocsLocation != "" {
info.UpstreamRepoPath = args.Local.DocsLocation
fullDocs = true
}
info.UpstreamRepoPath = tmpDir
}

return plugin.ParameterizeResponse{
Name: p.Name(),
Version: v,
Expand Down
10 changes: 9 additions & 1 deletion dynamic/parameterize/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,22 @@ type RemoteArgs struct {
type LocalArgs struct {
// Path is the path to the provider binary. It can be relative or absolute.
Path string
// DocsLocation is the path to the provider documentation.
DocsLocation string
}

func ParseArgs(args []string) (Args, error) {
// Check for a leading '.' or '/' to indicate a path

if len(args) >= 1 &&
(strings.HasPrefix(args[0], "./") || strings.HasPrefix(args[0], "/")) {
if len(args) > 1 {
return Args{}, fmt.Errorf("path based providers are only parameterized by 1 argument: <path>")
docsArg := args[1]
docsLocation, found := strings.CutPrefix(docsArg, "docsLocation=")
if !found {
return Args{}, fmt.Errorf("path based providers are only parameterized by 2 arguments: <path> [docsLocation]")
}
return Args{Local: &LocalArgs{Path: args[0], DocsLocation: docsLocation}}, nil
}
return Args{Local: &LocalArgs{Path: args[0]}}, nil
}
Expand Down
17 changes: 17 additions & 0 deletions dynamic/parameterize/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ func TestParseArgs(t *testing.T) {
args: []string{"./my-provider"},
expect: Args{Local: &LocalArgs{Path: "./my-provider"}},
},
{
name: "local too many args",
args: []string{"./my-provider", "nonsense"},
errMsg: autogold.Expect(
"path based providers are only parameterized by 2 arguments: <path> [docsLocation]",
),
},
{
name: "local with docs location",
args: []string{"./my-provider", "docsLocation=./my-provider"},
expect: Args{
Local: &LocalArgs{
Path: "./my-provider",
DocsLocation: "./my-provider",
},
},
},
{
name: "remote",
args: []string{"my-registry.io/typ"},
Expand Down

0 comments on commit 064910c

Please sign in to comment.