terraform: import verifies the refresh results in non-nil state
/cc @jen20
This commit is contained in:
parent
bf8d788489
commit
0d1debc0ae
|
@ -163,6 +163,44 @@ func TestContextImport_refresh(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestContextImport_refreshNil(t *testing.T) {
|
||||
p := testProvider("aws")
|
||||
ctx := testContext2(t, &ContextOpts{
|
||||
Providers: map[string]ResourceProviderFactory{
|
||||
"aws": testProviderFuncFixed(p),
|
||||
},
|
||||
})
|
||||
|
||||
p.ImportStateReturn = []*InstanceState{
|
||||
&InstanceState{
|
||||
ID: "foo",
|
||||
Ephemeral: EphemeralState{Type: "aws_instance"},
|
||||
},
|
||||
}
|
||||
|
||||
p.RefreshFn = func(info *InstanceInfo, s *InstanceState) (*InstanceState, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
state, err := ctx.Import(&ImportOpts{
|
||||
Targets: []*ImportTarget{
|
||||
&ImportTarget{
|
||||
Addr: "aws_instance.foo",
|
||||
ID: "bar",
|
||||
},
|
||||
},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("should error")
|
||||
}
|
||||
|
||||
actual := strings.TrimSpace(state.String())
|
||||
expected := "<no state>"
|
||||
if actual != expected {
|
||||
t.Fatalf("bad: \n%s", actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextImport_module(t *testing.T) {
|
||||
p := testProvider("aws")
|
||||
ctx := testContext2(t, &ContextOpts{
|
||||
|
|
|
@ -51,3 +51,26 @@ func (n *EvalImportState) Eval(ctx EvalContext) (interface{}, error) {
|
|||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// EvalImportStateVerify verifies the state after ImportState and
|
||||
// after the refresh to make sure it is non-nil and valid.
|
||||
type EvalImportStateVerify struct {
|
||||
Info *InstanceInfo
|
||||
Id string
|
||||
State **InstanceState
|
||||
}
|
||||
|
||||
// TODO: test
|
||||
func (n *EvalImportStateVerify) Eval(ctx EvalContext) (interface{}, error) {
|
||||
state := *n.State
|
||||
if state.Empty() {
|
||||
return nil, fmt.Errorf(
|
||||
"import %s (id: %s): Terraform detected a resource with this ID doesn't\n"+
|
||||
"exist. Please verify the ID is correct. You cannot import non-existent\n"+
|
||||
"resources using Terraform import.",
|
||||
n.Info.HumanId(),
|
||||
n.Id)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
|
|
@ -195,7 +195,7 @@ func (n *graphNodeImportStateSub) EvalTree() EvalNode {
|
|||
|
||||
// Build the resource info
|
||||
info := &InstanceInfo{
|
||||
Id: n.State.ID,
|
||||
Id: fmt.Sprintf("%s.%s", n.Target.Type, n.Target.Name),
|
||||
ModulePath: n.Path_,
|
||||
Type: n.State.Ephemeral.Type,
|
||||
}
|
||||
|
@ -221,6 +221,11 @@ func (n *graphNodeImportStateSub) EvalTree() EvalNode {
|
|||
Info: info,
|
||||
Output: &state,
|
||||
},
|
||||
&EvalImportStateVerify{
|
||||
Info: info,
|
||||
Id: n.State.ID,
|
||||
State: &state,
|
||||
},
|
||||
&EvalWriteState{
|
||||
Name: key.String(),
|
||||
ResourceType: info.Type,
|
||||
|
|
Loading…
Reference in New Issue