Skip to content

Commit

Permalink
Propagate TF_CLI_CONFIG_FILE env variable (#555)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
kanterov authored Jul 7, 2023
1 parent b14920c commit 1791544
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
24 changes: 20 additions & 4 deletions bundle/deploy/terraform/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions bundle/deploy/terraform/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 1791544

Please sign in to comment.