terraform: import verifies the refresh results in non-nil state

/cc @jen20
This commit is contained in:
Mitchell Hashimoto 2016-05-09 13:39:34 -07:00
parent bf8d788489
commit 0d1debc0ae
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
3 changed files with 67 additions and 1 deletions

View File

@ -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) { func TestContextImport_module(t *testing.T) {
p := testProvider("aws") p := testProvider("aws")
ctx := testContext2(t, &ContextOpts{ ctx := testContext2(t, &ContextOpts{

View File

@ -51,3 +51,26 @@ func (n *EvalImportState) Eval(ctx EvalContext) (interface{}, error) {
return nil, nil 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
}

View File

@ -195,7 +195,7 @@ func (n *graphNodeImportStateSub) EvalTree() EvalNode {
// Build the resource info // Build the resource info
info := &InstanceInfo{ info := &InstanceInfo{
Id: n.State.ID, Id: fmt.Sprintf("%s.%s", n.Target.Type, n.Target.Name),
ModulePath: n.Path_, ModulePath: n.Path_,
Type: n.State.Ephemeral.Type, Type: n.State.Ephemeral.Type,
} }
@ -221,6 +221,11 @@ func (n *graphNodeImportStateSub) EvalTree() EvalNode {
Info: info, Info: info,
Output: &state, Output: &state,
}, },
&EvalImportStateVerify{
Info: info,
Id: n.State.ID,
State: &state,
},
&EvalWriteState{ &EvalWriteState{
Name: key.String(), Name: key.String(),
ResourceType: info.Type, ResourceType: info.Type,