Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #348 from packethost/members
Browse files Browse the repository at this point in the history
implement MembersService
  • Loading branch information
displague authored Sep 29, 2022
2 parents 3db8aff + 65e2686 commit 1d7ae3f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
66 changes: 66 additions & 0 deletions members.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package packngo

import "path"

// API documentation https://metal.equinix.com/developers/api#tag/Memberships
const membersBasePath = "/members"

// OrganizationService interface defines available organization methods
type MemberService interface {
List(string, *ListOptions) ([]Member, *Response, error)
Delete(string, string) (*Response, error)
}

type membersRoot struct {
Members []Member `json:"members"`
Meta meta `json:"meta"`
}

// Member is the returned from organization/id/members
type Member struct {
*Href `json:",inline"`
ID string `json:"id"`
Roles []string `json:"roles"`
ProjectsCount int `json:"projects_count"`
User User `json:"user"`
Organization Organization `json:"organization"`
Projects []Project `json:"projects"`
}

// MemberServiceOp implements MemberService
type MemberServiceOp struct {
client *Client
}

var _ MemberService = (*MemberServiceOp)(nil)

// List returns the members in an organization
func (s *MemberServiceOp) List(organizationID string, opts *ListOptions) (orgs []Member, resp *Response, err error) {
subset := new(membersRoot)
endpointPath := path.Join(organizationBasePath, organizationID, membersBasePath)
apiPathQuery := opts.WithQuery(endpointPath)

for {
resp, err = s.client.DoRequest("GET", apiPathQuery, nil, subset)
if err != nil {
return nil, resp, err
}

orgs = append(orgs, subset.Members...)

if apiPathQuery = nextPage(subset.Meta, opts); apiPathQuery != "" {
continue
}
return
}
}

// Delete removes the given member from the given organization
func (s *MemberServiceOp) Delete(organizationID, memberID string) (*Response, error) {
if validateErr := ValidateUUID(organizationID); validateErr != nil {
return nil, validateErr
}
apiPath := path.Join(organizationBasePath, organizationID, membersBasePath, memberID)

return s.client.DoRequest("DELETE", apiPath, nil, nil)
}
3 changes: 2 additions & 1 deletion organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ type Organization struct {
LogoThumb string `json:"logo_thumb,omitempty"`
Projects []Project `json:"projects,omitempty"`
URL string `json:"href,omitempty"`
Users []User `json:"members,omitempty"`
Members []Member `json:"members,omitempty"`
Owners []User `json:"owners,omitempty"`
PrimaryOwner User `json:"primary_owner,omitempty"`
}

func (o Organization) String() string {
Expand Down
2 changes: 2 additions & 0 deletions packngo.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ type Client struct {
Facilities FacilityService
HardwareReservations HardwareReservationService
Invitations InvitationService
Members MemberService
Metros MetroService
Notifications NotificationService
OperatingSystems OSService
Expand Down Expand Up @@ -378,6 +379,7 @@ func NewClientWithBaseURL(consumerToken string, apiKey string, httpClient *http.
c.Facilities = &FacilityServiceOp{client: c}
c.HardwareReservations = &HardwareReservationServiceOp{client: c}
c.Invitations = &InvitationServiceOp{client: c}
c.Members = &MemberServiceOp{client: c}
c.Metros = &MetroServiceOp{client: c}
c.Notifications = &NotificationServiceOp{client: c}
c.OperatingSystems = &OSServiceOp{client: c}
Expand Down

0 comments on commit 1d7ae3f

Please sign in to comment.