Skip to content

Commit

Permalink
schemas: Account for tfexec.SetEnv() behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Oct 3, 2022
1 parent 014bfa8 commit afe08e4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions internal/schemas/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
gen-workspace
tf-plugin-cache
data/*
!data/git.keep
31 changes: 30 additions & 1 deletion internal/schemas/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func gen() error {
if err != nil {
return err
}
log.Printf("installed Terraform %s", coreVersion)
log.Printf("using Terraform %s (%s)", coreVersion, execPath)

workspacePath, err := filepath.Abs("gen-workspace")
if err != nil {
Expand Down Expand Up @@ -158,6 +158,16 @@ func gen() error {
}
}

cacheDirPath, err := filepath.Abs("tf-plugin-cache")
if err != nil {
return err
}
err = os.MkdirAll(cacheDirPath, 0755)
if err != nil {
return err
}
log.Printf("Terraform plugin cache will be stored at %s", cacheDirPath)

// install each provider and obtain schema for it
var wg sync.WaitGroup
for _, p := range providers {
Expand All @@ -170,6 +180,7 @@ func gen() error {
TerraformExecPath: execPath,
WorkspacePath: workspacePath,
DataDirPath: dataDirPath,
CacheDirPath: cacheDirPath,
CoreVersion: coreVersion,
Provider: p,
})
Expand All @@ -192,6 +203,7 @@ type Inputs struct {
TerraformExecPath string
WorkspacePath string
DataDirPath string
CacheDirPath string
CoreVersion *version.Version
Provider Provider
}
Expand Down Expand Up @@ -284,6 +296,23 @@ func schemaForProvider(ctx context.Context, client registry.Client, input Inputs
return nil, err
}

// See https://github.com/hashicorp/terraform-exec/issues/337
// Terraform would refuse to init any provider otherwise
// and some providers refuse to give schemas or break
// the gRPC protocol for some mysterious reason
env := make(map[string]string, 0)
for _, rawKeyPair := range os.Environ() {
parts := strings.Split(rawKeyPair, "=")
env[parts[0]] = os.Getenv(parts[0])
}
// This is to help keep paths short, esp. on Windows
// (260 characters by default)
// See https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#maximum-path-length-limitation
// and also to avoid embedding the provider binaries
env["TF_PLUGIN_CACHE_DIR"] = input.CacheDirPath

tf.SetEnv(env)

var initElapsed time.Duration
if !input.Provider.Addr.IsBuiltIn() {
initElapsed, err = retryInit(ctx, tf, input.Provider.Addr.ForDisplay(), 0)
Expand Down

0 comments on commit afe08e4

Please sign in to comment.