Skip to content

Commit

Permalink
updated create/get response
Browse files Browse the repository at this point in the history
  • Loading branch information
imaskm committed Nov 27, 2024
1 parent 9338200 commit 35b3a27
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
15 changes: 9 additions & 6 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) (*ReservedIPV6List, *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,9 @@ type ReservedIPV6 struct {
ReservedAt time.Time `json:"reserved_at"`
Droplet *Droplet `json:"droplet,omitempty"`
}
type ReservedIPV6Resp struct {
ReservedIPV6 *ReservedIPV6 `json:"reserved_ipv6"`
}

type ReservedIPV6List struct {
ReservedIPV6s []ReservedIPV6 `json:"reserved_ipv6s"`
Expand Down Expand Up @@ -84,15 +87,15 @@ func (r *ReservedIPV6sServiceOp) List(ctx context.Context, opt *ListOptions) (*R
}

// 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
14 changes: 7 additions & 7 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 Down Expand Up @@ -116,16 +116,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

0 comments on commit 35b3a27

Please sign in to comment.