github.com/pulumi/terraform@v1.4.0/pkg/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  type ModuleCall struct {
    10  	referenceable
    11  	Name string
    12  }
    13  
    14  func (c ModuleCall) String() string {
    15  	return "module." + c.Name
    16  }
    17  
    18  func (c ModuleCall) UniqueKey() UniqueKey {
    19  	return c // A ModuleCall is its own UniqueKey
    20  }
    21  
    22  func (c ModuleCall) uniqueKeySigil() {}
    23  
    24  // Instance returns the address of an instance of the receiver identified by
    25  // the given key.
    26  func (c ModuleCall) Instance(key InstanceKey) ModuleCallInstance {
    27  	return ModuleCallInstance{
    28  		Call: c,
    29  		Key:  key,
    30  	}
    31  }
    32  
    33  func (c ModuleCall) Absolute(moduleAddr ModuleInstance) AbsModuleCall {
    34  	return AbsModuleCall{
    35  		Module: moduleAddr,
    36  		Call:   c,
    37  	}
    38  }
    39  
    40  func (c ModuleCall) Equal(other ModuleCall) bool {
    41  	return c.Name == other.Name
    42  }
    43  
    44  // AbsModuleCall is the address of a "module" block relative to the root
    45  // of the configuration.
    46  //
    47  // This is similar to ModuleInstance alone, but specifically represents
    48  // the module block itself rather than any one of the instances that
    49  // module block declares.
    50  type AbsModuleCall struct {
    51  	Module ModuleInstance
    52  	Call   ModuleCall
    53  }
    54  
    55  func (c AbsModuleCall) absMoveableSigil() {
    56  	// AbsModuleCall is "moveable".
    57  }
    58  
    59  func (c AbsModuleCall) String() string {
    60  	if len(c.Module) == 0 {
    61  		return "module." + c.Call.Name
    62  
    63  	}
    64  	return fmt.Sprintf("%s.module.%s", c.Module, c.Call.Name)
    65  }
    66  
    67  func (c AbsModuleCall) Instance(key InstanceKey) ModuleInstance {
    68  	ret := make(ModuleInstance, len(c.Module), len(c.Module)+1)
    69  	copy(ret, c.Module)
    70  	ret = append(ret, ModuleInstanceStep{
    71  		Name:        c.Call.Name,
    72  		InstanceKey: key,
    73  	})
    74  	return ret
    75  }
    76  
    77  func (c AbsModuleCall) Equal(other AbsModuleCall) bool {
    78  	return c.Module.Equal(other.Module) && c.Call.Equal(other.Call)
    79  }
    80  
    81  type absModuleCallInstanceKey string
    82  
    83  func (c AbsModuleCall) UniqueKey() UniqueKey {
    84  	return absModuleCallInstanceKey(c.String())
    85  }
    86  
    87  func (mk absModuleCallInstanceKey) uniqueKeySigil() {}
    88  
    89  // ModuleCallInstance is the address of one instance of a module created from
    90  // a module call, which might create multiple instances using "count" or
    91  // "for_each" arguments.
    92  //
    93  // There is no "Abs" version of ModuleCallInstance because an absolute module
    94  // path is represented by ModuleInstance.
    95  type ModuleCallInstance struct {
    96  	referenceable
    97  	Call ModuleCall
    98  	Key  InstanceKey
    99  }
   100  
   101  func (c ModuleCallInstance) String() string {
   102  	if c.Key == NoKey {
   103  		return c.Call.String()
   104  	}
   105  	return fmt.Sprintf("module.%s%s", c.Call.Name, c.Key)
   106  }
   107  
   108  func (c ModuleCallInstance) UniqueKey() UniqueKey {
   109  	return c // A ModuleCallInstance is its own UniqueKey
   110  }
   111  
   112  func (c ModuleCallInstance) uniqueKeySigil() {}
   113  
   114  func (c ModuleCallInstance) Absolute(moduleAddr ModuleInstance) ModuleInstance {
   115  	ret := make(ModuleInstance, len(moduleAddr), len(moduleAddr)+1)
   116  	copy(ret, moduleAddr)
   117  	ret = append(ret, ModuleInstanceStep{
   118  		Name:        c.Call.Name,
   119  		InstanceKey: c.Key,
   120  	})
   121  	return ret
   122  }
   123  
   124  // ModuleInstance returns the address of the module instance that corresponds
   125  // to the receiving call instance when resolved in the given calling module.
   126  // In other words, it returns the child module instance that the receving
   127  // call instance creates.
   128  func (c ModuleCallInstance) ModuleInstance(caller ModuleInstance) ModuleInstance {
   129  	return caller.Child(c.Call.Name, c.Key)
   130  }
   131  
   132  // Output returns the absolute address of an output of the receiver identified by its
   133  // name.
   134  func (c ModuleCallInstance) Output(name string) ModuleCallInstanceOutput {
   135  	return ModuleCallInstanceOutput{
   136  		Call: c,
   137  		Name: name,
   138  	}
   139  }
   140  
   141  // ModuleCallOutput is the address of a named output and its associated
   142  // ModuleCall, which may expand into multiple module instances
   143  type ModuleCallOutput struct {
   144  	referenceable
   145  	Call ModuleCall
   146  	Name string
   147  }
   148  
   149  func (m ModuleCallOutput) String() string {
   150  	return fmt.Sprintf("%s.%s", m.Call.String(), m.Name)
   151  }
   152  
   153  func (m ModuleCallOutput) UniqueKey() UniqueKey {
   154  	return m // A ModuleCallOutput is its own UniqueKey
   155  }
   156  
   157  func (m ModuleCallOutput) uniqueKeySigil() {}
   158  
   159  // ModuleCallInstanceOutput is the address of a particular named output produced by
   160  // an instance of a module call.
   161  type ModuleCallInstanceOutput struct {
   162  	referenceable
   163  	Call ModuleCallInstance
   164  	Name string
   165  }
   166  
   167  // ModuleCallOutput returns the referenceable ModuleCallOutput for this
   168  // particular instance.
   169  func (co ModuleCallInstanceOutput) ModuleCallOutput() ModuleCallOutput {
   170  	return ModuleCallOutput{
   171  		Call: co.Call.Call,
   172  		Name: co.Name,
   173  	}
   174  }
   175  
   176  func (co ModuleCallInstanceOutput) String() string {
   177  	return fmt.Sprintf("%s.%s", co.Call.String(), co.Name)
   178  }
   179  
   180  func (co ModuleCallInstanceOutput) UniqueKey() UniqueKey {
   181  	return co // A ModuleCallInstanceOutput is its own UniqueKey
   182  }
   183  
   184  func (co ModuleCallInstanceOutput) uniqueKeySigil() {}
   185  
   186  // AbsOutputValue returns the absolute output value address that corresponds
   187  // to the receving module call output address, once resolved in the given
   188  // calling module.
   189  func (co ModuleCallInstanceOutput) AbsOutputValue(caller ModuleInstance) AbsOutputValue {
   190  	moduleAddr := co.Call.ModuleInstance(caller)
   191  	return moduleAddr.OutputValue(co.Name)
   192  }