github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/vcd/resource_vcd_network.go (about) 1 package vcd 2 3 import ( 4 "log" 5 6 "bytes" 7 "fmt" 8 "github.com/hashicorp/terraform/helper/hashcode" 9 "github.com/hashicorp/terraform/helper/schema" 10 types "github.com/hmrc/vmware-govcd/types/v56" 11 "strings" 12 ) 13 14 func resourceVcdNetwork() *schema.Resource { 15 return &schema.Resource{ 16 Create: resourceVcdNetworkCreate, 17 Read: resourceVcdNetworkRead, 18 Delete: resourceVcdNetworkDelete, 19 20 Schema: map[string]*schema.Schema{ 21 "name": &schema.Schema{ 22 Type: schema.TypeString, 23 Required: true, 24 ForceNew: true, 25 }, 26 27 "fence_mode": &schema.Schema{ 28 Type: schema.TypeString, 29 Optional: true, 30 ForceNew: true, 31 Default: "natRouted", 32 }, 33 34 "edge_gateway": &schema.Schema{ 35 Type: schema.TypeString, 36 Required: true, 37 ForceNew: true, 38 }, 39 40 "netmask": &schema.Schema{ 41 Type: schema.TypeString, 42 Optional: true, 43 ForceNew: true, 44 Default: "255.255.255.0", 45 }, 46 47 "gateway": &schema.Schema{ 48 Type: schema.TypeString, 49 Required: true, 50 ForceNew: true, 51 }, 52 53 "dns1": &schema.Schema{ 54 Type: schema.TypeString, 55 Optional: true, 56 ForceNew: true, 57 Default: "8.8.8.8", 58 }, 59 60 "dns2": &schema.Schema{ 61 Type: schema.TypeString, 62 Optional: true, 63 ForceNew: true, 64 Default: "8.8.4.4", 65 }, 66 67 "dns_suffix": &schema.Schema{ 68 Type: schema.TypeString, 69 Optional: true, 70 ForceNew: true, 71 }, 72 73 "href": &schema.Schema{ 74 Type: schema.TypeString, 75 Optional: true, 76 Computed: true, 77 ForceNew: true, 78 }, 79 80 "dhcp_pool": &schema.Schema{ 81 Type: schema.TypeSet, 82 Optional: true, 83 ForceNew: true, 84 Elem: &schema.Resource{ 85 Schema: map[string]*schema.Schema{ 86 "start_address": &schema.Schema{ 87 Type: schema.TypeString, 88 Required: true, 89 }, 90 91 "end_address": &schema.Schema{ 92 Type: schema.TypeString, 93 Required: true, 94 }, 95 }, 96 }, 97 Set: resourceVcdNetworkIPAddressHash, 98 }, 99 "static_ip_pool": &schema.Schema{ 100 Type: schema.TypeSet, 101 Optional: true, 102 ForceNew: true, 103 Elem: &schema.Resource{ 104 Schema: map[string]*schema.Schema{ 105 "start_address": &schema.Schema{ 106 Type: schema.TypeString, 107 Required: true, 108 }, 109 110 "end_address": &schema.Schema{ 111 Type: schema.TypeString, 112 Required: true, 113 }, 114 }, 115 }, 116 Set: resourceVcdNetworkIPAddressHash, 117 }, 118 }, 119 } 120 } 121 122 func resourceVcdNetworkCreate(d *schema.ResourceData, meta interface{}) error { 123 vcdClient := meta.(*VCDClient) 124 log.Printf("[TRACE] CLIENT: %#v", vcdClient) 125 vcdClient.Mutex.Lock() 126 defer vcdClient.Mutex.Unlock() 127 128 edgeGateway, err := vcdClient.OrgVdc.FindEdgeGateway(d.Get("edge_gateway").(string)) 129 130 ipRanges := expandIPRange(d.Get("static_ip_pool").(*schema.Set).List()) 131 132 newnetwork := &types.OrgVDCNetwork{ 133 Xmlns: "http://www.vmware.com/vcloud/v1.5", 134 Name: d.Get("name").(string), 135 Configuration: &types.NetworkConfiguration{ 136 FenceMode: d.Get("fence_mode").(string), 137 IPScopes: &types.IPScopes{ 138 IPScope: types.IPScope{ 139 IsInherited: false, 140 Gateway: d.Get("gateway").(string), 141 Netmask: d.Get("netmask").(string), 142 DNS1: d.Get("dns1").(string), 143 DNS2: d.Get("dns2").(string), 144 DNSSuffix: d.Get("dns_suffix").(string), 145 IPRanges: &ipRanges, 146 }, 147 }, 148 BackwardCompatibilityMode: true, 149 }, 150 EdgeGateway: &types.Reference{ 151 HREF: edgeGateway.EdgeGateway.HREF, 152 }, 153 IsShared: false, 154 } 155 156 log.Printf("[INFO] NETWORK: %#v", newnetwork) 157 158 err = retryCall(vcdClient.MaxRetryTimeout, func() error { 159 return vcdClient.OrgVdc.CreateOrgVDCNetwork(newnetwork) 160 }) 161 if err != nil { 162 return fmt.Errorf("Error: %#v", err) 163 } 164 165 err = vcdClient.OrgVdc.Refresh() 166 if err != nil { 167 return fmt.Errorf("Error refreshing vdc: %#v", err) 168 } 169 170 network, err := vcdClient.OrgVdc.FindVDCNetwork(d.Get("name").(string)) 171 if err != nil { 172 return fmt.Errorf("Error finding network: %#v", err) 173 } 174 175 if dhcp, ok := d.GetOk("dhcp_pool"); ok { 176 err = retryCall(vcdClient.MaxRetryTimeout, func() error { 177 task, err := edgeGateway.AddDhcpPool(network.OrgVDCNetwork, dhcp.(*schema.Set).List()) 178 if err != nil { 179 return fmt.Errorf("Error adding DHCP pool: %#v", err) 180 } 181 182 return task.WaitTaskCompletion() 183 }) 184 if err != nil { 185 return fmt.Errorf("Error completing tasks: %#v", err) 186 } 187 188 } 189 190 d.SetId(d.Get("name").(string)) 191 192 return resourceVcdNetworkRead(d, meta) 193 } 194 195 func resourceVcdNetworkRead(d *schema.ResourceData, meta interface{}) error { 196 vcdClient := meta.(*VCDClient) 197 log.Printf("[DEBUG] VCD Client configuration: %#v", vcdClient) 198 log.Printf("[DEBUG] VCD Client configuration: %#v", vcdClient.OrgVdc) 199 200 err := vcdClient.OrgVdc.Refresh() 201 if err != nil { 202 return fmt.Errorf("Error refreshing vdc: %#v", err) 203 } 204 205 network, err := vcdClient.OrgVdc.FindVDCNetwork(d.Id()) 206 if err != nil { 207 log.Printf("[DEBUG] Network no longer exists. Removing from tfstate") 208 d.SetId("") 209 return nil 210 } 211 212 d.Set("name", network.OrgVDCNetwork.Name) 213 d.Set("href", network.OrgVDCNetwork.HREF) 214 if c := network.OrgVDCNetwork.Configuration; c != nil { 215 d.Set("fence_mode", c.FenceMode) 216 if c.IPScopes != nil { 217 d.Set("gateway", c.IPScopes.IPScope.Gateway) 218 d.Set("netmask", c.IPScopes.IPScope.Netmask) 219 d.Set("dns1", c.IPScopes.IPScope.DNS1) 220 d.Set("dns2", c.IPScopes.IPScope.DNS2) 221 } 222 } 223 224 return nil 225 } 226 227 func resourceVcdNetworkDelete(d *schema.ResourceData, meta interface{}) error { 228 vcdClient := meta.(*VCDClient) 229 vcdClient.Mutex.Lock() 230 defer vcdClient.Mutex.Unlock() 231 err := vcdClient.OrgVdc.Refresh() 232 if err != nil { 233 return fmt.Errorf("Error refreshing vdc: %#v", err) 234 } 235 236 network, err := vcdClient.OrgVdc.FindVDCNetwork(d.Id()) 237 if err != nil { 238 return fmt.Errorf("Error finding network: %#v", err) 239 } 240 241 err = retryCall(vcdClient.MaxRetryTimeout, func() error { 242 task, err := network.Delete() 243 if err != nil { 244 return fmt.Errorf("Error Deleting Network: %#v", err) 245 } 246 return task.WaitTaskCompletion() 247 }) 248 if err != nil { 249 return err 250 } 251 252 return nil 253 } 254 255 func resourceVcdNetworkIPAddressHash(v interface{}) int { 256 var buf bytes.Buffer 257 m := v.(map[string]interface{}) 258 buf.WriteString(fmt.Sprintf("%s-", 259 strings.ToLower(m["start_address"].(string)))) 260 buf.WriteString(fmt.Sprintf("%s-", 261 strings.ToLower(m["end_address"].(string)))) 262 263 return hashcode.String(buf.String()) 264 }