func resourceLBMonitorV1Read(d *schema.ResourceData, meta interface{}) error {
	config := meta.(*Config)
	networkingClient, err := config.networkingV2Client(GetRegion(d))
	if err != nil {
		return fmt.Errorf("Error creating OpenStack networking client: %s", err)
	}

	m, err := monitors.Get(networkingClient, d.Id()).Extract()
	if err != nil {
		return CheckDeleted(d, err, "LB monitor")
	}

	log.Printf("[DEBUG] Retrieved 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)
	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))
	d.Set("region", GetRegion(d))

	return nil
}
Пример #2
0
func TestMonitorsCRUD(t *testing.T) {
	client, err := clients.NewNetworkV2Client()
	if err != nil {
		t.Fatalf("Unable to create a network client: %v", err)
	}

	monitor, err := CreateMonitor(t, client)
	if err != nil {
		t.Fatalf("Unable to create monitor: %v", err)
	}
	defer DeleteMonitor(t, client, monitor.ID)

	PrintMonitor(t, monitor)

	updateOpts := monitors.UpdateOpts{
		Delay: 999,
	}

	_, err = monitors.Update(client, monitor.ID, updateOpts).Extract()
	if err != nil {
		t.Fatalf("Unable to update monitor: %v")
	}

	newMonitor, err := monitors.Get(client, monitor.ID).Extract()
	if err != nil {
		t.Fatalf("Unable to get monitor: %v")
	}

	PrintMonitor(t, newMonitor)
}
func testAccCheckLBV1MonitorExists(n string, monitor *monitors.Monitor) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[n]
		if !ok {
			return fmt.Errorf("Not found: %s", n)
		}

		if rs.Primary.ID == "" {
			return fmt.Errorf("No ID is set")
		}

		config := testAccProvider.Meta().(*Config)
		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
		if err != nil {
			return fmt.Errorf("Error creating OpenStack networking client: %s", err)
		}

		found, err := monitors.Get(networkingClient, rs.Primary.ID).Extract()
		if err != nil {
			return err
		}

		if found.ID != rs.Primary.ID {
			return fmt.Errorf("Monitor not found")
		}

		*monitor = *found

		return nil
	}
}
func waitForLBMonitorActive(networkingClient *gophercloud.ServiceClient, monitorId string) resource.StateRefreshFunc {
	return func() (interface{}, string, error) {
		m, err := monitors.Get(networkingClient, monitorId).Extract()
		if err != nil {
			return nil, "", err
		}

		// The monitor resource has no Status attribute, so a successful Get is the best we can do
		log.Printf("[DEBUG] OpenStack LB Monitor: %+v", m)
		return m, "ACTIVE", nil
	}
}
Пример #5
0
func TestGet(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	th.Mux.HandleFunc("/v2.0/lb/health_monitors/f3eeab00-8367-4524-b662-55e64d4cacb5", func(w http.ResponseWriter, r *http.Request) {
		th.TestMethod(t, r, "GET")
		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)

		w.Header().Add("Content-Type", "application/json")
		w.WriteHeader(http.StatusOK)

		fmt.Fprintf(w, `
{
   "health_monitor":{
      "id":"f3eeab00-8367-4524-b662-55e64d4cacb5",
      "tenant_id":"453105b9-1754-413f-aab1-55f1af620750",
      "type":"HTTP",
      "delay":20,
      "timeout":10,
      "max_retries":5,
      "http_method":"GET",
      "url_path":"/check",
      "expected_codes":"200-299",
      "admin_state_up":true,
      "status":"ACTIVE"
   }
}
			`)
	})

	hm, err := monitors.Get(fake.ServiceClient(), "f3eeab00-8367-4524-b662-55e64d4cacb5").Extract()
	th.AssertNoErr(t, err)

	th.AssertEquals(t, "f3eeab00-8367-4524-b662-55e64d4cacb5", hm.ID)
	th.AssertEquals(t, "453105b9-1754-413f-aab1-55f1af620750", hm.TenantID)
	th.AssertEquals(t, "HTTP", hm.Type)
	th.AssertEquals(t, 20, hm.Delay)
	th.AssertEquals(t, 10, hm.Timeout)
	th.AssertEquals(t, 5, hm.MaxRetries)
	th.AssertEquals(t, "GET", hm.HTTPMethod)
	th.AssertEquals(t, "/check", hm.URLPath)
	th.AssertEquals(t, "200-299", hm.ExpectedCodes)
	th.AssertEquals(t, true, hm.AdminStateUp)
	th.AssertEquals(t, "ACTIVE", hm.Status)
}
func waitForLBMonitorDelete(networkingClient *gophercloud.ServiceClient, monitorId string) resource.StateRefreshFunc {
	return func() (interface{}, string, error) {
		log.Printf("[DEBUG] Attempting to delete OpenStack LB Monitor %s", monitorId)

		m, err := monitors.Get(networkingClient, monitorId).Extract()
		if err != nil {
			if _, ok := err.(gophercloud.ErrDefault404); ok {
				log.Printf("[DEBUG] Successfully deleted OpenStack LB Monitor %s", monitorId)
				return m, "DELETED", nil
			}

			if errCode, ok := err.(gophercloud.ErrUnexpectedResponseCode); ok {
				if errCode.Actual == 409 {
					log.Printf("[DEBUG] OpenStack LB Monitor (%s) is waiting for Pool to delete.", monitorId)
					return m, "PENDING", nil
				}
			}

			return m, "ACTIVE", err
		}

		log.Printf("[DEBUG] OpenStack LB Monitor: %+v", m)
		err = monitors.Delete(networkingClient, monitorId).ExtractErr()
		if err != nil {
			if _, ok := err.(gophercloud.ErrDefault404); ok {
				log.Printf("[DEBUG] Successfully deleted OpenStack LB Monitor %s", monitorId)
				return m, "DELETED", nil
			}

			if errCode, ok := err.(gophercloud.ErrUnexpectedResponseCode); ok {
				if errCode.Actual == 409 {
					log.Printf("[DEBUG] OpenStack LB Monitor (%s) is waiting for Pool to delete.", monitorId)
					return m, "PENDING", nil
				}
			}

			return m, "ACTIVE", err
		}

		log.Printf("[DEBUG] OpenStack LB Monitor %s still active.", monitorId)
		return m, "ACTIVE", nil
	}

}
func testAccCheckLBV1MonitorDestroy(s *terraform.State) error {
	config := testAccProvider.Meta().(*Config)
	networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
	if err != nil {
		return fmt.Errorf("Error creating OpenStack networking client: %s", err)
	}

	for _, rs := range s.RootModule().Resources {
		if rs.Type != "openstack_lb_monitor_v1" {
			continue
		}

		_, err := monitors.Get(networkingClient, rs.Primary.ID).Extract()
		if err == nil {
			return fmt.Errorf("LB monitor still exists")
		}
	}

	return nil
}