terraform: state add resource existing fails
This commit is contained in:
parent
21d7ffc3f3
commit
c324062645
|
@ -98,7 +98,11 @@ func stateAddFunc_Resource_Resource(s *State, addr *ResourceAddress, raw interfa
|
||||||
src := raw.(*ResourceState)
|
src := raw.(*ResourceState)
|
||||||
|
|
||||||
// Initialize the resource
|
// Initialize the resource
|
||||||
resource := stateAddInitAddr(s, addr).(*ResourceState)
|
resourceRaw, exists := stateAddInitAddr(s, addr)
|
||||||
|
if exists {
|
||||||
|
return fmt.Errorf("resource exists and not empty: %s", addr)
|
||||||
|
}
|
||||||
|
resource := resourceRaw.(*ResourceState)
|
||||||
resource.Type = src.Type
|
resource.Type = src.Type
|
||||||
|
|
||||||
// TODO: Dependencies
|
// TODO: Dependencies
|
||||||
|
@ -218,17 +222,19 @@ func detectValueAddLoc(raw interface{}) stateAddLoc {
|
||||||
// stateAddInitAddr takes a ResourceAddress and creates the non-existing
|
// stateAddInitAddr takes a ResourceAddress and creates the non-existing
|
||||||
// resources up to that point, returning the empty (or existing) interface
|
// resources up to that point, returning the empty (or existing) interface
|
||||||
// at that address.
|
// at that address.
|
||||||
func stateAddInitAddr(s *State, addr *ResourceAddress) interface{} {
|
func stateAddInitAddr(s *State, addr *ResourceAddress) (interface{}, bool) {
|
||||||
addType := detectAddrAddLoc(addr)
|
addType := detectAddrAddLoc(addr)
|
||||||
|
|
||||||
// Get the module
|
// Get the module
|
||||||
path := append([]string{"root"}, addr.Path...)
|
path := append([]string{"root"}, addr.Path...)
|
||||||
|
exists := true
|
||||||
mod := s.ModuleByPath(path)
|
mod := s.ModuleByPath(path)
|
||||||
if mod == nil {
|
if mod == nil {
|
||||||
mod = s.AddModule(path)
|
mod = s.AddModule(path)
|
||||||
|
exists = false
|
||||||
}
|
}
|
||||||
if addType == stateAddModule {
|
if addType == stateAddModule {
|
||||||
return mod
|
return mod, exists
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the resource
|
// Add the resource
|
||||||
|
@ -237,17 +243,20 @@ func stateAddInitAddr(s *State, addr *ResourceAddress) interface{} {
|
||||||
Type: addr.Type,
|
Type: addr.Type,
|
||||||
Index: addr.Index,
|
Index: addr.Index,
|
||||||
}).String()
|
}).String()
|
||||||
|
exists = true
|
||||||
resource, ok := mod.Resources[resourceKey]
|
resource, ok := mod.Resources[resourceKey]
|
||||||
if !ok {
|
if !ok {
|
||||||
resource = &ResourceState{Type: addr.Type}
|
resource = &ResourceState{Type: addr.Type}
|
||||||
resource.init()
|
resource.init()
|
||||||
mod.Resources[resourceKey] = resource
|
mod.Resources[resourceKey] = resource
|
||||||
|
exists = false
|
||||||
}
|
}
|
||||||
if addType == stateAddResource {
|
if addType == stateAddResource {
|
||||||
return resource
|
return resource, exists
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the instance
|
// Get the instance
|
||||||
|
exists = true
|
||||||
var instance *InstanceState
|
var instance *InstanceState
|
||||||
switch addr.InstanceType {
|
switch addr.InstanceType {
|
||||||
case TypePrimary:
|
case TypePrimary:
|
||||||
|
@ -271,7 +280,8 @@ func stateAddInitAddr(s *State, addr *ResourceAddress) interface{} {
|
||||||
}
|
}
|
||||||
if instance == nil {
|
if instance == nil {
|
||||||
instance = &InstanceState{}
|
instance = &InstanceState{}
|
||||||
|
exists = false
|
||||||
}
|
}
|
||||||
|
|
||||||
return instance
|
return instance, exists
|
||||||
}
|
}
|
||||||
|
|
|
@ -162,6 +162,34 @@ func TestStateAdd(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"ResourceState => Resource Addr (existing)": {
|
||||||
|
true,
|
||||||
|
"aws_instance.foo",
|
||||||
|
&ResourceState{
|
||||||
|
Type: "test_instance",
|
||||||
|
Primary: &InstanceState{
|
||||||
|
ID: "foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
&State{
|
||||||
|
Modules: []*ModuleState{
|
||||||
|
&ModuleState{
|
||||||
|
Path: []string{"root"},
|
||||||
|
Resources: map[string]*ResourceState{
|
||||||
|
"aws_instance.foo": &ResourceState{
|
||||||
|
Type: "test_instance",
|
||||||
|
Primary: &InstanceState{
|
||||||
|
ID: "foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
nil,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, tc := range cases {
|
for k, tc := range cases {
|
||||||
|
|
Loading…
Reference in New Issue