|
4 | 4 | "fmt" |
5 | 5 | "log" |
6 | 6 | "net" |
| 7 | + "strconv" |
7 | 8 | "strings" |
8 | 9 |
|
9 | 10 | "github.com/hashicorp/terraform/helper/schema" |
@@ -35,11 +36,38 @@ func resourceCloudStackNetwork() *schema.Resource { |
35 | 36 | ForceNew: true, |
36 | 37 | }, |
37 | 38 |
|
| 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 | + |
38 | 60 | "network_offering": &schema.Schema{ |
39 | 61 | Type: schema.TypeString, |
40 | 62 | Required: true, |
41 | 63 | }, |
42 | 64 |
|
| 65 | + "vlan": &schema.Schema{ |
| 66 | + Type: schema.TypeInt, |
| 67 | + Optional: true, |
| 68 | + ForceNew: true, |
| 69 | + }, |
| 70 | + |
43 | 71 | "vpc": &schema.Schema{ |
44 | 72 | Type: schema.TypeString, |
45 | 73 | Optional: true, |
@@ -94,17 +122,34 @@ func resourceCloudStackNetworkCreate(d *schema.ResourceData, meta interface{}) e |
94 | 122 | p := cs.Network.NewCreateNetworkParams(displaytext.(string), name, networkofferingid, zoneid) |
95 | 123 |
|
96 | 124 | // 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) |
98 | 139 | if err != nil { |
99 | 140 | return err |
100 | 141 | } |
101 | 142 |
|
102 | 143 | // Set the needed IP config |
103 | | - p.SetStartip(m["start"]) |
| 144 | + p.SetStartip(m["startip"]) |
104 | 145 | p.SetGateway(m["gateway"]) |
105 | | - p.SetEndip(m["end"]) |
| 146 | + p.SetEndip(m["endip"]) |
106 | 147 | p.SetNetmask(m["netmask"]) |
107 | 148 |
|
| 149 | + if vlan, ok := d.GetOk("vlan"); ok { |
| 150 | + p.SetVlan(strconv.Itoa(vlan.(int))) |
| 151 | + } |
| 152 | + |
108 | 153 | // Check is this network needs to be created in a VPC |
109 | 154 | vpc := d.Get("vpc").(string) |
110 | 155 | if vpc != "" { |
@@ -240,22 +285,36 @@ func resourceCloudStackNetworkDelete(d *schema.ResourceData, meta interface{}) e |
240 | 285 | return nil |
241 | 286 | } |
242 | 287 |
|
243 | | -func parseCIDR(cidr string) (map[string]string, error) { |
| 288 | +func parseCIDR(m_in map[string]string) (map[string]string, error) { |
244 | 289 | m := make(map[string]string, 4) |
245 | 290 |
|
246 | | - ip, ipnet, err := net.ParseCIDR(cidr) |
| 291 | + ip, ipnet, err := net.ParseCIDR(m_in["cidr"]) |
247 | 292 | 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) |
249 | 294 | } |
250 | 295 |
|
251 | 296 | msk := ipnet.Mask |
252 | 297 | sub := ip.Mask(msk) |
253 | 298 |
|
254 | 299 | 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 | + } |
259 | 318 |
|
260 | 319 | return m, nil |
261 | 320 | } |
0 commit comments