2015-01-14 19:54:52 +01:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/lbaas/monitors"
|
|
|
|
)
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceLBMonitorV1() *schema.Resource {
|
2015-01-14 19:54:52 +01:00
|
|
|
return &schema.Resource{
|
2015-01-26 19:09:27 +01:00
|
|
|
Create: resourceLBMonitorV1Create,
|
|
|
|
Read: resourceLBMonitorV1Read,
|
|
|
|
Update: resourceLBMonitorV1Update,
|
|
|
|
Delete: resourceLBMonitorV1Delete,
|
2015-01-14 19:54:52 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2015-01-26 18:54:07 +01:00
|
|
|
"region": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
2015-04-11 06:11:34 +02:00
|
|
|
DefaultFunc: envDefaultFuncAllowMissing("OS_REGION_NAME"),
|
2015-01-26 18:54:07 +01:00
|
|
|
},
|
2015-01-14 19:54:52 +01:00
|
|
|
"tenant_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2015-05-10 00:26:23 +02:00
|
|
|
Computed: true,
|
2015-01-14 19:54:52 +01:00
|
|
|
},
|
|
|
|
"type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"delay": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
"timeout": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
"max_retries": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
"url_path": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
"http_method": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
"expected_codes": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
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 resourceLBMonitorV1Create(d *schema.ResourceData, meta interface{}) error {
|
2015-01-14 19:54:52 +01:00
|
|
|
config := meta.(*Config)
|
2015-01-31 22:33:54 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
2015-01-26 18:54:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
2015-01-14 19:54:52 +01:00
|
|
|
|
|
|
|
createOpts := monitors.CreateOpts{
|
|
|
|
TenantID: d.Get("tenant_id").(string),
|
|
|
|
Type: d.Get("type").(string),
|
|
|
|
Delay: d.Get("delay").(int),
|
|
|
|
Timeout: d.Get("timeout").(int),
|
|
|
|
MaxRetries: d.Get("max_retries").(int),
|
|
|
|
URLPath: d.Get("url_path").(string),
|
|
|
|
ExpectedCodes: d.Get("expected_codes").(string),
|
|
|
|
HTTPMethod: d.Get("http_method").(string),
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2015-01-26 18:54:07 +01:00
|
|
|
m, err := monitors.Create(networkingClient, createOpts).Extract()
|
2015-01-14 19:54:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack LB Monitor: %s", err)
|
|
|
|
}
|
|
|
|
log.Printf("[INFO] LB Monitor ID: %s", m.ID)
|
|
|
|
|
|
|
|
d.SetId(m.ID)
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
return resourceLBMonitorV1Read(d, meta)
|
2015-01-14 19:54:52 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceLBMonitorV1Read(d *schema.ResourceData, meta interface{}) error {
|
2015-01-14 19:54:52 +01:00
|
|
|
config := meta.(*Config)
|
2015-01-31 22:33:54 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
2015-01-26 18:54:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
2015-01-14 19:54:52 +01:00
|
|
|
|
2015-01-26 18:54:07 +01:00
|
|
|
m, err := monitors.Get(networkingClient, d.Id()).Extract()
|
2015-01-14 19:54:52 +01:00
|
|
|
if err != nil {
|
2015-02-10 04:27:39 +01:00
|
|
|
return CheckDeleted(d, err, "LB monitor")
|
2015-01-14 19:54:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Retreived OpenStack LB Monitor %s: %+v", d.Id(), m)
|
|
|
|
|
|
|
|
d.Set("type", m.Type)
|
|
|
|
d.Set("delay", m.Delay)
|
|
|
|
d.Set("timeout", m.Timeout)
|
|
|
|
d.Set("max_retries", m.MaxRetries)
|
2015-03-24 13:59:55 +01:00
|
|
|
d.Set("tenant_id", m.TenantID)
|
|
|
|
d.Set("url_path", m.URLPath)
|
|
|
|
d.Set("http_method", m.HTTPMethod)
|
|
|
|
d.Set("expected_codes", m.ExpectedCodes)
|
|
|
|
d.Set("admin_state_up", strconv.FormatBool(m.AdminStateUp))
|
2015-01-14 19:54:52 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceLBMonitorV1Update(d *schema.ResourceData, meta interface{}) error {
|
2015-01-14 19:54:52 +01:00
|
|
|
config := meta.(*Config)
|
2015-01-31 22:33:54 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
2015-01-26 18:54:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
2015-01-14 19:54:52 +01:00
|
|
|
|
2015-02-01 01:55:42 +01:00
|
|
|
updateOpts := monitors.UpdateOpts{
|
|
|
|
Delay: d.Get("delay").(int),
|
|
|
|
Timeout: d.Get("timeout").(int),
|
|
|
|
MaxRetries: d.Get("max_retries").(int),
|
|
|
|
URLPath: d.Get("url_path").(string),
|
|
|
|
HTTPMethod: d.Get("http_method").(string),
|
|
|
|
ExpectedCodes: d.Get("expected_codes").(string),
|
2015-01-14 19:54:52 +01:00
|
|
|
}
|
2015-02-01 01:55:42 +01:00
|
|
|
|
2015-01-14 19:54:52 +01:00
|
|
|
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 Monitor %s with options: %+v", d.Id(), updateOpts)
|
|
|
|
|
2015-01-26 18:54:07 +01:00
|
|
|
_, err = monitors.Update(networkingClient, d.Id(), updateOpts).Extract()
|
2015-01-14 19:54:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating OpenStack LB Monitor: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
return resourceLBMonitorV1Read(d, meta)
|
2015-01-14 19:54:52 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 19:09:27 +01:00
|
|
|
func resourceLBMonitorV1Delete(d *schema.ResourceData, meta interface{}) error {
|
2015-01-14 19:54:52 +01:00
|
|
|
config := meta.(*Config)
|
2015-01-31 22:33:54 +01:00
|
|
|
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
|
2015-01-26 18:54:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
2015-01-14 19:54:52 +01:00
|
|
|
|
2015-01-26 18:54:07 +01:00
|
|
|
err = monitors.Delete(networkingClient, d.Id()).ExtractErr()
|
2015-01-14 19:54:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting OpenStack LB Monitor: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|