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