Merge pull request #10279 from hashicorp/jbardin/GH-9996
Set proper Mode when moving a data source in state
This commit is contained in:
commit
47b66a28b9
|
@ -1,8 +1,6 @@
|
||||||
package terraform
|
package terraform
|
||||||
|
|
||||||
import (
|
import "fmt"
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Add adds the item in the state at the given address.
|
// Add adds the item in the state at the given address.
|
||||||
//
|
//
|
||||||
|
@ -34,6 +32,7 @@ import (
|
||||||
//
|
//
|
||||||
func (s *State) Add(fromAddrRaw string, toAddrRaw string, raw interface{}) error {
|
func (s *State) Add(fromAddrRaw string, toAddrRaw string, raw interface{}) error {
|
||||||
// Parse the address
|
// Parse the address
|
||||||
|
|
||||||
toAddr, err := ParseResourceAddress(toAddrRaw)
|
toAddr, err := ParseResourceAddress(toAddrRaw)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -114,6 +113,7 @@ func stateAddFunc_Module_Module(s *State, fromAddr, addr *ResourceAddress, raw i
|
||||||
addrCopy.Type = resourceKey.Type
|
addrCopy.Type = resourceKey.Type
|
||||||
addrCopy.Name = resourceKey.Name
|
addrCopy.Name = resourceKey.Name
|
||||||
addrCopy.Index = resourceKey.Index
|
addrCopy.Index = resourceKey.Index
|
||||||
|
addrCopy.Mode = resourceKey.Mode
|
||||||
|
|
||||||
// Perform an add
|
// Perform an add
|
||||||
if err := s.Add(fromAddr.String(), addrCopy.String(), v); err != nil {
|
if err := s.Add(fromAddr.String(), addrCopy.String(), v); err != nil {
|
||||||
|
@ -333,6 +333,7 @@ func stateAddInitAddr(s *State, addr *ResourceAddress) (interface{}, bool) {
|
||||||
Name: addr.Name,
|
Name: addr.Name,
|
||||||
Type: addr.Type,
|
Type: addr.Type,
|
||||||
Index: addr.Index,
|
Index: addr.Index,
|
||||||
|
Mode: addr.Mode,
|
||||||
}).String()
|
}).String()
|
||||||
exists = true
|
exists = true
|
||||||
resource, ok := mod.Resources[resourceKey]
|
resource, ok := mod.Resources[resourceKey]
|
||||||
|
|
|
@ -1,17 +1,20 @@
|
||||||
package terraform
|
package terraform
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestStateAdd(t *testing.T) {
|
func TestStateAdd(t *testing.T) {
|
||||||
cases := map[string]struct {
|
cases := []struct {
|
||||||
|
Name string
|
||||||
Err bool
|
Err bool
|
||||||
From, To string
|
From, To string
|
||||||
Value interface{}
|
Value interface{}
|
||||||
One, Two *State
|
One, Two *State
|
||||||
}{
|
}{
|
||||||
"ModuleState => Module Addr (new)": {
|
{
|
||||||
|
"ModuleState => Module Addr (new)",
|
||||||
false,
|
false,
|
||||||
"",
|
"",
|
||||||
"module.foo",
|
"module.foo",
|
||||||
|
@ -59,7 +62,8 @@ func TestStateAdd(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
"ModuleState => Nested Module Addr (new)": {
|
{
|
||||||
|
"ModuleState => Nested Module Addr (new)",
|
||||||
false,
|
false,
|
||||||
"",
|
"",
|
||||||
"module.foo.module.bar",
|
"module.foo.module.bar",
|
||||||
|
@ -107,7 +111,8 @@ func TestStateAdd(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
"ModuleState w/ outputs and deps => Module Addr (new)": {
|
{
|
||||||
|
"ModuleState w/ outputs and deps => Module Addr (new)",
|
||||||
false,
|
false,
|
||||||
"",
|
"",
|
||||||
"module.foo",
|
"module.foo",
|
||||||
|
@ -171,7 +176,8 @@ func TestStateAdd(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
"ModuleState => Module Addr (existing)": {
|
{
|
||||||
|
"ModuleState => Module Addr (existing)",
|
||||||
true,
|
true,
|
||||||
"",
|
"",
|
||||||
"module.foo",
|
"module.foo",
|
||||||
|
@ -194,7 +200,8 @@ func TestStateAdd(t *testing.T) {
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
|
|
||||||
"ModuleState with children => Module Addr (new)": {
|
{
|
||||||
|
"ModuleState with children => Module Addr (new)",
|
||||||
false,
|
false,
|
||||||
"module.foo",
|
"module.foo",
|
||||||
"module.bar",
|
"module.bar",
|
||||||
|
@ -278,7 +285,8 @@ func TestStateAdd(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
"ResourceState => Resource Addr (new)": {
|
{
|
||||||
|
"ResourceState => Resource Addr (new)",
|
||||||
false,
|
false,
|
||||||
"aws_instance.bar",
|
"aws_instance.bar",
|
||||||
"aws_instance.foo",
|
"aws_instance.foo",
|
||||||
|
@ -307,7 +315,8 @@ func TestStateAdd(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
"ResourceState w/ deps, provider => Resource Addr (new)": {
|
{
|
||||||
|
"ResourceState w/ deps, provider => Resource Addr (new)",
|
||||||
false,
|
false,
|
||||||
"aws_instance.bar",
|
"aws_instance.bar",
|
||||||
"aws_instance.foo",
|
"aws_instance.foo",
|
||||||
|
@ -340,7 +349,8 @@ func TestStateAdd(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
"ResourceState tainted => Resource Addr (new)": {
|
{
|
||||||
|
"ResourceState tainted => Resource Addr (new)",
|
||||||
false,
|
false,
|
||||||
"aws_instance.bar",
|
"aws_instance.bar",
|
||||||
"aws_instance.foo",
|
"aws_instance.foo",
|
||||||
|
@ -371,7 +381,8 @@ func TestStateAdd(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
"ResourceState with count unspecified => Resource Addr (new)": {
|
{
|
||||||
|
"ResourceState with count unspecified => Resource Addr (new)",
|
||||||
false,
|
false,
|
||||||
"aws_instance.bar",
|
"aws_instance.bar",
|
||||||
"aws_instance.foo",
|
"aws_instance.foo",
|
||||||
|
@ -416,7 +427,8 @@ func TestStateAdd(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
"ResourceState with count unspecified => Resource Addr (new with count)": {
|
{
|
||||||
|
"ResourceState with count unspecified => Resource Addr (new with count)",
|
||||||
true,
|
true,
|
||||||
"aws_instance.bar",
|
"aws_instance.bar",
|
||||||
"aws_instance.foo[0]",
|
"aws_instance.foo[0]",
|
||||||
|
@ -440,7 +452,8 @@ func TestStateAdd(t *testing.T) {
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
|
|
||||||
"ResourceState with single count unspecified => Resource Addr (new with count)": {
|
{
|
||||||
|
"ResourceState with single count unspecified => Resource Addr (new with count)",
|
||||||
false,
|
false,
|
||||||
"aws_instance.bar",
|
"aws_instance.bar",
|
||||||
"aws_instance.foo[0]",
|
"aws_instance.foo[0]",
|
||||||
|
@ -471,7 +484,8 @@ func TestStateAdd(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
"ResourceState => Resource Addr (new with count)": {
|
{
|
||||||
|
"ResourceState => Resource Addr (new with count)",
|
||||||
false,
|
false,
|
||||||
"aws_instance.bar",
|
"aws_instance.bar",
|
||||||
"aws_instance.foo[0]",
|
"aws_instance.foo[0]",
|
||||||
|
@ -500,7 +514,8 @@ func TestStateAdd(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
"ResourceState => Resource Addr (existing)": {
|
{
|
||||||
|
"ResourceState => Resource Addr (existing)",
|
||||||
true,
|
true,
|
||||||
"aws_instance.bar",
|
"aws_instance.bar",
|
||||||
"aws_instance.foo",
|
"aws_instance.foo",
|
||||||
|
@ -529,7 +544,8 @@ func TestStateAdd(t *testing.T) {
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
|
|
||||||
"ResourceState => Module (new)": {
|
{
|
||||||
|
"ResourceState => Module (new)",
|
||||||
false,
|
false,
|
||||||
"aws_instance.bar",
|
"aws_instance.bar",
|
||||||
"module.foo",
|
"module.foo",
|
||||||
|
@ -558,7 +574,8 @@ func TestStateAdd(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
"InstanceState => Resource (new)": {
|
{
|
||||||
|
"InstanceState => Resource (new)",
|
||||||
false,
|
false,
|
||||||
"aws_instance.bar.primary",
|
"aws_instance.bar.primary",
|
||||||
"aws_instance.baz",
|
"aws_instance.baz",
|
||||||
|
@ -584,7 +601,8 @@ func TestStateAdd(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
"InstanceState => Module (new)": {
|
{
|
||||||
|
"InstanceState => Module (new)",
|
||||||
false,
|
false,
|
||||||
"aws_instance.bar.primary",
|
"aws_instance.bar.primary",
|
||||||
"module.foo",
|
"module.foo",
|
||||||
|
@ -609,32 +627,69 @@ func TestStateAdd(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
"ModuleState => Module Addr (new with data source)",
|
||||||
|
false,
|
||||||
|
"",
|
||||||
|
"module.foo",
|
||||||
|
&ModuleState{
|
||||||
|
Path: rootModulePath,
|
||||||
|
Resources: map[string]*ResourceState{
|
||||||
|
"data.test_instance.foo": &ResourceState{
|
||||||
|
Type: "test_instance",
|
||||||
|
Primary: &InstanceState{
|
||||||
|
ID: "foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
&State{},
|
||||||
|
&State{
|
||||||
|
Modules: []*ModuleState{
|
||||||
|
&ModuleState{
|
||||||
|
Path: []string{"root", "foo"},
|
||||||
|
Resources: map[string]*ResourceState{
|
||||||
|
"data.test_instance.foo": &ResourceState{
|
||||||
|
Type: "test_instance",
|
||||||
|
Primary: &InstanceState{
|
||||||
|
ID: "foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, tc := range cases {
|
for i, tc := range cases {
|
||||||
// Make sure they're both initialized as normal
|
t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
|
||||||
tc.One.init()
|
// Make sure they're both initialized as normal
|
||||||
if tc.Two != nil {
|
tc.One.init()
|
||||||
tc.Two.init()
|
if tc.Two != nil {
|
||||||
}
|
tc.Two.init()
|
||||||
|
}
|
||||||
|
|
||||||
// Add the value
|
// Add the value
|
||||||
err := tc.One.Add(tc.From, tc.To, tc.Value)
|
err := tc.One.Add(tc.From, tc.To, tc.Value)
|
||||||
if (err != nil) != tc.Err {
|
if (err != nil) != tc.Err {
|
||||||
t.Fatalf("bad: %s\n\n%s", k, err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if tc.Err {
|
if tc.Err {
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prune them both to be sure
|
// Prune them both to be sure
|
||||||
tc.One.prune()
|
tc.One.prune()
|
||||||
tc.Two.prune()
|
tc.Two.prune()
|
||||||
|
|
||||||
// Verify equality
|
// Verify equality
|
||||||
if !tc.One.Equal(tc.Two) {
|
if !tc.One.Equal(tc.Two) {
|
||||||
//t.Fatalf("Bad: %s\n\n%#v\n\n%#v", k, tc.One, tc.Two)
|
//t.Fatalf("Bad: %s\n\n%#v\n\n%#v", k, tc.One, tc.Two)
|
||||||
t.Fatalf("Bad: %s\n\n%s\n\n%s", k, tc.One.String(), tc.Two.String())
|
t.Fatalf("Bad: \n\n%s\n\n%s", tc.One.String(), tc.Two.String())
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue