From b4da9f79d4b7d3b3701613291ace44944c4c3e5f Mon Sep 17 00:00:00 2001 From: Marques Johansson Date: Mon, 10 May 2021 15:25:45 -0400 Subject: [PATCH] implement Hrefer in OrganizationOps Signed-off-by: Marques Johansson --- api_call_options.go | 2 +- devices_test.go | 4 ++-- hardware_reservations_test.go | 3 ++- organizations.go | 33 ++++++++++++++++++++++++++++----- packngo.go | 9 ++++++++- 5 files changed, 41 insertions(+), 10 deletions(-) diff --git a/api_call_options.go b/api_call_options.go index 381b5b84..bb6ac836 100644 --- a/api_call_options.go +++ b/api_call_options.go @@ -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 diff --git a/devices_test.go b/devices_test.go index 4b7a7d6d..40f91dc5 100644 --- a/devices_test.go +++ b/devices_test.go @@ -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) } @@ -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) } diff --git a/hardware_reservations_test.go b/hardware_reservations_test.go index bc386cd8..980fd178 100644 --- a/hardware_reservations_test.go +++ b/hardware_reservations_test.go @@ -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 } diff --git a/organizations.go b/organizations.go index 07b0d6bc..e12400a9 100644 --- a/organizations.go +++ b/organizations.go @@ -1,7 +1,10 @@ package packngo import ( + "net/http" "path" + + "github.com/packethost/packngo/href" ) // API documentation https://metal.equinix.com/developers/api/organizations/ @@ -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"` @@ -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"` @@ -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 } diff --git a/packngo.go b/packngo.go index d9b2ef33..d6660ee5 100644 --- a/packngo.go +++ b/packngo.go @@ -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() {