2014-12-10 22:20:52 +01:00
|
|
|
package cloudstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/xanzy/go-cloudstack/cloudstack"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceCloudStackVPC() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceCloudStackVPCCreate,
|
|
|
|
Read: resourceCloudStackVPCRead,
|
|
|
|
Update: resourceCloudStackVPCUpdate,
|
|
|
|
Delete: resourceCloudStackVPCDelete,
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
|
|
|
|
"vpc_offering": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-09-28 04:49:58 +02:00
|
|
|
"network_domain": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
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,
|
2014-12-10 22:20:52 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
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,
|
|
|
|
},
|
2014-12-10 22:20:52 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackVPCCreate(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 vpc_offering ID
|
|
|
|
vpcofferingid, e := retrieveID(cs, "vpc_offering", d.Get("vpc_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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the display text
|
|
|
|
displaytext, ok := d.GetOk("display_text")
|
|
|
|
if !ok {
|
2015-01-15 21:46:06 +01:00
|
|
|
displaytext = name
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new parameter struct
|
2015-09-28 04:49:58 +02:00
|
|
|
p := cs.VPC.NewCreateVPCParams(
|
|
|
|
d.Get("cidr").(string),
|
|
|
|
displaytext.(string),
|
|
|
|
name,
|
|
|
|
vpcofferingid,
|
|
|
|
zoneid,
|
|
|
|
)
|
|
|
|
|
|
|
|
// If there is a network domain supplied, make sure to add it to the request
|
|
|
|
if networkDomain, ok := d.GetOk("network_domain"); ok {
|
|
|
|
// Set the network domain
|
|
|
|
p.SetNetworkdomain(networkDomain.(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
|
2015-08-19 23:56:57 +02:00
|
|
|
if project, ok := d.GetOk("project"); ok {
|
2015-10-05 14:05:21 +02:00
|
|
|
// Retrieve the project ID
|
|
|
|
projectid, e := retrieveID(cs, "project", project.(string))
|
2015-08-19 23:56:57 +02:00
|
|
|
if e != nil {
|
|
|
|
return e.Error()
|
|
|
|
}
|
|
|
|
// Set the default project ID
|
|
|
|
p.SetProjectid(projectid)
|
|
|
|
}
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
// Create the new VPC
|
|
|
|
r, err := cs.VPC.CreateVPC(p)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating VPC %s: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(r.Id)
|
|
|
|
|
|
|
|
return resourceCloudStackVPCRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackVPCRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
// Get the VPC details
|
|
|
|
v, count, err := cs.VPC.GetVPCByID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
if count == 0 {
|
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] VPC %s does no longer exist", d.Get("name").(string))
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("name", v.Name)
|
2015-01-15 21:46:06 +01:00
|
|
|
d.Set("display_text", v.Displaytext)
|
2014-12-10 22:20:52 +01:00
|
|
|
d.Set("cidr", v.Cidr)
|
2015-09-28 04:49:58 +02:00
|
|
|
d.Set("network_domain", v.Networkdomain)
|
2015-04-29 11:21:37 +02:00
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
// Get the VPC offering details
|
|
|
|
o, _, err := cs.VPC.GetVPCOfferingByID(v.Vpcofferingid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-05 14:05:21 +02:00
|
|
|
setValueOrID(d, "vpc_offering", o.Name, v.Vpcofferingid)
|
|
|
|
setValueOrID(d, "project", v.Project, v.Projectid)
|
|
|
|
setValueOrID(d, "zone", v.Zonename, v.Zoneid)
|
2014-12-10 22:20:52 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackVPCUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
// Check if the name or display text is changed
|
|
|
|
if d.HasChange("name") || d.HasChange("display_text") {
|
|
|
|
// Create a new parameter struct
|
2015-01-15 10:04:09 +01:00
|
|
|
p := cs.VPC.NewUpdateVPCParams(d.Id())
|
2014-12-10 22:20:52 +01:00
|
|
|
|
|
|
|
// Set the display text
|
|
|
|
displaytext, ok := d.GetOk("display_text")
|
|
|
|
if !ok {
|
|
|
|
displaytext = d.Get("name")
|
|
|
|
}
|
|
|
|
// Set the (new) display text
|
|
|
|
p.SetDisplaytext(displaytext.(string))
|
|
|
|
|
|
|
|
// Update the VPC
|
|
|
|
_, err := cs.VPC.UpdateVPC(p)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error updating VPC %s: %s", d.Get("name").(string), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceCloudStackVPCRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackVPCDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
// Create a new parameter struct
|
|
|
|
p := cs.VPC.NewDeleteVPCParams(d.Id())
|
|
|
|
|
|
|
|
// Delete the VPC
|
|
|
|
_, err := cs.VPC.DeleteVPC(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 VPC %s: %s", d.Get("name").(string), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|