github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/addrs/module_call.go (about)

     1  package addrs
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // ModuleCall is the address of a call from the current module to a child
     8  // module.
     9  //
    10  // There is no "Abs" version of ModuleCall because an absolute module path
    11  // is represented by ModuleInstance.
    12  type ModuleCall struct {
    13  	referenceable
    14  	Name string
    15  }
    16  
    17  func (c ModuleCall) String() string {
    18  	return "module." + c.Name
    19  }
    20  
    21  // Instance returns the address of an instance of the receiver identified by
    22  // the given key.
    23  func (c ModuleCall) Instance(key InstanceKey) ModuleCallInstance {
    24  	return ModuleCallInstance{
    25  		Call: c,
    26  		Key:  key,
    27  	}
    28  }
    29  
    30  // ModuleCallInstance is the address of one instance of a module created from
    31  // a module call, which might create multiple instances using "count" or
    32  // "for_each" arguments.
    33  type ModuleCallInstance struct {
    34  	referenceable
    35  	Call ModuleCall
    36  	Key  InstanceKey
    37  }
    38  
    39  func (c ModuleCallInstance) String() string {
    40  	if c.Key == NoKey {
    41  		return c.Call.String()
    42  	}
    43  	return fmt.Sprintf("module.%s%s", c.Call.Name, c.Key)
    44  }
    45  
    46  // ModuleInstance returns the address of the module instance that corresponds
    47  // to the receiving call instance when resolved in the given calling module.
    48  // In other words, it returns the child module instance that the receving
    49  // call instance creates.
    50  func (c ModuleCallInstance) ModuleInstance(caller ModuleInstance) ModuleInstance {
    51  	return caller.Child(c.Call.Name, c.Key)
    52  }
    53  
    54  // Output returns the address of an output of the receiver identified by its
    55  // name.
    56  func (c ModuleCallInstance) Output(name string) ModuleCallOutput {
    57  	return ModuleCallOutput{
    58  		Call: c,
    59  		Name: name,
    60  	}
    61  }
    62  
    63  // ModuleCallOutput is the address of a particular named output produced by
    64  // an instance of a module call.
    65  type ModuleCallOutput struct {
    66  	referenceable
    67  	Call ModuleCallInstance
    68  	Name string
    69  }
    70  
    71  func (co ModuleCallOutput) String() string {
    72  	return fmt.Sprintf("%s.%s", co.Call.String(), co.Name)
    73  }
    74  
    75  // AbsOutputValue returns the absolute output value address that corresponds
    76  // to the receving module call output address, once resolved in the given
    77  // calling module.
    78  func (co ModuleCallOutput) AbsOutputValue(caller ModuleInstance) AbsOutputValue {
    79  	moduleAddr := co.Call.ModuleInstance(caller)
    80  	return moduleAddr.OutputValue(co.Name)
    81  }