2014-12-10 22:20:52 +01:00
|
|
|
package cloudstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
2016-01-15 05:58:33 +01:00
|
|
|
"strconv"
|
2014-12-10 22:20:52 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/xanzy/go-cloudstack/cloudstack"
|
|
|
|
)
|
|
|
|
|
2016-06-24 13:27:05 +02:00
|
|
|
const none = "none"
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
func resourceCloudStackNetwork() *schema.Resource {
|
2016-06-24 13:27:05 +02:00
|
|
|
aclidSchema := &schema.Schema{
|
2016-06-25 19:42:01 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Default: none,
|
2016-06-24 13:27:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
aclidSchema.StateFunc = func(v interface{}) string {
|
|
|
|
value := v.(string)
|
|
|
|
|
|
|
|
if value == none {
|
|
|
|
aclidSchema.ForceNew = true
|
2016-07-21 15:23:58 +02:00
|
|
|
} else {
|
|
|
|
aclidSchema.ForceNew = false
|
2016-06-24 13:27:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceCloudStackNetworkCreate,
|
|
|
|
Read: resourceCloudStackNetworkRead,
|
|
|
|
Update: resourceCloudStackNetworkUpdate,
|
|
|
|
Delete: resourceCloudStackNetworkDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"display_text": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"cidr": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-01-15 05:58:33 +01:00
|
|
|
"gateway": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"startip": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"endip": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-12-12 10:06:42 +01:00
|
|
|
"network_domain": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
"network_offering": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
2016-01-15 05:58:33 +01:00
|
|
|
"vlan": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-11 17:14:19 +02:00
|
|
|
"vpc_id": &schema.Schema{
|
2014-12-10 22:20:52 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-06-24 13:27:05 +02:00
|
|
|
"acl_id": aclidSchema,
|
2016-04-11 17:14:19 +02:00
|
|
|
|
2015-08-21 16:59:35 +02:00
|
|
|
"project": &schema.Schema{
|
2014-12-10 22:20:52 +01:00
|
|
|
Type: schema.TypeString,
|
2015-08-21 16:59:35 +02:00
|
|
|
Optional: true,
|
2016-07-08 17:16:35 +02:00
|
|
|
Computed: true,
|
2014-12-10 22:20:52 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
2015-08-19 23:56:57 +02:00
|
|
|
|
2015-08-21 16:59:35 +02:00
|
|
|
"zone": &schema.Schema{
|
2015-08-19 23:56:57 +02:00
|
|
|
Type: schema.TypeString,
|
2015-08-21 16:59:35 +02:00
|
|
|
Required: true,
|
2015-08-19 23:56:57 +02:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
2016-01-19 18:41:18 +01:00
|
|
|
|
|
|
|
"tags": tagsSchema(),
|
2014-12-10 22:20:52 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackNetworkCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
|
2015-10-05 14:05:21 +02:00
|
|
|
// Retrieve the network_offering ID
|
|
|
|
networkofferingid, e := retrieveID(cs, "network_offering", d.Get("network_offering").(string))
|
2014-12-10 22:20:52 +01:00
|
|
|
if e != nil {
|
|
|
|
return e.Error()
|
|
|
|
}
|
|
|
|
|
2015-10-05 14:05:21 +02:00
|
|
|
// Retrieve the zone ID
|
|
|
|
zoneid, e := retrieveID(cs, "zone", d.Get("zone").(string))
|
2014-12-10 22:20:52 +01:00
|
|
|
if e != nil {
|
|
|
|
return e.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute/set the display text
|
2015-01-15 21:46:06 +01:00
|
|
|
displaytext, ok := d.GetOk("display_text")
|
|
|
|
if !ok {
|
2014-12-10 22:20:52 +01:00
|
|
|
displaytext = name
|
|
|
|
}
|
2016-07-08 09:58:46 +02:00
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
// Create a new parameter struct
|
2015-01-15 21:46:06 +01:00
|
|
|
p := cs.Network.NewCreateNetworkParams(displaytext.(string), name, networkofferingid, zoneid)
|
2014-12-10 22:20:52 +01:00
|
|
|
|
2016-07-08 09:58:46 +02:00
|
|
|
// 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)
|
2014-12-10 22:20:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the needed IP config
|
|
|
|
p.SetGateway(m["gateway"])
|
|
|
|
p.SetNetmask(m["netmask"])
|
|
|
|
|
2016-07-08 09:58:46 +02:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2016-12-12 10:06:42 +01:00
|
|
|
// Set the network domain if we have one
|
|
|
|
if networkDomain, ok := d.GetOk("network_domain"); ok {
|
|
|
|
p.SetNetworkdomain(networkDomain.(string))
|
|
|
|
}
|
|
|
|
|
2016-01-15 05:58:33 +01:00
|
|
|
if vlan, ok := d.GetOk("vlan"); ok {
|
|
|
|
p.SetVlan(strconv.Itoa(vlan.(int)))
|
|
|
|
}
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
// Check is this network needs to be created in a VPC
|
2016-06-25 19:42:01 +02:00
|
|
|
if vpcid, ok := d.GetOk("vpc_id"); ok {
|
|
|
|
// Set the vpc id
|
|
|
|
p.SetVpcid(vpcid.(string))
|
2014-12-10 22:20:52 +01:00
|
|
|
|
|
|
|
// Since we're in a VPC, check if we want to assiciate an ACL list
|
2016-06-25 19:42:01 +02:00
|
|
|
if aclid, ok := d.GetOk("acl_id"); ok && aclid.(string) != none {
|
2015-10-05 14:05:21 +02:00
|
|
|
// Set the acl ID
|
2016-04-11 17:14:19 +02:00
|
|
|
p.SetAclid(aclid.(string))
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-11 20:56:20 +02:00
|
|
|
// If there is a project supplied, we retrieve and set the project id
|
2016-04-11 17:14:19 +02:00
|
|
|
if err := setProjectid(p, cs, d); err != nil {
|
|
|
|
return err
|
2015-08-21 16:59:35 +02:00
|
|
|
}
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
// Create the new network
|
|
|
|
r, err := cs.Network.CreateNetwork(p)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating network %s: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(r.Id)
|
|
|
|
|
2016-01-22 05:45:51 +01:00
|
|
|
err = setTags(cs, d, "network")
|
|
|
|
if err != nil {
|
2016-01-22 12:13:26 +01:00
|
|
|
return fmt.Errorf("Error setting tags: %s", err)
|
2016-01-22 05:45:51 +01:00
|
|
|
}
|
2016-01-19 18:41:18 +01:00
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
return resourceCloudStackNetworkRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackNetworkRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
// Get the virtual machine details
|
2016-04-21 17:31:53 +02:00
|
|
|
n, count, err := cs.Network.GetNetworkByID(
|
|
|
|
d.Id(),
|
|
|
|
cloudstack.WithProject(d.Get("project").(string)),
|
|
|
|
)
|
2014-12-10 22:20:52 +01:00
|
|
|
if err != nil {
|
|
|
|
if count == 0 {
|
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] Network %s does no longer exist", d.Get("name").(string))
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("name", n.Name)
|
2015-01-15 21:46:06 +01:00
|
|
|
d.Set("display_text", n.Displaytext)
|
2014-12-10 22:20:52 +01:00
|
|
|
d.Set("cidr", n.Cidr)
|
2016-01-15 21:24:18 +01:00
|
|
|
d.Set("gateway", n.Gateway)
|
2016-12-12 10:06:42 +01:00
|
|
|
d.Set("network_domain", n.Networkdomain)
|
2016-06-25 19:42:01 +02:00
|
|
|
d.Set("vpc_id", n.Vpcid)
|
2015-04-29 11:21:37 +02:00
|
|
|
|
2016-06-25 19:42:01 +02:00
|
|
|
if n.Aclid == "" {
|
|
|
|
n.Aclid = none
|
2016-04-11 17:14:19 +02:00
|
|
|
}
|
2016-06-25 19:42:01 +02:00
|
|
|
d.Set("acl_id", n.Aclid)
|
2016-04-11 17:14:19 +02:00
|
|
|
|
2016-01-22 05:45:51 +01:00
|
|
|
// Read the tags and store them in a map
|
2016-01-22 12:13:26 +01:00
|
|
|
tags := make(map[string]interface{})
|
2016-01-19 18:41:18 +01:00
|
|
|
for item := range n.Tags {
|
|
|
|
tags[n.Tags[item].Key] = n.Tags[item].Value
|
|
|
|
}
|
|
|
|
d.Set("tags", tags)
|
|
|
|
|
2015-10-05 14:05:21 +02:00
|
|
|
setValueOrID(d, "network_offering", n.Networkofferingname, n.Networkofferingid)
|
|
|
|
setValueOrID(d, "project", n.Project, n.Projectid)
|
|
|
|
setValueOrID(d, "zone", n.Zonename, n.Zoneid)
|
2014-12-10 22:20:52 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackNetworkUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
|
|
|
|
// Create a new parameter struct
|
|
|
|
p := cs.Network.NewUpdateNetworkParams(d.Id())
|
|
|
|
|
2015-04-13 17:33:22 +02:00
|
|
|
// Check if the name or display text is changed
|
|
|
|
if d.HasChange("name") || d.HasChange("display_text") {
|
2014-12-10 22:20:52 +01:00
|
|
|
p.SetName(name)
|
|
|
|
|
2015-04-13 17:33:22 +02:00
|
|
|
// Compute/set the display text
|
|
|
|
displaytext := d.Get("display_text").(string)
|
|
|
|
if displaytext == "" {
|
|
|
|
displaytext = name
|
|
|
|
}
|
|
|
|
p.SetDisplaytext(displaytext)
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the cidr is changed
|
|
|
|
if d.HasChange("cidr") {
|
|
|
|
p.SetGuestvmcidr(d.Get("cidr").(string))
|
|
|
|
}
|
|
|
|
|
2016-12-12 10:06:42 +01:00
|
|
|
// Check if the network domain is changed
|
|
|
|
if d.HasChange("network_domain") {
|
|
|
|
p.SetNetworkdomain(d.Get("network_domain").(string))
|
|
|
|
}
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
// Check if the network offering is changed
|
|
|
|
if d.HasChange("network_offering") {
|
2015-10-05 14:05:21 +02:00
|
|
|
// Retrieve the network_offering ID
|
|
|
|
networkofferingid, e := retrieveID(cs, "network_offering", d.Get("network_offering").(string))
|
2014-12-10 22:20:52 +01:00
|
|
|
if e != nil {
|
|
|
|
return e.Error()
|
|
|
|
}
|
|
|
|
// Set the new network offering
|
|
|
|
p.SetNetworkofferingid(networkofferingid)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the network
|
|
|
|
_, err := cs.Network.UpdateNetwork(p)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error updating network %s: %s", name, err)
|
|
|
|
}
|
|
|
|
|
2016-05-18 12:22:32 +02:00
|
|
|
// Replace the ACL if the ID has changed
|
2016-06-25 19:42:01 +02:00
|
|
|
if d.HasChange("acl_id") {
|
|
|
|
p := cs.NetworkACL.NewReplaceNetworkACLListParams(d.Get("acl_id").(string))
|
2016-05-18 12:22:32 +02:00
|
|
|
p.SetNetworkid(d.Id())
|
|
|
|
|
|
|
|
_, err := cs.NetworkACL.ReplaceNetworkACLList(p)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error replacing ACL: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 05:45:51 +01:00
|
|
|
// Update tags if they have changed
|
2016-01-22 12:13:26 +01:00
|
|
|
if d.HasChange("tags") {
|
|
|
|
err = setTags(cs, d, "network")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating tags: %s", err)
|
|
|
|
}
|
2016-01-19 18:41:18 +01:00
|
|
|
}
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
return resourceCloudStackNetworkRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackNetworkDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
// Create a new parameter struct
|
|
|
|
p := cs.Network.NewDeleteNetworkParams(d.Id())
|
|
|
|
|
|
|
|
// Delete the network
|
|
|
|
_, err := cs.Network.DeleteNetwork(p)
|
|
|
|
if err != nil {
|
2015-10-05 14:05:21 +02:00
|
|
|
// This is a very poor way to be told the ID does no longer exist :(
|
2014-12-10 22:20:52 +01:00
|
|
|
if strings.Contains(err.Error(), fmt.Sprintf(
|
|
|
|
"Invalid parameter id value=%s due to incorrect long value format, "+
|
|
|
|
"or entity does not exist", d.Id())) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Error deleting network %s: %s", d.Get("name").(string), err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-08 09:58:46 +02:00
|
|
|
func parseCIDR(d *schema.ResourceData, specifyiprange bool) (map[string]string, error) {
|
2014-12-10 22:20:52 +01:00
|
|
|
m := make(map[string]string, 4)
|
|
|
|
|
2016-01-15 21:24:18 +01:00
|
|
|
cidr := d.Get("cidr").(string)
|
2014-12-10 22:20:52 +01:00
|
|
|
ip, ipnet, err := net.ParseCIDR(cidr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to parse cidr %s: %s", cidr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msk := ipnet.Mask
|
|
|
|
sub := ip.Mask(msk)
|
|
|
|
|
|
|
|
m["netmask"] = fmt.Sprintf("%d.%d.%d.%d", msk[0], msk[1], msk[2], msk[3])
|
2016-01-15 21:24:18 +01:00
|
|
|
|
|
|
|
if gateway, ok := d.GetOk("gateway"); ok {
|
|
|
|
m["gateway"] = gateway.(string)
|
2016-01-15 05:58:33 +01:00
|
|
|
} else {
|
|
|
|
m["gateway"] = fmt.Sprintf("%d.%d.%d.%d", sub[0], sub[1], sub[2], sub[3]+1)
|
|
|
|
}
|
|
|
|
|
2016-01-15 21:24:18 +01:00
|
|
|
if startip, ok := d.GetOk("startip"); ok {
|
|
|
|
m["startip"] = startip.(string)
|
2016-07-08 09:58:46 +02:00
|
|
|
} else if specifyiprange {
|
2016-01-15 05:58:33 +01:00
|
|
|
m["startip"] = fmt.Sprintf("%d.%d.%d.%d", sub[0], sub[1], sub[2], sub[3]+2)
|
|
|
|
}
|
|
|
|
|
2016-01-15 21:24:18 +01:00
|
|
|
if endip, ok := d.GetOk("endip"); ok {
|
|
|
|
m["endip"] = endip.(string)
|
2016-07-08 09:58:46 +02:00
|
|
|
} else if specifyiprange {
|
2016-01-15 05:58:33 +01:00
|
|
|
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))
|
|
|
|
}
|
2014-12-10 22:20:52 +01:00
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|