github.com/opentofu/opentofu@v1.7.1/internal/legacy/helper/schema/resource_importer.go (about)

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