Skip to content

Commit

Permalink
modified List function for Groups to do the pagination and return all…
Browse files Browse the repository at this point in the history
… groups
  • Loading branch information
Caleb Washburn committed Jun 7, 2018
1 parent 15771b6 commit a52f2fc
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 49 deletions.
60 changes: 40 additions & 20 deletions groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ type Group struct {
// PaginatedGroupList is the response from the API for a single page of groups.
type PaginatedGroupList struct {
Resources []Group `json:"resources"`
StartIndex int32 `json:"startIndex"`
ItemsPerPage int32 `json:"itemsPerPage"`
TotalResults int32 `json:"totalResults"`
StartIndex int `json:"startIndex"`
ItemsPerPage int `json:"itemsPerPage"`
TotalResults int `json:"totalResults"`
Schemas []string `json:"schemas"`
}

Expand Down Expand Up @@ -82,19 +82,40 @@ func (gm GroupManager) GetByName(name, attributes string) (Group, error) {
}

filter := fmt.Sprintf(`displayName eq "%v"`, name)
groups, err := gm.List(filter, "", attributes, "", 0, 0)
groups, err := gm.List(filter, "", attributes, "")
if err != nil {
return Group{}, err
}
if len(groups.Resources) == 0 {
if len(groups) == 0 {
return Group{}, fmt.Errorf("group %v not found", name)
}
return groups.Resources[0], nil
return groups[0], nil
}

func getGroupPage(gm GroupManager, query url.Values, startIndex, count int) (PaginatedGroupList, error) {
if startIndex != 0 {
query.Add("startIndex", strconv.Itoa(startIndex))
}
if count != 0 {
query.Add("count", strconv.Itoa(count))
}

bytes, err := AuthenticatedRequestor{}.Get(gm.HTTPClient, gm.Config, groupResource, query.Encode())
if err != nil {
return PaginatedGroupList{}, err
}

groupList := PaginatedGroupList{}
err = json.Unmarshal(bytes, &groupList)
if err != nil {
return PaginatedGroupList{}, parseError(groupResource, bytes)
}
return groupList, nil
}

// List groups
// http://docs.cloudfoundry.org/api/uaa/version/4.14.0/index.html#list-4.
func (gm GroupManager) List(filter, sortBy, attributes string, sortOrder SortOrder, startIdx, count int) (PaginatedGroupList, error) {
func (gm GroupManager) List(filter, sortBy, attributes string, sortOrder SortOrder) ([]Group, error) {
query := url.Values{}
if filter != "" {
query.Add("filter", filter)
Expand All @@ -105,28 +126,27 @@ func (gm GroupManager) List(filter, sortBy, attributes string, sortOrder SortOrd
if sortBy != "" {
query.Add("sortBy", sortBy)
}
if count != 0 {
query.Add("count", strconv.Itoa(count))
}
if startIdx != 0 {
query.Add("startIndex", strconv.Itoa(startIdx))
}
if sortOrder != "" {
query.Add("sortOrder", string(sortOrder))
}

bytes, err := AuthenticatedRequestor{}.Get(gm.HTTPClient, gm.Config, groupResource, query.Encode())
results, err := getGroupPage(gm, query, 0, 0)
if err != nil {
return PaginatedGroupList{}, err
return []Group{}, err
}

groupsResp := PaginatedGroupList{}
err = json.Unmarshal(bytes, &groupsResp)
if err != nil {
return PaginatedGroupList{}, parseError(groupResource, bytes)
groupList := results.Resources
startIndex, count := results.StartIndex, results.ItemsPerPage
for results.TotalResults > len(groupList) {
startIndex += count
newResults, err := getGroupPage(gm, query, startIndex, count)
if err != nil {
return []Group{}, err
}
groupList = append(groupList, newResults.Resources...)
}

return groupsResp, err
return groupList, nil
}

// Create the given group
Expand Down
59 changes: 30 additions & 29 deletions groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ var _ = Describe("Groups", func() {
ghttp.VerifyHeaderKV("Authorization", "bearer access_token"),
))

resp, err := gm.List(`id eq "fb5f32e1-5cb3-49e6-93df-6df9c8c8bd7"`, "", "", "", 0, 0)
groupList, err := gm.List(`id eq "fb5f32e1-5cb3-49e6-93df-6df9c8c8bd7"`, "", "", "")

Expect(err).NotTo(HaveOccurred())
Expect(resp.Resources[0].DisplayName).To(Equal("uaa.admin"))
Expect(resp.Resources[1].DisplayName).To(Equal("cloud_controller.read"))
Expect(groupList[0].DisplayName).To(Equal("uaa.admin"))
Expect(groupList[1].DisplayName).To(Equal("cloud_controller.read"))
})

It("gets all groups when no filter is passed", func() {
Expand All @@ -172,11 +172,11 @@ var _ = Describe("Groups", func() {
ghttp.VerifyRequest("GET", "/Groups", ""),
))

resp, err := gm.List("", "", "", "", 0, 0)
groupList, err := gm.List("", "", "", "")

Expect(err).NotTo(HaveOccurred())
Expect(resp.Resources[0].DisplayName).To(Equal("uaa.admin"))
Expect(resp.Resources[1].DisplayName).To(Equal("cloud_controller.read"))
Expect(groupList[0].DisplayName).To(Equal("uaa.admin"))
Expect(groupList[1].DisplayName).To(Equal("cloud_controller.read"))
})

It("can accept an attributes list", func() {
Expand All @@ -185,10 +185,10 @@ var _ = Describe("Groups", func() {
ghttp.VerifyRequest("GET", "/Groups", "filter=id+eq+%22fb5f32e1-5cb3-49e6-93df-6df9c8c8bd7%22&attributes=displayName"),
))

resp, err := gm.List(`id eq "fb5f32e1-5cb3-49e6-93df-6df9c8c8bd7"`, "", "displayName", "", 0, 0)
groupList, err := gm.List(`id eq "fb5f32e1-5cb3-49e6-93df-6df9c8c8bd7"`, "", "displayName", "")
Expect(err).NotTo(HaveOccurred())
Expect(resp.Resources[0].DisplayName).To(Equal("uaa.admin"))
Expect(resp.Resources[1].DisplayName).To(Equal("cloud_controller.read"))
Expect(groupList[0].DisplayName).To(Equal("uaa.admin"))
Expect(groupList[1].DisplayName).To(Equal("cloud_controller.read"))
})

It("can accept sortBy", func() {
Expand All @@ -197,17 +197,7 @@ var _ = Describe("Groups", func() {
ghttp.VerifyRequest("GET", "/Groups", "sortBy=displayName"),
))

_, err := gm.List("", "displayName", "", "", 0, 0)
Expect(err).NotTo(HaveOccurred())
})

It("can accept count", func() {
uaaServer.RouteToHandler("GET", "/Groups", ghttp.CombineHandlers(
ghttp.RespondWith(http.StatusOK, groupListResponse),
ghttp.VerifyRequest("GET", "/Groups", "count=10"),
))

_, err := gm.List("", "", "", "", 0, 10)
_, err := gm.List("", "displayName", "", "")
Expect(err).NotTo(HaveOccurred())
})

Expand All @@ -217,18 +207,29 @@ var _ = Describe("Groups", func() {
ghttp.VerifyRequest("GET", "/Groups", "sortOrder=ascending"),
))

_, err := gm.List("", "", "", SortAscending, 0, 0)
_, err := gm.List("", "", "", SortAscending)
Expect(err).NotTo(HaveOccurred())
})

It("can accept startIndex", func() {
uaaServer.RouteToHandler("GET", "/Groups", ghttp.CombineHandlers(
ghttp.RespondWith(http.StatusOK, groupListResponse),
ghttp.VerifyRequest("GET", "/Groups", "startIndex=10"),
))
It("can retrieve multiple pages", func() {
page1 := MultiPaginatedResponse(1, 1, 2, Group{DisplayName: "uaa.admin"})
page2 := MultiPaginatedResponse(2, 1, 2, Group{DisplayName: "cloud_controller.read"})
uaaServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.RespondWith(http.StatusOK, page1),
ghttp.VerifyRequest("GET", "/Groups", ""),
),
ghttp.CombineHandlers(
ghttp.RespondWith(http.StatusOK, page2),
ghttp.VerifyRequest("GET", "/Groups", "count=1&startIndex=2"),
),
)

groupList, err := gm.List("", "", "", "")

_, err := gm.List("", "", "", "", 10, 0)
Expect(err).NotTo(HaveOccurred())
Expect(groupList[0].DisplayName).To(Equal("uaa.admin"))
Expect(groupList[1].DisplayName).To(Equal("cloud_controller.read"))
})

It("returns an error when /Groups doesn't respond", func() {
Expand All @@ -239,7 +240,7 @@ var _ = Describe("Groups", func() {
ghttp.VerifyHeaderKV("Authorization", "bearer access_token"),
))

_, err := gm.List(`id eq "fb5f32e1-5cb3-49e6-93df-6df9c8c8bd7"`, "", "", "", 0, 0)
_, err := gm.List(`id eq "fb5f32e1-5cb3-49e6-93df-6df9c8c8bd7"`, "", "", "")

Expect(err).To(HaveOccurred())
})
Expand All @@ -252,7 +253,7 @@ var _ = Describe("Groups", func() {
ghttp.VerifyHeaderKV("Authorization", "bearer access_token"),
))

_, err := gm.List(`id eq "fb5f32e1-5cb3-49e6-93df-6df9c8c8bd7"`, "", "", "", 0, 0)
_, err := gm.List(`id eq "fb5f32e1-5cb3-49e6-93df-6df9c8c8bd7"`, "", "", "")

Expect(err).To(HaveOccurred())
})
Expand Down

0 comments on commit a52f2fc

Please sign in to comment.