-
Notifications
You must be signed in to change notification settings - Fork 26
/
object.go
134 lines (112 loc) · 3.32 KB
/
object.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package infoblox
import (
"encoding/json"
"fmt"
"log"
"net/url"
)
// Object represents a WAPI object
type Object struct {
Ref string `json:"_ref"`
r *Resource
}
// Get fetches the Object from the Infoblox WAPI
func (o Object) Get(opts *Options) (map[string]interface{}, error) {
resp, err := o.get(opts)
if err != nil {
return nil, err
}
var out map[string]interface{}
err = resp.Parse(&out)
if err != nil {
return nil, fmt.Errorf("%+v", err)
}
return out, nil
}
func (o Object) get(opts *Options) (*APIResponse, error) {
q := o.r.getQuery(opts, []Condition{}, url.Values{})
resp, err := o.r.conn.SendRequest("GET", o.objectURI()+"?"+q.Encode(), "", nil)
if err != nil {
return nil, fmt.Errorf("Error sending request: %v", err)
}
return resp, nil
}
// Update updates the Object in the Infoblox WAPI
func (o Object) Update(data url.Values, opts *Options, body interface{}) (string, error) {
q := o.r.getQuery(opts, []Condition{}, data)
q.Set("_return_fields", "") //Force object response
var err error
head := make(map[string]string)
var bodyStr, urlStr string
if body == nil {
// Send URL-encoded data in the request body
urlStr = o.objectURI()
bodyStr = q.Encode()
head["Content-Type"] = "application/x-www-form-urlencoded"
} else {
// Put url-encoded data in the URL and send the body parameter as a JSON body.
bodyJSON, err := json.Marshal(body)
if err != nil {
return "", fmt.Errorf("Error creating request: %v", err)
}
log.Printf("PUT body: %s\n", bodyJSON)
urlStr = o.objectURI() + "?" + q.Encode()
bodyStr = string(bodyJSON)
head["Content-Type"] = "application/json"
}
resp, err := o.r.conn.SendRequest("PUT", urlStr, bodyStr, head)
if err != nil {
return "", fmt.Errorf("Error sending request: %v", err)
}
//fmt.Printf("%v", resp.ReadBody())
var responseData interface{}
var ret string
if err := resp.Parse(&responseData); err != nil {
return "", fmt.Errorf("%+v", err)
}
switch s := responseData.(type) {
case string:
ret = s
case map[string]interface{}:
ret = s["_ref"].(string)
default:
return "", fmt.Errorf("Invalid return type %T", s)
}
return ret, nil
}
// Delete deletes the Object from the Infoblox WAPI
func (o Object) Delete(opts *Options) error {
q := o.r.getQuery(opts, []Condition{}, url.Values{})
resp, err := o.r.conn.SendRequest("DELETE", o.objectURI()+"?"+q.Encode(), "", nil)
if err != nil {
return fmt.Errorf("Error sending request: %v", err)
}
//fmt.Printf("%v", resp.ReadBody())
var out interface{}
err = resp.Parse(&out)
if err != nil {
return fmt.Errorf("%+v", err)
}
return nil
}
// FunctionCall performs a function call on the Objects
func (o Object) FunctionCall(functionName string, jsonBody interface{}) (map[string]interface{}, error) {
data, err := json.Marshal(jsonBody)
if err != nil {
return nil, fmt.Errorf("Error sending request: %v", err)
}
resp, err := o.r.conn.SendRequest("POST", fmt.Sprintf("%s?_function=%s", o.objectURI(), functionName), string(data), map[string]string{"Content-Type": "application/json"})
if err != nil {
return nil, fmt.Errorf("Error sending request: %v", err)
}
//fmt.Printf("%v", resp.ReadBody())
var out map[string]interface{}
err = resp.Parse(&out)
if err != nil {
return nil, fmt.Errorf("%+v", err)
}
return out, nil
}
func (o Object) objectURI() string {
return BasePath + o.Ref
}