mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-24 23:46:26 -06:00
eec6c88fd2
random_shuffle takes a list of strings and returns a new list with the same items in a random permutation. Optionally allows the result list to be a different length than the input list. A shorter result than input results in some items being excluded. A longer result than input results in some items being repeated, but never more often than the number of input items.
83 lines
1.5 KiB
Go
83 lines
1.5 KiB
Go
package random
|
|
|
|
import (
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
)
|
|
|
|
func resourceShuffle() *schema.Resource {
|
|
return &schema.Resource{
|
|
Create: CreateShuffle,
|
|
Read: stubRead,
|
|
Delete: stubDelete,
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
"keepers": &schema.Schema{
|
|
Type: schema.TypeMap,
|
|
Optional: true,
|
|
ForceNew: true,
|
|
},
|
|
|
|
"seed": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Optional: true,
|
|
ForceNew: true,
|
|
},
|
|
|
|
"input": &schema.Schema{
|
|
Type: schema.TypeList,
|
|
Required: true,
|
|
ForceNew: true,
|
|
Elem: &schema.Schema{
|
|
Type: schema.TypeString,
|
|
},
|
|
},
|
|
|
|
"result": &schema.Schema{
|
|
Type: schema.TypeList,
|
|
Computed: true,
|
|
Elem: &schema.Schema{
|
|
Type: schema.TypeString,
|
|
},
|
|
},
|
|
|
|
"result_count": &schema.Schema{
|
|
Type: schema.TypeInt,
|
|
Optional: true,
|
|
ForceNew: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func CreateShuffle(d *schema.ResourceData, meta interface{}) error {
|
|
input := d.Get("input").([]interface{})
|
|
seed := d.Get("seed").(string)
|
|
|
|
resultCount := d.Get("result_count").(int)
|
|
if resultCount == 0 {
|
|
resultCount = len(input)
|
|
}
|
|
result := make([]interface{}, 0, resultCount)
|
|
|
|
rand := NewRand(seed)
|
|
|
|
// Keep producing permutations until we fill our result
|
|
Batches:
|
|
for {
|
|
perm := rand.Perm(len(input))
|
|
|
|
for _, i := range perm {
|
|
result = append(result, input[i])
|
|
|
|
if len(result) >= resultCount {
|
|
break Batches
|
|
}
|
|
}
|
|
}
|
|
|
|
d.SetId("-")
|
|
d.Set("result", result)
|
|
|
|
return nil
|
|
}
|