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

     1  package addrs
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // OutputValue is the address of an output value, in the context of the module
     8  // that is defining it.
     9  //
    10  // This is related to but separate from ModuleCallOutput, which represents
    11  // a module output from the perspective of its parent module. Since output
    12  // values cannot be represented from the module where they are defined,
    13  // OutputValue is not Referenceable, while ModuleCallOutput is.
    14  type OutputValue struct {
    15  	Name string
    16  }
    17  
    18  func (v OutputValue) String() string {
    19  	return "output." + v.Name
    20  }
    21  
    22  // Absolute converts the receiver into an absolute address within the given
    23  // module instance.
    24  func (v OutputValue) Absolute(m ModuleInstance) AbsOutputValue {
    25  	return AbsOutputValue{
    26  		Module:      m,
    27  		OutputValue: v,
    28  	}
    29  }
    30  
    31  // AbsOutputValue is the absolute address of an output value within a module instance.
    32  //
    33  // This represents an output globally within the namespace of a particular
    34  // configuration. It is related to but separate from ModuleCallOutput, which
    35  // represents a module output from the perspective of its parent module.
    36  type AbsOutputValue struct {
    37  	Module      ModuleInstance
    38  	OutputValue OutputValue
    39  }
    40  
    41  // OutputValue returns the absolute address of an output value of the given
    42  // name within the receiving module instance.
    43  func (m ModuleInstance) OutputValue(name string) AbsOutputValue {
    44  	return AbsOutputValue{
    45  		Module: m,
    46  		OutputValue: OutputValue{
    47  			Name: name,
    48  		},
    49  	}
    50  }
    51  
    52  func (v AbsOutputValue) String() string {
    53  	if v.Module.IsRoot() {
    54  		return v.OutputValue.String()
    55  	}
    56  	return fmt.Sprintf("%s.%s", v.Module.String(), v.OutputValue.String())
    57  }
    58  
    59  // ModuleCallOutput converts an AbsModuleOutput into a ModuleCallOutput,
    60  // returning also the module instance that the ModuleCallOutput is relative
    61  // to.
    62  //
    63  // The root module does not have a call, and so this method cannot be used
    64  // with outputs in the root module, and will panic in that case.
    65  func (v AbsOutputValue) ModuleCallOutput() (ModuleInstance, ModuleCallOutput) {
    66  	if v.Module.IsRoot() {
    67  		panic("ReferenceFromCall used with root module output")
    68  	}
    69  
    70  	caller, call := v.Module.CallInstance()
    71  	return caller, ModuleCallOutput{
    72  		Call: call,
    73  		Name: v.OutputValue.Name,
    74  	}
    75  }