2015-02-13 21:59:41 +01:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2016-09-04 04:52:58 +02:00
|
|
|
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips"
|
2015-02-13 21:59:41 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceComputeFloatingIPV2() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceComputeFloatingIPV2Create,
|
|
|
|
Read: resourceComputeFloatingIPV2Read,
|
|
|
|
Update: nil,
|
|
|
|
Delete: resourceComputeFloatingIPV2Delete,
|
2016-07-03 17:38:38 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2015-02-13 21:59:41 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"region": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
2016-04-08 23:28:54 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("OS_REGION_NAME", ""),
|
2015-02-13 21:59:41 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"pool": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
2016-04-08 23:28:54 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("OS_POOL_NAME", nil),
|
2015-02-13 21:59:41 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"fixed_ip": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"instance_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeFloatingIPV2Create(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
computeClient, err := config.computeV2Client(GetRegion(d))
|
2015-02-13 21:59:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-09-04 04:52:58 +02:00
|
|
|
createOpts := &floatingips.CreateOpts{
|
2015-02-13 21:59:41 +01:00
|
|
|
Pool: d.Get("pool").(string),
|
|
|
|
}
|
2015-03-24 13:59:55 +01:00
|
|
|
log.Printf("[DEBUG] Create Options: %#v", createOpts)
|
2016-09-04 04:52:58 +02:00
|
|
|
newFip, err := floatingips.Create(computeClient, createOpts).Extract()
|
2015-02-13 21:59:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating Floating IP: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(newFip.ID)
|
|
|
|
|
|
|
|
return resourceComputeFloatingIPV2Read(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeFloatingIPV2Read(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
computeClient, err := config.computeV2Client(GetRegion(d))
|
2015-02-13 21:59:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-09-04 04:52:58 +02:00
|
|
|
fip, err := floatingips.Get(computeClient, d.Id()).Extract()
|
2015-02-13 21:59:41 +01:00
|
|
|
if err != nil {
|
2015-02-20 06:08:09 +01:00
|
|
|
return CheckDeleted(d, err, "floating ip")
|
2015-02-13 21:59:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Retrieved Floating IP %s: %+v", d.Id(), fip)
|
|
|
|
|
|
|
|
d.Set("pool", fip.Pool)
|
|
|
|
d.Set("instance_id", fip.InstanceID)
|
|
|
|
d.Set("address", fip.IP)
|
|
|
|
d.Set("fixed_ip", fip.FixedIP)
|
2016-12-04 15:24:07 +01:00
|
|
|
d.Set("region", GetRegion(d))
|
2015-02-13 21:59:41 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeFloatingIPV2Delete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
2016-12-04 15:24:07 +01:00
|
|
|
computeClient, err := config.computeV2Client(GetRegion(d))
|
2015-02-13 21:59:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-02-20 06:08:09 +01:00
|
|
|
log.Printf("[DEBUG] Deleting Floating IP %s", d.Id())
|
2016-09-04 04:52:58 +02:00
|
|
|
if err := floatingips.Delete(computeClient, d.Id()).ExtractErr(); err != nil {
|
2015-02-13 21:59:41 +01:00
|
|
|
return fmt.Errorf("Error deleting Floating IP: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|