Skip to content

Commit

Permalink
Merge pull request #227 from optik-aper/reserved-ip-update
Browse files Browse the repository at this point in the history
Add support for updating reserved IP label
  • Loading branch information
Oogy authored Jun 13, 2022
2 parents 2436364 + 5362ba0 commit 945cc0b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
22 changes: 22 additions & 0 deletions reserved_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const ripPath = "/v2/reserved-ips"
// Link : https://www.vultr.com/api/#tag/reserved-ip
type ReservedIPService interface {
Create(ctx context.Context, ripCreate *ReservedIPReq) (*ReservedIP, error)
Update(ctx context.Context, id string, ripUpdate *ReservedIPUpdateReq) (*ReservedIP, error)
Get(ctx context.Context, id string) (*ReservedIP, error)
Delete(ctx context.Context, id string) error
List(ctx context.Context, options *ListOptions) ([]ReservedIP, *Meta, error)
Expand Down Expand Up @@ -48,6 +49,11 @@ type ReservedIPReq struct {
InstanceID string `json:"instance_id,omitempty"`
}

// ReservedIPUpdateReq represents the parameters for updating a Reserved IP on Vultr
type ReservedIPUpdateReq struct {
Label *string `json:"label"`
}

type reservedIPsBase struct {
ReservedIPs []ReservedIP `json:"reserved_ips"`
Meta *Meta `json:"meta"`
Expand Down Expand Up @@ -78,6 +84,22 @@ func (r *ReservedIPServiceHandler) Create(ctx context.Context, ripCreate *Reserv
return rip.ReservedIP, nil
}

// Update updates label on the Reserved IP
func (r *ReservedIPServiceHandler) Update(ctx context.Context, id string, ripUpdate *ReservedIPUpdateReq) (*ReservedIP, error) {
uri := fmt.Sprintf("%s/%s", ripPath, id)
req, err := r.client.NewRequest(ctx, http.MethodPatch, uri, ripUpdate)
if err != nil {
return nil, err
}

rip := new(reservedIPBase)
if err = r.client.DoWithContext(ctx, req, rip); err != nil {
return nil, err
}

return rip.ReservedIP, nil
}

// Get gets the reserved IP associated with provided ID
func (r *ReservedIPServiceHandler) Get(ctx context.Context, id string) (*ReservedIP, error) {
uri := fmt.Sprintf("%s/%s", ripPath, id)
Expand Down
46 changes: 46 additions & 0 deletions reserved_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,52 @@ func TestReservedIPServiceHandler_Create(t *testing.T) {
}
}

func TestReservedIPServiceHandler_Update(t *testing.T) {
setup()
defer teardown()

options := &ReservedIPUpdateReq{
Label: StringToStringPtr("my first reserved ip updated"),
}

mux.HandleFunc("/v2/reserved-ips/12345", func(writer http.ResponseWriter, request *http.Request) {
response := `
{
"reserved_ip": {
"id": "12345",
"region": "yto",
"ip_type": "v4",
"subnet": "10.234.22.53",
"subnet_size": 32,
"label": "my first reserved ip updated",
"instance_id": "123456"
}
}
`
fmt.Fprintf(writer, response)
})

ip, err := client.ReservedIP.Update(ctx, "12345", options)

expected := &ReservedIP{
ID: "12345",
Region: "yto",
IPType: "v4",
Subnet: "10.234.22.53",
SubnetSize: 32,
Label: "my first reserved ip updated",
InstanceID: "123456",
}

if err != nil {
t.Errorf("ReservedIP.Update returned %+v, expected %+v", err, nil)
}

if !reflect.DeepEqual(ip, expected) {
t.Errorf("ReservedIP.Update returned %+v, expected %+v", ip, expected)
}
}

func TestReservedIPServiceHandler_Delete(t *testing.T) {
setup()
defer teardown()
Expand Down

0 comments on commit 945cc0b

Please sign in to comment.