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

Commit

Permalink
implement Hrefer in OrganizationOps
Browse files Browse the repository at this point in the history
Signed-off-by: Marques Johansson <mjohansson@equinix.com>
  • Loading branch information
displague committed May 11, 2021
1 parent 6c74245 commit b4da9f7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion api_call_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func nextPage(meta meta, opts *GetOptions) (path string) {
if meta.Next != nil && (opts.GetPage() == 0) {
optsCopy := opts.CopyOrNew()
optsCopy.Page = meta.CurrentPageNum + 1
return optsCopy.WithQuery(stripQuery(meta.Next.Href))
return optsCopy.WithQuery(stripQuery(*meta.Next.Href))
}
if opts != nil {
opts.Meta = meta
Expand Down
4 changes: 2 additions & 2 deletions devices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func TestAccDeviceAssignGlobalIP(t *testing.T) {
}
t.Fatalf("assignment %s should be listed in device %s", assignment, d)

if assignment.AssignedTo.Href != d.Href {
if *assignment.AssignedTo.Href != d.Href {
t.Fatalf("device %s should be listed in assignment %s",
d, assignment)
}
Expand Down Expand Up @@ -549,7 +549,7 @@ func TestAccDeviceAssignIP(t *testing.T) {
}
t.Fatalf("assignment %s should be listed in device %s", assignment, d)

if assignment.AssignedTo.Href != d.Href {
if *assignment.AssignedTo.Href != d.Href {
t.Fatalf("device %s should be listed in assignment %s",
d, assignment)
}
Expand Down
3 changes: 2 additions & 1 deletion hardware_reservations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ func TestHardwareReservationServiceOp_List(t *testing.T) {
v.Meta.CurrentPageNum = page
if page < v.Meta.Total {
nextPage := page + 1
v.Meta.Next = &Href{Href: fmt.Sprintf("%s?page=%d", u.Path, nextPage)}
nextHref := fmt.Sprintf("%s?page=%d", u.Path, nextPage)
v.Meta.Next = &Href{Href: &nextHref}
}
return &Response{}, nil
}
Expand Down
33 changes: 28 additions & 5 deletions organizations.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package packngo

import (
"net/http"
"path"

"github.com/packethost/packngo/href"
)

// API documentation https://metal.equinix.com/developers/api/organizations/
Expand All @@ -25,6 +28,7 @@ type organizationsRoot struct {

// Organization represents an Equinix Metal organization
type Organization struct {
*Href `json:",inline"`
ID string `json:"id"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Expand All @@ -45,10 +49,19 @@ type Organization struct {
Owners []User `json:"owners,omitempty"`
}

func (o Organization) String() string {
func (o *Organization) String() string {
return Stringify(o)
}

func (o *Organization) SetHref(href string) {
o.Href = &Href{Href: &href}
}

func (o *Organization) SetID(id string) {
o.ID = id
o.Href = o.SetHref()
}

// OrganizationCreateRequest type used to create an Equinix Metal organization
type OrganizationCreateRequest struct {
Name string `json:"name"`
Expand Down Expand Up @@ -101,13 +114,23 @@ func (s *OrganizationServiceOp) List(opts *ListOptions) (orgs []Organization, re
}
}

func (s *OrganizationServiceOp) DefaultIncludes() []string {
return []string{}
}

func (s *OrganizationServiceOp) Hydrate(resource href.Hrefer, opts *GetOptions) (*Response, error) {
opts.Including(s.DefaultIncludes()...)
apiPathQuery := opts.WithQuery(resource.GetHref())

return s.client.DoRequest(http.MethodGet, apiPathQuery, nil, resource)
}

// Get returns a organization by id
func (s *OrganizationServiceOp) Get(organizationID string, opts *GetOptions) (*Organization, *Response, error) {
endpointPath := path.Join(organizationBasePath, organizationID)
apiPathQuery := opts.WithQuery(endpointPath)
organization := new(Organization)

resp, err := s.client.DoRequest("GET", apiPathQuery, nil, organization)
href := path.Join(organizationBasePath, organizationID)
organization.Href = &Href{Href: &href}
resp, err := s.Hydrate(organization, opts)
if err != nil {
return nil, resp, err
}
Expand Down
9 changes: 8 additions & 1 deletion packngo.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ type Response struct {

// Href is an API link
type Href struct {
Href string `json:"href"`
Href *string `json:"href,omitempty"`
}

func (h *Href) GetHref() string {
if h == nil {
return ""
}
return *h.Href
}

func (r *Response) populateRate() {
Expand Down

0 comments on commit b4da9f7

Please sign in to comment.