Skip to content

Commit

Permalink
Get all pages of services from query (#159)
Browse files Browse the repository at this point in the history
* Get all pages of services from query

* Update test
  • Loading branch information
AlexisJasso authored Jul 1, 2024
1 parent 5efad08 commit 56b8ace
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.9.2 (Unreleased)

BUG FIXES:
* `firehydrant_services` data source pulls all services correctly

## 0.9.1

BUG FIXES:
Expand Down
26 changes: 18 additions & 8 deletions firehydrant/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,24 @@ func (c *RESTServicesClient) Get(ctx context.Context, id string) (*ServiceRespon
func (c *RESTServicesClient) List(ctx context.Context, req *ServiceQuery) (*ServicesResponse, error) {
servicesResponse := &ServicesResponse{}
apiError := &APIError{}
response, err := c.restClient().Get("services").QueryStruct(req).Receive(servicesResponse, apiError)
if err != nil {
return nil, errors.Wrap(err, "could not get services")
}

err = checkResponseStatusCode(response, apiError)
if err != nil {
return nil, err
curPage := 1

for {
req.Page = curPage
response, err := c.restClient().Get("services").QueryStruct(req).Receive(servicesResponse, apiError)
if err != nil {
return nil, errors.Wrap(err, "could not get services")
}

err = checkResponseStatusCode(response, apiError)
if err != nil {
return nil, err
}

if servicesResponse.Pagination == nil || servicesResponse.Pagination.Last == curPage {
break
}
curPage += 1
}

return servicesResponse, nil
Expand Down
2 changes: 1 addition & 1 deletion firehydrant/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestGetServices(t *testing.T) {
t.Fatalf("Received error hitting ping endpoint: %s", err.Error())
}

if expected := "/services?labels=key1%3Dval1%2Ckey2%3Dval2&query=hello-world"; expected != requestPathRcvd {
if expected := "/services?labels=key1%3Dval1%2Ckey2%3Dval2&page=1&query=hello-world"; expected != requestPathRcvd {
t.Fatalf("Expected %s, Got: %s for request path", expected, requestPathRcvd)
}
}
4 changes: 3 additions & 1 deletion firehydrant/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ type ServiceQuery struct {
Query string `url:"query,omitempty"`
ServiceTier int `url:"int,service_tier,omitempty"`
LabelsSelector LabelsSelector `url:"labels,omitempty"`
Page int `url:"page,omitempty"`
}

type LabelsSelector map[string]string
Expand Down Expand Up @@ -146,7 +147,8 @@ var _ query.Encoder = LabelsSelector{}

// ServicesResponse is the payload for retrieving a list of services
type ServicesResponse struct {
Services []ServiceResponse `json:"data"`
Services []ServiceResponse `json:"data"`
Pagination *Pagination `json:"pagination,omitempty"`
}

// UserResponse is the payload for a user
Expand Down

0 comments on commit 56b8ace

Please # to comment.