github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/openstack/resource_openstack_compute_floatingip_v2.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  func resourceComputeFloatingIPV2() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceComputeFloatingIPV2Create,
    14  		Read:   resourceComputeFloatingIPV2Read,
    15  		Update: nil,
    16  		Delete: resourceComputeFloatingIPV2Delete,
    17  		Importer: &schema.ResourceImporter{
    18  			State: schema.ImportStatePassthrough,
    19  		},
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"region": &schema.Schema{
    23  				Type:        schema.TypeString,
    24  				Required:    true,
    25  				ForceNew:    true,
    26  				DefaultFunc: schema.EnvDefaultFunc("OS_REGION_NAME", ""),
    27  			},
    28  
    29  			"pool": &schema.Schema{
    30  				Type:        schema.TypeString,
    31  				Required:    true,
    32  				ForceNew:    true,
    33  				DefaultFunc: schema.EnvDefaultFunc("OS_POOL_NAME", nil),
    34  			},
    35  
    36  			"address": &schema.Schema{
    37  				Type:     schema.TypeString,
    38  				Computed: true,
    39  			},
    40  
    41  			"fixed_ip": &schema.Schema{
    42  				Type:     schema.TypeString,
    43  				Computed: true,
    44  			},
    45  
    46  			"instance_id": &schema.Schema{
    47  				Type:     schema.TypeString,
    48  				Computed: true,
    49  			},
    50  		},
    51  	}
    52  }
    53  
    54  func resourceComputeFloatingIPV2Create(d *schema.ResourceData, meta interface{}) error {
    55  	config := meta.(*Config)
    56  	computeClient, err := config.computeV2Client(GetRegion(d))
    57  	if err != nil {
    58  		return fmt.Errorf("Error creating OpenStack compute client: %s", err)
    59  	}
    60  
    61  	createOpts := &floatingips.CreateOpts{
    62  		Pool: d.Get("pool").(string),
    63  	}
    64  	log.Printf("[DEBUG] Create Options: %#v", createOpts)
    65  	newFip, err := floatingips.Create(computeClient, createOpts).Extract()
    66  	if err != nil {
    67  		return fmt.Errorf("Error creating Floating IP: %s", err)
    68  	}
    69  
    70  	d.SetId(newFip.ID)
    71  
    72  	return resourceComputeFloatingIPV2Read(d, meta)
    73  }
    74  
    75  func resourceComputeFloatingIPV2Read(d *schema.ResourceData, meta interface{}) error {
    76  	config := meta.(*Config)
    77  	computeClient, err := config.computeV2Client(GetRegion(d))
    78  	if err != nil {
    79  		return fmt.Errorf("Error creating OpenStack compute client: %s", err)
    80  	}
    81  
    82  	fip, err := floatingips.Get(computeClient, d.Id()).Extract()
    83  	if err != nil {
    84  		return CheckDeleted(d, err, "floating ip")
    85  	}
    86  
    87  	log.Printf("[DEBUG] Retrieved Floating IP %s: %+v", d.Id(), fip)
    88  
    89  	d.Set("pool", fip.Pool)
    90  	d.Set("instance_id", fip.InstanceID)
    91  	d.Set("address", fip.IP)
    92  	d.Set("fixed_ip", fip.FixedIP)
    93  	d.Set("region", GetRegion(d))
    94  
    95  	return nil
    96  }
    97  
    98  func resourceComputeFloatingIPV2Delete(d *schema.ResourceData, meta interface{}) error {
    99  	config := meta.(*Config)
   100  	computeClient, err := config.computeV2Client(GetRegion(d))
   101  	if err != nil {
   102  		return fmt.Errorf("Error creating OpenStack compute client: %s", err)
   103  	}
   104  
   105  	log.Printf("[DEBUG] Deleting Floating IP %s", d.Id())
   106  	if err := floatingips.Delete(computeClient, d.Id()).ExtractErr(); err != nil {
   107  		return fmt.Errorf("Error deleting Floating IP: %s", err)
   108  	}
   109  
   110  	return nil
   111  }