2016-05-10 05:32:31 +02:00
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
2018-10-16 03:15:08 +02:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2018-10-17 23:05:57 +02:00
|
|
|
"errors"
|
2016-05-10 05:32:31 +02:00
|
|
|
"fmt"
|
2018-10-16 03:15:08 +02:00
|
|
|
"log"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2016-05-10 05:32:31 +02:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
"github.com/hashicorp/terraform/config"
|
|
|
|
"github.com/hashicorp/terraform/config/hcl2shim"
|
|
|
|
"github.com/hashicorp/terraform/states"
|
|
|
|
|
|
|
|
"github.com/hashicorp/errwrap"
|
|
|
|
"github.com/hashicorp/terraform/plans"
|
2016-05-10 05:32:31 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2018-10-16 03:15:08 +02:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
2016-05-10 05:32:31 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// testStepConfig runs a config-mode test step
|
|
|
|
func testStepConfig(
|
|
|
|
opts terraform.ContextOpts,
|
|
|
|
state *terraform.State,
|
2018-12-07 03:14:31 +01:00
|
|
|
step TestStep) (*terraform.State, error) {
|
|
|
|
return testStep(opts, state, step)
|
2016-05-10 05:32:31 +02:00
|
|
|
}
|
|
|
|
|
2018-12-07 03:14:31 +01:00
|
|
|
func testStep(opts terraform.ContextOpts, state *terraform.State, step TestStep) (*terraform.State, error) {
|
2018-10-16 03:15:08 +02:00
|
|
|
if !step.Destroy {
|
|
|
|
if err := testStepTaint(state, step); err != nil {
|
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 23:24:45 +02:00
|
|
|
return state, err
|
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
}
|
2016-05-10 05:32:31 +02:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
cfg, err := testConfig(opts, step)
|
|
|
|
if err != nil {
|
|
|
|
return state, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var stepDiags tfdiags.Diagnostics
|
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 19:33:53 +02:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
// Build the context
|
|
|
|
opts.Config = cfg
|
2018-12-04 22:00:43 +01:00
|
|
|
opts.State, err = terraform.ShimLegacyState(state)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
opts.Destroy = step.Destroy
|
|
|
|
ctx, stepDiags := terraform.NewContext(&opts)
|
|
|
|
if stepDiags.HasErrors() {
|
|
|
|
return state, fmt.Errorf("Error initializing context: %s", stepDiags.Err())
|
|
|
|
}
|
|
|
|
if stepDiags := ctx.Validate(); len(stepDiags) > 0 {
|
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 19:33:53 +02:00
|
|
|
if stepDiags.HasErrors() {
|
2018-11-02 20:20:51 +01:00
|
|
|
return state, errwrap.Wrapf("config is invalid: {{err}}", stepDiags.Err())
|
2016-05-10 05:32:31 +02:00
|
|
|
}
|
2017-11-22 00:08:00 +01:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
log.Printf("[WARN] Config warnings:\n%s", stepDiags)
|
|
|
|
}
|
|
|
|
|
2018-12-07 03:14:31 +01:00
|
|
|
// We will need access to the schemas in order to shim to the old-style
|
|
|
|
// testing API.
|
|
|
|
schemas := ctx.Schemas()
|
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
// Refresh!
|
|
|
|
newState, stepDiags := ctx.Refresh()
|
2018-12-04 22:00:43 +01:00
|
|
|
// shim the state first so the test can check the state on errors
|
|
|
|
state, err = shimNewState(newState, schemas)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
if stepDiags.HasErrors() {
|
2018-12-07 21:17:55 +01:00
|
|
|
return state, newOperationError("refresh", stepDiags)
|
2018-10-16 03:15:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this step is a PlanOnly step, skip over this first Plan and subsequent
|
|
|
|
// Apply, and use the follow up Plan that checks for perpetual diffs
|
|
|
|
if !step.PlanOnly {
|
|
|
|
// Plan!
|
|
|
|
if p, stepDiags := ctx.Plan(); stepDiags.HasErrors() {
|
2018-12-07 21:17:55 +01:00
|
|
|
return state, newOperationError("plan", stepDiags)
|
2018-10-16 03:15:08 +02:00
|
|
|
} else {
|
|
|
|
log.Printf("[WARN] Test: Step plan: %s", legacyPlanComparisonString(newState, p.Changes))
|
2017-03-22 20:42:01 +01:00
|
|
|
}
|
2016-05-10 05:32:31 +02:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
// We need to keep a copy of the state prior to destroying
|
2018-10-30 17:59:45 +01:00
|
|
|
// such that destroy steps can verify their behavior in the check
|
2018-10-16 03:15:08 +02:00
|
|
|
// function
|
|
|
|
stateBeforeApplication := state.DeepCopy()
|
|
|
|
|
|
|
|
// Apply the diff, creating real resources.
|
|
|
|
newState, stepDiags = ctx.Apply()
|
2018-12-04 22:00:43 +01:00
|
|
|
// shim the state first so the test can check the state on errors
|
|
|
|
state, err = shimNewState(newState, schemas)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
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 19:33:53 +02:00
|
|
|
if stepDiags.HasErrors() {
|
2018-12-07 21:17:55 +01:00
|
|
|
return state, newOperationError("apply", stepDiags)
|
2017-03-22 20:42:01 +01:00
|
|
|
}
|
2016-05-10 05:32:31 +02:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
// Run any configured checks
|
|
|
|
if step.Check != nil {
|
|
|
|
if step.Destroy {
|
|
|
|
if err := step.Check(stateBeforeApplication); err != nil {
|
|
|
|
return state, fmt.Errorf("Check failed: %s", err)
|
|
|
|
}
|
2017-03-22 20:42:01 +01:00
|
|
|
} else {
|
2018-10-16 03:15:08 +02:00
|
|
|
if err := step.Check(state); err != nil {
|
|
|
|
return state, fmt.Errorf("Check failed: %s", err)
|
|
|
|
}
|
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 23:24:45 +02:00
|
|
|
}
|
2018-10-16 03:15:08 +02: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 23:24:45 +02:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
// Now, verify that Plan is now empty and we don't have a perpetual diff issue
|
|
|
|
// We do this with TWO plans. One without a refresh.
|
|
|
|
var p *plans.Plan
|
|
|
|
if p, stepDiags = ctx.Plan(); stepDiags.HasErrors() {
|
2018-12-07 21:17:55 +01:00
|
|
|
return state, newOperationError("follow-up plan", stepDiags)
|
2018-10-16 03:15:08 +02:00
|
|
|
}
|
|
|
|
if !p.Changes.Empty() {
|
|
|
|
if step.ExpectNonEmptyPlan {
|
|
|
|
log.Printf("[INFO] Got non-empty plan, as expected:\n\n%s", legacyPlanComparisonString(newState, p.Changes))
|
|
|
|
} else {
|
|
|
|
return state, fmt.Errorf(
|
|
|
|
"After applying this step, the plan was not empty:\n\n%s", legacyPlanComparisonString(newState, p.Changes))
|
|
|
|
}
|
|
|
|
}
|
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 23:24:45 +02:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
// And another after a Refresh.
|
|
|
|
if !step.Destroy || (step.Destroy && !step.PreventPostDestroyRefresh) {
|
|
|
|
newState, stepDiags = ctx.Refresh()
|
|
|
|
if stepDiags.HasErrors() {
|
2018-12-07 21:17:55 +01:00
|
|
|
return state, newOperationError("follow-up refresh", stepDiags)
|
2018-10-16 03:15:08 +02:00
|
|
|
}
|
2018-12-04 22:00:43 +01:00
|
|
|
|
|
|
|
state, err = shimNewState(newState, schemas)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
}
|
|
|
|
if p, stepDiags = ctx.Plan(); stepDiags.HasErrors() {
|
2018-12-07 21:17:55 +01:00
|
|
|
return state, newOperationError("second follow-up refresh", stepDiags)
|
2018-10-16 03:15:08 +02:00
|
|
|
}
|
|
|
|
empty := p.Changes.Empty()
|
|
|
|
|
|
|
|
// Data resources are tricky because they legitimately get instantiated
|
|
|
|
// during refresh so that they will be already populated during the
|
|
|
|
// plan walk. Because of this, if we have any data resources in the
|
|
|
|
// config we'll end up wanting to destroy them again here. This is
|
|
|
|
// acceptable and expected, and we'll treat it as "empty" for the
|
|
|
|
// sake of this testing.
|
|
|
|
if step.Destroy && !empty {
|
|
|
|
empty = true
|
|
|
|
for _, change := range p.Changes.Resources {
|
|
|
|
if change.Addr.Resource.Resource.Mode != addrs.DataResourceMode {
|
|
|
|
empty = false
|
|
|
|
break
|
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 23:24:45 +02:00
|
|
|
}
|
2018-10-16 03:15:08 +02: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 23:24:45 +02:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
if !empty {
|
|
|
|
if step.ExpectNonEmptyPlan {
|
|
|
|
log.Printf("[INFO] Got non-empty plan, as expected:\n\n%s", legacyPlanComparisonString(newState, p.Changes))
|
|
|
|
} else {
|
|
|
|
return state, fmt.Errorf(
|
|
|
|
"After applying this step and refreshing, "+
|
|
|
|
"the plan was not empty:\n\n%s", legacyPlanComparisonString(newState, p.Changes))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Made it here, but expected a non-empty plan, fail!
|
|
|
|
if step.ExpectNonEmptyPlan && empty {
|
|
|
|
return state, fmt.Errorf("Expected a non-empty plan, but got an empty plan!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Made it here? Good job test step!
|
|
|
|
return state, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// legacyPlanComparisonString produces a string representation of the changes
|
|
|
|
// from a plan and a given state togther, as was formerly produced by the
|
|
|
|
// String method of terraform.Plan.
|
|
|
|
//
|
|
|
|
// This is here only for compatibility with existing tests that predate our
|
|
|
|
// new plan and state types, and should not be used in new tests. Instead, use
|
|
|
|
// a library like "cmp" to do a deep equality check and diff on the two
|
|
|
|
// data structures.
|
|
|
|
func legacyPlanComparisonString(state *states.State, changes *plans.Changes) string {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"DIFF:\n\n%s\n\nSTATE:\n\n%s",
|
|
|
|
legacyDiffComparisonString(changes),
|
|
|
|
state.String(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// legacyDiffComparisonString produces a string representation of the changes
|
|
|
|
// from a planned changes object, as was formerly produced by the String method
|
|
|
|
// of terraform.Diff.
|
|
|
|
//
|
|
|
|
// This is here only for compatibility with existing tests that predate our
|
|
|
|
// new plan types, and should not be used in new tests. Instead, use a library
|
|
|
|
// like "cmp" to do a deep equality check and diff on the two data structures.
|
|
|
|
func legacyDiffComparisonString(changes *plans.Changes) string {
|
|
|
|
// The old string representation of a plan was grouped by module, but
|
|
|
|
// our new plan structure is not grouped in that way and so we'll need
|
|
|
|
// to preprocess it in order to produce that grouping.
|
|
|
|
type ResourceChanges struct {
|
|
|
|
Current *plans.ResourceInstanceChangeSrc
|
|
|
|
Deposed map[states.DeposedKey]*plans.ResourceInstanceChangeSrc
|
|
|
|
}
|
|
|
|
byModule := map[string]map[string]*ResourceChanges{}
|
|
|
|
resourceKeys := map[string][]string{}
|
|
|
|
var moduleKeys []string
|
|
|
|
for _, rc := range changes.Resources {
|
|
|
|
if rc.Action == plans.NoOp {
|
|
|
|
// We won't mention no-op changes here at all, since the old plan
|
|
|
|
// model we are emulating here didn't have such a concept.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
moduleKey := rc.Addr.Module.String()
|
|
|
|
if _, exists := byModule[moduleKey]; !exists {
|
|
|
|
moduleKeys = append(moduleKeys, moduleKey)
|
|
|
|
byModule[moduleKey] = make(map[string]*ResourceChanges)
|
|
|
|
}
|
|
|
|
resourceKey := rc.Addr.Resource.String()
|
|
|
|
if _, exists := byModule[moduleKey][resourceKey]; !exists {
|
|
|
|
resourceKeys[moduleKey] = append(resourceKeys[moduleKey], resourceKey)
|
|
|
|
byModule[moduleKey][resourceKey] = &ResourceChanges{
|
|
|
|
Deposed: make(map[states.DeposedKey]*plans.ResourceInstanceChangeSrc),
|
2016-05-10 05:32:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
if rc.DeposedKey == states.NotDeposed {
|
|
|
|
byModule[moduleKey][resourceKey].Current = rc
|
|
|
|
} else {
|
|
|
|
byModule[moduleKey][resourceKey].Deposed[rc.DeposedKey] = rc
|
2016-05-10 05:32:31 +02:00
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
}
|
|
|
|
sort.Strings(moduleKeys)
|
|
|
|
for _, ks := range resourceKeys {
|
|
|
|
sort.Strings(ks)
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
for _, moduleKey := range moduleKeys {
|
|
|
|
rcs := byModule[moduleKey]
|
|
|
|
var mBuf bytes.Buffer
|
|
|
|
|
|
|
|
for _, resourceKey := range resourceKeys[moduleKey] {
|
|
|
|
rc := rcs[resourceKey]
|
|
|
|
|
|
|
|
crud := "UPDATE"
|
|
|
|
if rc.Current != nil {
|
|
|
|
switch rc.Current.Action {
|
|
|
|
case plans.DeleteThenCreate:
|
|
|
|
crud = "DESTROY/CREATE"
|
|
|
|
case plans.CreateThenDelete:
|
|
|
|
crud = "CREATE/DESTROY"
|
|
|
|
case plans.Delete:
|
|
|
|
crud = "DESTROY"
|
|
|
|
case plans.Create:
|
|
|
|
crud = "CREATE"
|
|
|
|
}
|
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 23:24:45 +02:00
|
|
|
} else {
|
2018-10-16 03:15:08 +02:00
|
|
|
// We must be working on a deposed object then, in which
|
|
|
|
// case destroying is the only possible action.
|
|
|
|
crud = "DESTROY"
|
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 23:24:45 +02:00
|
|
|
}
|
2016-05-21 23:16:02 +02:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
extra := ""
|
|
|
|
if rc.Current == nil && len(rc.Deposed) > 0 {
|
|
|
|
extra = " (deposed only)"
|
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 23:24:45 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
fmt.Fprintf(
|
|
|
|
&mBuf, "%s: %s%s\n",
|
|
|
|
crud, resourceKey, extra,
|
|
|
|
)
|
|
|
|
|
|
|
|
attrNames := map[string]bool{}
|
|
|
|
var oldAttrs map[string]string
|
|
|
|
var newAttrs map[string]string
|
|
|
|
if rc.Current != nil {
|
|
|
|
if before := rc.Current.Before; before != nil {
|
|
|
|
ty, err := before.ImpliedType()
|
|
|
|
if err == nil {
|
|
|
|
val, err := before.Decode(ty)
|
|
|
|
if err == nil {
|
|
|
|
oldAttrs = hcl2shim.FlatmapValueFromHCL2(val)
|
|
|
|
for k := range oldAttrs {
|
|
|
|
attrNames[k] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if after := rc.Current.After; after != nil {
|
|
|
|
ty, err := after.ImpliedType()
|
|
|
|
if err == nil {
|
|
|
|
val, err := after.Decode(ty)
|
|
|
|
if err == nil {
|
|
|
|
newAttrs = hcl2shim.FlatmapValueFromHCL2(val)
|
|
|
|
for k := range newAttrs {
|
|
|
|
attrNames[k] = true
|
|
|
|
}
|
|
|
|
}
|
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 23:24:45 +02:00
|
|
|
}
|
2016-05-21 23:16:02 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
if oldAttrs == nil {
|
|
|
|
oldAttrs = make(map[string]string)
|
|
|
|
}
|
|
|
|
if newAttrs == nil {
|
|
|
|
newAttrs = make(map[string]string)
|
|
|
|
}
|
2016-05-21 23:16:02 +02:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
attrNamesOrder := make([]string, 0, len(attrNames))
|
|
|
|
keyLen := 0
|
|
|
|
for n := range attrNames {
|
|
|
|
attrNamesOrder = append(attrNamesOrder, n)
|
|
|
|
if len(n) > keyLen {
|
|
|
|
keyLen = len(n)
|
|
|
|
}
|
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 23:24:45 +02:00
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
sort.Strings(attrNamesOrder)
|
|
|
|
|
|
|
|
for _, attrK := range attrNamesOrder {
|
|
|
|
v := newAttrs[attrK]
|
|
|
|
u := oldAttrs[attrK]
|
|
|
|
|
|
|
|
if v == config.UnknownVariableValue {
|
|
|
|
v = "<computed>"
|
|
|
|
}
|
|
|
|
// NOTE: we don't support <sensitive> here because we would
|
|
|
|
// need schema to do that. Excluding sensitive values
|
|
|
|
// is now done at the UI layer, and so should not be tested
|
|
|
|
// at the core layer.
|
|
|
|
|
|
|
|
updateMsg := ""
|
|
|
|
// TODO: Mark " (forces new resource)" in updateMsg when appropriate.
|
|
|
|
|
|
|
|
fmt.Fprintf(
|
|
|
|
&mBuf, " %s:%s %#v => %#v%s\n",
|
|
|
|
attrK,
|
|
|
|
strings.Repeat(" ", keyLen-len(attrK)),
|
|
|
|
u, v,
|
|
|
|
updateMsg,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if moduleKey == "" { // root module
|
|
|
|
buf.Write(mBuf.Bytes())
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
continue
|
2016-05-10 05:32:31 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
fmt.Fprintf(&buf, "%s:\n", moduleKey)
|
|
|
|
s := bufio.NewScanner(&mBuf)
|
|
|
|
for s.Scan() {
|
|
|
|
buf.WriteString(fmt.Sprintf(" %s\n", s.Text()))
|
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 23:24:45 +02:00
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
}
|
2016-05-10 05:32:31 +02:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
return buf.String()
|
2016-05-10 05:32:31 +02:00
|
|
|
}
|
2018-05-25 16:50:30 +02:00
|
|
|
|
|
|
|
func testStepTaint(state *terraform.State, step TestStep) error {
|
|
|
|
for _, p := range step.Taint {
|
|
|
|
m := state.RootModule()
|
|
|
|
if m == nil {
|
|
|
|
return errors.New("no state")
|
|
|
|
}
|
|
|
|
rs, ok := m.Resources[p]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("resource %q not found in state", p)
|
|
|
|
}
|
|
|
|
log.Printf("[WARN] Test: Explicitly tainting resource %q", p)
|
|
|
|
rs.Taint()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|