From b1c22d333e006579cb6317ac9095727bf132a33f Mon Sep 17 00:00:00 2001
From: Sebastian Rivera <sebastian.rivera@hashicorp.com>
Date: Mon, 28 Mar 2022 14:00:10 -0400
Subject: [PATCH] Fix error formatting for missing env and config value

We previously used to throw an error denoting where in the configuration the attribute was missing or invalid.
Considering that organization can be now be omitted from the configuration, our previous error message will be
improperly formatted. This commit also updates the message to mention `TF_ORGANIZATION` as a valid substitute if
organization is missing or invalid in the configuration.
---
 internal/cloud/backend.go      |  2 +-
 internal/cloud/backend_test.go |  4 ++--
 internal/cloud/errors.go       | 16 +++++++++-------
 3 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/internal/cloud/backend.go b/internal/cloud/backend.go
index 65e1bcd15f..1d156410c0 100644
--- a/internal/cloud/backend.go
+++ b/internal/cloud/backend.go
@@ -157,7 +157,7 @@ func (b *Cloud) PrepareConfig(obj cty.Value) (cty.Value, tfdiags.Diagnostics) {
 		// organization is specified in the config but is invalid, so
 		// we'll fallback on TF_ORGANIZATION
 		if val := os.Getenv("TF_ORGANIZATION"); val == "" {
-			diags = diags.Append(invalidOrganizationConfigMissingValue)
+			diags = diags.Append(missingConfigAttributeAndEnvVar("organization", "TF_ORGANIZATION"))
 		}
 	}
 
diff --git a/internal/cloud/backend_test.go b/internal/cloud/backend_test.go
index 8bc1518ee3..2bb167f73c 100644
--- a/internal/cloud/backend_test.go
+++ b/internal/cloud/backend_test.go
@@ -86,7 +86,7 @@ func TestCloud_PrepareConfig(t *testing.T) {
 					"tags": cty.NullVal(cty.Set(cty.String)),
 				}),
 			}),
-			expectedErr: `Invalid organization value: The "organization" attribute value must not be empty.`,
+			expectedErr: `Invalid or missing required argument: "organization" must be set in the cloud configuration or as an environment variable: TF_ORGANIZATION.`,
 		},
 		"null workspace": {
 			config: cty.ObjectVal(map[string]cty.Value{
@@ -173,7 +173,7 @@ func TestCloud_PrepareConfigWithEnvVars(t *testing.T) {
 				}),
 			}),
 			vars:        map[string]string{},
-			expectedErr: `Invalid organization value: The "organization" attribute value must not be empty.`,
+			expectedErr: `Invalid or missing required argument: "organization" must be set in the cloud configuration or as an environment variable: TF_ORGANIZATION.`,
 		},
 	}
 
diff --git a/internal/cloud/errors.go b/internal/cloud/errors.go
index 58494f70eb..f387a37538 100644
--- a/internal/cloud/errors.go
+++ b/internal/cloud/errors.go
@@ -9,13 +9,6 @@ import (
 )
 
 var (
-	invalidOrganizationConfigMissingValue = tfdiags.AttributeValue(
-		tfdiags.Error,
-		"Invalid organization value",
-		`The "organization" attribute value must not be empty.\n\n%s`,
-		cty.Path{cty.GetAttrStep{Name: "organization"}},
-	)
-
 	invalidWorkspaceConfigMissingValues = tfdiags.AttributeValue(
 		tfdiags.Error,
 		"Invalid workspaces configuration",
@@ -33,6 +26,15 @@ var (
 
 const ignoreRemoteVersionHelp = "If you're sure you want to upgrade the state, you can force Terraform to continue using the -ignore-remote-version flag. This may result in an unusable workspace."
 
+func missingConfigAttributeAndEnvVar(attribute string, envVar string) tfdiags.Diagnostic {
+	detail := strings.TrimSpace(fmt.Sprintf("\"%s\" must be set in the cloud configuration or as an environment variable: %s.\n", attribute, envVar))
+	return tfdiags.AttributeValue(
+		tfdiags.Error,
+		"Invalid or missing required argument",
+		detail,
+		cty.Path{cty.GetAttrStep{Name: attribute}})
+}
+
 func incompatibleWorkspaceTerraformVersion(message string, ignoreVersionConflict bool) tfdiags.Diagnostic {
 	severity := tfdiags.Error
 	suggestion := ignoreRemoteVersionHelp
-- 
GitLab