/
rpc-collect-xml.go
151 lines (133 loc) · 4.18 KB
/
rpc-collect-xml.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package api
import (
"bytes"
"encoding/xml"
"io"
"net/http"
"strconv"
"strings"
)
// XMLObject represents an XML object
type XMLObject struct {
XMLName xml.Name `xml:"xml-object"`
Name string `xml:"name,attr"`
Type string `xml:"type,attr"`
XPath string `xml:"xpath,attr"`
}
// XMLResourceKey represents a resource key list inside a group
type XMLResourceKey struct {
XMLName xml.Name `xml:"resource-key"`
KeyXPaths []string `xml:"key-xpath"`
}
// XMLGroup represents an XML group
type XMLGroup struct {
XMLName xml.Name `xml:"xml-group"`
Name string `xml:"name,attr"`
ResourceType string `xml:"resource-type,attr"`
ResourceXPath string `xml:"resource-xpath,attr"`
KeyXPath string `xml:"key-xpath,attr"`
TimestampFormat string `xml:"timestamp-format,attr"`
TimestampXPath string `xml:"timestamp-xpath,attr"`
Objects []XMLObject `xml:"xml-object,omitempty"`
ResourceKey *XMLResourceKey `xml:"resource-key,omitempty"`
}
// HasMultipleResourceKeys checks if the group has multiple resource keys
func (group *XMLGroup) HasMultipleResourceKeys() bool {
return group.ResourceXPath != "" && group.ResourceKey != nil && len(group.ResourceKey.KeyXPaths) > 0
}
// XMLRequestParameter represents an XML request parameter
type XMLRequestParameter struct {
XMLName xml.Name `xml:"parameter"`
Name string `xml:"name,attr"`
Value string `xml:"value,attr"`
}
// XMLRequestHeader represents an XML request header
type XMLRequestHeader struct {
XMLName xml.Name `xml:"header"`
Name string `xml:"name,attr"`
Value string `xml:"value,attr"`
}
// XMLRequestContent represents an XML request content
type XMLRequestContent struct {
XMLName xml.Name `xml:"content"`
Type string `xml:"content-type,attr"`
Value string `xml:",innerxml"`
}
// XMLRequest represents an XML request
type XMLRequest struct {
XMLName xml.Name `xml:"request"`
Method string `xml:"method,attr"`
Headers []XMLRequestHeader `xml:"header,omitempty"`
Parameters []XMLRequestParameter `xml:"parameter,omitempty"`
Content *XMLRequestContent `xml:"content,omitempty"`
}
// GetMethod returns the HTTP method of the XML Request
func (r *XMLRequest) GetMethod() string {
if r.Method == "" {
return "GET"
}
return r.Method
}
// GetBody returns a reader with the request body if exist
func (r *XMLRequest) GetBody() io.Reader {
if r.Content != nil {
return bytes.NewBufferString(r.Content.Value)
}
return nil
}
// GetParameterAsInt gets the value of a request parameter as an integer
func (r *XMLRequest) GetParameterAsInt(key string) int {
val := r.GetParameterAsString(key)
v, err := strconv.Atoi(val)
if err == nil {
return v
}
return 0
}
// GetParameterAsString gets the value of a request parameter as a string
func (r *XMLRequest) GetParameterAsString(key string) string {
for _, p := range r.Parameters {
if p.Name == key {
return p.Value
}
}
return ""
}
// XMLSource represents an XML source
type XMLSource struct {
XMLName xml.Name `xml:"xml-source"`
URL string `xml:"url,attr"`
Groups []XMLGroup `xml:"xml-group"`
Request *XMLRequest `xml:"request,omitempty"`
}
// GetHTTPRequest gets an HTTP request
func (src *XMLSource) GetHTTPRequest() (*http.Request, error) {
req := src.GetRequest()
httpreq, err := http.NewRequest(req.GetMethod(), src.URL, req.GetBody())
if err != nil {
return nil, err
}
for _, header := range req.Headers {
httpreq.Header.Add(header.Name, header.Value)
}
return httpreq, err
}
// GetRequest gets the HTTP request
func (src *XMLSource) GetRequest() *XMLRequest {
if src.Request == nil {
return &XMLRequest{}
}
return src.Request
}
// SkipSSL checks whether or not to skip certificate validation for HTTPS
func (src *XMLSource) SkipSSL() bool {
v := src.GetRequest().GetParameterAsString("disable-ssl-verification")
return strings.ToLower(v) == "true"
}
// XMLCollection represents an XML collection object
type XMLCollection struct {
XMLName xml.Name `xml:"xml-collection"`
Name string `xml:"name,attr"`
RRD *RRD `xml:"rrd"`
Sources []XMLSource `xml:"xml-source"`
}