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-02-18 17:23:34 -05:00
|
|
|
package views
|
2017-01-18 23:47:56 -05:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"reflect"
|
|
|
|
|
"testing"
|
|
|
|
|
|
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 17:24:45 -04:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
|
2021-05-17 15:00:50 -04:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2021-05-17 15:33:17 -04:00
|
|
|
"github.com/hashicorp/terraform/internal/plans"
|
2021-05-17 15:43:35 -04:00
|
|
|
"github.com/hashicorp/terraform/internal/states"
|
2021-05-17 15:46:19 -04:00
|
|
|
"github.com/hashicorp/terraform/internal/terraform"
|
2020-11-18 17:38:18 -05:00
|
|
|
|
|
|
|
|
legacy "github.com/hashicorp/terraform/internal/legacy/terraform"
|
2017-01-18 23:47:56 -05:00
|
|
|
)
|
|
|
|
|
|
Refactor terraform.Hook to use a resource-identifying wrapper struct
The terraform.Hook interface lets other areas of code perform streaming
reactions to various events, generally in the service of some UI somewhere.
Nearly all of the methods on this interface take an `addrs.AbsResourceInstance`
as their first argument, to identify the resource that's being operated on.
However, that addrs struct doesn't necessarily contain everything you might want
in order to uniquely and usefully identify a resource. It has the module
instance and resource instance addresses, but it lacks the provider source
address, which can affect how the consuming UI should display the resource's
events. (For example, Terraform Cloud wants reliable info about who maintains a
given provider, what cloud provider it operates on, and where to find its
documentation.)
Instead of polluting `addrs.AbsResourceInstance` with extra information that
isn't relevant to other call sites, let's change the first argument of each Hook
method to be a wrapper struct defined in the package that owns the Hook
interface, and add the provider address to that wrapper as a sibling of the
resource address. This causes a big noisy commit today, but should streamline
future updates to the UI-facing "identity" of a resource; existing callers can
ignore any new fields they're uninterested in, or exploit new info as needed.
Other than making new information available for future edits to Hook
implementing types, this commit should have no effect on existing behavior.
2024-02-27 20:42:17 -05:00
|
|
|
func testCountHookResourceID(addr addrs.AbsResourceInstance) terraform.HookResourceIdentity {
|
|
|
|
|
return terraform.HookResourceIdentity{
|
|
|
|
|
Addr: addr,
|
|
|
|
|
ProviderAddr: addrs.Provider{
|
|
|
|
|
Type: "test",
|
|
|
|
|
Namespace: "hashicorp",
|
|
|
|
|
Hostname: "example.com",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-18 23:47:56 -05:00
|
|
|
func TestCountHook_impl(t *testing.T) {
|
2021-02-18 17:23:34 -05:00
|
|
|
var _ terraform.Hook = new(countHook)
|
2017-01-18 23:47:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCountHookPostDiff_DestroyDeposed(t *testing.T) {
|
2021-02-18 17:23:34 -05:00
|
|
|
h := new(countHook)
|
2017-01-18 23:47:56 -05:00
|
|
|
|
2020-11-18 17:38:18 -05:00
|
|
|
resources := map[string]*legacy.InstanceDiff{
|
2024-02-26 19:48:51 -05:00
|
|
|
"lorem": {DestroyDeposed: true},
|
2017-01-18 23:47:56 -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 17:24:45 -04:00
|
|
|
for k := range resources {
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: k,
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
2017-01-18 23:47:56 -05:00
|
|
|
|
2026-01-07 05:00:55 -05:00
|
|
|
h.PostDiff(testCountHookResourceID(addr), states.DeposedKey("deadbeef"), plans.Delete, cty.DynamicVal, cty.DynamicVal, nil)
|
2017-01-18 23:47:56 -05:00
|
|
|
}
|
|
|
|
|
|
2021-02-18 17:23:34 -05:00
|
|
|
expected := new(countHook)
|
2017-01-18 23:47:56 -05:00
|
|
|
expected.ToAdd = 0
|
|
|
|
|
expected.ToChange = 0
|
|
|
|
|
expected.ToRemoveAndAdd = 0
|
|
|
|
|
expected.ToRemove = 1
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expected, h) {
|
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 17:24:45 -04:00
|
|
|
t.Fatalf("Expected %#v, got %#v instead.", expected, h)
|
2017-01-18 23:47:56 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCountHookPostDiff_DestroyOnly(t *testing.T) {
|
2021-02-18 17:23:34 -05:00
|
|
|
h := new(countHook)
|
2017-01-18 23:47:56 -05:00
|
|
|
|
2020-11-18 17:38:18 -05:00
|
|
|
resources := map[string]*legacy.InstanceDiff{
|
2024-02-26 19:48:51 -05:00
|
|
|
"foo": {Destroy: true},
|
|
|
|
|
"bar": {Destroy: true},
|
|
|
|
|
"lorem": {Destroy: true},
|
|
|
|
|
"ipsum": {Destroy: true},
|
2017-01-18 23:47:56 -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 17:24:45 -04:00
|
|
|
for k := range resources {
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: k,
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
2017-01-18 23:47:56 -05:00
|
|
|
|
2026-01-07 05:00:55 -05:00
|
|
|
h.PostDiff(testCountHookResourceID(addr), addrs.NotDeposed, plans.Delete, cty.DynamicVal, cty.DynamicVal, nil)
|
2017-01-18 23:47:56 -05:00
|
|
|
}
|
|
|
|
|
|
2021-02-18 17:23:34 -05:00
|
|
|
expected := new(countHook)
|
2017-01-18 23:47:56 -05:00
|
|
|
expected.ToAdd = 0
|
|
|
|
|
expected.ToChange = 0
|
|
|
|
|
expected.ToRemoveAndAdd = 0
|
|
|
|
|
expected.ToRemove = 4
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expected, h) {
|
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 17:24:45 -04:00
|
|
|
t.Fatalf("Expected %#v, got %#v instead.", expected, h)
|
2017-01-18 23:47:56 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCountHookPostDiff_AddOnly(t *testing.T) {
|
2021-02-18 17:23:34 -05:00
|
|
|
h := new(countHook)
|
2017-01-18 23:47:56 -05:00
|
|
|
|
2020-11-18 17:38:18 -05:00
|
|
|
resources := map[string]*legacy.InstanceDiff{
|
2024-02-26 19:48:51 -05:00
|
|
|
"foo": {
|
2020-11-18 17:38:18 -05:00
|
|
|
Attributes: map[string]*legacy.ResourceAttrDiff{
|
2024-02-26 19:48:51 -05:00
|
|
|
"foo": {RequiresNew: true},
|
2017-01-18 23:47:56 -05:00
|
|
|
},
|
|
|
|
|
},
|
2024-02-26 19:48:51 -05:00
|
|
|
"bar": {
|
2020-11-18 17:38:18 -05:00
|
|
|
Attributes: map[string]*legacy.ResourceAttrDiff{
|
2024-02-26 19:48:51 -05:00
|
|
|
"foo": {RequiresNew: true},
|
2017-01-18 23:47:56 -05:00
|
|
|
},
|
|
|
|
|
},
|
2024-02-26 19:48:51 -05:00
|
|
|
"lorem": {
|
2020-11-18 17:38:18 -05:00
|
|
|
Attributes: map[string]*legacy.ResourceAttrDiff{
|
2024-02-26 19:48:51 -05:00
|
|
|
"foo": {RequiresNew: true},
|
2017-01-18 23:47:56 -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 17:24:45 -04:00
|
|
|
for k := range resources {
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: k,
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
2017-01-18 23:47:56 -05:00
|
|
|
|
2026-01-07 05:00:55 -05:00
|
|
|
h.PostDiff(testCountHookResourceID(addr), addrs.NotDeposed, plans.Create, cty.DynamicVal, cty.DynamicVal, nil)
|
2017-01-18 23:47:56 -05:00
|
|
|
}
|
|
|
|
|
|
2021-02-18 17:23:34 -05:00
|
|
|
expected := new(countHook)
|
2017-01-18 23:47:56 -05:00
|
|
|
expected.ToAdd = 3
|
|
|
|
|
expected.ToChange = 0
|
|
|
|
|
expected.ToRemoveAndAdd = 0
|
|
|
|
|
expected.ToRemove = 0
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expected, h) {
|
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 17:24:45 -04:00
|
|
|
t.Fatalf("Expected %#v, got %#v instead.", expected, h)
|
2017-01-18 23:47:56 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCountHookPostDiff_ChangeOnly(t *testing.T) {
|
2021-02-18 17:23:34 -05:00
|
|
|
h := new(countHook)
|
2017-01-18 23:47:56 -05:00
|
|
|
|
2020-11-18 17:38:18 -05:00
|
|
|
resources := map[string]*legacy.InstanceDiff{
|
2024-02-26 19:48:51 -05:00
|
|
|
"foo": {
|
2017-01-18 23:47:56 -05:00
|
|
|
Destroy: false,
|
2020-11-18 17:38:18 -05:00
|
|
|
Attributes: map[string]*legacy.ResourceAttrDiff{
|
2024-02-26 19:48:51 -05:00
|
|
|
"foo": {},
|
2017-01-18 23:47:56 -05:00
|
|
|
},
|
|
|
|
|
},
|
2024-02-26 19:48:51 -05:00
|
|
|
"bar": {
|
2017-01-18 23:47:56 -05:00
|
|
|
Destroy: false,
|
2020-11-18 17:38:18 -05:00
|
|
|
Attributes: map[string]*legacy.ResourceAttrDiff{
|
2024-02-26 19:48:51 -05:00
|
|
|
"foo": {},
|
2017-01-18 23:47:56 -05:00
|
|
|
},
|
|
|
|
|
},
|
2024-02-26 19:48:51 -05:00
|
|
|
"lorem": {
|
2017-01-18 23:47:56 -05:00
|
|
|
Destroy: false,
|
2020-11-18 17:38:18 -05:00
|
|
|
Attributes: map[string]*legacy.ResourceAttrDiff{
|
2024-02-26 19:48:51 -05:00
|
|
|
"foo": {},
|
2017-01-18 23:47:56 -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 17:24:45 -04:00
|
|
|
for k := range resources {
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: k,
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
2017-01-18 23:47:56 -05:00
|
|
|
|
2026-01-07 05:00:55 -05:00
|
|
|
h.PostDiff(testCountHookResourceID(addr), addrs.NotDeposed, plans.Update, cty.DynamicVal, cty.DynamicVal, nil)
|
2017-01-18 23:47:56 -05:00
|
|
|
}
|
|
|
|
|
|
2021-02-18 17:23:34 -05:00
|
|
|
expected := new(countHook)
|
2017-01-18 23:47:56 -05:00
|
|
|
expected.ToAdd = 0
|
|
|
|
|
expected.ToChange = 3
|
|
|
|
|
expected.ToRemoveAndAdd = 0
|
|
|
|
|
expected.ToRemove = 0
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expected, h) {
|
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 17:24:45 -04:00
|
|
|
t.Fatalf("Expected %#v, got %#v instead.", expected, h)
|
2017-01-18 23:47:56 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCountHookPostDiff_Mixed(t *testing.T) {
|
2021-02-18 17:23:34 -05:00
|
|
|
h := new(countHook)
|
2017-01-18 23:47:56 -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 17:24:45 -04:00
|
|
|
resources := map[string]plans.Action{
|
|
|
|
|
"foo": plans.Delete,
|
|
|
|
|
"bar": plans.NoOp,
|
|
|
|
|
"lorem": plans.Update,
|
|
|
|
|
"ipsum": plans.Delete,
|
2017-01-18 23:47:56 -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 17:24:45 -04:00
|
|
|
for k, a := range resources {
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: k,
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
2017-01-18 23:47:56 -05:00
|
|
|
|
2026-01-07 05:00:55 -05:00
|
|
|
h.PostDiff(testCountHookResourceID(addr), addrs.NotDeposed, a, cty.DynamicVal, cty.DynamicVal, nil)
|
2017-01-18 23:47:56 -05:00
|
|
|
}
|
|
|
|
|
|
2021-02-18 17:23:34 -05:00
|
|
|
expected := new(countHook)
|
2017-01-18 23:47:56 -05:00
|
|
|
expected.ToAdd = 0
|
|
|
|
|
expected.ToChange = 1
|
|
|
|
|
expected.ToRemoveAndAdd = 0
|
|
|
|
|
expected.ToRemove = 2
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expected, h) {
|
|
|
|
|
t.Fatalf("Expected %#v, got %#v instead.",
|
|
|
|
|
expected, h)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCountHookPostDiff_NoChange(t *testing.T) {
|
2021-02-18 17:23:34 -05:00
|
|
|
h := new(countHook)
|
2017-01-18 23:47:56 -05:00
|
|
|
|
2020-11-18 17:38:18 -05:00
|
|
|
resources := map[string]*legacy.InstanceDiff{
|
2024-02-26 19:48:51 -05:00
|
|
|
"foo": {},
|
|
|
|
|
"bar": {},
|
|
|
|
|
"lorem": {},
|
|
|
|
|
"ipsum": {},
|
2017-01-18 23:47:56 -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 17:24:45 -04:00
|
|
|
for k := range resources {
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: k,
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
2017-01-18 23:47:56 -05:00
|
|
|
|
2026-01-07 05:00:55 -05:00
|
|
|
h.PostDiff(testCountHookResourceID(addr), addrs.NotDeposed, plans.NoOp, cty.DynamicVal, cty.DynamicVal, nil)
|
2017-01-18 23:47:56 -05:00
|
|
|
}
|
|
|
|
|
|
2021-02-18 17:23:34 -05:00
|
|
|
expected := new(countHook)
|
2017-01-18 23:47:56 -05:00
|
|
|
expected.ToAdd = 0
|
|
|
|
|
expected.ToChange = 0
|
|
|
|
|
expected.ToRemoveAndAdd = 0
|
|
|
|
|
expected.ToRemove = 0
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expected, h) {
|
|
|
|
|
t.Fatalf("Expected %#v, got %#v instead.",
|
|
|
|
|
expected, h)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCountHookPostDiff_DataSource(t *testing.T) {
|
2021-02-18 17:23:34 -05:00
|
|
|
h := new(countHook)
|
2017-01-18 23:47:56 -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 17:24:45 -04:00
|
|
|
resources := map[string]plans.Action{
|
|
|
|
|
"foo": plans.Delete,
|
|
|
|
|
"bar": plans.NoOp,
|
|
|
|
|
"lorem": plans.Update,
|
|
|
|
|
"ipsum": plans.Delete,
|
2017-01-18 23:47:56 -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 17:24:45 -04:00
|
|
|
for k, a := range resources {
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.DataResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: k,
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
|
|
|
|
|
2026-01-07 05:00:55 -05:00
|
|
|
h.PostDiff(testCountHookResourceID(addr), addrs.NotDeposed, a, cty.DynamicVal, cty.DynamicVal, nil)
|
2017-01-18 23:47:56 -05:00
|
|
|
}
|
|
|
|
|
|
2021-02-18 17:23:34 -05:00
|
|
|
expected := new(countHook)
|
2017-01-18 23:47:56 -05:00
|
|
|
expected.ToAdd = 0
|
|
|
|
|
expected.ToChange = 0
|
|
|
|
|
expected.ToRemoveAndAdd = 0
|
|
|
|
|
expected.ToRemove = 0
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expected, h) {
|
|
|
|
|
t.Fatalf("Expected %#v, got %#v instead.",
|
|
|
|
|
expected, h)
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-11 10:33:24 -05:00
|
|
|
|
|
|
|
|
func TestCountHookApply_ChangeOnly(t *testing.T) {
|
2021-02-18 17:23:34 -05:00
|
|
|
h := new(countHook)
|
2018-12-11 10:33:24 -05:00
|
|
|
|
2020-11-18 17:38:18 -05:00
|
|
|
resources := map[string]*legacy.InstanceDiff{
|
2024-02-26 19:48:51 -05:00
|
|
|
"foo": {
|
2018-12-11 10:33:24 -05:00
|
|
|
Destroy: false,
|
2020-11-18 17:38:18 -05:00
|
|
|
Attributes: map[string]*legacy.ResourceAttrDiff{
|
2024-02-26 19:48:51 -05:00
|
|
|
"foo": {},
|
2018-12-11 10:33:24 -05:00
|
|
|
},
|
|
|
|
|
},
|
2024-02-26 19:48:51 -05:00
|
|
|
"bar": {
|
2018-12-11 10:33:24 -05:00
|
|
|
Destroy: false,
|
2020-11-18 17:38:18 -05:00
|
|
|
Attributes: map[string]*legacy.ResourceAttrDiff{
|
2024-02-26 19:48:51 -05:00
|
|
|
"foo": {},
|
2018-12-11 10:33:24 -05:00
|
|
|
},
|
|
|
|
|
},
|
2024-02-26 19:48:51 -05:00
|
|
|
"lorem": {
|
2018-12-11 10:33:24 -05:00
|
|
|
Destroy: false,
|
2020-11-18 17:38:18 -05:00
|
|
|
Attributes: map[string]*legacy.ResourceAttrDiff{
|
2024-02-26 19:48:51 -05:00
|
|
|
"foo": {},
|
2018-12-11 10:33:24 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for k := range resources {
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: k,
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
|
|
|
|
|
Refactor terraform.Hook to use a resource-identifying wrapper struct
The terraform.Hook interface lets other areas of code perform streaming
reactions to various events, generally in the service of some UI somewhere.
Nearly all of the methods on this interface take an `addrs.AbsResourceInstance`
as their first argument, to identify the resource that's being operated on.
However, that addrs struct doesn't necessarily contain everything you might want
in order to uniquely and usefully identify a resource. It has the module
instance and resource instance addresses, but it lacks the provider source
address, which can affect how the consuming UI should display the resource's
events. (For example, Terraform Cloud wants reliable info about who maintains a
given provider, what cloud provider it operates on, and where to find its
documentation.)
Instead of polluting `addrs.AbsResourceInstance` with extra information that
isn't relevant to other call sites, let's change the first argument of each Hook
method to be a wrapper struct defined in the package that owns the Hook
interface, and add the provider address to that wrapper as a sibling of the
resource address. This causes a big noisy commit today, but should streamline
future updates to the UI-facing "identity" of a resource; existing callers can
ignore any new fields they're uninterested in, or exploit new info as needed.
Other than making new information available for future edits to Hook
implementing types, this commit should have no effect on existing behavior.
2024-02-27 20:42:17 -05:00
|
|
|
h.PreApply(testCountHookResourceID(addr), addrs.NotDeposed, plans.Update, cty.DynamicVal, cty.DynamicVal)
|
|
|
|
|
h.PostApply(testCountHookResourceID(addr), addrs.NotDeposed, cty.DynamicVal, nil)
|
2018-12-11 10:33:24 -05:00
|
|
|
}
|
|
|
|
|
|
2021-02-18 17:23:34 -05:00
|
|
|
expected := &countHook{pending: make(map[string]plans.Action)}
|
2018-12-11 10:33:24 -05:00
|
|
|
expected.Added = 0
|
|
|
|
|
expected.Changed = 3
|
|
|
|
|
expected.Removed = 0
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expected, h) {
|
|
|
|
|
t.Fatalf("Expected:\n%#v\nGot:\n%#v\n", expected, h)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCountHookApply_DestroyOnly(t *testing.T) {
|
2021-02-18 17:23:34 -05:00
|
|
|
h := new(countHook)
|
2018-12-11 10:33:24 -05:00
|
|
|
|
2020-11-18 17:38:18 -05:00
|
|
|
resources := map[string]*legacy.InstanceDiff{
|
2024-02-26 19:48:51 -05:00
|
|
|
"foo": {Destroy: true},
|
|
|
|
|
"bar": {Destroy: true},
|
|
|
|
|
"lorem": {Destroy: true},
|
|
|
|
|
"ipsum": {Destroy: true},
|
2018-12-11 10:33:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for k := range resources {
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: k,
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
|
|
|
|
|
Refactor terraform.Hook to use a resource-identifying wrapper struct
The terraform.Hook interface lets other areas of code perform streaming
reactions to various events, generally in the service of some UI somewhere.
Nearly all of the methods on this interface take an `addrs.AbsResourceInstance`
as their first argument, to identify the resource that's being operated on.
However, that addrs struct doesn't necessarily contain everything you might want
in order to uniquely and usefully identify a resource. It has the module
instance and resource instance addresses, but it lacks the provider source
address, which can affect how the consuming UI should display the resource's
events. (For example, Terraform Cloud wants reliable info about who maintains a
given provider, what cloud provider it operates on, and where to find its
documentation.)
Instead of polluting `addrs.AbsResourceInstance` with extra information that
isn't relevant to other call sites, let's change the first argument of each Hook
method to be a wrapper struct defined in the package that owns the Hook
interface, and add the provider address to that wrapper as a sibling of the
resource address. This causes a big noisy commit today, but should streamline
future updates to the UI-facing "identity" of a resource; existing callers can
ignore any new fields they're uninterested in, or exploit new info as needed.
Other than making new information available for future edits to Hook
implementing types, this commit should have no effect on existing behavior.
2024-02-27 20:42:17 -05:00
|
|
|
h.PreApply(testCountHookResourceID(addr), addrs.NotDeposed, plans.Delete, cty.DynamicVal, cty.DynamicVal)
|
|
|
|
|
h.PostApply(testCountHookResourceID(addr), addrs.NotDeposed, cty.DynamicVal, nil)
|
2018-12-11 10:33:24 -05:00
|
|
|
}
|
|
|
|
|
|
2021-02-18 17:23:34 -05:00
|
|
|
expected := &countHook{pending: make(map[string]plans.Action)}
|
2018-12-11 10:33:24 -05:00
|
|
|
expected.Added = 0
|
|
|
|
|
expected.Changed = 0
|
|
|
|
|
expected.Removed = 4
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expected, h) {
|
|
|
|
|
t.Fatalf("Expected:\n%#v\nGot:\n%#v\n", expected, h)
|
|
|
|
|
}
|
|
|
|
|
}
|