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