forked from ns1/ns1-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscopegroup.go
106 lines (89 loc) · 2.61 KB
/
scopegroup.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
package rest
import (
"errors"
"fmt"
"gopkg.in/ns1/ns1-go.v2/rest/model/dhcp"
"net/http"
)
// ScopeGroupService handles the 'scope group' endpoints.
type ScopeGroupService service
// List returns a list of all scope groups.
//
// NS1 API docs: https://ns1.com/api#getlist-scope-groups
func (s *ScopeGroupService) List() ([]dhcp.ScopeGroup, *http.Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "dhcp/scopegroup", nil)
if err != nil {
return nil, nil, err
}
sgs := make([]dhcp.ScopeGroup, 0)
resp, err := s.client.Do(req, &sgs)
return sgs, resp, err
}
// Get returns the Scope Group corresponding to the provided scope group ID.
//
// NS1 API docs: https://ns1.com/api#getview-scope-group
func (s *ScopeGroupService) Get(sgID int) (*dhcp.ScopeGroup, *http.Response, error) {
reqPath := fmt.Sprintf("dhcp/scopegroup/%d", sgID)
req, err := s.client.NewRequest(http.MethodGet, reqPath, nil)
if err != nil {
return nil, nil, err
}
sg := &dhcp.ScopeGroup{}
var resp *http.Response
resp, err = s.client.Do(req, sg)
if err != nil {
return nil, resp, err
}
return sg, resp, nil
}
// Create creates a scope group.
// The Name field is required.
//
// NS1 API docs: https://ns1.com/api#putcreate-a-scope-group
func (s *ScopeGroupService) Create(sg *dhcp.ScopeGroup) (*dhcp.ScopeGroup, *http.Response, error) {
switch {
case sg.Name == "":
return nil, nil, errors.New("the Name field is required")
}
req, err := s.client.NewRequest(http.MethodPut, "dhcp/scopegroup", sg)
if err != nil {
return nil, nil, err
}
respSg := new(dhcp.ScopeGroup)
var resp *http.Response
resp, err = s.client.Do(req, respSg)
if err != nil {
return nil, resp, err
}
return respSg, resp, nil
}
// Edit updates an existing scope group.
// The ID field is required.
//
// NS1 API docs: https://ns1.com/api#postedit-scope-group
func (s *ScopeGroupService) Edit(sg *dhcp.ScopeGroup) (*dhcp.ScopeGroup, *http.Response, error) {
if sg.ID == nil {
return nil, nil, errors.New("the ID field is required")
}
reqPath := fmt.Sprintf("dhcp/scopegroup/%d", *sg.ID)
req, err := s.client.NewRequest(http.MethodPost, reqPath, sg)
if err != nil {
return nil, nil, err
}
resp, err := s.client.Do(req, sg)
if err != nil {
return nil, resp, err
}
return sg, resp, nil
}
// Delete removes a Scope Group entirely.
//
// NS1 API docs: https://ns1.com/api#deleteremove-scope-group-by-id
func (s *ScopeGroupService) Delete(id int) (*http.Response, error) {
reqPath := fmt.Sprintf("dhcp/scopegroup/%d", id)
req, err := s.client.NewRequest(http.MethodDelete, reqPath, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}