mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-25 08:21:07 -06:00
b9a93a0fe7
This is part of a general effort to move all of Terraform's non-library package surface under internal in order to reinforce that these are for internal use within Terraform only. If you were previously importing packages under this prefix into an external codebase, you could pin to an earlier release tag as an interim solution until you've make a plan to achieve the same functionality some other way.
86 lines
2.2 KiB
Go
86 lines
2.2 KiB
Go
package instances
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
|
)
|
|
|
|
// expansion is an internal interface used to represent the different
|
|
// ways expansion can operate depending on how repetition is configured for
|
|
// an object.
|
|
type expansion interface {
|
|
instanceKeys() []addrs.InstanceKey
|
|
repetitionData(addrs.InstanceKey) RepetitionData
|
|
}
|
|
|
|
// expansionSingle is the expansion corresponding to no repetition arguments
|
|
// at all, producing a single object with no key.
|
|
//
|
|
// expansionSingleVal is the only valid value of this type.
|
|
type expansionSingle uintptr
|
|
|
|
var singleKeys = []addrs.InstanceKey{addrs.NoKey}
|
|
var expansionSingleVal expansionSingle
|
|
|
|
func (e expansionSingle) instanceKeys() []addrs.InstanceKey {
|
|
return singleKeys
|
|
}
|
|
|
|
func (e expansionSingle) repetitionData(key addrs.InstanceKey) RepetitionData {
|
|
if key != addrs.NoKey {
|
|
panic("cannot use instance key with non-repeating object")
|
|
}
|
|
return RepetitionData{}
|
|
}
|
|
|
|
// expansionCount is the expansion corresponding to the "count" argument.
|
|
type expansionCount int
|
|
|
|
func (e expansionCount) instanceKeys() []addrs.InstanceKey {
|
|
ret := make([]addrs.InstanceKey, int(e))
|
|
for i := range ret {
|
|
ret[i] = addrs.IntKey(i)
|
|
}
|
|
return ret
|
|
}
|
|
|
|
func (e expansionCount) repetitionData(key addrs.InstanceKey) RepetitionData {
|
|
i := int(key.(addrs.IntKey))
|
|
if i < 0 || i >= int(e) {
|
|
panic(fmt.Sprintf("instance key %d out of range for count %d", i, e))
|
|
}
|
|
return RepetitionData{
|
|
CountIndex: cty.NumberIntVal(int64(i)),
|
|
}
|
|
}
|
|
|
|
// expansionForEach is the expansion corresponding to the "for_each" argument.
|
|
type expansionForEach map[string]cty.Value
|
|
|
|
func (e expansionForEach) instanceKeys() []addrs.InstanceKey {
|
|
ret := make([]addrs.InstanceKey, 0, len(e))
|
|
for k := range e {
|
|
ret = append(ret, addrs.StringKey(k))
|
|
}
|
|
sort.Slice(ret, func(i, j int) bool {
|
|
return ret[i].(addrs.StringKey) < ret[j].(addrs.StringKey)
|
|
})
|
|
return ret
|
|
}
|
|
|
|
func (e expansionForEach) repetitionData(key addrs.InstanceKey) RepetitionData {
|
|
k := string(key.(addrs.StringKey))
|
|
v, ok := e[k]
|
|
if !ok {
|
|
panic(fmt.Sprintf("instance key %q does not match any instance", k))
|
|
}
|
|
return RepetitionData{
|
|
EachKey: cty.StringVal(k),
|
|
EachValue: v,
|
|
}
|
|
}
|