Skip to content

Commit c80ddcc

Browse files
Carles FiguerolaCarles Figuerola
authored andcommitted
Add vlan, startip, endip and gateway for network creation
1 parent 532812d commit c80ddcc

2 files changed

Lines changed: 83 additions & 10 deletions

File tree

‎builtin/providers/cloudstack/resource_cloudstack_network.go‎

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"log"
66
"net"
7+
"strconv"
78
"strings"
89

910
"github.com/hashicorp/terraform/helper/schema"
@@ -35,11 +36,38 @@ func resourceCloudStackNetwork() *schema.Resource {
3536
ForceNew: true,
3637
},
3738

39+
"gateway": &schema.Schema{
40+
Type: schema.TypeString,
41+
Optional: true,
42+
Computed: true,
43+
ForceNew: true,
44+
},
45+
46+
"startip": &schema.Schema{
47+
Type: schema.TypeString,
48+
Optional: true,
49+
Computed: true,
50+
ForceNew: true,
51+
},
52+
53+
"endip": &schema.Schema{
54+
Type: schema.TypeString,
55+
Optional: true,
56+
Computed: true,
57+
ForceNew: true,
58+
},
59+
3860
"network_offering": &schema.Schema{
3961
Type: schema.TypeString,
4062
Required: true,
4163
},
4264

65+
"vlan": &schema.Schema{
66+
Type: schema.TypeInt,
67+
Optional: true,
68+
ForceNew: true,
69+
},
70+
4371
"vpc": &schema.Schema{
4472
Type: schema.TypeString,
4573
Optional: true,
@@ -94,17 +122,34 @@ func resourceCloudStackNetworkCreate(d *schema.ResourceData, meta interface{}) e
94122
p := cs.Network.NewCreateNetworkParams(displaytext.(string), name, networkofferingid, zoneid)
95123

96124
// Get the network details from the CIDR
97-
m, err := parseCIDR(d.Get("cidr").(string))
125+
m := make(map[string]string, 4)
126+
cidr := d.Get("cidr").(string)
127+
m["cidr"] = cidr
128+
if startip, ok := d.GetOk("startip"); ok {
129+
m["startip"] = startip.(string)
130+
}
131+
if endip, ok := d.GetOk("endip"); ok {
132+
m["endip"] = endip.(string)
133+
}
134+
if gateway, ok := d.GetOk("gateway"); ok {
135+
m["gateway"] = gateway.(string)
136+
}
137+
138+
m, err := parseCIDR(m)
98139
if err != nil {
99140
return err
100141
}
101142

102143
// Set the needed IP config
103-
p.SetStartip(m["start"])
144+
p.SetStartip(m["startip"])
104145
p.SetGateway(m["gateway"])
105-
p.SetEndip(m["end"])
146+
p.SetEndip(m["endip"])
106147
p.SetNetmask(m["netmask"])
107148

149+
if vlan, ok := d.GetOk("vlan"); ok {
150+
p.SetVlan(strconv.Itoa(vlan.(int)))
151+
}
152+
108153
// Check is this network needs to be created in a VPC
109154
vpc := d.Get("vpc").(string)
110155
if vpc != "" {
@@ -240,22 +285,36 @@ func resourceCloudStackNetworkDelete(d *schema.ResourceData, meta interface{}) e
240285
return nil
241286
}
242287

243-
func parseCIDR(cidr string) (map[string]string, error) {
288+
func parseCIDR(m_in map[string]string) (map[string]string, error) {
244289
m := make(map[string]string, 4)
245290

246-
ip, ipnet, err := net.ParseCIDR(cidr)
291+
ip, ipnet, err := net.ParseCIDR(m_in["cidr"])
247292
if err != nil {
248-
return nil, fmt.Errorf("Unable to parse cidr %s: %s", cidr, err)
293+
return nil, fmt.Errorf("Unable to parse cidr %s: %s", m_in["cidr"], err)
249294
}
250295

251296
msk := ipnet.Mask
252297
sub := ip.Mask(msk)
253298

254299
m["netmask"] = fmt.Sprintf("%d.%d.%d.%d", msk[0], msk[1], msk[2], msk[3])
255-
m["gateway"] = fmt.Sprintf("%d.%d.%d.%d", sub[0], sub[1], sub[2], sub[3]+1)
256-
m["start"] = fmt.Sprintf("%d.%d.%d.%d", sub[0], sub[1], sub[2], sub[3]+2)
257-
m["end"] = fmt.Sprintf("%d.%d.%d.%d",
258-
sub[0]+(0xff-msk[0]), sub[1]+(0xff-msk[1]), sub[2]+(0xff-msk[2]), sub[3]+(0xff-msk[3]-1))
300+
if gateway, ok := m_in["gateway"]; ok {
301+
m["gateway"] = gateway
302+
} else {
303+
m["gateway"] = fmt.Sprintf("%d.%d.%d.%d", sub[0], sub[1], sub[2], sub[3]+1)
304+
}
305+
306+
if startip, ok := m_in["startip"]; ok {
307+
m["startip"] = startip
308+
} else {
309+
m["startip"] = fmt.Sprintf("%d.%d.%d.%d", sub[0], sub[1], sub[2], sub[3]+2)
310+
}
311+
312+
if endip, ok := m_in["endip"]; ok {
313+
m["endip"] = endip
314+
} else {
315+
m["endip"] = fmt.Sprintf("%d.%d.%d.%d",
316+
sub[0]+(0xff-msk[0]), sub[1]+(0xff-msk[1]), sub[2]+(0xff-msk[2]), sub[3]+(0xff-msk[3]-1))
317+
}
259318

260319
return m, nil
261320
}

‎website/source/docs/providers/cloudstack/r/network.html.markdown‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,23 @@ The following arguments are supported:
3434
* `cidr` - (Required) The CIDR block for the network. Changing this forces a new
3535
resource to be created.
3636

37+
* `startip` - (Optional) Start of the IP block that will be available on the
38+
network. It will be autocalculated to the second available IP in the range
39+
if it's left out.
40+
41+
* `endip` - (Optional) End of the IP block that will be available on the
42+
network. It will be autocalculated to the last available IP in the range
43+
if it's left out.
44+
45+
* `gateway` - (Optional) Gateway that will be provided to the instances in this
46+
network. It defaults to the first usable IP if it's left empty.
47+
3748
* `network_offering` - (Required) The name or ID of the network offering to use
3849
for this network.
3950

51+
* `vlan` - (Optional) The VLAN number (1-4095) the network will use. This might be
52+
required by the Network Offering if specifyVlan=true is set.
53+
4054
* `vpc` - (Optional) The name or ID of the VPC to create this network for. Changing
4155
this forces a new resource to be created.
4256

0 commit comments

Comments
 (0)