Skip to content

Tf 34724 terraform stacks cli should be easier to use with non standard hosts tfe#38896

Open
lakshyaa-nathan-15 wants to merge 4 commits into
mainfrom
TF-34724-terraform-stacks-cli-should-be-easier-to-use-with-non-standard-hosts-tfe
Open

Tf 34724 terraform stacks cli should be easier to use with non standard hosts tfe#38896
lakshyaa-nathan-15 wants to merge 4 commits into
mainfrom
TF-34724-terraform-stacks-cli-should-be-easier-to-use-with-non-standard-hosts-tfe

Conversation

@lakshyaa-nathan-15

@lakshyaa-nathan-15 lakshyaa-nathan-15 commented Jul 20, 2026

Copy link
Copy Markdown

Summary

Improves hostname resolution for the Stacks plugin by automatically inferring the target hostname from the local credentials file (credentials.tfrc.json) when no explicit hostname environment variable is set.

Previously, the StacksCommand only checked the TF_STACKS_HOSTNAME environment variable and fell back to the default hostname (app.terraform.io) if it was unset. This meant users running Terraform Enterprise (TFE) instances needed to always remember to set the environment variable explicitly.

Changes

The new resolution logic follows this priority order:

  1. TF_STACKS_HOSTNAME - used directly if set.

  2. TF_CLOUD_HOSTNAME - used if set and TF_STACKS_HOSTNAME is not

  3. Check for credentials file override

    a. Single entry in credentials.tfrc.json - if exactly one hostname is present in the credentials file, it is used automatically with a warning message

    b. Single entry in custom credentials file - if exactly one hostname is present in the credentials file, it is used automatically with a warning message

  4. Multiple entries in credentials file - an error diagnostic is returned because the intended host is ambiguous; the user must set TF_STACKS_HOSTNAME or TF_CLOUD_HOSTNAME to disambiguate.

  5. Default hostname - falls back to app.terraform.io when no credentials are present.

Additional changes:

  • Added ReadCredentialHostsInOrder (and internal readCredentialHostsInOrderFile) to internal/command/cliconfig. This function parses the credentials file using a JSON decoder so that the original declaration order of hostnames is preserved
  • Improved the error message for hostname discovery failure to include a hint about setting TF_STACKS_HOSTNAME or TF_CLOUD_HOSTNAME.

Testing

Added/updated tests covering:

  • TF_STACKS_HOSTNAME set explicitly
  • TF_CLOUD_HOSTNAME fallback
  • Credentials file override
  • Single credential entry auto-resolution with warning
  • Multiple credential entries returning ambiguity error
  • Default hostname fallback when no credentials are present
  • ReadCredentialHostsInOrder preserving declaration order

Target Release

1.16.x

Rollback Plan

  • If a change needs to be reverted, we will roll out an update to the code within 7 days.

Changes to Security Controls

Are there any changes to security controls (access controls, encryption, logging) in this pull request? If so, explain.

CHANGELOG entry

  • This change is user-facing and I added a changelog entry.
  • This change is not user-facing.

Created ReadCredentialHostsInOrder to return hostnames from credentials.tfrc.json in file order instead of map order.  Added focused tests for missing/empty files, single and multiple hosts, ordering guarantees, and invalid-host filtering.
Adds hostname resolution logic for stacks that prefers TF_STACKS_HOSTNAME, then TF_CLOUD_HOSTNAME, then infers from the credentials file when there is exactly one host.
@lakshyaa-nathan-15
lakshyaa-nathan-15 requested a review from a team as a code owner July 20, 2026 19:43
@hashicorp-cla-app

hashicorp-cla-app Bot commented Jul 20, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Comment thread internal/command/stacks.go Outdated
tfdiags.Warning,
"No hostname environment variable set, using hostname from credentials file",
fmt.Sprintf("No hostname environment variable set. Using %q from your credentials file. Set TF_STACKS_HOSTNAME or TF_CLOUD_HOSTNAME to override.", displayHostname),
))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually don't think we need the warning here, only when there are more than 1. I think we can think of a single entry as the happy path 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants