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 resourceCloudStackIPAddress() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceCloudStackIPAddressCreate,
|
|
|
|
Read: resourceCloudStackIPAddressRead,
|
|
|
|
Delete: resourceCloudStackIPAddressDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"network": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"vpc": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-08-20 14:32:57 +02:00
|
|
|
"project": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2015-08-21 16:59:35 +02:00
|
|
|
|
2016-04-04 22:13:27 +02:00
|
|
|
"ip_address": &schema.Schema{
|
2015-08-21 16:59:35 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2014-12-10 22:20:52 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackIPAddressCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
if err := verifyIPAddressParams(d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new parameter struct
|
|
|
|
p := cs.Address.NewAssociateIpAddressParams()
|
|
|
|
|
|
|
|
if network, ok := d.GetOk("network"); ok {
|
2015-10-05 14:05:21 +02:00
|
|
|
// Retrieve the network ID
|
|
|
|
networkid, e := retrieveID(cs, "network", network.(string))
|
2014-12-10 22:20:52 +01:00
|
|
|
if e != nil {
|
|
|
|
return e.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the networkid
|
|
|
|
p.SetNetworkid(networkid)
|
|
|
|
}
|
|
|
|
|
|
|
|
if vpc, ok := d.GetOk("vpc"); ok {
|
2015-10-05 14:05:21 +02:00
|
|
|
// Retrieve the vpc ID
|
|
|
|
vpcid, e := retrieveID(cs, "vpc", vpc.(string))
|
2014-12-10 22:20:52 +01:00
|
|
|
if e != nil {
|
|
|
|
return e.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the vpcid
|
|
|
|
p.SetVpcid(vpcid)
|
|
|
|
}
|
|
|
|
|
2015-09-11 20:56:20 +02:00
|
|
|
// If there is a project supplied, we retrieve and set the project id
|
2015-08-21 16:59:35 +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-21 16:59:35 +02:00
|
|
|
if e != nil {
|
|
|
|
return e.Error()
|
|
|
|
}
|
|
|
|
// Set the default project ID
|
|
|
|
p.SetProjectid(projectid)
|
|
|
|
}
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
// Associate a new IP address
|
|
|
|
r, err := cs.Address.AssociateIpAddress(p)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error associating a new IP address: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(r.Id)
|
|
|
|
|
|
|
|
return resourceCloudStackIPAddressRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackIPAddressRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
2016-04-04 15:48:14 +02:00
|
|
|
// Get the IP address details
|
2016-04-04 22:13:27 +02:00
|
|
|
ip, count, err := cs.Address.GetPublicIpAddressByID(d.Id())
|
2014-12-10 22:20:52 +01:00
|
|
|
if err != nil {
|
|
|
|
if count == 0 {
|
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] IP address with ID %s is no longer associated", d.Id())
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updated the IP address
|
2016-04-04 22:13:27 +02:00
|
|
|
d.Set("ip_address", ip.Ipaddress)
|
2014-12-10 22:20:52 +01:00
|
|
|
|
|
|
|
if _, ok := d.GetOk("network"); ok {
|
|
|
|
// Get the network details
|
2016-04-04 22:13:27 +02:00
|
|
|
n, _, err := cs.Network.GetNetworkByID(ip.Associatednetworkid)
|
2014-12-10 22:20:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-04 22:13:27 +02:00
|
|
|
setValueOrID(d, "network", n.Name, ip.Associatednetworkid)
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := d.GetOk("vpc"); ok {
|
|
|
|
// Get the VPC details
|
2016-04-04 22:13:27 +02:00
|
|
|
v, _, err := cs.VPC.GetVPCByID(ip.Vpcid)
|
2014-12-10 22:20:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-04 22:13:27 +02:00
|
|
|
setValueOrID(d, "vpc", v.Name, ip.Vpcid)
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
|
2016-04-04 22:13:27 +02:00
|
|
|
setValueOrID(d, "project", ip.Project, ip.Projectid)
|
2015-08-20 14:32:57 +02:00
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackIPAddressDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
// Create a new parameter struct
|
|
|
|
p := cs.Address.NewDisassociateIpAddressParams(d.Id())
|
|
|
|
|
|
|
|
// Disassociate the IP address
|
|
|
|
if _, err := cs.Address.DisassociateIpAddress(p); 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
|
|
|
|
}
|
|
|
|
|
2015-06-09 12:38:05 +02:00
|
|
|
return fmt.Errorf("Error disassociating IP address %s: %s", d.Get("name").(string), err)
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyIPAddressParams(d *schema.ResourceData) error {
|
|
|
|
_, network := d.GetOk("network")
|
|
|
|
_, vpc := d.GetOk("vpc")
|
|
|
|
|
2015-10-08 14:48:04 +02:00
|
|
|
if network && vpc || !network && !vpc {
|
2015-01-15 10:04:09 +01:00
|
|
|
return fmt.Errorf(
|
|
|
|
"You must supply a value for either (so not both) the 'network' or 'vpc' parameter")
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|