-
Notifications
You must be signed in to change notification settings - Fork 26
/
search.go
72 lines (63 loc) · 1.37 KB
/
search.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
package infoblox
import (
"encoding/json"
"errors"
"io/ioutil"
)
// https://102.168.2.200/wapidoc/objects/record.a.html
func (c *Client) GlobalSearch() *Resource {
return &Resource{
conn: c,
wapiObject: "search",
}
}
type SearchObject struct {
Object
Comment string `json:"comment,omitempty"`
Ipv4Addr string `json:"ipv4addr,omitempty"`
Name string `json:"name,omitempty"`
Ttl int `json:"ttl,omitempty"`
View string `json:"view,omitempty"`
}
func (c *Client) SearchObject(ref string) *RecordAObject {
a := RecordAObject{}
a.Object = Object{
Ref: ref,
r: c.RecordA(),
}
return &a
}
func (c *Client) Search(name string, objtype string) (string, error) {
field := "search_string"
conditions := []Condition{
Condition{
Field: &field,
Modifiers: "~",
Value: name,
},
}
if objtype != "" {
objtypestring := "objtype"
conditions = append(conditions, Condition{
Field: &objtypestring,
Value: objtype,
})
}
resp, respErr := c.GlobalSearch().find(conditions, nil)
if respErr != nil {
return "", respErr
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
if resp.StatusCode == 400 {
var err APIErrorResponse
if err2 := json.Unmarshal(body, &err); err2 != nil {
return "", err2
}
return "", errors.New(err.Text)
}
return string(body[:]), nil
}