Skip to content

Commit

Permalink
Octavia l7 rule support - part 3: get
Browse files Browse the repository at this point in the history
  • Loading branch information
lingxiankong committed Apr 15, 2018
1 parent e7e2296 commit a1471c7
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 3 deletions.
17 changes: 14 additions & 3 deletions acceptance/openstack/loadbalancer/v2/loadbalancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,34 @@ func TestLoadbalancersCRUD(t *testing.T) {
tools.PrintResource(t, newPolicy)

// L7 rule
_, err = CreateL7Rule(t, lbClient, newPolicy.ID, lb)
rule, err := CreateL7Rule(t, lbClient, newPolicy.ID, lb)
if err != nil {
t.Fatalf("Unable to create l7 rule: %v", err)
}

allPages, err = l7policies.ListRules(lbClient, policy.ID, l7policies.ListRulesOpts{}).AllPages()
allPages, err := l7policies.ListRules(lbClient, policy.ID, l7policies.ListRulesOpts{}).AllPages()
if err != nil {
t.Fatalf("Unable to get l7 rules: %v", err)
}
allRules, err = l7policies.ExtractRules(allPages)
allRules, err := l7policies.ExtractRules(allPages)
if err != nil {
t.Fatalf("Unable to extract l7 rules: %v", err)
}
for _, rule := range allRules {
tools.PrintResource(t, rule)
}

newRule, err := l7policies.GetRule(lbClient, newPolicy.ID, rule.ID).Extract()
if err != nil {
t.Fatalf("Unable to get l7 rule: %v", err)
}

if err := WaitForLoadBalancerState(lbClient, lb.ID, "ACTIVE", loadbalancerActiveTimeoutSeconds); err != nil {
t.Fatalf("Timed out waiting for loadbalancer to become active")
}

tools.PrintResource(t, newRule)

// Pool
pool, err := CreatePool(t, lbClient, lb)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions openstack/loadbalancer/v2/l7policies/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,12 @@ Example to List L7 Rules
for _, rule := allRules {
fmt.Printf("%+v\n", rule)
}
Example to Get a l7 rule
l7rule, err := l7policies.GetRule(lbClient, "023f2e34-7806-443b-bfae-16c324569a3d", "53ad8ab8-40fa-11e8-a508-00224d6b7bc1").Extract()
if err != nil {
panic(err)
}
*/
package l7policies
6 changes: 6 additions & 0 deletions openstack/loadbalancer/v2/l7policies/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,9 @@ func ListRules(c *gophercloud.ServiceClient, policyID string, opts ListRulesOpts
return RulePage{pagination.LinkedPageBase{PageResult: r}}
})
}

// GetRule retrieves a particular L7Policy Rule based on its unique ID.
func GetRule(c *gophercloud.ServiceClient, policyID string, ruleID string) (r GetRuleResult) {
_, r.Err = c.Get(ruleResourceURL(c, policyID, ruleID), &r.Body, nil)
return
}
6 changes: 6 additions & 0 deletions openstack/loadbalancer/v2/l7policies/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,9 @@ func ExtractRules(r pagination.Page) ([]Rule, error) {
err := (r.(RulePage)).ExtractInto(&s)
return s.Rules, err
}

// GetRuleResult represents the result of a GetRule operation.
// Call its Extract method to interpret it as a Rule.
type GetRuleResult struct {
commonRuleResult
}
11 changes: 11 additions & 0 deletions openstack/loadbalancer/v2/l7policies/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,14 @@ func HandleRuleListSuccessfully(t *testing.T) {
}
})
}

// HandleRuleGetSuccessfully sets up the test server to respond to a rule Get request.
func HandleRuleGetSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules/16621dbb-a736-4888-a57a-3ecd53df784c", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
th.TestHeader(t, r, "Accept", "application/json")

fmt.Fprintf(w, SingleRuleBody)
})
}
14 changes: 14 additions & 0 deletions openstack/loadbalancer/v2/l7policies/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,17 @@ func TestListAllRules(t *testing.T) {
th.CheckDeepEquals(t, RulePath, actual[0])
th.CheckDeepEquals(t, RuleHostName, actual[1])
}

func TestGetRule(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleRuleGetSuccessfully(t)

client := fake.ServiceClient()
actual, err := l7policies.GetRule(client, "8a1412f0-4c32-4257-8b07-af4770b604fd", "16621dbb-a736-4888-a57a-3ecd53df784c").Extract()
if err != nil {
t.Fatalf("Unexpected Get error: %v", err)
}

th.CheckDeepEquals(t, RulePath, *actual)
}
4 changes: 4 additions & 0 deletions openstack/loadbalancer/v2/l7policies/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ func resourceURL(c *gophercloud.ServiceClient, id string) string {
func ruleRootURL(c *gophercloud.ServiceClient, policyID string) string {
return c.ServiceURL(rootPath, resourcePath, policyID, rulePath)
}

func ruleResourceURL(c *gophercloud.ServiceClient, policyID string, ruleID string) string {
return c.ServiceURL(rootPath, resourcePath, policyID, rulePath, ruleID)
}

0 comments on commit a1471c7

Please sign in to comment.