Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
234 changes: 234 additions & 0 deletions rackspace/autoscale/v1/groups/fixtures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
// +build fixtures

package groups

import (
"fmt"
"net/http"
"testing"

th "github.com/rackspace/gophercloud/testhelper"
"github.com/rackspace/gophercloud/testhelper/client"
)

// GroupListBody contains the canned body of a groups.List response.
const GroupListBody = `
{
"groups_links": [],
"groups": [
{
"state": {
"status": "ACTIVE",
"desiredCapacity": 2,
"paused": false,
"active": [
{
"id": "449cead0-48b2-44fe-9107-dea7cdb6d925",
"links": [
{
"href": "https://dfw.servers.api.rackspacecloud.com/v2/123456/servers/449cead0-48b2-44fe-9107-dea7cdb6d925",
"rel": "self"
},
{
"href": "https://dfw.servers.api.rackspacecloud.com/123456/servers/449cead0-48b2-44fe-9107-dea7cdb6d925",
"rel": "bookmark"
}
]
},
{
"id": "d8c2696f-1936-45c7-892d-f5f741ef0f60",
"links": [
{
"href": "https://dfw.servers.api.rackspacecloud.com/v2/123456/servers/d8c2696f-1936-45c7-892d-f5f741ef0f60",
"rel": "self"
},
{
"href": "https://dfw.servers.api.rackspacecloud.com/123456/servers/d8c2696f-1936-45c7-892d-f5f741ef0f60",
"rel": "bookmark"
}
]
}
],
"pendingCapacity": 0,
"activeCapacity": 2,
"name": "first-group"
},
"id": "10eb3219-1b12-4b34-b1e4-e10ee4f24c65",
"links": [
{
"href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/10eb3219-1b12-4b34-b1e4-e10ee4f24c65/",
"rel": "self"
}
]
},
{
"state": {
"status": "ACTIVE",
"desiredCapacity": 3,
"paused": false,
"active": [
{
"id": "6cca7222-8ab5-4361-ac2c-d35eb0b78ab4",
"links": [
{
"href": "https://dfw.servers.api.rackspacecloud.com/v2/123456/servers/6cca7222-8ab5-4361-ac2c-d35eb0b78ab4",
"rel": "self"
},
{
"href": "https://dfw.servers.api.rackspacecloud.com/123456/servers/6cca7222-8ab5-4361-ac2c-d35eb0b78ab4",
"rel": "bookmark"
}
]
},
{
"id": "44764e46-9ab2-48ce-8512-f7691e0cd9d2",
"links": [
{
"href": "https://dfw.servers.api.rackspacecloud.com/v2/123456/servers/44764e46-9ab2-48ce-8512-f7691e0cd9d2",
"rel": "self"
},
{
"href": "https://dfw.servers.api.rackspacecloud.com/123456/servers/44764e46-9ab2-48ce-8512-f7691e0cd9d2",
"rel": "bookmark"
}
]
},
{
"id": "11a31131-9233-4dac-bcab-15ef06f6b939",
"links": [
{
"href": "https://dfw.servers.api.rackspacecloud.com/v2/123456/servers/11a31131-9233-4dac-bcab-15ef06f6b939",
"rel": "self"
},
{
"href": "https://dfw.servers.api.rackspacecloud.com/123456/servers/11a31131-9233-4dac-bcab-15ef06f6b939",
"rel": "bookmark"
}
]
}
],
"pendingCapacity": 0,
"activeCapacity": 3,
"name": "second-group"
},
"id": "e21c7d72-2faa-475a-a35c-8c51d9c66e01",
"links": [
{
"href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/e21c7d72-2faa-475a-a35c-8c51d9c66e01/",
"rel": "self"
}
]
},
{
"state": {
"status": "ACTIVE",
"desiredCapacity": 2,
"paused": false,
"active": [
{
"id": "f4ff054b-b78c-4123-98f4-7f0e343c64cd",
"links": [
{
"href": "https://dfw.servers.api.rackspacecloud.com/v2/123456/servers/f4ff054b-b78c-4123-98f4-7f0e343c64cd",
"rel": "self"
},
{
"href": "https://dfw.servers.api.rackspacecloud.com/123456/servers/f4ff054b-b78c-4123-98f4-7f0e343c64cd",
"rel": "bookmark"
}
]
},
{
"id": "c89cfdbf-e3fa-419b-844c-70c3e8016268",
"links": [
{
"href": "https://dfw.servers.api.rackspacecloud.com/v2/123456/servers/c89cfdbf-e3fa-419b-844c-70c3e8016268",
"rel": "self"
},
{
"href": "https://dfw.servers.api.rackspacecloud.com/123456/servers/c89cfdbf-e3fa-419b-844c-70c3e8016268",
"rel": "bookmark"
}
]
}
],
"pendingCapacity": 0,
"activeCapacity": 2,
"name": "third-group"
},
"id": "e34fa1e9-d0f4-47c1-9a01-e531204e1f25",
"links": [
{
"href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/e34fa1e9-d0f4-47c1-9a01-e531204e1f25/",
"rel": "self"
}
]
}
]
}
`

var (
// FirstGroup is a Group struct corresponding to the first result in GroupListBody.
FirstGroup = Group{
ID: "10eb3219-1b12-4b34-b1e4-e10ee4f24c65",
State: State{
Name: "first-group",
Status: ACTIVE,
DesiredCapacity: 2,
PendingCapacity: 0,
ActiveCapacity: 2,
Paused: false,
Active: []map[string]interface{}{
{
"id": "449cead0-48b2-44fe-9107-dea7cdb6d925",
"links": []map[string]string{
{
"href": "https://dfw.servers.api.rackspacecloud.com/v2/123456/servers/449cead0-48b2-44fe-9107-dea7cdb6d925",
"rel": "self",
},
{
"href": "https://dfw.servers.api.rackspacecloud.com/123456/servers/449cead0-48b2-44fe-9107-dea7cdb6d925",
"rel": "bookmark",
},
},
},
{
"id": "d8c2696f-1936-45c7-892d-f5f741ef0f60",
"links": []map[string]string{
{
"href": "https://dfw.servers.api.rackspacecloud.com/v2/123456/servers/d8c2696f-1936-45c7-892d-f5f741ef0f60",
"rel": "self",
},
{
"href": "https://dfw.servers.api.rackspacecloud.com/v2/123456/servers/d8c2696f-1936-45c7-892d-f5f741ef0f60",
"rel": "bookmark",
},
},
},
},
},
}
)

// HandleGroupListSuccessfully sets up the test server to respond to a group List request.
func HandleGroupListSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/groups", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)

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

r.ParseForm()
marker := r.Form.Get("marker")

switch marker {
case "":
fmt.Fprintf(w, GroupListBody)
case "e34fa1e9-d0f4-47c1-9a01-e531204e1f25":
fmt.Fprintf(w, `{ "servers": [] }`)
default:
t.Fatalf("/groups invoked with unexpected marker=[%s]", marker)
}
})
}
54 changes: 54 additions & 0 deletions rackspace/autoscale/v1/groups/requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package groups

import (
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)

// ListOptsBuilder allows extensions to add additional parameters to the
// List request.
type ListOptsBuilder interface {
ToGroupListQuery() (string, error)
}

// ListOpts allows the filtering and sorting of paginated collections through
// the API.
type ListOpts struct {
// UUID of the group at which you want to set a marker.
Marker string `q:"marker"`

// Integer value for the limit of values to return.
Limit int `q:"limit"`
}

// ToGroupListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToGroupListQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)

if err != nil {
return "", err
}

return q.String(), nil
}

// List returns all scaling groups.
func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := groupsURL(client)

if opts != nil {
query, err := opts.ToGroupListQuery()

if err != nil {
return pagination.Pager{Err: err}
}

url += query
}

createPageFn := func(r pagination.PageResult) pagination.Page {
return GroupPage{pagination.LinkedPageBase{PageResult: r}}
}

return pagination.NewPager(client, url, createPageFn)
}
40 changes: 40 additions & 0 deletions rackspace/autoscale/v1/groups/requests_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package groups

import (
"testing"

"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
"github.com/rackspace/gophercloud/testhelper/client"
)

func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleGroupListSuccessfully(t)

pages := 0
err := List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
pages++

groups, err := ExtractGroups(page)

if err != nil {
return false, err
}

if len(groups) != 3 {
t.Fatalf("Expected 3 groups, got %d", len(groups))
}

th.CheckDeepEquals(t, FirstGroup, groups[0])

return true, nil
})

th.AssertNoErr(t, err)

if pages != 1 {
t.Errorf("Expected 1 page, saw %d", pages)
}
}
Loading