2016-04-04 15:48:14 +02:00
|
|
|
package cloudstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/xanzy/go-cloudstack/cloudstack"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceCloudStackStaticNAT() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceCloudStackStaticNATCreate,
|
|
|
|
Exists: resourceCloudStackStaticNATExists,
|
|
|
|
Read: resourceCloudStackStaticNATRead,
|
|
|
|
Delete: resourceCloudStackStaticNATDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-04-11 17:14:19 +02:00
|
|
|
"ip_address_id": &schema.Schema{
|
2016-04-04 15:48:14 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-11 17:14:19 +02:00
|
|
|
"virtual_machine_id": &schema.Schema{
|
2016-04-04 15:48:14 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"vm_guest_ip": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2016-07-27 17:30:18 +02:00
|
|
|
|
|
|
|
"project": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2016-04-04 15:48:14 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackStaticNATCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
2016-04-11 17:14:19 +02:00
|
|
|
ipaddressid := d.Get("ip_address_id").(string)
|
2016-04-04 15:48:14 +02:00
|
|
|
|
2016-11-18 08:20:31 +01:00
|
|
|
vm, _, err := cs.VirtualMachine.GetVirtualMachineByID(
|
|
|
|
d.Get("virtual_machine_id").(string),
|
|
|
|
cloudstack.WithProject(d.Get("project").(string)),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-04-04 15:48:14 +02:00
|
|
|
}
|
|
|
|
|
2016-11-18 08:20:31 +01:00
|
|
|
// Create a new parameter struct
|
|
|
|
p := cs.NAT.NewEnableStaticNatParams(ipaddressid, vm.Id)
|
|
|
|
|
2016-04-04 15:48:14 +02:00
|
|
|
if vmGuestIP, ok := d.GetOk("vm_guest_ip"); ok {
|
|
|
|
p.SetVmguestip(vmGuestIP.(string))
|
2016-11-18 08:20:31 +01:00
|
|
|
|
|
|
|
// Set the network ID based on the guest IP, needed when the public IP address
|
2016-11-29 20:48:38 +01:00
|
|
|
// is not associated with any network yet
|
|
|
|
NICS:
|
2016-11-18 08:20:31 +01:00
|
|
|
for _, nic := range vm.Nic {
|
|
|
|
if vmGuestIP.(string) == nic.Ipaddress {
|
|
|
|
p.SetNetworkid(nic.Networkid)
|
2016-11-29 20:48:38 +01:00
|
|
|
break NICS
|
|
|
|
}
|
|
|
|
for _, ip := range nic.Secondaryip {
|
|
|
|
if vmGuestIP.(string) == ip.Ipaddress {
|
|
|
|
p.SetNetworkid(nic.Networkid)
|
|
|
|
break NICS
|
|
|
|
}
|
2016-11-18 08:20:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If no guest IP is configured, use the primary NIC
|
|
|
|
p.SetNetworkid(vm.Nic[0].Networkid)
|
2016-04-04 15:48:14 +02:00
|
|
|
}
|
|
|
|
|
2016-11-18 08:20:31 +01:00
|
|
|
_, err = cs.NAT.EnableStaticNat(p)
|
2016-04-04 15:48:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error enabling static NAT: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(ipaddressid)
|
|
|
|
|
|
|
|
return resourceCloudStackStaticNATRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackStaticNATExists(d *schema.ResourceData, meta interface{}) (bool, error) {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
// Get the IP address details
|
2016-07-27 17:30:18 +02:00
|
|
|
ip, count, err := cs.Address.GetPublicIpAddressByID(
|
|
|
|
d.Id(),
|
|
|
|
cloudstack.WithProject(d.Get("project").(string)),
|
|
|
|
)
|
2016-04-04 15:48:14 +02:00
|
|
|
if err != nil {
|
|
|
|
if count == 0 {
|
|
|
|
log.Printf("[DEBUG] IP address with ID %s no longer exists", d.Id())
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ip.Isstaticnat, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackStaticNATRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
// Get the IP address details
|
2016-07-27 17:30:18 +02:00
|
|
|
ip, count, err := cs.Address.GetPublicIpAddressByID(
|
|
|
|
d.Id(),
|
|
|
|
cloudstack.WithProject(d.Get("project").(string)),
|
|
|
|
)
|
2016-04-04 15:48:14 +02:00
|
|
|
if err != nil {
|
|
|
|
if count == 0 {
|
|
|
|
log.Printf("[DEBUG] IP address with ID %s no longer exists", d.Id())
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ip.Isstaticnat {
|
|
|
|
log.Printf("[DEBUG] Static NAT is no longer enabled for IP address with ID %s", d.Id())
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-11 17:14:19 +02:00
|
|
|
d.Set("virtual_machine_id", ip.Virtualmachineid)
|
2016-04-04 15:48:14 +02:00
|
|
|
d.Set("vm_guest_ip", ip.Vmipaddress)
|
|
|
|
|
2016-07-27 17:30:18 +02:00
|
|
|
setValueOrID(d, "project", ip.Project, ip.Projectid)
|
|
|
|
|
2016-04-04 15:48:14 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackStaticNATDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
// Create a new parameter struct
|
|
|
|
p := cs.NAT.NewDisableStaticNatParams(d.Id())
|
|
|
|
|
|
|
|
// Disable static NAT
|
|
|
|
_, err := cs.NAT.DisableStaticNat(p)
|
|
|
|
if err != nil {
|
|
|
|
// This is a very poor way to be told the ID does no longer exist :(
|
|
|
|
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 disabling static NAT: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|