github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/addrs/resource_phase.go (about)

     1  package addrs
     2  
     3  import "fmt"
     4  
     5  // ResourceInstancePhase is a special kind of reference used only internally
     6  // during graph building to represent resource instances that are in a
     7  // non-primary state.
     8  //
     9  // Graph nodes can declare themselves referenceable via an instance phase
    10  // or can declare that they reference an instance phase in order to accomodate
    11  // secondary graph nodes dealing with, for example, destroy actions.
    12  //
    13  // This special reference type cannot be accessed directly by end-users, and
    14  // should never be shown in the UI.
    15  type ResourceInstancePhase struct {
    16  	referenceable
    17  	ResourceInstance ResourceInstance
    18  	Phase            ResourceInstancePhaseType
    19  }
    20  
    21  var _ Referenceable = ResourceInstancePhase{}
    22  
    23  // Phase returns a special "phase address" for the receving instance. See the
    24  // documentation of ResourceInstancePhase for the limited situations where this
    25  // is intended to be used.
    26  func (r ResourceInstance) Phase(rpt ResourceInstancePhaseType) ResourceInstancePhase {
    27  	return ResourceInstancePhase{
    28  		ResourceInstance: r,
    29  		Phase:            rpt,
    30  	}
    31  }
    32  
    33  // ContainingResource returns an address for the same phase of the resource
    34  // that this instance belongs to.
    35  func (rp ResourceInstancePhase) ContainingResource() ResourcePhase {
    36  	return rp.ResourceInstance.Resource.Phase(rp.Phase)
    37  }
    38  
    39  func (rp ResourceInstancePhase) String() string {
    40  	// We use a different separator here than usual to ensure that we'll
    41  	// never conflict with any non-phased resource instance string. This
    42  	// is intentionally something that would fail parsing with ParseRef,
    43  	// because this special address type should never be exposed in the UI.
    44  	return fmt.Sprintf("%s#%s", rp.ResourceInstance, rp.Phase)
    45  }
    46  
    47  // ResourceInstancePhaseType is an enumeration used with ResourceInstancePhase.
    48  type ResourceInstancePhaseType string
    49  
    50  const (
    51  	// ResourceInstancePhaseDestroy represents the "destroy" phase of a
    52  	// resource instance.
    53  	ResourceInstancePhaseDestroy ResourceInstancePhaseType = "destroy"
    54  
    55  	// ResourceInstancePhaseDestroyCBD is similar to ResourceInstancePhaseDestroy
    56  	// but is used for resources that have "create_before_destroy" set, thus
    57  	// requiring a different dependency ordering.
    58  	ResourceInstancePhaseDestroyCBD ResourceInstancePhaseType = "destroy-cbd"
    59  )
    60  
    61  func (rpt ResourceInstancePhaseType) String() string {
    62  	return string(rpt)
    63  }
    64  
    65  // ResourcePhase is a special kind of reference used only internally
    66  // during graph building to represent resources that are in a
    67  // non-primary state.
    68  //
    69  // Graph nodes can declare themselves referenceable via a resource phase
    70  // or can declare that they reference a resource phase in order to accomodate
    71  // secondary graph nodes dealing with, for example, destroy actions.
    72  //
    73  // Since resources (as opposed to instances) aren't actually phased, this
    74  // address type is used only as an approximation during initial construction
    75  // of the resource-oriented plan graph, under the assumption that resource
    76  // instances with ResourceInstancePhase addresses will be created in dynamic
    77  // subgraphs during the graph walk.
    78  //
    79  // This special reference type cannot be accessed directly by end-users, and
    80  // should never be shown in the UI.
    81  type ResourcePhase struct {
    82  	referenceable
    83  	Resource Resource
    84  	Phase    ResourceInstancePhaseType
    85  }
    86  
    87  var _ Referenceable = ResourcePhase{}
    88  
    89  // Phase returns a special "phase address" for the receving instance. See the
    90  // documentation of ResourceInstancePhase for the limited situations where this
    91  // is intended to be used.
    92  func (r Resource) Phase(rpt ResourceInstancePhaseType) ResourcePhase {
    93  	return ResourcePhase{
    94  		Resource: r,
    95  		Phase:    rpt,
    96  	}
    97  }
    98  
    99  func (rp ResourcePhase) String() string {
   100  	// We use a different separator here than usual to ensure that we'll
   101  	// never conflict with any non-phased resource instance string. This
   102  	// is intentionally something that would fail parsing with ParseRef,
   103  	// because this special address type should never be exposed in the UI.
   104  	return fmt.Sprintf("%s#%s", rp.Resource, rp.Phase)
   105  }