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

     1  package arukas
     2  
     3  import (
     4  	API "github.com/arukasio/cli"
     5  	"github.com/hashicorp/terraform/helper/schema"
     6  	"net"
     7  )
     8  
     9  // Takes the result of flatmap.Expand for an array of strings
    10  // and returns a []string
    11  func expandStringList(configured []interface{}) []string {
    12  	vs := make([]string, 0, len(configured))
    13  	for _, v := range configured {
    14  		vs = append(vs, string(v.(string)))
    15  	}
    16  	return vs
    17  }
    18  
    19  // Takes the result of schema.Set of strings and returns a []string
    20  func expandStringSet(configured *schema.Set) []string {
    21  	return expandStringList(configured.List())
    22  }
    23  
    24  // Takes list of pointers to strings. Expand to an array
    25  // of raw strings and returns a []interface{}
    26  // to keep compatibility w/ schema.NewSetschema.NewSet
    27  func flattenStringList(list []string) []interface{} {
    28  	vs := make([]interface{}, 0, len(list))
    29  	for _, v := range list {
    30  		vs = append(vs, v)
    31  	}
    32  	return vs
    33  }
    34  
    35  func expandEnvs(configured interface{}) API.Envs {
    36  	var envs API.Envs
    37  	if configured == nil {
    38  		return envs
    39  	}
    40  	rawEnvs := configured.([]interface{})
    41  	for _, raw := range rawEnvs {
    42  		env := raw.(map[string]interface{})
    43  		envs = append(envs, API.Env{Key: env["key"].(string), Value: env["value"].(string)})
    44  	}
    45  	return envs
    46  }
    47  
    48  func flattenEnvs(envs API.Envs) []interface{} {
    49  	var ret []interface{}
    50  	for _, env := range envs {
    51  		r := map[string]interface{}{}
    52  		r["key"] = env.Key
    53  		r["value"] = env.Value
    54  		ret = append(ret, r)
    55  	}
    56  	return ret
    57  }
    58  
    59  func expandPorts(configured interface{}) API.Ports {
    60  	var ports API.Ports
    61  	if configured == nil {
    62  		return ports
    63  	}
    64  	rawPorts := configured.([]interface{})
    65  	for _, raw := range rawPorts {
    66  		port := raw.(map[string]interface{})
    67  		ports = append(ports, API.Port{Protocol: port["protocol"].(string), Number: port["number"].(int)})
    68  	}
    69  	return ports
    70  }
    71  
    72  func flattenPorts(ports API.Ports) []interface{} {
    73  	var ret []interface{}
    74  	for _, port := range ports {
    75  		r := map[string]interface{}{}
    76  		r["protocol"] = port.Protocol
    77  		r["number"] = port.Number
    78  		ret = append(ret, r)
    79  	}
    80  	return ret
    81  }
    82  func flattenPortMappings(ports API.PortMappings) []interface{} {
    83  	var ret []interface{}
    84  	for _, tasks := range ports {
    85  		for _, port := range tasks {
    86  			r := map[string]interface{}{}
    87  			ip := ""
    88  
    89  			addrs, err := net.LookupHost(port.Host)
    90  			if err == nil && len(addrs) > 0 {
    91  				ip = addrs[0]
    92  			}
    93  
    94  			r["host"] = port.Host
    95  			r["ipaddress"] = ip
    96  			r["container_port"] = port.ContainerPort
    97  			r["service_port"] = port.ServicePort
    98  			ret = append(ret, r)
    99  		}
   100  	}
   101  	return ret
   102  }
   103  
   104  func forceString(target interface{}) string {
   105  	if target == nil {
   106  		return ""
   107  	}
   108  
   109  	return target.(string)
   110  }