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