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