forked from ns1/ns1-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptiondef.go
90 lines (77 loc) · 2.56 KB
/
optiondef.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
package rest
import (
"errors"
"fmt"
"gopkg.in/ns1/ns1-go.v2/rest/model/dhcp"
"net/http"
)
// OptionDefService handles the 'scope group' endpoints.
type OptionDefService service
// List returns a list of all option definitions.
//
// NS1 API docs: https://ns1.com/api#getlist-dhcp-option-definitions
func (s *OptionDefService) List() ([]dhcp.OptionDef, *http.Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "dhcp/optiondef", nil)
if err != nil {
return nil, nil, err
}
ods := make([]dhcp.OptionDef, 0)
resp, err := s.client.Do(req, &ods)
return ods, resp, err
}
// Get returns the option definition corresponding to the provided ID.
//
// NS1 API docs: https://ns1.com/api#getview-dhcp-option-definition
func (s *OptionDefService) Get(odSpace, odKey string) (*dhcp.OptionDef, *http.Response, error) {
reqPath := fmt.Sprintf("dhcp/optiondef/%s/%s", odSpace, odKey)
req, err := s.client.NewRequest(http.MethodGet, reqPath, nil)
if err != nil {
return nil, nil, err
}
od := &dhcp.OptionDef{}
var resp *http.Response
resp, err = s.client.Do(req, od)
if err != nil {
return nil, resp, err
}
return od, resp, nil
}
// Create creates or updates an option definition.
// The FriendlyName, Description, Code, Schema.Type fields are required.
//
// NS1 API docs: https://ns1.com/api#putcreate-an-custom-dhcp-option-definition
func (s *OptionDefService) Create(od *dhcp.OptionDef, odSpace, odKey string) (*dhcp.OptionDef, *http.Response, error) {
switch {
case od.FriendlyName == "":
return nil, nil, errors.New("the FriendlyName field is required")
case od.Description == "":
return nil, nil, errors.New("the Description field is required")
case od.Code == 0:
return nil, nil, errors.New("the Code field is required")
case od.Schema.Type == "":
return nil, nil, errors.New("the Schema.Type field is required")
}
reqPath := fmt.Sprintf("dhcp/optiondef/%s/%s", odSpace, odKey)
req, err := s.client.NewRequest(http.MethodPut, reqPath, od)
if err != nil {
return nil, nil, err
}
respOd := new(dhcp.OptionDef)
var resp *http.Response
resp, err = s.client.Do(req, respOd)
if err != nil {
return nil, resp, err
}
return respOd, resp, nil
}
// Delete removes a option definition entirely.
//
// NS1 API docs: https://ns1.com/api#deletedelete-a-custom-dhcp-option-definition
func (s *OptionDefService) Delete(odSpace, odKey string) (*http.Response, error) {
reqPath := fmt.Sprintf("dhcp/optiondef/%s/%s", odSpace, odKey)
req, err := s.client.NewRequest(http.MethodDelete, reqPath, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}