github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/cloudstack/resource_cloudstack_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 resourceCloudStackIPAddress() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceCloudStackIPAddressCreate,
    15  		Read:   resourceCloudStackIPAddressRead,
    16  		Delete: resourceCloudStackIPAddressDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"network": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Optional: true,
    22  				ForceNew: true,
    23  			},
    24  
    25  			"vpc": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Optional: true,
    28  				ForceNew: true,
    29  			},
    30  
    31  			"ipaddress": &schema.Schema{
    32  				Type:     schema.TypeString,
    33  				Computed: true,
    34  			},
    35  		},
    36  	}
    37  }
    38  
    39  func resourceCloudStackIPAddressCreate(d *schema.ResourceData, meta interface{}) error {
    40  	cs := meta.(*cloudstack.CloudStackClient)
    41  
    42  	if err := verifyIPAddressParams(d); err != nil {
    43  		return err
    44  	}
    45  
    46  	// Create a new parameter struct
    47  	p := cs.Address.NewAssociateIpAddressParams()
    48  
    49  	if network, ok := d.GetOk("network"); ok {
    50  		// Retrieve the network UUID
    51  		networkid, e := retrieveUUID(cs, "network", network.(string))
    52  		if e != nil {
    53  			return e.Error()
    54  		}
    55  
    56  		// Set the networkid
    57  		p.SetNetworkid(networkid)
    58  	}
    59  
    60  	if vpc, ok := d.GetOk("vpc"); ok {
    61  		// Retrieve the vpc UUID
    62  		vpcid, e := retrieveUUID(cs, "vpc", vpc.(string))
    63  		if e != nil {
    64  			return e.Error()
    65  		}
    66  
    67  		// Set the vpcid
    68  		p.SetVpcid(vpcid)
    69  	}
    70  
    71  	// Associate a new IP address
    72  	r, err := cs.Address.AssociateIpAddress(p)
    73  	if err != nil {
    74  		return fmt.Errorf("Error associating a new IP address: %s", err)
    75  	}
    76  
    77  	d.SetId(r.Id)
    78  
    79  	return resourceCloudStackIPAddressRead(d, meta)
    80  }
    81  
    82  func resourceCloudStackIPAddressRead(d *schema.ResourceData, meta interface{}) error {
    83  	cs := meta.(*cloudstack.CloudStackClient)
    84  
    85  	// Get the network ACL list details
    86  	f, count, err := cs.Address.GetPublicIpAddressByID(d.Id())
    87  	if err != nil {
    88  		if count == 0 {
    89  			log.Printf(
    90  				"[DEBUG] IP address with ID %s is no longer associated", d.Id())
    91  			d.SetId("")
    92  			return nil
    93  		}
    94  
    95  		return err
    96  	}
    97  
    98  	// Updated the IP address
    99  	d.Set("ipaddress", f.Ipaddress)
   100  
   101  	if _, ok := d.GetOk("network"); ok {
   102  		// Get the network details
   103  		n, _, err := cs.Network.GetNetworkByID(f.Associatednetworkid)
   104  		if err != nil {
   105  			return err
   106  		}
   107  
   108  		d.Set("network", n.Name)
   109  	}
   110  
   111  	if _, ok := d.GetOk("vpc"); ok {
   112  		// Get the VPC details
   113  		v, _, err := cs.VPC.GetVPCByID(f.Vpcid)
   114  		if err != nil {
   115  			return err
   116  		}
   117  
   118  		d.Set("vpc", v.Name)
   119  	}
   120  
   121  	return nil
   122  }
   123  
   124  func resourceCloudStackIPAddressDelete(d *schema.ResourceData, meta interface{}) error {
   125  	cs := meta.(*cloudstack.CloudStackClient)
   126  
   127  	// Create a new parameter struct
   128  	p := cs.Address.NewDisassociateIpAddressParams(d.Id())
   129  
   130  	// Disassociate the IP address
   131  	if _, err := cs.Address.DisassociateIpAddress(p); err != nil {
   132  		// This is a very poor way to be told the UUID does no longer exist :(
   133  		if strings.Contains(err.Error(), fmt.Sprintf(
   134  			"Invalid parameter id value=%s due to incorrect long value format, "+
   135  				"or entity does not exist", d.Id())) {
   136  			return nil
   137  		}
   138  
   139  		return fmt.Errorf("Error deleting network ACL list %s: %s", d.Get("name").(string), err)
   140  	}
   141  
   142  	return nil
   143  }
   144  
   145  func verifyIPAddressParams(d *schema.ResourceData) error {
   146  	_, network := d.GetOk("network")
   147  	_, vpc := d.GetOk("vpc")
   148  
   149  	if (network && vpc) || (!network && !vpc) {
   150  		return fmt.Errorf("You must supply a value for either (so not both) the 'network' or 'vpc' argument")
   151  	}
   152  
   153  	return nil
   154  }