github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/cloudstack/resource_cloudstack_nic.go (about)

     1  package cloudstack
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"github.com/xanzy/go-cloudstack/cloudstack"
    10  )
    11  
    12  func resourceCloudStackNIC() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceCloudStackNICCreate,
    15  		Read:   resourceCloudStackNICRead,
    16  		Delete: resourceCloudStackNICDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"network": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  
    25  			"ipaddress": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Optional: true,
    28  				Computed: true,
    29  				ForceNew: true,
    30  			},
    31  
    32  			"virtual_machine": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Required: true,
    35  				ForceNew: true,
    36  			},
    37  		},
    38  	}
    39  }
    40  
    41  func resourceCloudStackNICCreate(d *schema.ResourceData, meta interface{}) error {
    42  	cs := meta.(*cloudstack.CloudStackClient)
    43  
    44  	// Retrieve the network ID
    45  	networkid, e := retrieveID(cs, "network", d.Get("network").(string))
    46  	if e != nil {
    47  		return e.Error()
    48  	}
    49  
    50  	// Retrieve the virtual_machine ID
    51  	virtualmachineid, e := retrieveID(cs, "virtual_machine", d.Get("virtual_machine").(string))
    52  	if e != nil {
    53  		return e.Error()
    54  	}
    55  
    56  	// Create a new parameter struct
    57  	p := cs.VirtualMachine.NewAddNicToVirtualMachineParams(networkid, virtualmachineid)
    58  
    59  	// If there is a ipaddres supplied, add it to the parameter struct
    60  	if ipaddress, ok := d.GetOk("ipaddress"); ok {
    61  		p.SetIpaddress(ipaddress.(string))
    62  	}
    63  
    64  	// Create and attach the new NIC
    65  	r, err := cs.VirtualMachine.AddNicToVirtualMachine(p)
    66  	if err != nil {
    67  		return fmt.Errorf("Error creating the new NIC: %s", err)
    68  	}
    69  
    70  	found := false
    71  	for _, n := range r.Nic {
    72  		if n.Networkid == networkid {
    73  			d.SetId(n.Id)
    74  			found = true
    75  			break
    76  		}
    77  	}
    78  
    79  	if !found {
    80  		return fmt.Errorf("Could not find NIC ID for network: %s", d.Get("network").(string))
    81  	}
    82  
    83  	return resourceCloudStackNICRead(d, meta)
    84  }
    85  
    86  func resourceCloudStackNICRead(d *schema.ResourceData, meta interface{}) error {
    87  	cs := meta.(*cloudstack.CloudStackClient)
    88  
    89  	// Get the virtual machine details
    90  	vm, count, err := cs.VirtualMachine.GetVirtualMachineByName(d.Get("virtual_machine").(string))
    91  	if err != nil {
    92  		if count == 0 {
    93  			log.Printf("[DEBUG] Instance %s does no longer exist", d.Get("virtual_machine").(string))
    94  			d.SetId("")
    95  			return nil
    96  		} else {
    97  			return err
    98  		}
    99  	}
   100  
   101  	// Read NIC info
   102  	found := false
   103  	for _, n := range vm.Nic {
   104  		if n.Id == d.Id() {
   105  			d.Set("ipaddress", n.Ipaddress)
   106  			setValueOrID(d, "network", n.Networkname, n.Networkid)
   107  			setValueOrID(d, "virtual_machine", vm.Name, vm.Id)
   108  			found = true
   109  			break
   110  		}
   111  	}
   112  
   113  	if !found {
   114  		log.Printf("[DEBUG] NIC for network %s does no longer exist", d.Get("network").(string))
   115  		d.SetId("")
   116  	}
   117  
   118  	return nil
   119  }
   120  
   121  func resourceCloudStackNICDelete(d *schema.ResourceData, meta interface{}) error {
   122  	cs := meta.(*cloudstack.CloudStackClient)
   123  
   124  	// Retrieve the virtual_machine ID
   125  	virtualmachineid, e := retrieveID(cs, "virtual_machine", d.Get("virtual_machine").(string))
   126  	if e != nil {
   127  		return e.Error()
   128  	}
   129  
   130  	// Create a new parameter struct
   131  	p := cs.VirtualMachine.NewRemoveNicFromVirtualMachineParams(d.Id(), virtualmachineid)
   132  
   133  	// Remove the NIC
   134  	_, err := cs.VirtualMachine.RemoveNicFromVirtualMachine(p)
   135  	if err != nil {
   136  		// This is a very poor way to be told the ID does no longer exist :(
   137  		if strings.Contains(err.Error(), fmt.Sprintf(
   138  			"Invalid parameter id value=%s due to incorrect long value format, "+
   139  				"or entity does not exist", d.Id())) {
   140  			return nil
   141  		}
   142  
   143  		return fmt.Errorf("Error deleting NIC: %s", err)
   144  	}
   145  
   146  	return nil
   147  }