2023-05-02 11:33:06 -04:00
// Copyright IBM Corp. 2014, 2026
2023-08-10 18:43:27 -04:00
// SPDX-License-Identifier: BUSL-1.1
2023-05-02 11:33:06 -04:00
2021-09-08 19:02:25 -04:00
package cloud
import (
2022-04-13 13:34:11 -04:00
"errors"
2021-09-13 15:18:32 -04:00
"fmt"
2021-09-29 17:55:43 -04:00
"strings"
2021-09-13 15:18:32 -04:00
2021-09-08 19:02:25 -04:00
"github.com/hashicorp/terraform/internal/tfdiags"
"github.com/zclconf/go-cty/cty"
)
2022-04-13 13:34:11 -04:00
// String based errors
var (
errApplyDiscarded = errors . New ( "Apply discarded." )
errDestroyDiscarded = errors . New ( "Destroy discarded." )
errRunApproved = errors . New ( "approved using the UI or API" )
errRunDiscarded = errors . New ( "discarded using the UI or API" )
errRunOverridden = errors . New ( "overridden using the UI or API" )
errApplyNeedsUIConfirmation = errors . New ( "Cannot confirm apply due to -input=false. Please handle run confirmation in the UI." )
errPolicyOverrideNeedsUIConfirmation = errors . New ( "Cannot override soft failed policy checks when -input=false. Please open the run in the UI to override." )
)
// Diagnostic error messages
2021-09-08 19:02:25 -04:00
var (
invalidWorkspaceConfigMissingValues = tfdiags . AttributeValue (
tfdiags . Error ,
"Invalid workspaces configuration" ,
2021-10-11 17:26:07 -04:00
fmt . Sprintf ( "Missing workspace mapping strategy. Either workspace \"tags\" or \"name\" is required.\n\n%s" , workspaceConfigurationHelp ) ,
2021-09-08 19:02:25 -04:00
cty . Path { cty . GetAttrStep { Name : "workspaces" } } ,
)
invalidWorkspaceConfigMisconfiguration = tfdiags . AttributeValue (
tfdiags . Error ,
"Invalid workspaces configuration" ,
2021-10-11 17:26:07 -04:00
fmt . Sprintf ( "Only one of workspace \"tags\" or \"name\" is allowed.\n\n%s" , workspaceConfigurationHelp ) ,
2021-09-08 19:02:25 -04:00
cty . Path { cty . GetAttrStep { Name : "workspaces" } } ,
2023-10-06 19:33:16 -04:00
)
invalidWorkspaceConfigNameConflict = tfdiags . AttributeValue (
tfdiags . Error ,
"Invalid workspaces configuration" ,
fmt . Sprintf ( "Specified workspace \"name\" conflicts with TF_WORKSPACE environment variable.\n\n%s" , workspaceConfigurationHelp ) ,
cty . Path { cty . GetAttrStep { Name : "workspaces" } } ,
2021-09-08 19:02:25 -04:00
)
)
2021-09-29 17:55:43 -04:00
2021-10-12 20:59:56 -04:00
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."
2021-09-29 17:55:43 -04:00
2022-03-28 14:00:10 -04:00
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 } } )
}
2021-10-12 20:59:56 -04:00
func incompatibleWorkspaceTerraformVersion ( message string , ignoreVersionConflict bool ) tfdiags . Diagnostic {
2021-09-29 17:55:43 -04:00
severity := tfdiags . Error
2021-10-12 20:59:56 -04:00
suggestion := ignoreRemoteVersionHelp
2021-09-29 17:55:43 -04:00
if ignoreVersionConflict {
severity = tfdiags . Warning
suggestion = ""
}
2021-10-12 20:59:56 -04:00
description := strings . TrimSpace ( fmt . Sprintf ( "%s\n\n%s" , message , suggestion ) )
return tfdiags . Sourceless ( severity , "Incompatible Terraform version" , description )
2021-09-29 17:55:43 -04:00
}