github.com/ottenhoff/terraform@v0.7.0-rc1.0.20160607213102-ac2d195cc560/builtin/providers/cloudstack/resource_cloudstack_nic.go (about)

     1  package cloudstack
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"log"
     7  	"strings"
     8  
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  	"github.com/xanzy/go-cloudstack/cloudstack"
    11  )
    12  
    13  func resourceCloudStackNIC() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceCloudStackNICCreate,
    16  		Read:   resourceCloudStackNICRead,
    17  		Delete: resourceCloudStackNICDelete,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"network_id": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Optional: true,
    23  				Computed: true,
    24  				ForceNew: true,
    25  			},
    26  
    27  			"network": &schema.Schema{
    28  				Type:       schema.TypeString,
    29  				Optional:   true,
    30  				ForceNew:   true,
    31  				Deprecated: "Please use the `network_id` field instead",
    32  			},
    33  
    34  			"ip_address": &schema.Schema{
    35  				Type:     schema.TypeString,
    36  				Optional: true,
    37  				Computed: true,
    38  				ForceNew: true,
    39  			},
    40  
    41  			"ipaddress": &schema.Schema{
    42  				Type:       schema.TypeString,
    43  				Optional:   true,
    44  				ForceNew:   true,
    45  				Deprecated: "Please use the `ip_address` field instead",
    46  			},
    47  
    48  			"virtual_machine_id": &schema.Schema{
    49  				Type:     schema.TypeString,
    50  				Optional: true,
    51  				Computed: true,
    52  				ForceNew: true,
    53  			},
    54  
    55  			"virtual_machine": &schema.Schema{
    56  				Type:       schema.TypeString,
    57  				Optional:   true,
    58  				ForceNew:   true,
    59  				Deprecated: "Please use the `virtual_machine_id` field instead",
    60  			},
    61  		},
    62  	}
    63  }
    64  
    65  func resourceCloudStackNICCreate(d *schema.ResourceData, meta interface{}) error {
    66  	cs := meta.(*cloudstack.CloudStackClient)
    67  
    68  	network, ok := d.GetOk("network_id")
    69  	if !ok {
    70  		network, ok = d.GetOk("network")
    71  	}
    72  	if !ok {
    73  		return errors.New("Either `network_id` or [deprecated] `network` must be provided.")
    74  	}
    75  
    76  	// Retrieve the network ID
    77  	networkid, e := retrieveID(cs, "network", network.(string))
    78  	if e != nil {
    79  		return e.Error()
    80  	}
    81  
    82  	virtualmachine, ok := d.GetOk("virtual_machine_id")
    83  	if !ok {
    84  		virtualmachine, ok = d.GetOk("virtual_machine")
    85  	}
    86  	if !ok {
    87  		return errors.New(
    88  			"Either `virtual_machine_id` or [deprecated] `virtual_machine` must be provided.")
    89  	}
    90  
    91  	// Retrieve the virtual_machine ID
    92  	virtualmachineid, e := retrieveID(cs, "virtual_machine", virtualmachine.(string))
    93  	if e != nil {
    94  		return e.Error()
    95  	}
    96  
    97  	// Create a new parameter struct
    98  	p := cs.VirtualMachine.NewAddNicToVirtualMachineParams(networkid, virtualmachineid)
    99  
   100  	// If there is a ipaddres supplied, add it to the parameter struct
   101  	ipaddress, ok := d.GetOk("ip_address")
   102  	if !ok {
   103  		ipaddress, ok = d.GetOk("ipaddress")
   104  	}
   105  	if ok {
   106  		p.SetIpaddress(ipaddress.(string))
   107  	}
   108  
   109  	// Create and attach the new NIC
   110  	r, err := cs.VirtualMachine.AddNicToVirtualMachine(p)
   111  	if err != nil {
   112  		return fmt.Errorf("Error creating the new NIC: %s", err)
   113  	}
   114  
   115  	found := false
   116  	for _, n := range r.Nic {
   117  		if n.Networkid == networkid {
   118  			d.SetId(n.Id)
   119  			found = true
   120  			break
   121  		}
   122  	}
   123  
   124  	if !found {
   125  		return fmt.Errorf("Could not find NIC ID for network ID: %s", networkid)
   126  	}
   127  
   128  	return resourceCloudStackNICRead(d, meta)
   129  }
   130  
   131  func resourceCloudStackNICRead(d *schema.ResourceData, meta interface{}) error {
   132  	cs := meta.(*cloudstack.CloudStackClient)
   133  
   134  	virtualmachine, ok := d.GetOk("virtual_machine_id")
   135  	if !ok {
   136  		virtualmachine, ok = d.GetOk("virtual_machine")
   137  	}
   138  	if !ok {
   139  		return errors.New(
   140  			"Either `virtual_machine_id` or [deprecated] `virtual_machine` must be provided.")
   141  	}
   142  
   143  	// Retrieve the virtual_machine ID
   144  	virtualmachineid, e := retrieveID(cs, "virtual_machine", virtualmachine.(string))
   145  	if e != nil {
   146  		return e.Error()
   147  	}
   148  
   149  	// Get the virtual machine details
   150  	vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(virtualmachineid)
   151  	if err != nil {
   152  		if count == 0 {
   153  			log.Printf("[DEBUG] Instance %s does no longer exist", d.Get("virtual_machine").(string))
   154  			d.SetId("")
   155  			return nil
   156  		}
   157  
   158  		return err
   159  	}
   160  
   161  	// Read NIC info
   162  	found := false
   163  	for _, n := range vm.Nic {
   164  		if n.Id == d.Id() {
   165  			d.Set("ip_address", n.Ipaddress)
   166  			d.Set("network_id", n.Networkid)
   167  			d.Set("virtual_machine_id", vm.Id)
   168  			found = true
   169  			break
   170  		}
   171  	}
   172  
   173  	if !found {
   174  		log.Printf("[DEBUG] NIC for network ID %s does no longer exist", d.Get("network_id").(string))
   175  		d.SetId("")
   176  	}
   177  
   178  	return nil
   179  }
   180  
   181  func resourceCloudStackNICDelete(d *schema.ResourceData, meta interface{}) error {
   182  	cs := meta.(*cloudstack.CloudStackClient)
   183  
   184  	virtualmachine, ok := d.GetOk("virtual_machine_id")
   185  	if !ok {
   186  		virtualmachine, ok = d.GetOk("virtual_machine")
   187  	}
   188  	if !ok {
   189  		return errors.New(
   190  			"Either `virtual_machine_id` or [deprecated] `virtual_machine` must be provided.")
   191  	}
   192  
   193  	// Retrieve the virtual_machine ID
   194  	virtualmachineid, e := retrieveID(cs, "virtual_machine", virtualmachine.(string))
   195  	if e != nil {
   196  		return e.Error()
   197  	}
   198  
   199  	// Create a new parameter struct
   200  	p := cs.VirtualMachine.NewRemoveNicFromVirtualMachineParams(d.Id(), virtualmachineid)
   201  
   202  	// Remove the NIC
   203  	_, err := cs.VirtualMachine.RemoveNicFromVirtualMachine(p)
   204  	if err != nil {
   205  		// This is a very poor way to be told the ID does no longer exist :(
   206  		if strings.Contains(err.Error(), fmt.Sprintf(
   207  			"Invalid parameter id value=%s due to incorrect long value format, "+
   208  				"or entity does not exist", d.Id())) {
   209  			return nil
   210  		}
   211  
   212  		return fmt.Errorf("Error deleting NIC: %s", err)
   213  	}
   214  
   215  	return nil
   216  }