2016-03-25 12:17:25 -05:00
package command
import (
2016-05-11 11:16:48 -05:00
"fmt"
2018-10-25 08:41:00 -05:00
"sort"
2016-05-11 11:16:48 -05:00
"time"
2016-03-25 12:17:25 -05:00
2018-10-22 08:52:53 -05:00
"github.com/hashicorp/terraform/addrs"
2016-05-10 19:03:58 -05:00
"github.com/hashicorp/terraform/state"
2018-10-17 16:01:15 -05:00
"github.com/hashicorp/terraform/states"
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
"github.com/hashicorp/terraform/states/statemgr"
2019-03-15 21:51:26 -05:00
"github.com/hashicorp/terraform/tfdiags"
2018-10-31 10:45:03 -05:00
backendLocal "github.com/hashicorp/terraform/backend/local"
2016-03-25 12:17:25 -05:00
)
// StateMeta is the meta struct that should be embedded in state subcommands.
2017-07-26 12:08:09 -05:00
type StateMeta struct {
Meta
}
2016-03-25 12:17:25 -05:00
2017-03-01 09:10:47 -06:00
// State returns the state for this meta. This gets the appropriate state from
// the backend, but changes the way that backups are done. This configures
// backups to be timestamped rather than just the original state path plus a
// backup path.
2017-07-26 12:08:09 -05:00
func ( c * StateMeta ) State ( ) ( state . State , error ) {
2017-06-23 13:41:49 -05:00
var realState state . State
2017-07-26 12:08:09 -05:00
backupPath := c . backupPath
stateOutPath := c . statePath
2016-05-11 11:16:48 -05:00
2017-06-23 13:41:49 -05:00
// use the specified state
2017-07-26 12:08:09 -05:00
if c . statePath != "" {
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
realState = statemgr . NewFilesystem ( c . statePath )
2017-06-23 13:41:49 -05:00
} else {
// Load the backend
2018-03-27 17:31:05 -05:00
b , backendDiags := c . Backend ( nil )
if backendDiags . HasErrors ( ) {
return nil , backendDiags . Err ( )
2017-06-23 13:41:49 -05:00
}
2017-01-18 22:50:45 -06:00
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
workspace := c . Workspace ( )
2017-06-23 13:41:49 -05:00
// Get the state
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
s , err := b . StateMgr ( workspace )
2017-06-23 13:41:49 -05:00
if err != nil {
return nil , err
}
// Get a local backend
2018-03-27 17:31:05 -05:00
localRaw , backendDiags := c . Backend ( & BackendOpts { ForceLocal : true } )
if backendDiags . HasErrors ( ) {
2017-06-23 13:41:49 -05:00
// This should never fail
2018-03-27 17:31:05 -05:00
panic ( backendDiags . Err ( ) )
2017-06-23 13:41:49 -05:00
}
2018-10-31 10:45:03 -05:00
localB := localRaw . ( * backendLocal . Local )
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
_ , stateOutPath , _ = localB . StatePaths ( workspace )
2017-06-23 13:41:49 -05:00
if err != nil {
return nil , err
}
realState = s
2017-02-22 12:11:26 -06:00
}
2017-01-18 22:50:45 -06:00
2017-06-23 13:41:49 -05:00
// We always backup state commands, so set the back if none was specified
// (the default is "-", but some tests bypass the flag parsing).
if backupPath == "-" || backupPath == "" {
// Determine the backup path. stateOutPath is set to the resulting
// file where state is written (cached in the case of remote state)
backupPath = fmt . Sprintf (
"%s.%d%s" ,
stateOutPath ,
time . Now ( ) . UTC ( ) . Unix ( ) ,
DefaultBackupExtension )
}
2016-05-11 11:16:48 -05:00
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
// If the backend is local (which it should always be, given our asserting
// of it above) we can now enable backups for it.
if lb , ok := realState . ( * statemgr . Filesystem ) ; ok {
lb . SetBackupPath ( backupPath )
2016-05-11 11:16:48 -05:00
}
2017-06-23 13:41:49 -05:00
return realState , nil
2016-05-10 19:03:58 -05:00
}
2019-03-15 21:51:26 -05:00
func ( c * StateMeta ) lookupResourceInstanceAddr ( state * states . State , allowMissing bool , addrStr string ) ( [ ] addrs . AbsResourceInstance , tfdiags . Diagnostics ) {
target , diags := addrs . ParseTargetStr ( addrStr )
if diags . HasErrors ( ) {
return nil , diags
}
2016-03-25 12:17:25 -05:00
2019-03-15 21:51:26 -05:00
targetAddr := target . Subject
var ret [ ] addrs . AbsResourceInstance
switch addr := targetAddr . ( type ) {
case addrs . ModuleInstance :
// Matches all instances within the indicated module and all of its
// descendent modules.
ms := state . Module ( addr )
if ms == nil {
if ! allowMissing {
diags = diags . Append ( tfdiags . Sourceless (
tfdiags . Error ,
"Unknown module" ,
fmt . Sprintf ( ` The current state contains no module at %s. If you've just added this module to the configuration, you must run "terraform apply" first to create the module's entry in the state. ` , addr ) ,
) )
}
break
2016-03-25 12:17:25 -05:00
}
2019-03-15 21:51:26 -05:00
ret = append ( ret , c . collectModuleResourceInstances ( ms ) ... )
for _ , cms := range state . Modules {
candidateAddr := ms . Addr
if len ( candidateAddr ) > len ( addr ) && candidateAddr [ : len ( addr ) ] . Equal ( addr ) {
ret = append ( ret , c . collectModuleResourceInstances ( cms ) ... )
}
}
case addrs . AbsResource :
// Matches all instances of the specific selected resource.
rs := state . Resource ( addr )
if rs == nil {
if ! allowMissing {
diags = diags . Append ( tfdiags . Sourceless (
tfdiags . Error ,
"Unknown resource" ,
fmt . Sprintf ( ` The current state contains no resource %s. If you've just added this resource to the configuration, you must run "terraform apply" first to create the resource's entry in the state. ` , addr ) ,
) )
2018-10-22 08:52:53 -05:00
}
2019-03-15 21:51:26 -05:00
break
2018-10-22 08:52:53 -05:00
}
2019-03-15 21:51:26 -05:00
ret = append ( ret , c . collectResourceInstances ( addr . Module , rs ) ... )
case addrs . AbsResourceInstance :
is := state . ResourceInstance ( addr )
if is == nil {
if ! allowMissing {
diags = diags . Append ( tfdiags . Sourceless (
tfdiags . Error ,
"Unknown resource instance" ,
fmt . Sprintf ( ` The current state contains no resource instance %s. If you've just added its resource to the configuration or have changed the count or for_each arguments, you must run "terraform apply" first to update the resource's entry in the state. ` , addr ) ,
) )
}
break
}
ret = append ( ret , addr )
2016-03-25 12:17:25 -05:00
}
2019-03-15 21:51:26 -05:00
sort . Slice ( ret , func ( i , j int ) bool {
return ret [ i ] . Less ( ret [ j ] )
} )
2016-03-25 12:17:25 -05:00
2019-03-15 21:51:26 -05:00
return ret , diags
}
2018-10-25 08:41:00 -05:00
2019-03-15 21:51:26 -05:00
func ( c * StateMeta ) lookupSingleResourceInstanceAddr ( state * states . State , addrStr string ) ( addrs . AbsResourceInstance , tfdiags . Diagnostics ) {
return addrs . ParseAbsResourceInstanceStr ( addrStr )
}
2018-10-25 08:41:00 -05:00
2019-03-15 21:51:26 -05:00
func ( c * StateMeta ) lookupSingleStateObjectAddr ( state * states . State , addrStr string ) ( addrs . Targetable , tfdiags . Diagnostics ) {
target , diags := addrs . ParseTargetStr ( addrStr )
if diags . HasErrors ( ) {
return nil , diags
}
return target . Subject , diags
}
func ( c * StateMeta ) lookupResourceInstanceAddrs ( state * states . State , addrStrs ... string ) ( [ ] addrs . AbsResourceInstance , tfdiags . Diagnostics ) {
var ret [ ] addrs . AbsResourceInstance
var diags tfdiags . Diagnostics
for _ , addrStr := range addrStrs {
moreAddrs , moreDiags := c . lookupResourceInstanceAddr ( state , false , addrStr )
ret = append ( ret , moreAddrs ... )
diags = diags . Append ( moreDiags )
}
return ret , diags
}
2018-10-25 08:41:00 -05:00
2019-03-15 21:51:26 -05:00
func ( c * StateMeta ) lookupAllResourceInstanceAddrs ( state * states . State ) ( [ ] addrs . AbsResourceInstance , tfdiags . Diagnostics ) {
var ret [ ] addrs . AbsResourceInstance
var diags tfdiags . Diagnostics
for _ , ms := range state . Modules {
ret = append ( ret , c . collectModuleResourceInstances ( ms ) ... )
}
sort . Slice ( ret , func ( i , j int ) bool {
return ret [ i ] . Less ( ret [ j ] )
2018-10-25 08:41:00 -05:00
} )
2019-03-15 21:51:26 -05:00
return ret , diags
}
func ( c * StateMeta ) collectModuleResourceInstances ( ms * states . Module ) [ ] addrs . AbsResourceInstance {
var ret [ ] addrs . AbsResourceInstance
for _ , rs := range ms . Resources {
ret = append ( ret , c . collectResourceInstances ( ms . Addr , rs ) ... )
}
return ret
}
2018-10-25 08:41:00 -05:00
2019-03-15 21:51:26 -05:00
func ( c * StateMeta ) collectResourceInstances ( moduleAddr addrs . ModuleInstance , rs * states . Resource ) [ ] addrs . AbsResourceInstance {
var ret [ ] addrs . AbsResourceInstance
for key := range rs . Instances {
ret = append ( ret , rs . Addr . Instance ( key ) . Absolute ( moduleAddr ) )
}
return ret
2016-03-25 12:17:25 -05:00
}