]>
Commit | Line | Data |
---|---|---|
bae9f6d2 JC |
1 | package schema |
2 | ||
3 | // ResourceImporter defines how a resource is imported in Terraform. This | |
4 | // can be set onto a Resource struct to make it Importable. Not all resources | |
5 | // have to be importable; if a Resource doesn't have a ResourceImporter then | |
6 | // it won't be importable. | |
7 | // | |
8 | // "Importing" in Terraform is the process of taking an already-created | |
9 | // resource and bringing it under Terraform management. This can include | |
10 | // updating Terraform state, generating Terraform configuration, etc. | |
11 | type ResourceImporter struct { | |
12 | // The functions below must all be implemented for importing to work. | |
13 | ||
14 | // State is called to convert an ID to one or more InstanceState to | |
15 | // insert into the Terraform state. If this isn't specified, then | |
16 | // the ID is passed straight through. | |
17 | State StateFunc | |
18 | } | |
19 | ||
20 | // StateFunc is the function called to import a resource into the | |
21 | // Terraform state. It is given a ResourceData with only ID set. This | |
22 | // ID is going to be an arbitrary value given by the user and may not map | |
23 | // directly to the ID format that the resource expects, so that should | |
24 | // be validated. | |
25 | // | |
26 | // This should return a slice of ResourceData that turn into the state | |
27 | // that was imported. This might be as simple as returning only the argument | |
28 | // that was given to the function. In other cases (such as AWS security groups), | |
29 | // an import may fan out to multiple resources and this will have to return | |
30 | // multiple. | |
31 | // | |
32 | // To create the ResourceData structures for other resource types (if | |
33 | // you have to), instantiate your resource and call the Data function. | |
34 | type StateFunc func(*ResourceData, interface{}) ([]*ResourceData, error) | |
35 | ||
36 | // InternalValidate should be called to validate the structure of this | |
37 | // importer. This should be called in a unit test. | |
38 | // | |
39 | // Resource.InternalValidate() will automatically call this, so this doesn't | |
40 | // need to be called manually. Further, Resource.InternalValidate() is | |
41 | // automatically called by Provider.InternalValidate(), so you only need | |
42 | // to internal validate the provider. | |
43 | func (r *ResourceImporter) InternalValidate() error { | |
44 | return nil | |
45 | } | |
46 | ||
47 | // ImportStatePassthrough is an implementation of StateFunc that can be | |
48 | // used to simply pass the ID directly through. This should be used only | |
49 | // in the case that an ID-only refresh is possible. | |
50 | func ImportStatePassthrough(d *ResourceData, m interface{}) ([]*ResourceData, error) { | |
51 | return []*ResourceData{d}, nil | |
52 | } |