Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding query parameter to Compute list services API #1904

Merged
merged 4 commits into from
Mar 24, 2020
Merged
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
31 changes: 30 additions & 1 deletion acceptance/openstack/compute/v2/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestServicesList(t *testing.T) {
client, err := clients.NewComputeV2Client()
th.AssertNoErr(t, err)

allPages, err := services.List(client).AllPages()
allPages, err := services.List(client, nil).AllPages()
th.AssertNoErr(t, err)

allServices, err := services.ExtractServices(allPages)
Expand All @@ -34,3 +34,32 @@ func TestServicesList(t *testing.T) {

th.AssertEquals(t, found, true)
}

func TestServicesListWithOpts(t *testing.T) {
clients.RequireAdmin(t)

client, err := clients.NewComputeV2Client()
th.AssertNoErr(t, err)

opts := services.ListOpts{
Binary: "nova-scheduler",
}

allPages, err := services.List(client, opts).AllPages()
th.AssertNoErr(t, err)

allServices, err := services.ExtractServices(allPages)
th.AssertNoErr(t, err)

var found bool
for _, service := range allServices {
tools.PrintResource(t, service)
th.AssertEquals(t, service.Binary, "nova-scheduler")

if service.Binary == "nova-scheduler" {
found = true
}
}

th.AssertEquals(t, found, true)
}
6 changes: 5 additions & 1 deletion openstack/compute/v2/extensions/services/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ cloud.

Example of Retrieving list of all services

allPages, err := services.List(computeClient).AllPages()
opts := services.ListOpts{
Binary: "nova-scheduler",
}

allPages, err := services.List(computeClient, opts).AllPages()
if err != nil {
panic(err)
}
Expand Down
31 changes: 29 additions & 2 deletions openstack/compute/v2/extensions/services/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,36 @@ import (
"github.com/gophercloud/gophercloud/pagination"
)

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

// ListOpts represents options to list services.
type ListOpts struct {
Binary string `q:"binary"`
Host string `q:"host"`
}

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

// List makes a request against the API to list services.
func List(client *gophercloud.ServiceClient) pagination.Pager {
return pagination.NewPager(client, listURL(client), func(r pagination.PageResult) pagination.Page {
func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := listURL(client)
if opts != nil {
query, err := opts.ToServicesListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}

return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return ServicePage{pagination.SinglePageBase(r)}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestListServicesPre253(t *testing.T) {
HandleListPre253Successfully(t)

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

actual, err := services.ExtractServices(page)
Expand Down Expand Up @@ -47,7 +47,11 @@ func TestListServices(t *testing.T) {
HandleListSuccessfully(t)

pages := 0
err := services.List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
opts := services.ListOpts{
Binary: "fake-binary",
Host: "host123",
}
err := services.List(client.ServiceClient(), opts).EachPage(func(page pagination.Page) (bool, error) {
pages++

actual, err := services.ExtractServices(page)
Expand Down