-
Notifications
You must be signed in to change notification settings - Fork 1
/
container.go
112 lines (95 loc) · 2.81 KB
/
container.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
package infoblox
import (
"fmt"
"net/http"
)
const (
containerBasePath = "networkcontainer"
containerReturnFields = "comment,network,network_view,extattrs"
)
// GetContainerByRef gets A record by reference
func (c *Client) GetContainerByRef(ref string, queryParams map[string]string) (NetworkContainer, error) {
var ret NetworkContainer
if queryParams == nil {
queryParams = map[string]string{
"_return_fields": containerReturnFields,
}
} else {
queryParams["_return_fields"] = containerReturnFields
}
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
}
// GetContainerByQuery gets A records by query parameters
func (c *Client) GetContainerByQuery(queryParams map[string]string) ([]NetworkContainer, error) {
var ret []NetworkContainer
queryParams["_return_fields"] = containerReturnFields
queryParams["_paging"] = "0"
queryParams["_max_results"] = "2"
queryParamString := c.BuildQuery(queryParams)
request, err := c.CreateJSONRequest(http.MethodGet, fmt.Sprintf("%s?%s", containerBasePath, queryParamString), nil)
if err != nil {
return nil, err
}
response := c.Call(request, &ret)
if response != nil {
return nil, fmt.Errorf(response.ErrorMessage)
}
return ret, nil
}
// CreateContainer creates A record
func (c *Client) CreateContainer(record *NetworkContainer) error {
queryParams := map[string]string{
"_return_fields": containerReturnFields,
}
queryParamString := c.BuildQuery(queryParams)
request, err := c.CreateJSONRequest(http.MethodPost, fmt.Sprintf("%s?%s", containerBasePath, queryParamString), record)
if err != nil {
return err
}
response := c.Call(request, &record)
if response != nil {
return fmt.Errorf(response.ErrorMessage)
}
return nil
}
// UpdateContainer creates A record
func (c *Client) UpdateContainer(ref string, network NetworkContainer) (NetworkContainer, error) {
var ret NetworkContainer
queryParams := map[string]string{
"_return_fields": containerReturnFields,
}
queryParamString := c.BuildQuery(queryParams)
request, err := c.CreateJSONRequest(http.MethodPut, fmt.Sprintf("%s?%s", ref, queryParamString), network)
if err != nil {
return ret, err
}
response := c.Call(request, &ret)
if response != nil {
return ret, fmt.Errorf(response.ErrorMessage)
}
return ret, nil
}
// DeleteContainer creates A record
func (c *Client) DeleteContainer(ref string) error {
request, err := c.CreateJSONRequest(http.MethodDelete, 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
}