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

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