forked from ns1/ns1-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreservation.go
114 lines (95 loc) · 2.81 KB
/
reservation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package rest
import (
"errors"
"fmt"
"net/http"
"gopkg.in/ns1/ns1-go.v2/rest/model/dhcp"
)
// ReservationService handles the 'reservation' endpoints.
type ReservationService service
// List returns a list of all reservations.
//
// NS1 API docs: https://ns1.com/api#getlist-reservations
func (s *ReservationService) List() ([]dhcp.Reservation, *http.Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "dhcp/reservation", nil)
if err != nil {
return nil, nil, err
}
scs := make([]dhcp.Reservation, 0)
resp, err := s.client.Do(req, &scs)
if err != nil {
return nil, resp, err
}
return scs, resp, nil
}
// Get returns the reservation corresponding to the provided reservation ID.
//
// NS1 API docs: https://ns1.com/api#getview-a-reservations-details
func (s *ReservationService) Get(scID int) (*dhcp.Reservation, *http.Response, error) {
reqPath := fmt.Sprintf("dhcp/reservation/%d", scID)
req, err := s.client.NewRequest(http.MethodGet, reqPath, nil)
if err != nil {
return nil, nil, err
}
sc := &dhcp.Reservation{}
var resp *http.Response
resp, err = s.client.Do(req, sc)
if err != nil {
return nil, resp, err
}
return sc, resp, nil
}
// Create creates a reservation.
// The Options field is required.
//
// NS1 API docs: https://ns1.com/api#putcreate-a-reservation
func (s *ReservationService) Create(sc *dhcp.Reservation) (*dhcp.Reservation, *http.Response, error) {
switch {
case sc.Options == nil:
return nil, nil, errors.New("the Options field is required")
}
req, err := s.client.NewRequest(http.MethodPut, "dhcp/reservation", sc)
if err != nil {
return nil, nil, err
}
respSc := new(dhcp.Reservation)
var resp *http.Response
resp, err = s.client.Do(req, respSc)
if err != nil {
return nil, resp, err
}
return respSc, resp, nil
}
// Edit updates an existing reservation.
// The ID, Options fields are required.
//
// NS1 API docs: https://ns1.com/api#postmodify-a-reservation
func (s *ReservationService) Edit(sc *dhcp.Reservation) (*dhcp.Reservation, *http.Response, error) {
switch {
case sc.ID == nil:
return nil, nil, errors.New("the ID field is required")
case sc.Options == nil:
return nil, nil, errors.New("the Options field is required")
}
reqPath := fmt.Sprintf("dhcp/reservation/%d", *sc.ID)
req, err := s.client.NewRequest(http.MethodPost, reqPath, sc)
if err != nil {
return nil, nil, err
}
resp, err := s.client.Do(req, sc)
if err != nil {
return nil, resp, err
}
return sc, resp, nil
}
// Delete removes a reservation entirely.
//
// NS1 API docs: https://ns1.com/api#deletedelete-a-reservation
func (s *ReservationService) Delete(id int) (*http.Response, error) {
reqPath := fmt.Sprintf("dhcp/reservation/%d", id)
req, err := s.client.NewRequest(http.MethodDelete, reqPath, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}