Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated list ipv6 response #762

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions reserved_ipv6.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const reservedIPV6sBasePath = "v2/reserved_ipv6"
// endpoints of the Digital Ocean API.
type ReservedIPV6sService interface {
List(context.Context, *ListOptions) ([]ReservedIPV6, *Response, error)
Get(context.Context, string) (*ReservedIPV6, *Response, error)
Create(context.Context, *ReservedIPV6CreateRequest) (*ReservedIPV6, *Response, error)
Get(context.Context, string) (*ReservedIPV6Resp, *Response, error)
Create(context.Context, *ReservedIPV6CreateRequest) (*ReservedIPV6Resp, *Response, error)
Delete(context.Context, string) (*Response, error)
}

Expand All @@ -34,6 +34,15 @@ type ReservedIPV6 struct {
ReservedAt time.Time `json:"reserved_at"`
Droplet *Droplet `json:"droplet,omitempty"`
}
type ReservedIPV6Resp struct {
ReservedIPV6 *ReservedIPV6 `json:"reserved_ipv6"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 Hey Ashwani, Thanks for the PR 🚀.

Here are some suggestions you may consider:

  1. Could you please add Links to the ReservedIPV6Resp as it is done here? This will require some changes in Create. You can check reserved ips for reference.

  2. Could you also rename ReservedIPV6Resp to reservedIPV6Root for consistency with existing code? 🙏

  3. Let's return *ReservedIPV6 instead of *ReservedIPV6Resp for consistency with existing code? 🙏

  4. Let's try to reuse reservedIPV6Root in List as it is done here

Please, let me know when you are done. I'll be happy to take a look again 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@loosla

  1. We don't have any Links in Create and Get response for IPv6 API.
  2. Ok but *Root didn't make sense
  3. Ok
  4. for list it's plural reservedIPsRoot is being in IPv4

}

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,12 +53,6 @@ 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"`
Expand All @@ -68,31 +71,31 @@ 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.ReservedIPV6s, resp, err
}

// Get an individual reserved IPv6.
func (r *ReservedIPV6sServiceOp) Get(ctx context.Context, ip string) (*ReservedIPV6, *Response, error) {
func (r *ReservedIPV6sServiceOp) Get(ctx context.Context, ip string) (*ReservedIPV6Resp, *Response, error) {
path := fmt.Sprintf("%s/%s", reservedIPV6sBasePath, ip)

req, err := r.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

root := new(ReservedIPV6)
root := new(ReservedIPV6Resp)
resp, err := r.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
Expand All @@ -102,15 +105,15 @@ func (r *ReservedIPV6sServiceOp) Get(ctx context.Context, ip string) (*ReservedI
}

// Create a new IPv6
func (r *ReservedIPV6sServiceOp) Create(ctx context.Context, reserveRequest *ReservedIPV6CreateRequest) (*ReservedIPV6, *Response, error) {
func (r *ReservedIPV6sServiceOp) Create(ctx context.Context, reserveRequest *ReservedIPV6CreateRequest) (*ReservedIPV6Resp, *Response, error) {
path := reservedIPV6sBasePath

req, err := r.client.NewRequest(ctx, http.MethodPost, path, reserveRequest)
if err != nil {
return nil, nil, err
}

root := new(ReservedIPV6)
root := new(ReservedIPV6Resp)
resp, err := r.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
Expand Down
23 changes: 12 additions & 11 deletions reserved_ipv6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ func TestReservedIPV6s_Create(t *testing.T) {
t.Errorf("Request body = %+v, expected %+v", v, reserveRequest)
}

fmt.Fprint(w, `{"ip":"2604:a880:800:14::42c3:d000","region_slug":"nyc3","reserved_at":"`+nowTime.Format(time.RFC3339Nano)+`"}`)
fmt.Fprint(w, `{"reserved_ipv6":{"ip":"2604:a880:800:14::42c3:d000","region_slug":"nyc3","reserved_at":"`+nowTime.Format(time.RFC3339Nano)+`"}}`)
})

reservedIP, _, err := client.ReservedIPV6s.Create(ctx, reserveRequest)
if err != nil {
t.Errorf("ReservedIPV6s.Create returned error: %v", err)
}

expected := &ReservedIPV6{RegionSlug: "nyc3", IP: "2604:a880:800:14::42c3:d000", ReservedAt: nowTime}
expected := &ReservedIPV6Resp{ReservedIPV6: &ReservedIPV6{RegionSlug: "nyc3", IP: "2604:a880:800:14::42c3:d000", ReservedAt: nowTime}}

if !equalReserveIPv6Objects(reservedIP, expected) {
t.Errorf("ReservedIPs.Create returned %+v, expected %+v", reservedIP, expected)
if !equalReserveIPv6Objects(reservedIP.ReservedIPV6, expected.ReservedIPV6) {
t.Errorf("ReservedIPV6s.Create returned %+v, expected %+v", reservedIP, expected)
}
}

Expand All @@ -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 @@ -75,6 +75,7 @@ func TestReservedIPV6s_List(t *testing.T) {
{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) {
t.Errorf("ReservedIPV6s.List returned reserved IPs %+v, expected %+v", reservedIPs, expectedReservedIPs)
}
Expand All @@ -83,7 +84,7 @@ func TestReservedIPV6s_List(t *testing.T) {
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 +94,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 All @@ -114,16 +115,16 @@ func TestReservedIPV6s_Get(t *testing.T) {
nowTime := time.Now()
mux.HandleFunc("/v2/reserved_ipv6/2604:a880:800:14::42c3:d001", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"region_slug":"nyc3","droplet":{"id":1},"ip":"2604:a880:800:14::42c3:d001", "reserved_at":"`+nowTime.Format(time.RFC3339Nano)+`"}`)
fmt.Fprint(w, `{"reserved_ipv6":{"region_slug":"nyc3","droplet":{"id":1},"ip":"2604:a880:800:14::42c3:d001", "reserved_at":"`+nowTime.Format(time.RFC3339Nano)+`"}}`)
})

reservedIP, _, err := client.ReservedIPV6s.Get(ctx, "2604:a880:800:14::42c3:d001")
if err != nil {
t.Errorf("ReservedIPV6s.Get returned error: %v", err)
}

expected := &ReservedIPV6{RegionSlug: "nyc3", Droplet: &Droplet{ID: 1}, IP: "2604:a880:800:14::42c3:d001", ReservedAt: nowTime}
if !equalReserveIPv6Objects(reservedIP, expected) {
expected := &ReservedIPV6Resp{ReservedIPV6: &ReservedIPV6{RegionSlug: "nyc3", Droplet: &Droplet{ID: 1}, IP: "2604:a880:800:14::42c3:d001", ReservedAt: nowTime}}
if !equalReserveIPv6Objects(reservedIP.ReservedIPV6, expected.ReservedIPV6) {
t.Errorf("ReservedIPV6s.Get returned %+v, expected %+v", reservedIP, expected)
}
}
Expand Down
Loading