summaryrefslogtreecommitdiff
path: root/vendor/github.com/hashicorp/terraform/terraform/eval_import_state.go
blob: 62cc581fadc296ad7b5f4472c441d2af1025b90d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
	Id       string
	Output   *[]*InstanceState
}

// TODO: test
func (n *EvalImportState) Eval(ctx EvalContext) (interface{}, error) {
	provider := *n.Provider

	{
		// Call pre-import hook
		err := ctx.Hook(func(h Hook) (HookAction, error) {
			return h.PreImportState(n.Info, n.Id)
		})
		if err != nil {
			return nil, err
		}
	}

	// Import!
	state, err := provider.ImportState(n.Info, n.Id)
	if err != nil {
		return nil, fmt.Errorf(
			"import %s (id: %s): %s", n.Info.HumanId(), n.Id, err)
	}

	if n.Output != nil {
		*n.Output = state
	}

	{
		// Call post-import hook
		err := ctx.Hook(func(h Hook) (HookAction, error) {
			return h.PostImportState(n.Info, state)
		})
		if err != nil {
			return nil, err
		}
	}

	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
}