github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/cloudstack/resource_cloudstack_secondary_ipaddress.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 resourceCloudStackSecondaryIPAddress() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceCloudStackSecondaryIPAddressCreate,
    15  		Read:   resourceCloudStackSecondaryIPAddressRead,
    16  		Delete: resourceCloudStackSecondaryIPAddressDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"ipaddress": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Optional: true,
    22  				Computed: true,
    23  				ForceNew: true,
    24  			},
    25  
    26  			"nicid": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Optional: true,
    29  				Computed: true,
    30  				ForceNew: true,
    31  			},
    32  
    33  			"virtual_machine": &schema.Schema{
    34  				Type:     schema.TypeString,
    35  				Required: true,
    36  				ForceNew: true,
    37  			},
    38  		},
    39  	}
    40  }
    41  
    42  func resourceCloudStackSecondaryIPAddressCreate(d *schema.ResourceData, meta interface{}) error {
    43  	cs := meta.(*cloudstack.CloudStackClient)
    44  
    45  	nicid := d.Get("nicid").(string)
    46  	if nicid == "" {
    47  		// Retrieve the virtual_machine ID
    48  		virtualmachineid, e := retrieveID(cs, "virtual_machine", d.Get("virtual_machine").(string))
    49  		if e != nil {
    50  			return e.Error()
    51  		}
    52  
    53  		// Get the virtual machine details
    54  		vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(virtualmachineid)
    55  		if err != nil {
    56  			if count == 0 {
    57  				log.Printf("[DEBUG] Instance %s does no longer exist", d.Get("virtual_machine").(string))
    58  				d.SetId("")
    59  				return nil
    60  			}
    61  			return err
    62  		}
    63  
    64  		nicid = vm.Nic[0].Id
    65  	}
    66  
    67  	// Create a new parameter struct
    68  	p := cs.Nic.NewAddIpToNicParams(nicid)
    69  
    70  	if addr := d.Get("ipaddress").(string); addr != "" {
    71  		p.SetIpaddress(addr)
    72  	}
    73  
    74  	ip, err := cs.Nic.AddIpToNic(p)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	d.SetId(ip.Id)
    80  
    81  	return nil
    82  }
    83  
    84  func resourceCloudStackSecondaryIPAddressRead(d *schema.ResourceData, meta interface{}) error {
    85  	cs := meta.(*cloudstack.CloudStackClient)
    86  
    87  	// Retrieve the virtual_machine ID
    88  	virtualmachineid, e := retrieveID(cs, "virtual_machine", d.Get("virtual_machine").(string))
    89  	if e != nil {
    90  		return e.Error()
    91  	}
    92  
    93  	// Get the virtual machine details
    94  	vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(virtualmachineid)
    95  	if err != nil {
    96  		if count == 0 {
    97  			log.Printf("[DEBUG] Instance %s does no longer exist", d.Get("virtual_machine").(string))
    98  			d.SetId("")
    99  			return nil
   100  		}
   101  		return err
   102  	}
   103  
   104  	nicid := d.Get("nicid").(string)
   105  	if nicid == "" {
   106  		nicid = vm.Nic[0].Id
   107  	}
   108  
   109  	p := cs.Nic.NewListNicsParams(virtualmachineid)
   110  	p.SetNicid(nicid)
   111  
   112  	l, err := cs.Nic.ListNics(p)
   113  	if err != nil {
   114  		return err
   115  	}
   116  
   117  	if l.Count == 0 {
   118  		log.Printf("[DEBUG] NIC %s does no longer exist", d.Get("nicid").(string))
   119  		d.SetId("")
   120  		return nil
   121  	}
   122  
   123  	if l.Count > 1 {
   124  		return fmt.Errorf("Found more then one possible result: %v", l.Nics)
   125  	}
   126  
   127  	for _, ip := range l.Nics[0].Secondaryip {
   128  		if ip.Id == d.Id() {
   129  			d.Set("ipaddress", ip.Ipaddress)
   130  			d.Set("nicid", l.Nics[0].Id)
   131  			return nil
   132  		}
   133  	}
   134  
   135  	log.Printf("[DEBUG] IP %s no longer exist", d.Get("ipaddress").(string))
   136  	d.SetId("")
   137  
   138  	return nil
   139  }
   140  
   141  func resourceCloudStackSecondaryIPAddressDelete(d *schema.ResourceData, meta interface{}) error {
   142  	cs := meta.(*cloudstack.CloudStackClient)
   143  
   144  	// Create a new parameter struct
   145  	p := cs.Nic.NewRemoveIpFromNicParams(d.Id())
   146  
   147  	log.Printf("[INFO] Removing secondary IP address: %s", d.Get("ipaddress").(string))
   148  	if _, err := cs.Nic.RemoveIpFromNic(p); err != nil {
   149  		// This is a very poor way to be told the ID does no longer exist :(
   150  		if strings.Contains(err.Error(), fmt.Sprintf(
   151  			"Invalid parameter id value=%s due to incorrect long value format, "+
   152  				"or entity does not exist", d.Id())) {
   153  			return nil
   154  		}
   155  
   156  		return fmt.Errorf("Error removing secondary IP address: %s", err)
   157  	}
   158  
   159  	return nil
   160  }