github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 if v, ok := d.GetOk("internal"); ok { 26 createOpts.Internal = v.(bool) 27 } 28 29 ipamOpts := dc.IPAMOptions{} 30 ipamOptsSet := false 31 if v, ok := d.GetOk("ipam_driver"); ok { 32 ipamOpts.Driver = v.(string) 33 ipamOptsSet = true 34 } 35 if v, ok := d.GetOk("ipam_config"); ok { 36 ipamOpts.Config = ipamConfigSetToIpamConfigs(v.(*schema.Set)) 37 ipamOptsSet = true 38 } 39 40 if ipamOptsSet { 41 createOpts.IPAM = ipamOpts 42 } 43 44 var err error 45 var retNetwork *dc.Network 46 if retNetwork, err = client.CreateNetwork(createOpts); err != nil { 47 return fmt.Errorf("Unable to create network: %s", err) 48 } 49 if retNetwork == nil { 50 return fmt.Errorf("Returned network is nil") 51 } 52 53 d.SetId(retNetwork.ID) 54 d.Set("name", retNetwork.Name) 55 d.Set("scope", retNetwork.Scope) 56 d.Set("driver", retNetwork.Driver) 57 d.Set("options", retNetwork.Options) 58 59 // The 'internal' property is not send back when create network 60 d.Set("internal", createOpts.Internal) 61 62 return nil 63 } 64 65 func resourceDockerNetworkRead(d *schema.ResourceData, meta interface{}) error { 66 client := meta.(*dc.Client) 67 68 var err error 69 var retNetwork *dc.Network 70 if retNetwork, err = client.NetworkInfo(d.Id()); err != nil { 71 if _, ok := err.(*dc.NoSuchNetwork); !ok { 72 return fmt.Errorf("Unable to inspect network: %s", err) 73 } 74 } 75 if retNetwork == nil { 76 d.SetId("") 77 return nil 78 } 79 80 d.Set("scope", retNetwork.Scope) 81 d.Set("driver", retNetwork.Driver) 82 d.Set("options", retNetwork.Options) 83 d.Set("internal", retNetwork.Internal) 84 85 return nil 86 } 87 88 func resourceDockerNetworkDelete(d *schema.ResourceData, meta interface{}) error { 89 client := meta.(*dc.Client) 90 91 if err := client.RemoveNetwork(d.Id()); err != nil { 92 if _, ok := err.(*dc.NoSuchNetwork); !ok { 93 return fmt.Errorf("Error deleting network %s: %s", d.Id(), err) 94 } 95 } 96 97 d.SetId("") 98 return nil 99 } 100 101 func ipamConfigSetToIpamConfigs(ipamConfigSet *schema.Set) []dc.IPAMConfig { 102 ipamConfigs := make([]dc.IPAMConfig, ipamConfigSet.Len()) 103 104 for i, ipamConfigInt := range ipamConfigSet.List() { 105 ipamConfigRaw := ipamConfigInt.(map[string]interface{}) 106 107 ipamConfig := dc.IPAMConfig{} 108 ipamConfig.Subnet = ipamConfigRaw["subnet"].(string) 109 ipamConfig.IPRange = ipamConfigRaw["ip_range"].(string) 110 ipamConfig.Gateway = ipamConfigRaw["gateway"].(string) 111 112 auxAddressRaw := ipamConfigRaw["aux_address"].(map[string]interface{}) 113 ipamConfig.AuxAddress = make(map[string]string, len(auxAddressRaw)) 114 for k, v := range auxAddressRaw { 115 ipamConfig.AuxAddress[k] = v.(string) 116 } 117 118 ipamConfigs[i] = ipamConfig 119 } 120 121 return ipamConfigs 122 }