Merge pull request #30748 from hashicorp/sebasslash/add-tf-hostname-env-var

Add TF_HOSTNAME env var support
This commit is contained in:
Sebastian Rivera 2022-03-30 15:29:05 -04:00 committed by GitHub
commit 910966ccbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 1 deletions

View File

@ -12,6 +12,8 @@ ENHANCEMENTS:
* Error messages for preconditions, postconditions, and custom variable validations are now evaluated as expressions, allowing interpolation of relevant values into the output. ([#30613](https://github.com/hashicorp/terraform/issues/30613))
* There are some small improvements to the error and warning messages Terraform will emit in the case of invalid provider configuration passing between modules. There are no changes to which situations will produce errors and warnings, but the messages now include additional information intended to clarify what problem Terraform is describing and how to address it. ([#30639](https://github.com/hashicorp/terraform/issues/30639))
* When running `terraform plan`, only show external changes which may have contributed to the current plan ([#30486](https://github.com/hashicorp/terraform/issues/30486))
* Add `TF_ORGANIZATION` environment variable fallback for `organization` in the cloud configuration
* Add `TF_HOSTNAME` environment variable fallback for `hostname` in the cloud configuration
BUG FIXES:

View File

@ -341,9 +341,10 @@ func (b *Cloud) setConfigurationFields(obj cty.Value) tfdiags.Diagnostics {
var diags tfdiags.Diagnostics
// Get the hostname.
b.hostname = os.Getenv("TF_HOSTNAME")
if val := obj.GetAttr("hostname"); !val.IsNull() && val.AsString() != "" {
b.hostname = val.AsString()
} else {
} else if b.hostname == "" {
b.hostname = defaultHostname
}

View File

@ -201,6 +201,7 @@ func TestCloud_configWithEnvVars(t *testing.T) {
config cty.Value
vars map[string]string
expectedOrganization string
expectedHostname string
}{
"with no organization specified": {
config: cty.ObjectVal(map[string]cty.Value{
@ -232,6 +233,36 @@ func TestCloud_configWithEnvVars(t *testing.T) {
},
expectedOrganization: "hashicorp",
},
"with no hostname specified": {
config: cty.ObjectVal(map[string]cty.Value{
"hostname": cty.NullVal(cty.String),
"token": cty.NullVal(cty.String),
"organization": cty.StringVal("hashicorp"),
"workspaces": cty.ObjectVal(map[string]cty.Value{
"name": cty.StringVal("prod"),
"tags": cty.NullVal(cty.Set(cty.String)),
}),
}),
vars: map[string]string{
"TF_HOSTNAME": "app.terraform.io",
},
expectedHostname: "app.terraform.io",
},
"with hostname and env var specified": {
config: cty.ObjectVal(map[string]cty.Value{
"hostname": cty.StringVal("app.terraform.io"),
"token": cty.NullVal(cty.String),
"organization": cty.StringVal("hashicorp"),
"workspaces": cty.ObjectVal(map[string]cty.Value{
"name": cty.StringVal("prod"),
"tags": cty.NullVal(cty.Set(cty.String)),
}),
}),
vars: map[string]string{
"TF_HOSTNAME": "mycool.tfe-host.io",
},
expectedHostname: "app.terraform.io",
},
}
for name, tc := range cases {
@ -256,6 +287,10 @@ func TestCloud_configWithEnvVars(t *testing.T) {
if tc.expectedOrganization != "" && tc.expectedOrganization != b.organization {
t.Fatalf("%s: organization not valid: %s, expected: %s", name, b.organization, tc.expectedOrganization)
}
if tc.expectedHostname != "" && tc.expectedHostname != b.hostname {
t.Fatalf("%s: hostname not valid: %s, expected: %s", name, b.hostname, tc.expectedHostname)
}
}
}

View File

@ -89,6 +89,8 @@ Alternatively, the `cloud` block can be configured with the following environmen
- `TF_ORGANIZATION` - The name of the organization. Serves as a fallback for `organization`
in the cloud configuration. If both are specified, the configuration takes precedence.
- `TF_HOSTNAME` - The hostname of a Terraform Enterprise installation. Serves as a fallback if `hostname` is not specified in the cloud configuration. If both are specified, the configuration takes precendence.
## Excluding Files from Upload with .terraformignore
When executing a remote `plan` or `apply` in a [CLI-driven run](/cloud-docs/run/cli),