Skip to content

Commit

Permalink
go client updates to add & test hierarchy endpoints (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobsee authored Aug 7, 2023
1 parent b35d2fe commit 69af300
Show file tree
Hide file tree
Showing 4 changed files with 583 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/api/v1beta1/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
type User struct {
*models.User
Memberships []string `json:"memberships,omitempty"`
MembershipsDirect []string `json:"memberships_direct,omitempty"`
MembershipRequests []string `json:"membership_requests,omitempty"`
}

Expand Down
34 changes: 34 additions & 0 deletions pkg/client/governor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,40 @@ var (
"github_id": 10000001,
"github_username": "johnnyTog"
}
`)

testGroupHierarchiesResponse = []byte(`
[
{
"id": "31bcb9c0-95e0-4c78-b9af-8b998c8bd21c",
"parent_group_id": "186c5a52-4421-4573-8bbf-78d85d3c277e",
"parent_group_slug": "test-1",
"member_group_id": "f94c8cc2-375b-4043-863d-1dcd57ff60c7",
"member_group_slug": "test-2",
"expires_at": null
},
{
"id": "622b27f2-c1b6-4b91-aed7-784c8bf76736",
"parent_group_id": "f94c8cc2-375b-4043-863d-1dcd57ff60c7",
"parent_group_slug": "test-2",
"member_group_id": "fa606133-18f0-4ff4-b92e-d344398ed05b",
"member_group_slug": "test-3",
"expires_at": null
}
]
`)

testMemberGroupsResponse = []byte(`
[
{
"id": "31bcb9c0-95e0-4c78-b9af-8b998c8bd21c",
"parent_group_id": "186c5a52-4421-4573-8bbf-78d85d3c277e",
"parent_group_slug": "test-1",
"member_group_id": "f94c8cc2-375b-4043-863d-1dcd57ff60c7",
"member_group_slug": "test-2",
"expires_at": null
}
]
`)
)

Expand Down
176 changes: 176 additions & 0 deletions pkg/client/hierarchies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package client

import (
"bytes"
"context"
"fmt"
"io"
"net/http"

"github.com/goccy/go-json"
"github.com/metal-toolbox/governor-api/pkg/api/v1alpha1"
"github.com/volatiletech/null/v8"
)

// GroupHierarchies lists all hierarchical group relationships in governor
func (c *Client) GroupHierarchies(ctx context.Context) (*[]v1alpha1.GroupHierarchy, error) {
u := fmt.Sprintf("%s/api/%s/groups/hierarchies", c.url, governorAPIVersionAlpha)

req, err := c.newGovernorRequest(ctx, http.MethodGet, u)
if err != nil {
return nil, err
}

resp, err := c.httpClient.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, ErrRequestNonSuccess
}

out := []v1alpha1.GroupHierarchy{}
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
return nil, err
}

return &out, nil
}

// MemberGroups lists member groups of a parent group in governor
func (c *Client) MemberGroups(ctx context.Context, id string) (*[]v1alpha1.GroupHierarchy, error) {
if id == "" {
return nil, ErrMissingGroupID
}

u := fmt.Sprintf("%s/api/%s/groups/%s/hierarchies", c.url, governorAPIVersionAlpha, id)

req, err := c.newGovernorRequest(ctx, http.MethodGet, u)
if err != nil {
return nil, err
}

resp, err := c.httpClient.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, ErrRequestNonSuccess
}

out := []v1alpha1.GroupHierarchy{}
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
return nil, err
}

return &out, nil
}

// AddMemberGroup creates a new group hierarchy relationship in governor
func (c *Client) AddMemberGroup(ctx context.Context, parentGroupID, memberGroupID string, expiresAt null.Time) error {
if parentGroupID == "" || memberGroupID == "" {
return ErrNilGroupRequest
}

body := struct {
ExpiresAt null.Time `json:"expires_at"`
MemberGroupID string `json:"member_group_id"`
}{
ExpiresAt: expiresAt,
MemberGroupID: memberGroupID,
}

req, err := c.newGovernorRequest(ctx, http.MethodPost, fmt.Sprintf("%s/api/%s/groups/%s/hierarchies", c.url, governorAPIVersionAlpha, parentGroupID))
if err != nil {
return err
}

b, err := json.Marshal(body)
if err != nil {
return err
}

req.Body = io.NopCloser(bytes.NewBuffer(b))

resp, err := c.httpClient.Do(req.WithContext(ctx))
if err != nil {
return err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusAccepted && resp.StatusCode != http.StatusNoContent {
return ErrRequestNonSuccess
}

return nil
}

// UpdateMemberGroup updates the expiration on a group hierarchy relationship in governor
func (c *Client) UpdateMemberGroup(ctx context.Context, parentGroupID, memberGroupID string, expiresAt null.Time) error {
if parentGroupID == "" || memberGroupID == "" {
return ErrNilGroupRequest
}

body := struct {
ExpiresAt null.Time `json:"expires_at"`
}{
ExpiresAt: expiresAt,
}

req, err := c.newGovernorRequest(ctx, http.MethodPatch, fmt.Sprintf("%s/api/%s/groups/%s/hierarchies/%s", c.url, governorAPIVersionAlpha, parentGroupID, memberGroupID))
if err != nil {
return err
}

b, err := json.Marshal(body)
if err != nil {
return err
}

req.Body = io.NopCloser(bytes.NewBuffer(b))

resp, err := c.httpClient.Do(req.WithContext(ctx))
if err != nil {
return err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusAccepted && resp.StatusCode != http.StatusNoContent {
return ErrRequestNonSuccess
}

return nil
}

// DeleteMemberGroup deletes a group hierarchy relationship in governor
func (c *Client) DeleteMemberGroup(ctx context.Context, parentGroupID, memberGroupID string) error {
if parentGroupID == "" || memberGroupID == "" {
return ErrNilGroupRequest
}

req, err := c.newGovernorRequest(ctx, http.MethodDelete, fmt.Sprintf("%s/api/%s/groups/%s/hierarchies/%s", c.url, governorAPIVersionAlpha, parentGroupID, memberGroupID))
if err != nil {
return err
}

resp, err := c.httpClient.Do(req.WithContext(ctx))
if err != nil {
return err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusAccepted && resp.StatusCode != http.StatusNoContent {
return ErrRequestNonSuccess
}

return nil
}
Loading

0 comments on commit 69af300

Please sign in to comment.