Skip to content

Commit

Permalink
updated list ipv6 response
Browse files Browse the repository at this point in the history
  • Loading branch information
imaskm committed Nov 27, 2024
1 parent d1b5da3 commit 9338200
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
28 changes: 14 additions & 14 deletions reserved_ipv6.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const reservedIPV6sBasePath = "v2/reserved_ipv6"
// ReservedIPV6sService is an interface for interfacing with the reserved IPV6s
// endpoints of the Digital Ocean API.
type ReservedIPV6sService interface {
List(context.Context, *ListOptions) ([]ReservedIPV6, *Response, error)
List(context.Context, *ListOptions) (*ReservedIPV6List, *Response, error)
Get(context.Context, string) (*ReservedIPV6, *Response, error)
Create(context.Context, *ReservedIPV6CreateRequest) (*ReservedIPV6, *Response, error)
Delete(context.Context, string) (*Response, error)
Expand All @@ -35,6 +35,12 @@ type ReservedIPV6 struct {
Droplet *Droplet `json:"droplet,omitempty"`
}

type ReservedIPV6List struct {
ReservedIPV6s []ReservedIPV6 `json:"reserved_ipv6s"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}

func (f ReservedIPV6) String() string {
return Stringify(f)
}
Expand All @@ -44,19 +50,13 @@ func (f ReservedIPV6) URN() string {
return ToURN(resourceV6Type, f.IP)
}

type reservedIPV6sRoot struct {
ReservedIPs []ReservedIPV6 `json:"reserved_ips"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}

// ReservedIPV6CreateRequest represents a request to reserve a reserved IP.
type ReservedIPV6CreateRequest struct {
Region string `json:"region_slug,omitempty"`
}

// List all reserved IPV6s.
func (r *ReservedIPV6sServiceOp) List(ctx context.Context, opt *ListOptions) ([]ReservedIPV6, *Response, error) {
func (r *ReservedIPV6sServiceOp) List(ctx context.Context, opt *ListOptions) (*ReservedIPV6List, *Response, error) {
path := reservedIPV6sBasePath
path, err := addOptions(path, opt)
if err != nil {
Expand All @@ -68,19 +68,19 @@ func (r *ReservedIPV6sServiceOp) List(ctx context.Context, opt *ListOptions) ([]
return nil, nil, err
}

root := new(reservedIPV6sRoot)
root := new(ReservedIPV6List)
resp, err := r.client.Do(ctx, req, root)
if err != nil {
return nil, nil, err
}
if l := root.Links; l != nil {
resp.Links = l
if root.Meta != nil {
resp.Meta = root.Meta
}
if m := root.Meta; m != nil {
resp.Meta = m
if root.Links != nil {
resp.Links = root.Links
}

return root.ReservedIPs, resp, err
return root, resp, err
}

// Get an individual reserved IPv6.
Expand Down
18 changes: 10 additions & 8 deletions reserved_ipv6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ func TestReservedIPV6s_List(t *testing.T) {

mux.HandleFunc("/v2/reserved_ipv6", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"reserved_ips": [
fmt.Fprint(w, `{"reserved_ipv6s": [
{"region_slug":"nyc3","droplet":{"id":1},"ip":"2604:a880:800:14::42c3:d000"},
{"region_slug":"nyc3","droplet":{"id":2},"ip":"2604:a880:800:14::42c3:d001"}
],
"meta":{"total":2}
"meta": {"total": 2}
}`)
})

Expand All @@ -71,19 +71,21 @@ func TestReservedIPV6s_List(t *testing.T) {
t.Errorf("ReservedIPs.List returned error: %v", err)
}

expectedReservedIPs := []ReservedIPV6{
{RegionSlug: "nyc3", Droplet: &Droplet{ID: 1}, IP: "2604:a880:800:14::42c3:d000"},
{RegionSlug: "nyc3", Droplet: &Droplet{ID: 2}, IP: "2604:a880:800:14::42c3:d001"},
expectedReservedIPs := ReservedIPV6List{
ReservedIPV6s: []ReservedIPV6{
{RegionSlug: "nyc3", Droplet: &Droplet{ID: 1}, IP: "2604:a880:800:14::42c3:d000"},
{RegionSlug: "nyc3", Droplet: &Droplet{ID: 2}, IP: "2604:a880:800:14::42c3:d001"},
},
}
if !reflect.DeepEqual(reservedIPs, expectedReservedIPs) {
if !reflect.DeepEqual(reservedIPs.ReservedIPV6s, expectedReservedIPs.ReservedIPV6s) {
t.Errorf("ReservedIPV6s.List returned reserved IPs %+v, expected %+v", reservedIPs, expectedReservedIPs)
}

expectedMeta := &Meta{
Total: 2,
}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("ReservedIPs.List returned meta %+v, expected %+v", resp.Meta, expectedMeta)
t.Errorf("ReservedIPV6s.List returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}

Expand All @@ -93,7 +95,7 @@ func TestReservedIPV6s_ListReservedIPsMultiplePages(t *testing.T) {

mux.HandleFunc("/v2/reserved_ipv6", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"reserved_ips": [
fmt.Fprint(w, `{"reserved_ipv6s": [
{"region_slug":"nyc3","droplet":{"id":1},"ip":"2604:a880:800:14::42c3:d001"},
{"region":{"slug":"nyc3"},"droplet":{"id":2},"ip":"2604:a880:800:14::42c3:d002"}],
"links":{"pages":{"next":"http://example.com/v2/reserved_ipv6/?page=2"}}}
Expand Down

0 comments on commit 9338200

Please sign in to comment.