From 179154477e1384db3e0bb8bc3aa3e3ee653eb334 Mon Sep 17 00:00:00 2001 From: Gleb Kanterov Date: Fri, 7 Jul 2023 13:20:37 +0200 Subject: [PATCH] Propagate TF_CLI_CONFIG_FILE env variable (#555) ## Changes Propagate `TF_CLI_CONFIG_FILE` env variable. From Terraform documentation: > The location of the Terraform CLI configuration file can also be specified using the TF_CLI_CONFIG_FILE [environment variable](https://developer.hashicorp.com/terraform/cli/config/environment-variables) It allows using custom builds of terraform-provider-databricks, using config files like: ```tf provider_installation { dev_overrides { "databricks/databricks" = "/Users/gleb.kanterov/terraform-provider-databricks" } direct {} } ``` ## Tests I added unit tests. --- bundle/deploy/terraform/init.go | 24 ++++++++++++++++++++---- bundle/deploy/terraform/init_test.go | 16 ++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/bundle/deploy/terraform/init.go b/bundle/deploy/terraform/init.go index 0ce15acb..eb3e99d1 100644 --- a/bundle/deploy/terraform/init.go +++ b/bundle/deploy/terraform/init.go @@ -70,6 +70,23 @@ func (m *initialize) findExecPath(ctx context.Context, b *bundle.Bundle, tf *con return tf.ExecPath, nil } +// This function inherits some environment variables for Terraform CLI. +func inheritEnvVars(env map[string]string) error { + // Include $HOME in set of environment variables to pass along. + home, ok := os.LookupEnv("HOME") + if ok { + env["HOME"] = home + } + + // Include $TF_CLI_CONFIG_FILE to override terraform provider in development. + configFile, ok := os.LookupEnv("TF_CLI_CONFIG_FILE") + if ok { + env["TF_CLI_CONFIG_FILE"] = configFile + } + + return nil +} + // This function sets temp dir location for terraform to use. If user does not // specify anything here, we fall back to a `tmp` directory in the bundle's cache // directory @@ -145,10 +162,9 @@ func (m *initialize) Apply(ctx context.Context, b *bundle.Bundle) error { return err } - // Include $HOME in set of environment variables to pass along. - home, ok := os.LookupEnv("HOME") - if ok { - env["HOME"] = home + err = inheritEnvVars(env) + if err != nil { + return err } // Set the temporary directory environment variables diff --git a/bundle/deploy/terraform/init_test.go b/bundle/deploy/terraform/init_test.go index aafe5660..79e18170 100644 --- a/bundle/deploy/terraform/init_test.go +++ b/bundle/deploy/terraform/init_test.go @@ -272,3 +272,19 @@ func TestSetProxyEnvVars(t *testing.T) { require.NoError(t, err) assert.ElementsMatch(t, []string{"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY"}, maps.Keys(env)) } + +func TestInheritEnvVars(t *testing.T) { + env := map[string]string{} + + t.Setenv("HOME", "/home/testuser") + t.Setenv("TF_CLI_CONFIG_FILE", "/tmp/config.tfrc") + + err := inheritEnvVars(env) + + require.NoError(t, err) + + require.Equal(t, map[string]string{ + "HOME": "/home/testuser", + "TF_CLI_CONFIG_FILE": "/tmp/config.tfrc", + }, env) +}