terraform/builtin/providers/openstack/resource_openstack_lb_vip_v...

259 lines
6.6 KiB
Go
Raw Normal View History

2015-01-14 19:55:36 +01:00
package openstack
import (
"fmt"
"log"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/lbaas/vips"
)
2015-01-26 19:09:27 +01:00
func resourceLBVipV1() *schema.Resource {
2015-01-14 19:55:36 +01:00
return &schema.Resource{
2015-01-26 19:09:27 +01:00
Create: resourceLBVipV1Create,
Read: resourceLBVipV1Read,
Update: resourceLBVipV1Update,
Delete: resourceLBVipV1Delete,
2015-01-14 19:55:36 +01:00
Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
DefaultFunc: envDefaultFunc("OS_REGION_NAME"),
},
2015-01-14 19:55:36 +01:00
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: false,
},
"subnet_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"protocol": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"port": &schema.Schema{
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"pool_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: false,
},
"tenant_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"address": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: false,
},
"persistence": &schema.Schema{
2015-01-26 19:38:33 +01:00
Type: schema.TypeMap,
2015-01-14 19:55:36 +01:00
Optional: true,
ForceNew: false,
},
"conn_limit": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
ForceNew: false,
},
"admin_state_up": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: false,
},
},
}
}
2015-01-26 19:09:27 +01:00
func resourceLBVipV1Create(d *schema.ResourceData, meta interface{}) error {
2015-01-14 19:55:36 +01:00
config := meta.(*Config)
2015-01-31 22:33:54 +01:00
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
2015-01-14 19:55:36 +01:00
createOpts := vips.CreateOpts{
Name: d.Get("name").(string),
SubnetID: d.Get("subnet_id").(string),
Protocol: d.Get("protocol").(string),
ProtocolPort: d.Get("port").(int),
PoolID: d.Get("pool_id").(string),
TenantID: d.Get("tenant_id").(string),
Address: d.Get("address").(string),
Description: d.Get("description").(string),
2015-01-26 19:09:27 +01:00
Persistence: resourceVipPersistenceV1(d),
2015-01-14 19:55:36 +01:00
ConnLimit: gophercloud.MaybeInt(d.Get("conn_limit").(int)),
}
asuRaw := d.Get("admin_state_up").(string)
if asuRaw != "" {
asu, err := strconv.ParseBool(asuRaw)
if err != nil {
return fmt.Errorf("admin_state_up, if provided, must be either 'true' or 'false'")
}
createOpts.AdminStateUp = &asu
}
2015-03-24 13:59:55 +01:00
log.Printf("[DEBUG] Create Options: %#v", createOpts)
p, err := vips.Create(networkingClient, createOpts).Extract()
2015-01-14 19:55:36 +01:00
if err != nil {
return fmt.Errorf("Error creating OpenStack LB VIP: %s", err)
}
log.Printf("[INFO] LB VIP ID: %s", p.ID)
d.SetId(p.ID)
2015-01-26 19:09:27 +01:00
return resourceLBVipV1Read(d, meta)
2015-01-14 19:55:36 +01:00
}
2015-01-26 19:09:27 +01:00
func resourceLBVipV1Read(d *schema.ResourceData, meta interface{}) error {
2015-01-14 19:55:36 +01:00
config := meta.(*Config)
2015-01-31 22:33:54 +01:00
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
2015-01-14 19:55:36 +01:00
p, err := vips.Get(networkingClient, d.Id()).Extract()
2015-01-14 19:55:36 +01:00
if err != nil {
return CheckDeleted(d, err, "LB VIP")
2015-01-14 19:55:36 +01:00
}
log.Printf("[DEBUG] Retreived OpenStack LB VIP %s: %+v", d.Id(), p)
d.Set("name", p.Name)
d.Set("subnet_id", p.SubnetID)
d.Set("protocol", p.Protocol)
d.Set("port", p.ProtocolPort)
d.Set("pool_id", p.PoolID)
2015-01-27 02:28:01 +01:00
if t, exists := d.GetOk("tenant_id"); exists && t != "" {
d.Set("tenant_id", p.TenantID)
2015-01-14 19:55:36 +01:00
} else {
d.Set("tenant_id", "")
}
2015-01-27 02:28:01 +01:00
if t, exists := d.GetOk("address"); exists && t != "" {
d.Set("address", p.Address)
2015-01-14 19:55:36 +01:00
} else {
d.Set("address", "")
}
2015-01-27 02:28:01 +01:00
if t, exists := d.GetOk("description"); exists && t != "" {
d.Set("description", p.Description)
2015-01-14 19:55:36 +01:00
} else {
d.Set("description", "")
}
2015-01-27 02:28:01 +01:00
if t, exists := d.GetOk("persistence"); exists && t != "" {
2015-01-27 18:19:11 +01:00
d.Set("persistence", p.Description)
2015-01-14 19:55:36 +01:00
}
2015-01-27 02:28:01 +01:00
if t, exists := d.GetOk("conn_limit"); exists && t != "" {
d.Set("conn_limit", p.ConnLimit)
2015-01-14 19:55:36 +01:00
} else {
d.Set("conn_limit", "")
}
2015-01-27 02:28:01 +01:00
if t, exists := d.GetOk("admin_state_up"); exists && t != "" {
d.Set("admin_state_up", strconv.FormatBool(p.AdminStateUp))
2015-01-14 19:55:36 +01:00
} else {
d.Set("admin_state_up", "")
}
return nil
}
2015-01-26 19:09:27 +01:00
func resourceLBVipV1Update(d *schema.ResourceData, meta interface{}) error {
2015-01-14 19:55:36 +01:00
config := meta.(*Config)
2015-01-31 22:33:54 +01:00
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
2015-01-14 19:55:36 +01:00
var updateOpts vips.UpdateOpts
if d.HasChange("name") {
updateOpts.Name = d.Get("name").(string)
}
if d.HasChange("pool_id") {
updateOpts.PoolID = d.Get("pool_id").(string)
}
if d.HasChange("description") {
updateOpts.Description = d.Get("description").(string)
}
if d.HasChange("persistence") {
2015-01-26 19:09:27 +01:00
updateOpts.Persistence = resourceVipPersistenceV1(d)
2015-01-14 19:55:36 +01:00
}
if d.HasChange("conn_limit") {
updateOpts.ConnLimit = gophercloud.MaybeInt(d.Get("conn_limit").(int))
}
if d.HasChange("admin_state_up") {
asuRaw := d.Get("admin_state_up").(string)
if asuRaw != "" {
asu, err := strconv.ParseBool(asuRaw)
if err != nil {
return fmt.Errorf("admin_state_up, if provided, must be either 'true' or 'false'")
}
updateOpts.AdminStateUp = &asu
}
}
log.Printf("[DEBUG] Updating OpenStack LB VIP %s with options: %+v", d.Id(), updateOpts)
_, err = vips.Update(networkingClient, d.Id(), updateOpts).Extract()
2015-01-14 19:55:36 +01:00
if err != nil {
return fmt.Errorf("Error updating OpenStack LB VIP: %s", err)
}
2015-01-26 19:09:27 +01:00
return resourceLBVipV1Read(d, meta)
2015-01-14 19:55:36 +01:00
}
2015-01-26 19:09:27 +01:00
func resourceLBVipV1Delete(d *schema.ResourceData, meta interface{}) error {
2015-01-14 19:55:36 +01:00
config := meta.(*Config)
2015-01-31 22:33:54 +01:00
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
2015-01-14 19:55:36 +01:00
err = vips.Delete(networkingClient, d.Id()).ExtractErr()
2015-01-14 19:55:36 +01:00
if err != nil {
return fmt.Errorf("Error deleting OpenStack LB VIP: %s", err)
}
d.SetId("")
return nil
}
2015-01-26 19:09:27 +01:00
func resourceVipPersistenceV1(d *schema.ResourceData) *vips.SessionPersistence {
2015-01-14 19:55:36 +01:00
rawP := d.Get("persistence").(interface{})
rawMap := rawP.(map[string]interface{})
2015-01-26 19:38:33 +01:00
if len(rawMap) != 0 {
2015-01-26 19:09:27 +01:00
p := vips.SessionPersistence{}
if t, ok := rawMap["type"]; ok {
p.Type = t.(string)
}
if c, ok := rawMap["cookie_name"]; ok {
p.CookieName = c.(string)
}
return &p
2015-01-14 19:55:36 +01:00
}
2015-01-26 19:09:27 +01:00
return nil
2015-01-14 19:55:36 +01:00
}