2015-02-11 10:48:45 -06:00
package terraform
2015-02-12 16:46:22 -06:00
import (
2015-07-22 06:51:14 -05:00
"fmt"
2015-02-12 16:46:22 -06:00
"log"
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
2018-09-07 16:53:36 -05:00
"github.com/zclconf/go-cty/cty"
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
"github.com/hashicorp/terraform/addrs"
2018-08-16 10:40:08 -05:00
"github.com/hashicorp/terraform/providers"
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"
2018-08-24 18:13:50 -05:00
"github.com/hashicorp/terraform/tfdiags"
2015-02-12 16:46:22 -06:00
)
2015-02-11 10:48:45 -06:00
// EvalRefresh is an EvalNode implementation that does a refresh for
// a resource.
type EvalRefresh struct {
2018-08-24 18:13:50 -05:00
Addr addrs . ResourceInstance
ProviderAddr addrs . AbsProviderConfig
Provider * providers . Interface
ProviderSchema * * ProviderSchema
State * * states . ResourceInstanceObject
Output * * states . ResourceInstanceObject
2015-02-11 10:48:45 -06:00
}
// TODO: test
2015-02-14 00:58:41 -06:00
func ( n * EvalRefresh ) Eval ( ctx EvalContext ) ( interface { } , error ) {
2015-02-12 16:46:22 -06:00
state := * n . 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
absAddr := n . Addr . Absolute ( ctx . Path ( ) )
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
2018-08-24 18:13:50 -05:00
var diags tfdiags . Diagnostics
2015-02-11 10:52:19 -06:00
// If we have no state, we don't do any refreshing
if state == nil {
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
log . Printf ( "[DEBUG] refresh: %s: no state, so not refreshing" , n . Addr . Absolute ( ctx . Path ( ) ) )
2018-08-24 18:13:50 -05:00
return nil , diags . ErrWithWarnings ( )
}
schema := ( * n . ProviderSchema ) . ResourceTypes [ n . Addr . Resource . Type ]
if schema == nil {
// Should be caught during validation, so we don't bother with a pretty error here
return nil , fmt . Errorf ( "provider does not support resource type %q" , n . Addr . Resource . Type )
2015-02-11 10:52:19 -06:00
}
2015-02-11 15:43:07 -06:00
// Call pre-refresh hook
err := ctx . Hook ( func ( h Hook ) ( HookAction , error ) {
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
return h . PreRefresh ( absAddr , states . CurrentGen , state . Value )
2015-02-11 15:43:07 -06:00
} )
if err != nil {
2018-08-24 18:13:50 -05:00
return nil , diags . ErrWithWarnings ( )
2015-02-11 15:43:07 -06:00
}
// Refresh!
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
priorVal := state . Value
2018-08-16 12:05:05 -05:00
req := providers . ReadResourceRequest {
TypeName : n . Addr . Resource . Type ,
PriorState : priorVal ,
2015-02-11 15:43:07 -06:00
}
2018-08-16 12:05:05 -05:00
provider := * n . Provider
resp := provider . ReadResource ( req )
2018-08-24 18:13:50 -05:00
diags = diags . Append ( resp . Diagnostics )
if diags . HasErrors ( ) {
return nil , diags . Err ( )
}
2018-09-07 16:53:36 -05:00
if resp . NewState == cty . NilVal {
// This ought not to happen in real cases since it's not possible to
// send NilVal over the plugin RPC channel, but it can come up in
// tests due to sloppy mocking.
panic ( "new state is cty.NilVal" )
}
2018-08-24 18:13:50 -05:00
for _ , err := range schema . ImpliedType ( ) . TestConformance ( resp . NewState . Type ( ) ) {
diags = diags . Append ( tfdiags . Sourceless (
tfdiags . Error ,
"Provider produced invalid object" ,
fmt . Sprintf (
2018-09-07 16:45:51 -05:00
"Provider %q planned an invalid value for %s: %s during refresh.\n\nThis is a bug in the provider, which should be reported in the provider's own issue tracker." ,
2018-08-24 18:13:50 -05:00
n . ProviderAddr . ProviderConfig . Type , absAddr , tfdiags . FormatError ( err ) ,
) ,
) )
}
if diags . HasErrors ( ) {
return nil , diags . Err ( )
2018-05-29 15:13:45 -05:00
}
2015-02-11 15:43:07 -06:00
2018-09-12 13:15:31 -05:00
newState := state . DeepCopy ( )
newState . Value = resp . NewState
2018-08-16 12:05:05 -05:00
2015-02-11 15:43:07 -06:00
// Call post-refresh hook
err = ctx . Hook ( func ( h Hook ) ( HookAction , error ) {
2018-09-12 13:15:31 -05:00
return h . PostRefresh ( absAddr , states . CurrentGen , priorVal , newState . Value )
2015-02-11 15:43:07 -06:00
} )
if err != nil {
return nil , err
}
2015-02-12 16:46:22 -06:00
if n . Output != nil {
2018-09-12 13:15:31 -05:00
* n . Output = newState
2015-02-12 16:46:22 -06:00
}
2015-02-11 10:48:45 -06:00
2018-08-24 18:13:50 -05:00
return nil , diags . ErrWithWarnings ( )
2015-02-11 10:48:45 -06:00
}