From b55da4c3e75f1fffcddac6d88755f96e8df3b57a Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Fri, 8 Jul 2016 09:58:46 +0200 Subject: [PATCH] Fix creating networks without specifying IP ranges --- .../cloudstack/resource_cloudstack_network.go | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/builtin/providers/cloudstack/resource_cloudstack_network.go b/builtin/providers/cloudstack/resource_cloudstack_network.go index 049f127ad..646897c15 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_network.go +++ b/builtin/providers/cloudstack/resource_cloudstack_network.go @@ -133,20 +133,35 @@ func resourceCloudStackNetworkCreate(d *schema.ResourceData, meta interface{}) e if !ok { displaytext = name } + // Create a new parameter struct p := cs.Network.NewCreateNetworkParams(displaytext.(string), name, networkofferingid, zoneid) - m, err := parseCIDR(d) + // Get the network offering to check if it supports specifying IP ranges + no, _, err := cs.NetworkOffering.GetNetworkOfferingByID(networkofferingid) + if err != nil { + return err + } + + m, err := parseCIDR(d, no.Specifyipranges) if err != nil { return err } // Set the needed IP config - p.SetStartip(m["startip"]) p.SetGateway(m["gateway"]) - p.SetEndip(m["endip"]) p.SetNetmask(m["netmask"]) + // Only set the start IP if we have one + if startip, ok := m["startip"]; ok { + p.SetStartip(startip) + } + + // Only set the end IP if we have one + if endip, ok := m["endip"]; ok { + p.SetEndip(endip) + } + if vlan, ok := d.GetOk("vlan"); ok { p.SetVlan(strconv.Itoa(vlan.(int))) } @@ -313,7 +328,7 @@ func resourceCloudStackNetworkDelete(d *schema.ResourceData, meta interface{}) e return nil } -func parseCIDR(d *schema.ResourceData) (map[string]string, error) { +func parseCIDR(d *schema.ResourceData, specifyiprange bool) (map[string]string, error) { m := make(map[string]string, 4) cidr := d.Get("cidr").(string) @@ -335,13 +350,13 @@ func parseCIDR(d *schema.ResourceData) (map[string]string, error) { if startip, ok := d.GetOk("startip"); ok { m["startip"] = startip.(string) - } else { + } else if specifyiprange { m["startip"] = fmt.Sprintf("%d.%d.%d.%d", sub[0], sub[1], sub[2], sub[3]+2) } if endip, ok := d.GetOk("endip"); ok { m["endip"] = endip.(string) - } else { + } else if specifyiprange { m["endip"] = fmt.Sprintf("%d.%d.%d.%d", sub[0]+(0xff-msk[0]), sub[1]+(0xff-msk[1]), sub[2]+(0xff-msk[2]), sub[3]+(0xff-msk[3]-1)) }