github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/docker/resource_docker_network.go (about)

     1  package docker
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"sort"
     7  
     8  	"github.com/hashicorp/terraform/helper/hashcode"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func resourceDockerNetwork() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceDockerNetworkCreate,
    15  		Read:   resourceDockerNetworkRead,
    16  		Delete: resourceDockerNetworkDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"name": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  
    25  			"check_duplicate": &schema.Schema{
    26  				Type:     schema.TypeBool,
    27  				Optional: true,
    28  				ForceNew: true,
    29  			},
    30  
    31  			"driver": &schema.Schema{
    32  				Type:     schema.TypeString,
    33  				Optional: true,
    34  				ForceNew: true,
    35  				Computed: true,
    36  			},
    37  
    38  			"options": &schema.Schema{
    39  				Type:     schema.TypeMap,
    40  				Optional: true,
    41  				ForceNew: true,
    42  				Computed: true,
    43  			},
    44  
    45  			"ipam_driver": &schema.Schema{
    46  				Type:     schema.TypeString,
    47  				Optional: true,
    48  				ForceNew: true,
    49  			},
    50  
    51  			"ipam_config": &schema.Schema{
    52  				Type:     schema.TypeSet,
    53  				Optional: true,
    54  				ForceNew: true,
    55  				Elem:     getIpamConfigElem(),
    56  				Set:      resourceDockerIpamConfigHash,
    57  			},
    58  
    59  			"id": &schema.Schema{
    60  				Type:     schema.TypeString,
    61  				Computed: true,
    62  			},
    63  
    64  			"scope": &schema.Schema{
    65  				Type:     schema.TypeString,
    66  				Computed: true,
    67  			},
    68  		},
    69  	}
    70  }
    71  
    72  func getIpamConfigElem() *schema.Resource {
    73  	return &schema.Resource{
    74  		Schema: map[string]*schema.Schema{
    75  			"subnet": &schema.Schema{
    76  				Type:     schema.TypeString,
    77  				Optional: true,
    78  				ForceNew: true,
    79  			},
    80  
    81  			"ip_range": &schema.Schema{
    82  				Type:     schema.TypeString,
    83  				Optional: true,
    84  				ForceNew: true,
    85  			},
    86  
    87  			"gateway": &schema.Schema{
    88  				Type:     schema.TypeString,
    89  				Optional: true,
    90  				ForceNew: true,
    91  			},
    92  
    93  			"aux_address": &schema.Schema{
    94  				Type:     schema.TypeMap,
    95  				Optional: true,
    96  				ForceNew: true,
    97  			},
    98  		},
    99  	}
   100  }
   101  
   102  func resourceDockerIpamConfigHash(v interface{}) int {
   103  	var buf bytes.Buffer
   104  	m := v.(map[string]interface{})
   105  
   106  	if v, ok := m["subnet"]; ok {
   107  		buf.WriteString(fmt.Sprintf("%v-", v.(string)))
   108  	}
   109  
   110  	if v, ok := m["ip_range"]; ok {
   111  		buf.WriteString(fmt.Sprintf("%v-", v.(string)))
   112  	}
   113  
   114  	if v, ok := m["gateway"]; ok {
   115  		buf.WriteString(fmt.Sprintf("%v-", v.(string)))
   116  	}
   117  
   118  	if v, ok := m["aux_address"]; ok {
   119  		auxAddress := v.(map[string]interface{})
   120  
   121  		keys := make([]string, len(auxAddress))
   122  		i := 0
   123  		for k, _ := range auxAddress {
   124  			keys[i] = k
   125  			i++
   126  		}
   127  		sort.Strings(keys)
   128  
   129  		for _, k := range keys {
   130  			buf.WriteString(fmt.Sprintf("%v-%v-", k, auxAddress[k].(string)))
   131  		}
   132  	}
   133  
   134  	return hashcode.String(buf.String())
   135  }