github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/docker/resource_docker_network_funcs.go (about)

     1  package docker
     2  
     3  import (
     4  	"fmt"
     5  
     6  	dc "github.com/fsouza/go-dockerclient"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  func resourceDockerNetworkCreate(d *schema.ResourceData, meta interface{}) error {
    11  	client := meta.(*dc.Client)
    12  
    13  	createOpts := dc.CreateNetworkOptions{
    14  		Name: d.Get("name").(string),
    15  	}
    16  	if v, ok := d.GetOk("check_duplicate"); ok {
    17  		createOpts.CheckDuplicate = v.(bool)
    18  	}
    19  	if v, ok := d.GetOk("driver"); ok {
    20  		createOpts.Driver = v.(string)
    21  	}
    22  	if v, ok := d.GetOk("options"); ok {
    23  		createOpts.Options = v.(map[string]interface{})
    24  	}
    25  
    26  	ipamOpts := dc.IPAMOptions{}
    27  	ipamOptsSet := false
    28  	if v, ok := d.GetOk("ipam_driver"); ok {
    29  		ipamOpts.Driver = v.(string)
    30  		ipamOptsSet = true
    31  	}
    32  	if v, ok := d.GetOk("ipam_config"); ok {
    33  		ipamOpts.Config = ipamConfigSetToIpamConfigs(v.(*schema.Set))
    34  		ipamOptsSet = true
    35  	}
    36  
    37  	if ipamOptsSet {
    38  		createOpts.IPAM = ipamOpts
    39  	}
    40  
    41  	var err error
    42  	var retNetwork *dc.Network
    43  	if retNetwork, err = client.CreateNetwork(createOpts); err != nil {
    44  		return fmt.Errorf("Unable to create network: %s", err)
    45  	}
    46  	if retNetwork == nil {
    47  		return fmt.Errorf("Returned network is nil")
    48  	}
    49  
    50  	d.SetId(retNetwork.ID)
    51  	d.Set("name", retNetwork.Name)
    52  	d.Set("scope", retNetwork.Scope)
    53  	d.Set("driver", retNetwork.Driver)
    54  	d.Set("options", retNetwork.Options)
    55  
    56  	return nil
    57  }
    58  
    59  func resourceDockerNetworkRead(d *schema.ResourceData, meta interface{}) error {
    60  	client := meta.(*dc.Client)
    61  
    62  	var err error
    63  	var retNetwork *dc.Network
    64  	if retNetwork, err = client.NetworkInfo(d.Id()); err != nil {
    65  		if _, ok := err.(*dc.NoSuchNetwork); !ok {
    66  			return fmt.Errorf("Unable to inspect network: %s", err)
    67  		}
    68  	}
    69  	if retNetwork == nil {
    70  		d.SetId("")
    71  		return nil
    72  	}
    73  
    74  	d.Set("scope", retNetwork.Scope)
    75  	d.Set("driver", retNetwork.Driver)
    76  	d.Set("options", retNetwork.Options)
    77  
    78  	return nil
    79  }
    80  
    81  func resourceDockerNetworkDelete(d *schema.ResourceData, meta interface{}) error {
    82  	client := meta.(*dc.Client)
    83  
    84  	if err := client.RemoveNetwork(d.Id()); err != nil {
    85  		if _, ok := err.(*dc.NoSuchNetwork); !ok {
    86  			return fmt.Errorf("Error deleting network %s: %s", d.Id(), err)
    87  		}
    88  	}
    89  
    90  	d.SetId("")
    91  	return nil
    92  }
    93  
    94  func ipamConfigSetToIpamConfigs(ipamConfigSet *schema.Set) []dc.IPAMConfig {
    95  	ipamConfigs := make([]dc.IPAMConfig, ipamConfigSet.Len())
    96  
    97  	for i, ipamConfigInt := range ipamConfigSet.List() {
    98  		ipamConfigRaw := ipamConfigInt.(map[string]interface{})
    99  
   100  		ipamConfig := dc.IPAMConfig{}
   101  		ipamConfig.Subnet = ipamConfigRaw["subnet"].(string)
   102  		ipamConfig.IPRange = ipamConfigRaw["ip_range"].(string)
   103  		ipamConfig.Gateway = ipamConfigRaw["gateway"].(string)
   104  
   105  		auxAddressRaw := ipamConfigRaw["aux_address"].(map[string]interface{})
   106  		ipamConfig.AuxAddress = make(map[string]string, len(auxAddressRaw))
   107  		for k, v := range auxAddressRaw {
   108  			ipamConfig.AuxAddress[k] = v.(string)
   109  		}
   110  
   111  		ipamConfigs[i] = ipamConfig
   112  	}
   113  
   114  	return ipamConfigs
   115  }