opentofu/builtin/providers/random/resource_shuffle.go
Martin Atkins eec6c88fd2 provider/random: random_shuffle resource
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.
2016-05-14 16:48:45 -07:00

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
}