2016-04-28 04:46:46 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EvalImportState is an EvalNode implementation that performs an
|
|
|
|
// ImportState operation on a provider. This will return the imported
|
|
|
|
// states but won't modify any actual state.
|
|
|
|
type EvalImportState struct {
|
|
|
|
Provider *ResourceProvider
|
|
|
|
Info *InstanceInfo
|
2016-05-04 14:40:45 -05:00
|
|
|
Id string
|
2016-04-28 04:46:46 -05:00
|
|
|
Output *[]*InstanceState
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: test
|
|
|
|
func (n *EvalImportState) Eval(ctx EvalContext) (interface{}, error) {
|
|
|
|
provider := *n.Provider
|
|
|
|
|
2016-05-04 13:55:30 -05:00
|
|
|
{
|
|
|
|
// Call pre-import hook
|
|
|
|
err := ctx.Hook(func(h Hook) (HookAction, error) {
|
2016-05-04 15:02:51 -05:00
|
|
|
return h.PreImportState(n.Info, n.Id)
|
2016-05-04 13:55:30 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Import!
|
2016-05-04 14:40:45 -05:00
|
|
|
state, err := provider.ImportState(n.Info, n.Id)
|
2016-04-28 04:46:46 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
2016-05-04 14:40:45 -05:00
|
|
|
"import %s (id: %s): %s", n.Info.HumanId(), n.Id, err)
|
2016-04-28 04:46:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if n.Output != nil {
|
|
|
|
*n.Output = state
|
|
|
|
}
|
|
|
|
|
2016-05-04 13:55:30 -05:00
|
|
|
{
|
|
|
|
// Call post-import hook
|
|
|
|
err := ctx.Hook(func(h Hook) (HookAction, error) {
|
|
|
|
return h.PostImportState(n.Info, state)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 04:46:46 -05:00
|
|
|
return nil, nil
|
|
|
|
}
|
2016-05-09 15:39:34 -05:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|