2024-02-08 04:48:59 -05:00
// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2023 HashiCorp, Inc.
2023-05-02 11:33:06 -04:00
// SPDX-License-Identifier: MPL-2.0
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
2023-09-20 07:35:35 -04:00
"github.com/opentofu/opentofu/internal/tfdiags"
2021-09-08 19:02:25 -04:00
"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-11-16 12:43:47 -05:00
)
2021-09-08 19:02:25 -04:00
)
2021-09-29 17:55:43 -04:00
2023-09-21 04:39:47 -04:00
const ignoreRemoteVersionHelp = "If you're sure you want to upgrade the state, you can force OpenTofu 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 ) )
2023-08-22 10:31:53 -04:00
return tfdiags . Sourceless ( severity , "Incompatible TF version" , description )
2021-09-29 17:55:43 -04:00
}
2024-08-22 03:55:50 -04:00
func invalidWorkspaceConfigInconsistentNameAndEnvVar ( ) tfdiags . Diagnostic {
return tfdiags . AttributeValue (
tfdiags . Error ,
"Invalid workspaces configuration" ,
fmt . Sprintf ( "The workspace defined using the environment variable \"TF_WORKSPACE\" is not consistent with the workspace \"name\" in the configuration.\n\n%s" , workspaceConfigurationHelp ) ,
cty . Path { cty . GetAttrStep { Name : "workspaces" } } ,
)
}