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 resourceCloudStackNIC() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceCloudStackNICCreate,
|
|
|
|
Read: resourceCloudStackNICRead,
|
|
|
|
Delete: resourceCloudStackNICDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-04-11 17:14:19 +02:00
|
|
|
"network_id": &schema.Schema{
|
2014-12-10 22:20:52 +01:00
|
|
|
Type: schema.TypeString,
|
2016-06-25 19:42:01 +02:00
|
|
|
Required: true,
|
2014-12-10 22:20:52 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-04 22:13:27 +02:00
|
|
|
"ip_address": &schema.Schema{
|
2014-12-10 22:20:52 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-11 17:14:19 +02:00
|
|
|
"virtual_machine_id": &schema.Schema{
|
2014-12-10 22:20:52 +01:00
|
|
|
Type: schema.TypeString,
|
2016-06-25 19:42:01 +02:00
|
|
|
Required: true,
|
2014-12-10 22:20:52 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackNICCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
// Create a new parameter struct
|
2016-06-25 19:42:01 +02:00
|
|
|
p := cs.VirtualMachine.NewAddNicToVirtualMachineParams(
|
|
|
|
d.Get("network_id").(string),
|
|
|
|
d.Get("virtual_machine_id").(string),
|
|
|
|
)
|
2014-12-10 22:20:52 +01:00
|
|
|
|
|
|
|
// If there is a ipaddres supplied, add it to the parameter struct
|
2016-06-25 19:42:01 +02:00
|
|
|
if ipaddress, ok := d.GetOk("ip_address"); ok {
|
2014-12-10 22:20:52 +01:00
|
|
|
p.SetIpaddress(ipaddress.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and attach the new NIC
|
2016-07-07 15:09:58 +02:00
|
|
|
r, err := Retry(10, retryableAddNicFunc(cs, p))
|
2014-12-10 22:20:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating the new NIC: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
found := false
|
2016-07-07 15:09:58 +02:00
|
|
|
for _, n := range r.(*cloudstack.AddNicToVirtualMachineResponse).Nic {
|
2016-06-25 19:42:01 +02:00
|
|
|
if n.Networkid == d.Get("network_id").(string) {
|
2014-12-10 22:20:52 +01:00
|
|
|
d.SetId(n.Id)
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2016-06-25 19:42:01 +02:00
|
|
|
return fmt.Errorf("Could not find NIC ID for network ID: %s", d.Get("network_id").(string))
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return resourceCloudStackNICRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackNICRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
// Get the virtual machine details
|
2016-06-25 19:42:01 +02:00
|
|
|
vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(d.Get("virtual_machine_id").(string))
|
2014-12-10 22:20:52 +01:00
|
|
|
if err != nil {
|
|
|
|
if count == 0 {
|
2016-06-25 19:42:01 +02:00
|
|
|
log.Printf("[DEBUG] Instance %s does no longer exist", d.Get("virtual_machine_id").(string))
|
2014-12-10 22:20:52 +01:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2016-04-04 22:13:27 +02:00
|
|
|
|
|
|
|
return err
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read NIC info
|
|
|
|
found := false
|
|
|
|
for _, n := range vm.Nic {
|
|
|
|
if n.Id == d.Id() {
|
2016-04-04 22:13:27 +02:00
|
|
|
d.Set("ip_address", n.Ipaddress)
|
2016-04-11 17:14:19 +02:00
|
|
|
d.Set("network_id", n.Networkid)
|
|
|
|
d.Set("virtual_machine_id", vm.Id)
|
2014-12-10 22:20:52 +01:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2016-04-11 17:14:19 +02:00
|
|
|
log.Printf("[DEBUG] NIC for network ID %s does no longer exist", d.Get("network_id").(string))
|
2014-12-10 22:20:52 +01:00
|
|
|
d.SetId("")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackNICDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
// Create a new parameter struct
|
2016-06-25 19:42:01 +02:00
|
|
|
p := cs.VirtualMachine.NewRemoveNicFromVirtualMachineParams(
|
|
|
|
d.Id(),
|
|
|
|
d.Get("virtual_machine_id").(string),
|
|
|
|
)
|
2014-12-10 22:20:52 +01:00
|
|
|
|
|
|
|
// Remove the NIC
|
|
|
|
_, err := cs.VirtualMachine.RemoveNicFromVirtualMachine(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 NIC: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-07 15:09:58 +02:00
|
|
|
|
|
|
|
func retryableAddNicFunc(cs *cloudstack.CloudStackClient, p *cloudstack.AddNicToVirtualMachineParams) func() (interface{}, error) {
|
|
|
|
return func() (interface{}, error) {
|
|
|
|
r, err := cs.VirtualMachine.AddNicToVirtualMachine(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
}
|