github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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_id": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Optional: true,
    22  				ForceNew: true,
    23  			},
    24  
    25  			"vpc_id": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Optional: true,
    28  				ForceNew: true,
    29  			},
    30  
    31  			"zone_id": &schema.Schema{
    32  				Type:     schema.TypeString,
    33  				Optional: true,
    34  				ForceNew: true,
    35  			},
    36  
    37  			"project": &schema.Schema{
    38  				Type:     schema.TypeString,
    39  				Optional: true,
    40  				Computed: true,
    41  				ForceNew: true,
    42  			},
    43  
    44  			"ip_address": &schema.Schema{
    45  				Type:     schema.TypeString,
    46  				Computed: true,
    47  			},
    48  		},
    49  	}
    50  }
    51  
    52  func resourceCloudStackIPAddressCreate(d *schema.ResourceData, meta interface{}) error {
    53  	cs := meta.(*cloudstack.CloudStackClient)
    54  
    55  	if err := verifyIPAddressParams(d); err != nil {
    56  		return err
    57  	}
    58  
    59  	// Create a new parameter struct
    60  	p := cs.Address.NewAssociateIpAddressParams()
    61  
    62  	if networkid, ok := d.GetOk("network_id"); ok {
    63  		// Set the networkid
    64  		p.SetNetworkid(networkid.(string))
    65  	}
    66  
    67  	if vpcid, ok := d.GetOk("vpc_id"); ok {
    68  		// Set the vpcid
    69  		p.SetVpcid(vpcid.(string))
    70  	}
    71  
    72  	if zoneid, ok := d.GetOk("zone_id"); ok {
    73  		// Set the vpcid
    74  		p.SetZoneid(zoneid.(string))
    75  	}
    76  
    77  	// If there is a project supplied, we retrieve and set the project id
    78  	if err := setProjectid(p, cs, d); err != nil {
    79  		return err
    80  	}
    81  
    82  	// Associate a new IP address
    83  	r, err := cs.Address.AssociateIpAddress(p)
    84  	if err != nil {
    85  		return fmt.Errorf("Error associating a new IP address: %s", err)
    86  	}
    87  
    88  	d.SetId(r.Id)
    89  
    90  	return resourceCloudStackIPAddressRead(d, meta)
    91  }
    92  
    93  func resourceCloudStackIPAddressRead(d *schema.ResourceData, meta interface{}) error {
    94  	cs := meta.(*cloudstack.CloudStackClient)
    95  
    96  	// Get the IP address details
    97  	ip, count, err := cs.Address.GetPublicIpAddressByID(
    98  		d.Id(),
    99  		cloudstack.WithProject(d.Get("project").(string)),
   100  	)
   101  	if err != nil {
   102  		if count == 0 {
   103  			log.Printf(
   104  				"[DEBUG] IP address with ID %s is no longer associated", d.Id())
   105  			d.SetId("")
   106  			return nil
   107  		}
   108  
   109  		return err
   110  	}
   111  
   112  	// Updated the IP address
   113  	d.Set("ip_address", ip.Ipaddress)
   114  
   115  	if _, ok := d.GetOk("network_id"); ok {
   116  		d.Set("network_id", ip.Associatednetworkid)
   117  	}
   118  
   119  	if _, ok := d.GetOk("vpc_id"); ok {
   120  		d.Set("vpc_id", ip.Vpcid)
   121  	}
   122  
   123  	if _, ok := d.GetOk("zone_id"); ok {
   124  		d.Set("zone_id", ip.Zoneid)
   125  	}
   126  
   127  	setValueOrID(d, "project", ip.Project, ip.Projectid)
   128  
   129  	return nil
   130  }
   131  
   132  func resourceCloudStackIPAddressDelete(d *schema.ResourceData, meta interface{}) error {
   133  	cs := meta.(*cloudstack.CloudStackClient)
   134  
   135  	// Create a new parameter struct
   136  	p := cs.Address.NewDisassociateIpAddressParams(d.Id())
   137  
   138  	// Disassociate the IP address
   139  	if _, err := cs.Address.DisassociateIpAddress(p); err != nil {
   140  		// This is a very poor way to be told the ID does no longer exist :(
   141  		if strings.Contains(err.Error(), fmt.Sprintf(
   142  			"Invalid parameter id value=%s due to incorrect long value format, "+
   143  				"or entity does not exist", d.Id())) {
   144  			return nil
   145  		}
   146  
   147  		return fmt.Errorf("Error disassociating IP address %s: %s", d.Get("name").(string), err)
   148  	}
   149  
   150  	return nil
   151  }
   152  
   153  func verifyIPAddressParams(d *schema.ResourceData) error {
   154  	_, network := d.GetOk("network_id")
   155  	_, vpc := d.GetOk("vpc_id")
   156  
   157  	if (network && vpc) || (!network && !vpc) {
   158  		return fmt.Errorf(
   159  			"You must supply a value for either (so not both) the 'network_id' or 'vpc_id' parameter")
   160  	}
   161  
   162  	return nil
   163  }