Skip to content

Commit a732b60

Browse files
committed
make hostname and token optional, read token from cli file
1 parent 6c283cb commit a732b60

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

internal/cloud/backend.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ func (b *Cloud) Configure(obj cty.Value) tfdiags.Diagnostics {
296296
// Get the token from the CLI Config File in the credentials section
297297
// if no token was set in the configuration
298298
if token == "" {
299-
token, err = cliConfigToken(hostname, b.services)
299+
token, err = CliConfigToken(hostname, b.services)
300300
if err != nil {
301301
diags = diags.Append(tfdiags.AttributeValue(
302302
tfdiags.Error,
@@ -593,10 +593,10 @@ func resolveCloudConfig(obj cty.Value) (cloudConfig, tfdiags.Diagnostics) {
593593
return ret, diags
594594
}
595595

596-
// cliConfigToken returns the token for this host as configured in the credentials
596+
// CliConfigToken returns the token for this host as configured in the credentials
597597
// section of the CLI Config File. If no token was configured, an empty
598598
// string will be returned instead.
599-
func cliConfigToken(hostname svchost.Hostname, services *disco.Disco) (string, error) {
599+
func CliConfigToken(hostname svchost.Hostname, services *disco.Disco) (string, error) {
600600
creds, err := services.CredentialsForHost(hostname)
601601
if err != nil {
602602
log.Printf("[WARN] Failed to get credentials for %s: %s (ignoring)", hostname.ForDisplay(), err)

internal/cloud/test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func (runner *TestSuiteRunner) client(addr tfaddr.Module, id tfe.RegistryModuleI
354354
return nil, nil, diags
355355
}
356356

357-
token, err := cliConfigToken(addr.Package.Host, runner.Services)
357+
token, err := CliConfigToken(addr.Package.Host, runner.Services)
358358
if err != nil {
359359
diags = diags.Append(tfdiags.AttributeValue(
360360
tfdiags.Error,

internal/command/stacks.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ const (
5959
// The stacks plugin release download service that the BinaryManager relies
6060
// on to fetch the plugin.
6161
stackspluginServiceID = "stacksplugin.v1"
62+
63+
defaultHostname = "app.terraform.io"
6264
)
6365

6466
var (
@@ -165,20 +167,8 @@ func (c *StacksCommand) discoverAndConfigure() tfdiags.Diagnostics {
165167

166168
displayHostname := os.Getenv("TF_STACKS_HOSTNAME")
167169
if strings.TrimSpace(displayHostname) == "" {
168-
return diags.Append(tfdiags.Sourceless(
169-
tfdiags.Error,
170-
"TF_STACKS_HOSTNAME is not set",
171-
"TF_STACKS_HOSTNAME must be set to the hostname of the HCP Terraform instance",
172-
))
173-
}
174-
175-
token := os.Getenv("TF_STACKS_TOKEN")
176-
if strings.TrimSpace(token) == "" {
177-
return diags.Append(tfdiags.Sourceless(
178-
tfdiags.Error,
179-
"TF_STACKS_TOKEN is not set",
180-
"TF_STACKS_TOKEN must be set to the token of the HCP Terraform instance",
181-
))
170+
log.Printf("[TRACE] stacksplugin hostname not set, falling back to %q", defaultHostname)
171+
displayHostname = defaultHostname
182172
}
183173

184174
hostname, err := svchost.ForComparison(displayHostname)
@@ -208,6 +198,20 @@ func (c *StacksCommand) discoverAndConfigure() tfdiags.Diagnostics {
208198
// The discovery request worked, so cache the full results.
209199
cb.ServicesHost = host
210200

201+
token := os.Getenv("TF_STACKS_TOKEN")
202+
if strings.TrimSpace(token) == "" {
203+
// attempt to read from the credentials file
204+
token, err = cloud.CliConfigToken(hostname, cb.Services())
205+
if err != nil {
206+
// some commands like stacks init and validate could be run without a token so allow it without errors
207+
diags.Append(tfdiags.Sourceless(
208+
tfdiags.Warning,
209+
"Could not read token from credentials file, proceeding without a token",
210+
err.Error(),
211+
))
212+
}
213+
}
214+
211215
// re-use the cached service discovery info for this TFC
212216
// instance to find our plugin service and TFE API URLs:
213217
pluginService, err := cb.ServicesHost.ServiceURL(stackspluginServiceID)
@@ -244,6 +248,7 @@ func (c *StacksCommand) discoverAndConfigure() tfdiags.Diagnostics {
244248
OrganizationName: orgName,
245249
ProjectName: projectName,
246250
StackName: stackName,
251+
TerminalWidth: c.Meta.Streams.Stdout.Columns(),
247252
}
248253

249254
return diags
@@ -346,6 +351,7 @@ type StacksPluginConfig struct {
346351
OrganizationName string `md:"tfc-organization"`
347352
ProjectName string `md:"tfc-project"`
348353
StackName string `md:"tfc-stack"`
354+
TerminalWidth int `md:"terminal-width"`
349355
}
350356

351357
func (c StacksPluginConfig) ToMetadata() metadata.MD {
@@ -358,6 +364,7 @@ func (c StacksPluginConfig) ToMetadata() metadata.MD {
358364
"tfc-organization", c.OrganizationName,
359365
"tfc-project", c.ProjectName,
360366
"tfc-stack", c.StackName,
367+
"terminal-width", fmt.Sprintf("%d", c.TerminalWidth),
361368
)
362369
return md
363370
}

internal/command/stacks_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func TestStacksPluginConfig_ToMetadata(t *testing.T) {
2020
"tfc-project", "example-project",
2121
"tfc-stack", "example-stack",
2222
"terraform-binary-path", "",
23+
"terminal-width", "78",
2324
)
2425
inputStruct := StacksPluginConfig{
2526
Address: "https://app.staging.terraform.io",
@@ -30,6 +31,7 @@ func TestStacksPluginConfig_ToMetadata(t *testing.T) {
3031
ProjectName: "example-project",
3132
StackName: "example-stack",
3233
TerraformBinaryPath: "",
34+
TerminalWidth: 78,
3335
}
3436
result := inputStruct.ToMetadata()
3537
if !reflect.DeepEqual(expected, result) {

0 commit comments

Comments
 (0)