github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/helpers.go (about)

     1  package opc
     2  
     3  import (
     4  	"sort"
     5  
     6  	"github.com/hashicorp/terraform/helper/schema"
     7  )
     8  
     9  // Helper function to get a string list from the schema, and alpha-sort it
    10  func getStringList(d *schema.ResourceData, key string) []string {
    11  	if _, ok := d.GetOk(key); !ok {
    12  		return nil
    13  	}
    14  	l := d.Get(key).([]interface{})
    15  	res := make([]string, len(l))
    16  	for i, v := range l {
    17  		res[i] = v.(string)
    18  	}
    19  	sort.Strings(res)
    20  	return res
    21  }
    22  
    23  // Helper function to set a string list in the schema, in an alpha-sorted order.
    24  func setStringList(d *schema.ResourceData, key string, value []string) error {
    25  	sort.Strings(value)
    26  	return d.Set(key, value)
    27  }
    28  
    29  // Helper function to get an int list from the schema, and numerically sort it
    30  func getIntList(d *schema.ResourceData, key string) []int {
    31  	if _, ok := d.GetOk(key); !ok {
    32  		return nil
    33  	}
    34  
    35  	l := d.Get(key).([]interface{})
    36  	res := make([]int, len(l))
    37  	for i, v := range l {
    38  		res[i] = v.(int)
    39  	}
    40  	sort.Ints(res)
    41  	return res
    42  }
    43  
    44  func setIntList(d *schema.ResourceData, key string, value []int) error {
    45  	sort.Ints(value)
    46  	return d.Set(key, value)
    47  }