2015-07-16 17:40:04 +02:00
|
|
|
package cloudstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/xanzy/go-cloudstack/cloudstack"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceCloudStackSecondaryIPAddress() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceCloudStackSecondaryIPAddressCreate,
|
|
|
|
Read: resourceCloudStackSecondaryIPAddressRead,
|
|
|
|
Delete: resourceCloudStackSecondaryIPAddressDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-04-04 22:13:27 +02:00
|
|
|
"ip_address": &schema.Schema{
|
2015-07-16 17:40:04 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-04 22:13:27 +02:00
|
|
|
"ipaddress": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Deprecated: "Please use the `ip_address` field instead",
|
|
|
|
},
|
|
|
|
|
2015-07-16 17:40:04 +02:00
|
|
|
"nicid": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"virtual_machine": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackSecondaryIPAddressCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
nicid := d.Get("nicid").(string)
|
|
|
|
if nicid == "" {
|
2015-10-05 14:05:21 +02:00
|
|
|
// Retrieve the virtual_machine ID
|
|
|
|
virtualmachineid, e := retrieveID(cs, "virtual_machine", d.Get("virtual_machine").(string))
|
2015-07-16 17:40:04 +02:00
|
|
|
if e != nil {
|
|
|
|
return e.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the virtual machine details
|
|
|
|
vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(virtualmachineid)
|
|
|
|
if err != nil {
|
|
|
|
if count == 0 {
|
|
|
|
log.Printf("[DEBUG] Instance %s does no longer exist", d.Get("virtual_machine").(string))
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nicid = vm.Nic[0].Id
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new parameter struct
|
|
|
|
p := cs.Nic.NewAddIpToNicParams(nicid)
|
|
|
|
|
2016-04-04 22:13:27 +02:00
|
|
|
// If there is a ipaddres supplied, add it to the parameter struct
|
|
|
|
ipaddress, ok := d.GetOk("ip_address")
|
|
|
|
if !ok {
|
|
|
|
ipaddress, ok = d.GetOk("ipaddress")
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
p.SetIpaddress(ipaddress.(string))
|
2015-07-16 17:40:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ip, err := cs.Nic.AddIpToNic(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(ip.Id)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackSecondaryIPAddressRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
2015-10-05 14:05:21 +02:00
|
|
|
// Retrieve the virtual_machine ID
|
|
|
|
virtualmachineid, e := retrieveID(cs, "virtual_machine", d.Get("virtual_machine").(string))
|
2015-07-16 17:40:04 +02:00
|
|
|
if e != nil {
|
|
|
|
return e.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the virtual machine details
|
|
|
|
vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(virtualmachineid)
|
|
|
|
if err != nil {
|
|
|
|
if count == 0 {
|
|
|
|
log.Printf("[DEBUG] Instance %s does no longer exist", d.Get("virtual_machine").(string))
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nicid := d.Get("nicid").(string)
|
|
|
|
if nicid == "" {
|
|
|
|
nicid = vm.Nic[0].Id
|
|
|
|
}
|
|
|
|
|
|
|
|
p := cs.Nic.NewListNicsParams(virtualmachineid)
|
|
|
|
p.SetNicid(nicid)
|
|
|
|
|
|
|
|
l, err := cs.Nic.ListNics(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.Count == 0 {
|
|
|
|
log.Printf("[DEBUG] NIC %s does no longer exist", d.Get("nicid").(string))
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.Count > 1 {
|
|
|
|
return fmt.Errorf("Found more then one possible result: %v", l.Nics)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ip := range l.Nics[0].Secondaryip {
|
|
|
|
if ip.Id == d.Id() {
|
2016-04-04 22:13:27 +02:00
|
|
|
d.Set("ip_address", ip.Ipaddress)
|
2015-07-16 17:40:04 +02:00
|
|
|
d.Set("nicid", l.Nics[0].Id)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 22:13:27 +02:00
|
|
|
log.Printf("[DEBUG] IP %s no longer exist", d.Get("ip_address").(string))
|
2015-07-16 17:40:04 +02:00
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackSecondaryIPAddressDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
// Create a new parameter struct
|
|
|
|
p := cs.Nic.NewRemoveIpFromNicParams(d.Id())
|
|
|
|
|
2016-04-04 22:13:27 +02:00
|
|
|
log.Printf("[INFO] Removing secondary IP address: %s", d.Get("ip_address").(string))
|
2015-07-16 17:40:04 +02:00
|
|
|
if _, err := cs.Nic.RemoveIpFromNic(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 :(
|
2015-07-16 17:40:04 +02: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 removing secondary IP address: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|