func testAccCheckLBV1MemberExists(n string, member *members.Member) 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 := members.Get(networkingClient, rs.Primary.ID).Extract() if err != nil { return err } if found.ID != rs.Primary.ID { return fmt.Errorf("Member not found") } *member = *found return nil } }
func waitForLBMemberActive(networkingClient *gophercloud.ServiceClient, memberId string) resource.StateRefreshFunc { return func() (interface{}, string, error) { m, err := members.Get(networkingClient, memberId).Extract() if err != nil { return nil, "", err } log.Printf("[DEBUG] OpenStack LB member: %+v", m) if m.Status == "ACTIVE" { return m, "ACTIVE", nil } return m, m.Status, nil } }
func TestMembersCRUD(t *testing.T) { client, err := clients.NewNetworkV2Client() if err != nil { t.Fatalf("Unable to create a network client: %v", err) } network, err := networking.CreateNetwork(t, client) if err != nil { t.Fatalf("Unable to create network: %v", err) } defer networking.DeleteNetwork(t, client, network.ID) subnet, err := networking.CreateSubnet(t, client, network.ID) if err != nil { t.Fatalf("Unable to create subnet: %v", err) } defer networking.DeleteSubnet(t, client, subnet.ID) pool, err := CreatePool(t, client, subnet.ID) if err != nil { t.Fatalf("Unable to create pool: %v", err) } defer DeletePool(t, client, pool.ID) member, err := CreateMember(t, client, pool.ID) if err != nil { t.Fatalf("Unable to create member: %v", err) } defer DeleteMember(t, client, member.ID) PrintMember(t, member) updateOpts := members.UpdateOpts{ AdminStateUp: gophercloud.Enabled, } _, err = members.Update(client, member.ID, updateOpts).Extract() if err != nil { t.Fatalf("Unable to update member: %v") } newMember, err := members.Get(client, member.ID).Extract() if err != nil { t.Fatalf("Unable to get member: %v") } PrintMember(t, newMember) }
func waitForLBMemberDelete(networkingClient *gophercloud.ServiceClient, memberId string) resource.StateRefreshFunc { return func() (interface{}, string, error) { log.Printf("[DEBUG] Attempting to delete OpenStack LB member %s", memberId) m, err := members.Get(networkingClient, memberId).Extract() if err != nil { if _, ok := err.(gophercloud.ErrDefault404); ok { log.Printf("[DEBUG] Successfully deleted OpenStack LB member %s", memberId) return m, "DELETED", nil } return m, "ACTIVE", err } log.Printf("[DEBUG] OpenStack LB member %s still active.", memberId) return m, "ACTIVE", nil } }
func testAccCheckLBV1MemberDestroy(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_member_v1" { continue } _, err := members.Get(networkingClient, rs.Primary.ID).Extract() if err == nil { return fmt.Errorf("LB Member still exists") } } return nil }
func resourceLBMemberV1Read(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) networkingClient, err := config.networkingV2Client(d.Get("region").(string)) if err != nil { return fmt.Errorf("Error creating OpenStack networking client: %s", err) } m, err := members.Get(networkingClient, d.Id()).Extract() if err != nil { return CheckDeleted(d, err, "LB member") } log.Printf("[DEBUG] Retrieved OpenStack LB member %s: %+v", d.Id(), m) d.Set("address", m.Address) d.Set("pool_id", m.PoolID) d.Set("port", m.ProtocolPort) d.Set("weight", m.Weight) d.Set("admin_state_up", m.AdminStateUp) return nil }
func TestGet(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/v2.0/lb/members/975592ca-e308-48ad-8298-731935ee9f45", 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, ` { "member":{ "id":"975592ca-e308-48ad-8298-731935ee9f45", "address":"192.0.2.14", "protocol_port":8080, "tenant_id":"453105b9-1754-413f-aab1-55f1af620750", "admin_state_up":true, "weight":1, "status":"DOWN" } } `) }) m, err := members.Get(fake.ServiceClient(), "975592ca-e308-48ad-8298-731935ee9f45").Extract() th.AssertNoErr(t, err) th.AssertEquals(t, "975592ca-e308-48ad-8298-731935ee9f45", m.ID) th.AssertEquals(t, "192.0.2.14", m.Address) th.AssertEquals(t, 8080, m.ProtocolPort) th.AssertEquals(t, "453105b9-1754-413f-aab1-55f1af620750", m.TenantID) th.AssertEquals(t, true, m.AdminStateUp) th.AssertEquals(t, 1, m.Weight) th.AssertEquals(t, "DOWN", m.Status) }