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-08-12 15:30:24 -04:00
package cloud
import (
"context"
"fmt"
"log"
2022-03-15 17:42:11 -04:00
tfe "github.com/hashicorp/go-tfe"
backendrun: Separate the types/etc for backends that support operations
We previously had all of the types and helpers for all kinds of backends
together in package backend. That kept things relatively simple, but it
also meant that the majority of backends that only deal with remote state
storage ended up still indirectly depending on the entire Terraform modules
runtime, configuration loader, etc, etc, which brings into scope a bunch
of external dependencies that the remote state backends don't really need.
Since backends that support operations are a rare exception, we'll move the
types and helpers for those into a separate package "backendrun", and
then the main package backend can have a much more modest set of types and,
more importantly, a modest set of dependencies on other packages in this
codebase.
This is part of an ongoing effort to reduce the exposure of Terraform Core
and CLI code to the remote backends and vice-versa, so that in the long
run we can more often treat them as separate for dependency maintenance
purposes.
2024-03-11 19:27:44 -04:00
"github.com/hashicorp/hcl/v2"
2021-08-12 15:30:24 -04:00
"github.com/hashicorp/hcl/v2/hclsyntax"
backendrun: Separate the types/etc for backends that support operations
We previously had all of the types and helpers for all kinds of backends
together in package backend. That kept things relatively simple, but it
also meant that the majority of backends that only deal with remote state
storage ended up still indirectly depending on the entire Terraform modules
runtime, configuration loader, etc, etc, which brings into scope a bunch
of external dependencies that the remote state backends don't really need.
Since backends that support operations are a rare exception, we'll move the
types and helpers for those into a separate package "backendrun", and
then the main package backend can have a much more modest set of types and,
more importantly, a modest set of dependencies on other packages in this
codebase.
This is part of an ongoing effort to reduce the exposure of Terraform Core
and CLI code to the remote backends and vice-versa, so that in the long
run we can more often treat them as separate for dependency maintenance
purposes.
2024-03-11 19:27:44 -04:00
"github.com/zclconf/go-cty/cty"
2021-08-12 15:30:24 -04:00
"github.com/hashicorp/terraform/internal/backend"
backendrun: Separate the types/etc for backends that support operations
We previously had all of the types and helpers for all kinds of backends
together in package backend. That kept things relatively simple, but it
also meant that the majority of backends that only deal with remote state
storage ended up still indirectly depending on the entire Terraform modules
runtime, configuration loader, etc, etc, which brings into scope a bunch
of external dependencies that the remote state backends don't really need.
Since backends that support operations are a rare exception, we'll move the
types and helpers for those into a separate package "backendrun", and
then the main package backend can have a much more modest set of types and,
more importantly, a modest set of dependencies on other packages in this
codebase.
This is part of an ongoing effort to reduce the exposure of Terraform Core
and CLI code to the remote backends and vice-versa, so that in the long
run we can more often treat them as separate for dependency maintenance
purposes.
2024-03-11 19:27:44 -04:00
"github.com/hashicorp/terraform/internal/backend/backendrun"
2026-02-17 08:56:46 -05:00
"github.com/hashicorp/terraform/internal/command/arguments"
2021-08-12 15:30:24 -04:00
"github.com/hashicorp/terraform/internal/configs"
"github.com/hashicorp/terraform/internal/states/statemgr"
"github.com/hashicorp/terraform/internal/terraform"
"github.com/hashicorp/terraform/internal/tfdiags"
)
2025-03-20 12:52:00 -04:00
// LocalRun implements backendrun.Local
backendrun: Separate the types/etc for backends that support operations
We previously had all of the types and helpers for all kinds of backends
together in package backend. That kept things relatively simple, but it
also meant that the majority of backends that only deal with remote state
storage ended up still indirectly depending on the entire Terraform modules
runtime, configuration loader, etc, etc, which brings into scope a bunch
of external dependencies that the remote state backends don't really need.
Since backends that support operations are a rare exception, we'll move the
types and helpers for those into a separate package "backendrun", and
then the main package backend can have a much more modest set of types and,
more importantly, a modest set of dependencies on other packages in this
codebase.
This is part of an ongoing effort to reduce the exposure of Terraform Core
and CLI code to the remote backends and vice-versa, so that in the long
run we can more often treat them as separate for dependency maintenance
purposes.
2024-03-11 19:27:44 -04:00
func ( b * Cloud ) LocalRun ( op * backendrun . Operation ) ( * backendrun . LocalRun , statemgr . Full , tfdiags . Diagnostics ) {
2021-08-12 15:30:24 -04:00
var diags tfdiags . Diagnostics
backendrun: Separate the types/etc for backends that support operations
We previously had all of the types and helpers for all kinds of backends
together in package backend. That kept things relatively simple, but it
also meant that the majority of backends that only deal with remote state
storage ended up still indirectly depending on the entire Terraform modules
runtime, configuration loader, etc, etc, which brings into scope a bunch
of external dependencies that the remote state backends don't really need.
Since backends that support operations are a rare exception, we'll move the
types and helpers for those into a separate package "backendrun", and
then the main package backend can have a much more modest set of types and,
more importantly, a modest set of dependencies on other packages in this
codebase.
This is part of an ongoing effort to reduce the exposure of Terraform Core
and CLI code to the remote backends and vice-versa, so that in the long
run we can more often treat them as separate for dependency maintenance
purposes.
2024-03-11 19:27:44 -04:00
ret := & backendrun . LocalRun {
2021-08-30 18:27:58 -04:00
PlanOpts : & terraform . PlanOpts {
Mode : op . PlanMode ,
Targets : op . Targets ,
} ,
}
2021-08-12 15:30:24 -04:00
op . StateLocker = op . StateLocker . WithContext ( context . Background ( ) )
// Get the remote workspace name.
remoteWorkspaceName := b . getRemoteWorkspaceName ( op . Workspace )
// Get the latest state.
log . Printf ( "[TRACE] cloud: requesting state manager for workspace %q" , remoteWorkspaceName )
2025-09-04 06:14:35 -04:00
stateMgr , sDiags := b . StateMgr ( op . Workspace )
if sDiags . HasErrors ( ) {
diags = diags . Append ( fmt . Errorf ( "error loading state: %w" , sDiags . Err ( ) ) )
2021-08-12 15:30:24 -04:00
return nil , nil , diags
}
log . Printf ( "[TRACE] cloud: requesting state lock for workspace %q" , remoteWorkspaceName )
if diags := op . StateLocker . Lock ( stateMgr , op . Type . String ( ) ) ; diags . HasErrors ( ) {
return nil , nil , diags
}
defer func ( ) {
// If we're returning with errors, and thus not producing a valid
// context, we'll want to avoid leaving the remote workspace locked.
if diags . HasErrors ( ) {
diags = diags . Append ( op . StateLocker . Unlock ( ) )
}
} ( )
log . Printf ( "[TRACE] cloud: reading remote state for workspace %q" , remoteWorkspaceName )
if err := stateMgr . RefreshState ( ) ; err != nil {
2021-08-30 18:27:58 -04:00
diags = diags . Append ( fmt . Errorf ( "error loading state: %w" , err ) )
2021-08-12 15:30:24 -04:00
return nil , nil , diags
}
// Initialize our context options
var opts terraform . ContextOpts
if v := b . ContextOpts ; v != nil {
opts = * v
}
// Copy set options from the operation
opts . UIInput = op . UIIn
// Load the latest state. If we enter contextFromPlanFile below then the
// state snapshot in the plan file must match this, or else it'll return
// error diagnostics.
log . Printf ( "[TRACE] cloud: retrieving remote state snapshot for workspace %q" , remoteWorkspaceName )
2021-08-30 18:27:58 -04:00
ret . InputState = stateMgr . State ( )
2021-08-12 15:30:24 -04:00
2026-02-26 11:33:01 -05:00
log . Printf ( "[TRACE] cloud: loading root module for the current working directory" )
rootMod , configDiags := op . ConfigLoader . LoadRootModule ( op . ConfigDir )
2021-08-12 15:30:24 -04:00
diags = diags . Append ( configDiags )
if configDiags . HasErrors ( ) {
return nil , nil , diags
}
if op . AllowUnsetVariables {
// If we're not going to use the variables in an operation we'll be
// more lax about them, stubbing out any unset ones as unknown.
// This gives us enough information to produce a consistent context,
// but not enough information to run a real operation (plan, apply, etc)
2026-02-26 11:33:01 -05:00
ret . PlanOpts . SetVariables = stubAllVariables ( op . Variables , rootMod . Variables )
2021-08-12 15:30:24 -04:00
} else {
2021-10-11 13:43:39 -04:00
// The underlying API expects us to use the opaque workspace id to request
// variables, so we'll need to look that up using our organization name
// and workspace name.
remoteWorkspaceID , err := b . getRemoteWorkspaceID ( context . Background ( ) , op . Workspace )
if err != nil {
diags = diags . Append ( fmt . Errorf ( "error finding remote workspace: %w" , err ) )
return nil , nil , diags
}
2023-12-20 20:59:35 -05:00
w , err := b . fetchWorkspace ( context . Background ( ) , b . Organization , op . Workspace )
2022-03-15 17:42:11 -04:00
if err != nil {
diags = diags . Append ( fmt . Errorf ( "error loading workspace: %w" , err ) )
2021-10-11 13:43:39 -04:00
return nil , nil , diags
}
2022-03-23 16:58:47 -04:00
2022-03-15 17:42:11 -04:00
if isLocalExecutionMode ( w . ExecutionMode ) {
2023-12-20 20:59:35 -05:00
log . Printf ( "[TRACE] skipping retrieving variables from workspace %s/%s (%s), workspace is in Local Execution mode" , remoteWorkspaceName , b . Organization , remoteWorkspaceID )
2022-03-15 17:42:11 -04:00
} else {
2023-12-20 20:59:35 -05:00
log . Printf ( "[TRACE] cloud: retrieving variables from workspace %s/%s (%s)" , remoteWorkspaceName , b . Organization , remoteWorkspaceID )
2025-09-03 09:26:07 -04:00
tfeVariables , err := b . client . Variables . ListAll ( context . Background ( ) , remoteWorkspaceID , nil )
2022-03-15 17:42:11 -04:00
if err != nil && err != tfe . ErrResourceNotFound {
diags = diags . Append ( fmt . Errorf ( "error loading variables: %w" , err ) )
return nil , nil , diags
2021-08-12 15:30:24 -04:00
}
2022-03-15 17:42:11 -04:00
if tfeVariables != nil {
if op . Variables == nil {
2026-02-17 08:56:46 -05:00
op . Variables = make ( map [ string ] arguments . UnparsedVariableValue )
2022-03-15 17:42:11 -04:00
}
for _ , v := range tfeVariables . Items {
if v . Category == tfe . CategoryTerraform {
if _ , ok := op . Variables [ v . Key ] ; ! ok {
op . Variables [ v . Key ] = & remoteStoredVariableValue {
definition : v ,
}
}
2021-08-12 15:30:24 -04:00
}
}
}
}
if op . Variables != nil {
2026-02-26 11:33:01 -05:00
variables , varDiags := backendrun . ParseVariableValues ( op . Variables , rootMod . Variables )
2021-08-12 15:30:24 -04:00
diags = diags . Append ( varDiags )
if diags . HasErrors ( ) {
return nil , nil , diags
}
2021-08-30 18:27:58 -04:00
ret . PlanOpts . SetVariables = variables
2021-08-12 15:30:24 -04:00
}
}
tfCtx , ctxDiags := terraform . NewContext ( & opts )
diags = diags . Append ( ctxDiags )
2021-08-30 18:27:58 -04:00
ret . Core = tfCtx
2026-02-26 11:33:01 -05:00
if diags . HasErrors ( ) {
return nil , nil , diags
}
log . Printf ( "[TRACE] cloud: building configuration for the current working directory" )
config , buildDiags := terraform . BuildConfigWithGraph (
rootMod ,
op . ConfigLoader . ModuleWalker ( ) ,
ret . PlanOpts . SetVariables ,
configs . MockDataLoaderFunc ( op . ConfigLoader . LoadExternalMockData ) ,
)
diags = diags . Append ( buildDiags )
if diags . HasErrors ( ) {
return nil , nil , diags
}
ret . Config = config
2021-08-12 15:30:24 -04:00
log . Printf ( "[TRACE] cloud: finished building terraform.Context" )
2021-08-30 18:27:58 -04:00
return ret , stateMgr , diags
2021-08-12 15:30:24 -04:00
}
func ( b * Cloud ) getRemoteWorkspaceName ( localWorkspaceName string ) string {
switch {
case localWorkspaceName == backend . DefaultStateName :
2021-10-11 17:26:07 -04:00
// The default workspace name is a special case
2021-09-20 14:07:53 -04:00
return b . WorkspaceMapping . Name
2021-08-12 15:30:24 -04:00
default :
return localWorkspaceName
}
}
func ( b * Cloud ) getRemoteWorkspace ( ctx context . Context , localWorkspaceName string ) ( * tfe . Workspace , error ) {
remoteWorkspaceName := b . getRemoteWorkspaceName ( localWorkspaceName )
2023-12-20 20:59:35 -05:00
log . Printf ( "[TRACE] cloud: looking up workspace for %s/%s" , b . Organization , remoteWorkspaceName )
remoteWorkspace , err := b . client . Workspaces . Read ( ctx , b . Organization , remoteWorkspaceName )
2021-08-12 15:30:24 -04:00
if err != nil {
return nil , err
}
return remoteWorkspace , nil
}
func ( b * Cloud ) getRemoteWorkspaceID ( ctx context . Context , localWorkspaceName string ) ( string , error ) {
remoteWorkspace , err := b . getRemoteWorkspace ( ctx , localWorkspaceName )
if err != nil {
return "" , err
}
return remoteWorkspace . ID , nil
}
2026-02-17 08:56:46 -05:00
func stubAllVariables ( vv map [ string ] arguments . UnparsedVariableValue , decls map [ string ] * configs . Variable ) terraform . InputValues {
2021-08-12 15:30:24 -04:00
ret := make ( terraform . InputValues , len ( decls ) )
for name , cfg := range decls {
raw , exists := vv [ name ]
if ! exists {
ret [ name ] = & terraform . InputValue {
Value : cty . UnknownVal ( cfg . Type ) ,
SourceType : terraform . ValueFromConfig ,
}
continue
}
val , diags := raw . ParseVariableValue ( cfg . ParsingMode )
if diags . HasErrors ( ) {
ret [ name ] = & terraform . InputValue {
Value : cty . UnknownVal ( cfg . Type ) ,
SourceType : terraform . ValueFromConfig ,
}
continue
}
ret [ name ] = val
}
return ret
}
2025-03-20 12:52:00 -04:00
// remoteStoredVariableValue is a backendrun.UnparsedVariableValue implementation
2021-08-12 15:30:24 -04:00
// that translates from the go-tfe representation of stored variables into
// the Terraform Core backend representation of variables.
type remoteStoredVariableValue struct {
definition * tfe . Variable
}
2026-02-17 08:56:46 -05:00
var _ arguments . UnparsedVariableValue = ( * remoteStoredVariableValue ) ( nil )
2021-08-12 15:30:24 -04:00
func ( v * remoteStoredVariableValue ) ParseVariableValue ( mode configs . VariableParsingMode ) ( * terraform . InputValue , tfdiags . Diagnostics ) {
var diags tfdiags . Diagnostics
var val cty . Value
switch {
case v . definition . Sensitive :
// If it's marked as sensitive then it's not available for use in
// local operations. We'll use an unknown value as a placeholder for
// it so that operations that don't need it might still work, but
// we'll also produce a warning about it to add context for any
// errors that might result here.
val = cty . DynamicVal
if ! v . definition . HCL {
// If it's not marked as HCL then we at least know that the
// value must be a string, so we'll set that in case it allows
// us to do some more precise type checking.
val = cty . UnknownVal ( cty . String )
}
diags = diags . Append ( tfdiags . Sourceless (
tfdiags . Warning ,
fmt . Sprintf ( "Value for var.%s unavailable" , v . definition . Key ) ,
fmt . Sprintf ( "The value of variable %q is marked as sensitive in the remote workspace. This operation always runs locally, so the value for that variable is not available." , v . definition . Key ) ,
) )
case v . definition . HCL :
// If the variable value is marked as being in HCL syntax, we need to
// parse it the same way as it would be interpreted in a .tfvars
// file because that is how it would get passed to Terraform CLI for
// a remote operation and we want to mimic that result as closely as
// possible.
var exprDiags hcl . Diagnostics
expr , exprDiags := hclsyntax . ParseExpression ( [ ] byte ( v . definition . Value ) , "<remote workspace>" , hcl . Pos { Line : 1 , Column : 1 } )
if expr != nil {
var moreDiags hcl . Diagnostics
val , moreDiags = expr . Value ( nil )
exprDiags = append ( exprDiags , moreDiags ... )
} else {
// We'll have already put some errors in exprDiags above, so we'll
// just stub out the value here.
val = cty . DynamicVal
}
// We don't have sufficient context to return decent error messages
// for syntax errors in the remote values, so we'll just return a
// generic message instead for now.
// (More complete error messages will still result from true remote
// operations, because they'll run on the remote system where we've
// materialized the values into a tfvars file we can report from.)
if exprDiags . HasErrors ( ) {
diags = diags . Append ( tfdiags . Sourceless (
tfdiags . Error ,
fmt . Sprintf ( "Invalid expression for var.%s" , v . definition . Key ) ,
fmt . Sprintf ( "The value of variable %q is marked in the remote workspace as being specified in HCL syntax, but the given value is not valid HCL. Stored variable values must be valid literal expressions and may not contain references to other variables or calls to functions." , v . definition . Key ) ,
) )
}
default :
// A variable value _not_ marked as HCL is always be a string, given
// literally.
val = cty . StringVal ( v . definition . Value )
}
return & terraform . InputValue {
Value : val ,
// We mark these as "from input" with the rationale that entering
2024-04-22 15:21:52 -04:00
// variable values into the HCP Terraform or Enterprise UI is,
2021-08-12 15:30:24 -04:00
// roughly speaking, a similar idea to entering variable values at
// the interactive CLI prompts. It's not a perfect correspondance,
// but it's closer than the other options.
SourceType : terraform . ValueFromInput ,
} , diags
}