-
Notifications
You must be signed in to change notification settings - Fork 1
/
fixed_address.go
114 lines (96 loc) · 3 KB
/
fixed_address.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 infoblox
import (
"fmt"
"net/http"
)
const (
fixedAddressBasePath = "fixedaddress"
fixedAddressReturnFields = "extattrs,ipv4addr,network_view,disable,comment,name,match_client,mac,network"
)
// GetFixedAddressByRef gets fixed address by reference
func (c *Client) GetFixedAddressByRef(ref string, queryParams map[string]string) (FixedAddress, error) {
var ret FixedAddress
if queryParams == nil {
queryParams = map[string]string{
"_return_fields": fixedAddressReturnFields,
}
} else {
queryParams["_return_fields"] = fixedAddressReturnFields
}
queryParamString := c.BuildQuery(queryParams)
request, err := c.CreateJSONRequest(http.MethodGet, fmt.Sprintf("%s?%s", ref, queryParamString), nil)
if err != nil {
return ret, err
}
response := c.Call(request, &ret)
if response != nil {
return ret, fmt.Errorf(response.ErrorMessage)
}
return ret, nil
}
// GetFixedAddressByQuery gets fixed address by query parameters
func (c *Client) GetFixedAddressByQuery(queryParams map[string]string) ([]FixedAddress, error) {
var ret FixedAddressQueryResult
queryParams["_return_fields"] = fixedAddressReturnFields
queryParams["_return_as_object"] = "1"
queryParams["_paging"] = "0"
queryParams["_max_results"] = "2"
queryParamString := c.BuildQuery(queryParams)
request, err := c.CreateJSONRequest(http.MethodGet, fmt.Sprintf("%s?%s", fixedAddressBasePath, queryParamString), nil)
if err != nil {
return nil, err
}
response := c.Call(request, &ret)
if response != nil {
return nil, fmt.Errorf(response.ErrorMessage)
}
return ret.Results, nil
}
// CreateFixedAddress creates fixed address
func (c *Client) CreateFixedAddress(fixedAddress *FixedAddress) error {
queryParams := map[string]string{
"_return_fields": fixedAddressReturnFields,
}
queryParamString := c.BuildQuery(queryParams)
request, err := c.CreateJSONRequest(http.MethodPost, fmt.Sprintf("%s?%s", fixedAddressBasePath, queryParamString), fixedAddress)
if err != nil {
return err
}
response := c.Call(request, &fixedAddress)
if response != nil {
return fmt.Errorf(response.ErrorMessage)
}
return nil
}
// UpdateFixedAddress creates fixed address
func (c *Client) UpdateFixedAddress(ref string, fixedAddress FixedAddress) (FixedAddress, error) {
var ret FixedAddress
queryParams := map[string]string{
"_return_fields": fixedAddressReturnFields,
}
queryParamString := c.BuildQuery(queryParams)
request, err := c.CreateJSONRequest(http.MethodPut, fmt.Sprintf("%s?%s", ref, queryParamString), fixedAddress)
if err != nil {
return ret, err
}
response := c.Call(request, &ret)
if response != nil {
return ret, fmt.Errorf(response.ErrorMessage)
}
return ret, nil
}
// DeleteFixedAddress creates fixed address
func (c *Client) DeleteFixedAddress(ref string) error {
request, err := c.CreateJSONRequest(http.MethodDelete, fmt.Sprintf("%s", ref), nil)
if err != nil {
return err
}
response := c.Call(request, nil)
if response != nil {
if response.StatusCode == 404 {
return nil
}
return err
}
return nil
}