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