github.com/pulumi/terraform@v1.4.0/pkg/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  func (rp ResourceInstancePhase) UniqueKey() UniqueKey {
    48  	return rp // A ResourceInstancePhase is its own UniqueKey
    49  }
    50  
    51  func (rp ResourceInstancePhase) uniqueKeySigil() {}
    52  
    53  // ResourceInstancePhaseType is an enumeration used with ResourceInstancePhase.
    54  type ResourceInstancePhaseType string
    55  
    56  const (
    57  	// ResourceInstancePhaseDestroy represents the "destroy" phase of a
    58  	// resource instance.
    59  	ResourceInstancePhaseDestroy ResourceInstancePhaseType = "destroy"
    60  
    61  	// ResourceInstancePhaseDestroyCBD is similar to ResourceInstancePhaseDestroy
    62  	// but is used for resources that have "create_before_destroy" set, thus
    63  	// requiring a different dependency ordering.
    64  	ResourceInstancePhaseDestroyCBD ResourceInstancePhaseType = "destroy-cbd"
    65  )
    66  
    67  func (rpt ResourceInstancePhaseType) String() string {
    68  	return string(rpt)
    69  }
    70  
    71  // ResourcePhase is a special kind of reference used only internally
    72  // during graph building to represent resources that are in a
    73  // non-primary state.
    74  //
    75  // Graph nodes can declare themselves referenceable via a resource phase
    76  // or can declare that they reference a resource phase in order to accomodate
    77  // secondary graph nodes dealing with, for example, destroy actions.
    78  //
    79  // Since resources (as opposed to instances) aren't actually phased, this
    80  // address type is used only as an approximation during initial construction
    81  // of the resource-oriented plan graph, under the assumption that resource
    82  // instances with ResourceInstancePhase addresses will be created in dynamic
    83  // subgraphs during the graph walk.
    84  //
    85  // This special reference type cannot be accessed directly by end-users, and
    86  // should never be shown in the UI.
    87  type ResourcePhase struct {
    88  	referenceable
    89  	Resource Resource
    90  	Phase    ResourceInstancePhaseType
    91  }
    92  
    93  var _ Referenceable = ResourcePhase{}
    94  
    95  // Phase returns a special "phase address" for the receving instance. See the
    96  // documentation of ResourceInstancePhase for the limited situations where this
    97  // is intended to be used.
    98  func (r Resource) Phase(rpt ResourceInstancePhaseType) ResourcePhase {
    99  	return ResourcePhase{
   100  		Resource: r,
   101  		Phase:    rpt,
   102  	}
   103  }
   104  
   105  func (rp ResourcePhase) String() string {
   106  	// We use a different separator here than usual to ensure that we'll
   107  	// never conflict with any non-phased resource instance string. This
   108  	// is intentionally something that would fail parsing with ParseRef,
   109  	// because this special address type should never be exposed in the UI.
   110  	return fmt.Sprintf("%s#%s", rp.Resource, rp.Phase)
   111  }
   112  
   113  func (rp ResourcePhase) UniqueKey() UniqueKey {
   114  	return rp // A ResourcePhase is its own UniqueKey
   115  }
   116  
   117  func (rp ResourcePhase) uniqueKeySigil() {}