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