From b4242e6f358727385e1d012eb253adb1e8c201a5 Mon Sep 17 00:00:00 2001 From: Joe Topjian Date: Sat, 14 Nov 2015 20:19:06 +0000 Subject: [PATCH] provider/openstack: Clean up some attributes in LBaaS VIP resource This commit makes a few attributes computed so the generated information is accessible after creation. It also fixes the "persistence" attribute, which previously had a typo. Finally, it converts "admin_state_up" to a Boolean to match the majority of other attributes of the same name. --- .../openstack/resource_openstack_lb_vip_v1.go | 67 +++++-------------- .../resource_openstack_lb_vip_v1_test.go | 6 ++ 2 files changed, 21 insertions(+), 52 deletions(-) diff --git a/builtin/providers/openstack/resource_openstack_lb_vip_v1.go b/builtin/providers/openstack/resource_openstack_lb_vip_v1.go index 934215d2c..dd165df77 100644 --- a/builtin/providers/openstack/resource_openstack_lb_vip_v1.go +++ b/builtin/providers/openstack/resource_openstack_lb_vip_v1.go @@ -3,7 +3,6 @@ package openstack import ( "fmt" "log" - "strconv" "github.com/hashicorp/terraform/helper/schema" "github.com/rackspace/gophercloud" @@ -53,16 +52,19 @@ func resourceLBVipV1() *schema.Resource { "tenant_id": &schema.Schema{ Type: schema.TypeString, Optional: true, + Computed: true, ForceNew: true, }, "address": &schema.Schema{ Type: schema.TypeString, Optional: true, + Computed: true, ForceNew: true, }, "description": &schema.Schema{ Type: schema.TypeString, Optional: true, + Computed: true, ForceNew: false, }, "persistence": &schema.Schema{ @@ -73,6 +75,7 @@ func resourceLBVipV1() *schema.Resource { "conn_limit": &schema.Schema{ Type: schema.TypeInt, Optional: true, + Computed: true, ForceNew: false, }, "port_id": &schema.Schema{ @@ -86,8 +89,9 @@ func resourceLBVipV1() *schema.Resource { ForceNew: false, }, "admin_state_up": &schema.Schema{ - Type: schema.TypeString, + Type: schema.TypeBool, Optional: true, + Computed: true, ForceNew: false, }, }, @@ -114,14 +118,8 @@ func resourceLBVipV1Create(d *schema.ResourceData, meta interface{}) error { 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 - } + asu := d.Get("admin_state_up").(bool) + createOpts.AdminStateUp = &asu log.Printf("[DEBUG] Create Options: %#v", createOpts) p, err := vips.Create(networkingClient, createOpts).Extract() @@ -160,40 +158,11 @@ func resourceLBVipV1Read(d *schema.ResourceData, meta interface{}) error { d.Set("port", p.ProtocolPort) d.Set("pool_id", p.PoolID) d.Set("port_id", p.PortID) - - if t, exists := d.GetOk("tenant_id"); exists && t != "" { - d.Set("tenant_id", p.TenantID) - } else { - d.Set("tenant_id", "") - } - - if t, exists := d.GetOk("address"); exists && t != "" { - d.Set("address", p.Address) - } else { - d.Set("address", "") - } - - if t, exists := d.GetOk("description"); exists && t != "" { - d.Set("description", p.Description) - } else { - d.Set("description", "") - } - - if t, exists := d.GetOk("persistence"); exists && t != "" { - d.Set("persistence", p.Description) - } - - if t, exists := d.GetOk("conn_limit"); exists && t != "" { - d.Set("conn_limit", p.ConnLimit) - } else { - d.Set("conn_limit", "") - } - - if t, exists := d.GetOk("admin_state_up"); exists && t != "" { - d.Set("admin_state_up", strconv.FormatBool(p.AdminStateUp)) - } else { - d.Set("admin_state_up", "") - } + d.Set("tenant_id", p.TenantID) + d.Set("address", p.Address) + d.Set("description", p.Description) + d.Set("conn_limit", p.ConnLimit) + d.Set("admin_state_up", p.AdminStateUp) return nil } @@ -255,14 +224,8 @@ func resourceLBVipV1Update(d *schema.ResourceData, meta interface{}) error { } } 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 - } + asu := d.Get("admin_state_up").(bool) + updateOpts.AdminStateUp = &asu } log.Printf("[DEBUG] Updating OpenStack LB VIP %s with options: %+v", d.Id(), updateOpts) diff --git a/builtin/providers/openstack/resource_openstack_lb_vip_v1_test.go b/builtin/providers/openstack/resource_openstack_lb_vip_v1_test.go index f30cd9d56..0ef369a4e 100644 --- a/builtin/providers/openstack/resource_openstack_lb_vip_v1_test.go +++ b/builtin/providers/openstack/resource_openstack_lb_vip_v1_test.go @@ -116,6 +116,9 @@ var testAccLBV1VIP_basic = fmt.Sprintf(` protocol = "HTTP" port = 80 pool_id = "${openstack_lb_pool_v1.pool_1.id}" + persistence { + type = "SOURCE_IP" + } }`, OS_REGION_NAME, OS_REGION_NAME, OS_REGION_NAME) @@ -148,5 +151,8 @@ var testAccLBV1VIP_update = fmt.Sprintf(` protocol = "HTTP" port = 80 pool_id = "${openstack_lb_pool_v1.pool_1.id}" + persistence { + type = "SOURCE_IP" + } }`, OS_REGION_NAME, OS_REGION_NAME, OS_REGION_NAME)