diff --git a/members.go b/members.go new file mode 100644 index 0000000..6ce6c1c --- /dev/null +++ b/members.go @@ -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) +} diff --git a/organizations.go b/organizations.go index 80f2d32..c4e2c77 100644 --- a/organizations.go +++ b/organizations.go @@ -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 { diff --git a/packngo.go b/packngo.go index f3dcf2e..0932ab8 100644 --- a/packngo.go +++ b/packngo.go @@ -107,6 +107,7 @@ type Client struct { Facilities FacilityService HardwareReservations HardwareReservationService Invitations InvitationService + Members MemberService Metros MetroService Notifications NotificationService OperatingSystems OSService @@ -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}