forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
200 lines (171 loc) · 5.19 KB
/
client.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package nsxtreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/nsxtreceiver"
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"go.opentelemetry.io/collector/component"
"go.uber.org/zap"
dm "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/nsxtreceiver/internal/model"
)
var _ (Client) = &nsxClient{}
// Client is a way of interacting with the NSX REST API
type Client interface {
TransportNodes(ctx context.Context) ([]dm.TransportNode, error)
ClusterNodes(ctx context.Context) ([]dm.ClusterNode, error)
NodeStatus(ctx context.Context, nodeID string, class nodeClass) (*dm.NodeStatus, error)
Interfaces(ctx context.Context, nodeID string, class nodeClass) ([]dm.NetworkInterface, error)
InterfaceStatus(ctx context.Context, nodeID, interfaceID string, class nodeClass) (*dm.NetworkInterfaceStats, error)
}
type nsxClient struct {
config *Config
client *http.Client
endpoint *url.URL
logger *zap.Logger
}
var (
errUnauthorized = errors.New("STATUS 403, unauthorized")
)
func newClient(c *Config, settings component.TelemetrySettings, host component.Host, logger *zap.Logger) (*nsxClient, error) {
client, err := c.HTTPClientSettings.ToClient(host, settings)
if err != nil {
return nil, err
}
endpoint, err := url.Parse(c.Endpoint)
if err != nil {
return nil, err
}
return &nsxClient{
config: c,
client: client,
endpoint: endpoint,
logger: logger,
}, nil
}
func (c *nsxClient) TransportNodes(ctx context.Context) ([]dm.TransportNode, error) {
body, err := c.doRequest(
ctx,
"/api/v1/transport-nodes",
)
if err != nil {
return nil, err
}
var nodes dm.TransportNodeList
err = json.Unmarshal(body, &nodes)
return nodes.Results, err
}
func (c *nsxClient) ClusterNodes(ctx context.Context) ([]dm.ClusterNode, error) {
body, err := c.doRequest(
ctx,
"/api/v1/cluster/nodes",
)
if err != nil {
return nil, fmt.Errorf("unable to get cluster nodes: %w", err)
}
var nodes dm.ClusterNodeList
err = json.Unmarshal(body, &nodes)
return nodes.Results, err
}
func (c *nsxClient) NodeStatus(ctx context.Context, nodeID string, class nodeClass) (*dm.NodeStatus, error) {
body, err := c.doRequest(
ctx,
c.nodeStatusEndpoint(class, nodeID),
)
if err != nil {
return nil, fmt.Errorf("unable to get a node's status from the REST API: %w", err)
}
if class == transportClass {
var nodeStatus dm.TransportNodeStatus
err = json.Unmarshal(body, &nodeStatus)
return &nodeStatus.NodeStatus, err
}
var nodeStatus dm.NodeStatus
err = json.Unmarshal(body, &nodeStatus)
return &nodeStatus, err
}
func (c *nsxClient) Interfaces(
ctx context.Context,
nodeID string,
class nodeClass,
) ([]dm.NetworkInterface, error) {
body, err := c.doRequest(
ctx,
c.interfacesEndpoint(class, nodeID),
)
if err != nil {
return nil, err
}
var interfaces dm.NodeNetworkInterfacePropertiesListResult
err = json.Unmarshal(body, &interfaces)
return interfaces.Results, err
}
func (c *nsxClient) InterfaceStatus(
ctx context.Context,
nodeID, interfaceID string,
class nodeClass,
) (*dm.NetworkInterfaceStats, error) {
body, err := c.doRequest(
ctx,
c.interfaceStatusEndpoint(class, nodeID, interfaceID),
)
if err != nil {
return nil, fmt.Errorf("unable to get interface stats: %w", err)
}
var interfaceStats dm.NetworkInterfaceStats
err = json.Unmarshal(body, &interfaceStats)
return &interfaceStats, err
}
func (c *nsxClient) doRequest(ctx context.Context, path string) ([]byte, error) {
endpoint, err := c.endpoint.Parse(path)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), nil)
if err != nil {
return nil, err
}
req.SetBasicAuth(c.config.Username, string(c.config.Password))
h := req.Header
h.Add("User-Agent", "opentelemetry-collector")
h.Add("Accept", "application/json")
h.Add("Connection", "keep-alive")
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return io.ReadAll(resp.Body)
}
body, _ := io.ReadAll(resp.Body)
switch resp.StatusCode {
case 403:
return nil, errUnauthorized
default:
c.logger.Info(fmt.Sprintf("%v", req))
return nil, fmt.Errorf("got non 200 status code %d: %w, %s", resp.StatusCode, err, string(body))
}
}
func (c *nsxClient) nodeStatusEndpoint(class nodeClass, nodeID string) string {
if class == transportClass {
return fmt.Sprintf("/api/v1/transport-nodes/%s/status", nodeID)
}
return fmt.Sprintf("/api/v1/cluster/nodes/%s/status", nodeID)
}
func (c *nsxClient) interfacesEndpoint(class nodeClass, nodeID string) string {
if class == transportClass {
return fmt.Sprintf("/api/v1/transport-nodes/%s/network/interfaces", nodeID)
}
return fmt.Sprintf("/api/v1/cluster/nodes/%s/network/interfaces", nodeID)
}
func (c *nsxClient) interfaceStatusEndpoint(class nodeClass, nodeID, interfaceID string) string {
if class == transportClass {
return fmt.Sprintf("/api/v1/transport-nodes/%s/network/interfaces/%s/stats", nodeID, interfaceID)
}
return fmt.Sprintf("/api/v1/cluster/nodes/%s/network/interfaces/%s/stats", nodeID, interfaceID)
}