|
| 1 | +package addrs |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +// Action is an address for an action block within configuration, which |
| 9 | +// contains potentially-multiple action instances if that configuration |
| 10 | +// block uses "count" or "for_each". |
| 11 | +type Action struct { |
| 12 | + referenceable |
| 13 | + Type string |
| 14 | + Name string |
| 15 | +} |
| 16 | + |
| 17 | +func (a Action) String() string { |
| 18 | + return fmt.Sprintf("action.%s.%s", a.Type, a.Name) |
| 19 | +} |
| 20 | + |
| 21 | +func (a Action) Equal(o Action) bool { |
| 22 | + return a.Name == o.Name && a.Type == o.Type |
| 23 | +} |
| 24 | + |
| 25 | +func (a Action) Less(o Action) bool { |
| 26 | + switch { |
| 27 | + case a.Type != o.Type: |
| 28 | + return a.Type < o.Type |
| 29 | + |
| 30 | + case a.Name != o.Name: |
| 31 | + return a.Name < o.Name |
| 32 | + |
| 33 | + default: |
| 34 | + return false |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func (a Action) UniqueKey() UniqueKey { |
| 39 | + return a // An Action is its own UniqueKey |
| 40 | +} |
| 41 | + |
| 42 | +func (a Action) uniqueKeySigil() {} |
| 43 | + |
| 44 | +// Instance produces the address for a specific instance of the receiver |
| 45 | +// that is identified by the given key. |
| 46 | +func (a Action) Instance(key InstanceKey) ActionInstance { |
| 47 | + return ActionInstance{ |
| 48 | + Action: a, |
| 49 | + Key: key, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// Absolute returns an AbsAction from the receiver and the given module |
| 54 | +// instance address. |
| 55 | +func (a Action) Absolute(module ModuleInstance) AbsAction { |
| 56 | + return AbsAction{ |
| 57 | + Module: module, |
| 58 | + Action: a, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// InModule returns a ConfigAction from the receiver and the given module |
| 63 | +// address. |
| 64 | +func (a Action) InModule(module Module) ConfigAction { |
| 65 | + return ConfigAction{ |
| 66 | + Module: module, |
| 67 | + Action: a, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// ImpliedProvider returns the implied provider type name, for e.g. the "aws" in |
| 72 | +// "aws_instance" |
| 73 | +func (a Action) ImpliedProvider() string { |
| 74 | + typeName := a.Type |
| 75 | + if under := strings.Index(typeName, "_"); under != -1 { |
| 76 | + typeName = typeName[:under] |
| 77 | + } |
| 78 | + |
| 79 | + return typeName |
| 80 | +} |
| 81 | + |
| 82 | +// ActionInstance is an address for a specific instance of an action. |
| 83 | +// When an action is defined in configuration with "count" or "for_each" it |
| 84 | +// produces zero or more instances, which can be addressed using this type. |
| 85 | +type ActionInstance struct { |
| 86 | + referenceable |
| 87 | + Action Action |
| 88 | + Key InstanceKey |
| 89 | +} |
| 90 | + |
| 91 | +func (a ActionInstance) ContainingAction() Action { |
| 92 | + return a.Action |
| 93 | +} |
| 94 | + |
| 95 | +func (a ActionInstance) String() string { |
| 96 | + if a.Key == NoKey { |
| 97 | + return a.Action.String() |
| 98 | + } |
| 99 | + return a.Action.String() + a.Key.String() |
| 100 | +} |
| 101 | + |
| 102 | +func (a ActionInstance) Equal(o ActionInstance) bool { |
| 103 | + return a.Key == o.Key && a.Action.Equal(o.Action) |
| 104 | +} |
| 105 | + |
| 106 | +func (a ActionInstance) Less(o ActionInstance) bool { |
| 107 | + if !a.Action.Equal(o.Action) { |
| 108 | + return a.Action.Less(o.Action) |
| 109 | + } |
| 110 | + |
| 111 | + if a.Key != o.Key { |
| 112 | + return InstanceKeyLess(a.Key, o.Key) |
| 113 | + } |
| 114 | + |
| 115 | + return false |
| 116 | +} |
| 117 | + |
| 118 | +func (a ActionInstance) UniqueKey() UniqueKey { |
| 119 | + return a // An ActionInstance is its own UniqueKey |
| 120 | +} |
| 121 | + |
| 122 | +func (a ActionInstance) uniqueKeySigil() {} |
| 123 | + |
| 124 | +// Absolute returns an AbsActionInstance from the receiver and the given module |
| 125 | +// instance address. |
| 126 | +func (a ActionInstance) Absolute(module ModuleInstance) AbsActionInstance { |
| 127 | + return AbsActionInstance{ |
| 128 | + Module: module, |
| 129 | + Action: a, |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// AbsAction is an absolute address for an action under a given module path. |
| 134 | +type AbsAction struct { |
| 135 | + Module ModuleInstance |
| 136 | + Action Action |
| 137 | +} |
| 138 | + |
| 139 | +// Action returns the address of a particular action within the receiver. |
| 140 | +func (m ModuleInstance) Action(typeName string, name string) AbsAction { |
| 141 | + return AbsAction{ |
| 142 | + Module: m, |
| 143 | + Action: Action{ |
| 144 | + Type: typeName, |
| 145 | + Name: name, |
| 146 | + }, |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +// Instance produces the address for a specific instance of the receiver that is |
| 151 | +// identified by the given key. |
| 152 | +func (a AbsAction) Instance(key InstanceKey) AbsActionInstance { |
| 153 | + return AbsActionInstance{ |
| 154 | + Module: a.Module, |
| 155 | + Action: a.Action.Instance(key), |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +// Config returns the unexpanded ConfigAction for this AbsAction. |
| 160 | +func (a AbsAction) Config() ConfigAction { |
| 161 | + return ConfigAction{ |
| 162 | + Module: a.Module.Module(), |
| 163 | + Action: a.Action, |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +func (a AbsAction) String() string { |
| 168 | + if len(a.Module) == 0 { |
| 169 | + return a.Action.String() |
| 170 | + } |
| 171 | + return fmt.Sprintf("%s.%s", a.Module.String(), a.Action.String()) |
| 172 | +} |
| 173 | + |
| 174 | +// AffectedAbsAction returns the AbsAction. |
| 175 | +func (a AbsAction) AffectedAbsAction() AbsAction { |
| 176 | + return a |
| 177 | +} |
| 178 | + |
| 179 | +func (a AbsAction) Equal(o AbsAction) bool { |
| 180 | + return a.Module.Equal(o.Module) && a.Action.Equal(o.Action) |
| 181 | +} |
| 182 | + |
| 183 | +func (a AbsAction) Less(o AbsAction) bool { |
| 184 | + if !a.Module.Equal(o.Module) { |
| 185 | + return a.Module.Less(o.Module) |
| 186 | + } |
| 187 | + |
| 188 | + if !a.Action.Equal(o.Action) { |
| 189 | + return a.Action.Less(o.Action) |
| 190 | + } |
| 191 | + |
| 192 | + return false |
| 193 | +} |
| 194 | + |
| 195 | +type absActionKey string |
| 196 | + |
| 197 | +func (a absActionKey) uniqueKeySigil() {} |
| 198 | + |
| 199 | +func (a AbsAction) UniqueKey() UniqueKey { |
| 200 | + return absActionKey(a.String()) |
| 201 | +} |
| 202 | + |
| 203 | +// AbsActionInstance is an absolute address for an action instance under a |
| 204 | +// given module path. |
| 205 | +type AbsActionInstance struct { |
| 206 | + Module ModuleInstance |
| 207 | + Action ActionInstance |
| 208 | +} |
| 209 | + |
| 210 | +// ActionInstance returns the address of a particular action instance within the receiver. |
| 211 | +func (m ModuleInstance) ActionInstance(typeName string, name string, key InstanceKey) AbsActionInstance { |
| 212 | + return AbsActionInstance{ |
| 213 | + Module: m, |
| 214 | + Action: ActionInstance{ |
| 215 | + Action: Action{ |
| 216 | + Type: typeName, |
| 217 | + Name: name, |
| 218 | + }, |
| 219 | + Key: key, |
| 220 | + }, |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +// ContainingAction returns the address of the action that contains the |
| 225 | +// receiving action instance. In other words, it discards the key portion of the |
| 226 | +// address to produce an AbsAction value. |
| 227 | +func (a AbsActionInstance) ContainingAction() AbsAction { |
| 228 | + return AbsAction{ |
| 229 | + Module: a.Module, |
| 230 | + Action: a.Action.ContainingAction(), |
| 231 | + } |
| 232 | +} |
| 233 | + |
| 234 | +// ConfigAction returns the address of the configuration block that declared |
| 235 | +// this instance. |
| 236 | +func (a AbsActionInstance) ConfigAction() ConfigAction { |
| 237 | + return ConfigAction{ |
| 238 | + Module: a.Module.Module(), |
| 239 | + Action: a.Action.Action, |
| 240 | + } |
| 241 | +} |
| 242 | + |
| 243 | +func (a AbsActionInstance) String() string { |
| 244 | + if len(a.Module) == 0 { |
| 245 | + return a.Action.String() |
| 246 | + } |
| 247 | + return fmt.Sprintf("%s.%s", a.Module.String(), a.Action.String()) |
| 248 | +} |
| 249 | + |
| 250 | +// AffectedAbsAction returns the AbsAction for the instance. |
| 251 | +func (a AbsActionInstance) AffectedAbsAction() AbsAction { |
| 252 | + return AbsAction{ |
| 253 | + Module: a.Module, |
| 254 | + Action: a.Action.Action, |
| 255 | + } |
| 256 | +} |
| 257 | + |
| 258 | +func (a AbsActionInstance) Equal(o AbsActionInstance) bool { |
| 259 | + return a.Module.Equal(o.Module) && a.Action.Equal(o.Action) |
| 260 | +} |
| 261 | + |
| 262 | +// Less returns true if the receiver should sort before the given other value |
| 263 | +// in a sorted list of addresses. |
| 264 | +func (a AbsActionInstance) Less(o AbsActionInstance) bool { |
| 265 | + if !a.Module.Equal(o.Module) { |
| 266 | + return a.Module.Less(o.Module) |
| 267 | + } |
| 268 | + |
| 269 | + if !a.Action.Equal(o.Action) { |
| 270 | + return a.Action.Less(o.Action) |
| 271 | + } |
| 272 | + |
| 273 | + return false |
| 274 | +} |
| 275 | + |
| 276 | +type absActionInstanceKey string |
| 277 | + |
| 278 | +func (a AbsActionInstance) UniqueKey() UniqueKey { |
| 279 | + return absActionInstanceKey(a.String()) |
| 280 | +} |
| 281 | + |
| 282 | +func (r absActionInstanceKey) uniqueKeySigil() {} |
| 283 | + |
| 284 | +// ConfigAction is the address for an action within the configuration. |
| 285 | +type ConfigAction struct { |
| 286 | + Module Module |
| 287 | + Action Action |
| 288 | +} |
| 289 | + |
| 290 | +// Action returns the address of a particular action within the module. |
| 291 | +func (m Module) Action(typeName string, name string) ConfigAction { |
| 292 | + return ConfigAction{ |
| 293 | + Module: m, |
| 294 | + Action: Action{ |
| 295 | + Type: typeName, |
| 296 | + Name: name, |
| 297 | + }, |
| 298 | + } |
| 299 | +} |
| 300 | + |
| 301 | +// Absolute produces the address for the receiver within a specific module instance. |
| 302 | +func (a ConfigAction) Absolute(module ModuleInstance) AbsAction { |
| 303 | + return AbsAction{ |
| 304 | + Module: module, |
| 305 | + Action: a.Action, |
| 306 | + } |
| 307 | +} |
| 308 | + |
| 309 | +func (a ConfigAction) String() string { |
| 310 | + if len(a.Module) == 0 { |
| 311 | + return a.Action.String() |
| 312 | + } |
| 313 | + return fmt.Sprintf("%s.%s", a.Module.String(), a.Action.String()) |
| 314 | +} |
| 315 | + |
| 316 | +func (a ConfigAction) Equal(o ConfigAction) bool { |
| 317 | + return a.Module.Equal(o.Module) && a.Action.Equal(o.Action) |
| 318 | +} |
| 319 | + |
| 320 | +func (a ConfigAction) UniqueKey() UniqueKey { |
| 321 | + return configActionKey(a.String()) |
| 322 | +} |
| 323 | + |
| 324 | +type configActionKey string |
| 325 | + |
| 326 | +func (k configActionKey) uniqueKeySigil() {} |
0 commit comments