github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/addrs/resource_phase.go (about)

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