forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diagnostics.go
101 lines (87 loc) · 3.72 KB
/
diagnostics.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
package cloudflare
import (
"encoding/json"
"net/http"
"github.com/pkg/errors"
)
// DiagnosticsTracerouteConfiguration is the overarching structure of the
// diagnostics traceroute requests.
type DiagnosticsTracerouteConfiguration struct {
Targets []string `json:"targets"`
Colos []string `json:"colos"`
Options DiagnosticsTracerouteConfigurationOptions `json:"options,omitempty"`
}
// DiagnosticsTracerouteConfigurationOptions contains the options for performing
// traceroutes.
type DiagnosticsTracerouteConfigurationOptions struct {
PacketsPerTTL int `json:"packets_per_ttl"`
PacketType string `json:"packet_type"`
MaxTTL int `json:"max_ttl"`
WaitTime int `json:"wait_time"`
}
// DiagnosticsTracerouteResponse is the outer response of the API response.
type DiagnosticsTracerouteResponse struct {
Response
Result []DiagnosticsTracerouteResponseResult `json:"result"`
}
// DiagnosticsTracerouteResponseResult is the inner API response for the
// traceroute request.
type DiagnosticsTracerouteResponseResult struct {
Target string `json:"target"`
Colos []DiagnosticsTracerouteResponseColos `json:"colos"`
}
// DiagnosticsTracerouteResponseColo contains the Name and City of a colocation.
type DiagnosticsTracerouteResponseColo struct {
Name string `json:"name"`
City string `json:"city"`
}
// DiagnosticsTracerouteResponseNodes holds a summary of nodes contacted in the
// traceroute.
type DiagnosticsTracerouteResponseNodes struct {
Asn string `json:"asn"`
IP string `json:"ip"`
Name string `json:"name"`
PacketCount int `json:"packet_count"`
MeanRttMs float64 `json:"mean_rtt_ms"`
StdDevRttMs float64 `json:"std_dev_rtt_ms"`
MinRttMs float64 `json:"min_rtt_ms"`
MaxRttMs float64 `json:"max_rtt_ms"`
}
// DiagnosticsTracerouteResponseHops holds packet and node information of the
// hops.
type DiagnosticsTracerouteResponseHops struct {
PacketsTTL int `json:"packets_ttl"`
PacketsSent int `json:"packets_sent"`
PacketsLost int `json:"packets_lost"`
Nodes []DiagnosticsTracerouteResponseNodes `json:"nodes"`
}
// DiagnosticsTracerouteResponseColos is the summary struct of a colocation test.
type DiagnosticsTracerouteResponseColos struct {
Error string `json:"error"`
Colo DiagnosticsTracerouteResponseColo `json:"colo"`
TracerouteTimeMs int `json:"traceroute_time_ms"`
TargetSummary DiagnosticsTracerouteResponseNodes `json:"target_summary"`
Hops []DiagnosticsTracerouteResponseHops `json:"hops"`
}
// PerformTraceroute initiates a traceroute from the Cloudflare network to the
// requested targets.
//
// API documentation: https://api.cloudflare.com/#diagnostics-traceroute
func (api *API) PerformTraceroute(accountID string, targets, colos []string, tracerouteOptions DiagnosticsTracerouteConfigurationOptions) ([]DiagnosticsTracerouteResponseResult, error) {
uri := "/accounts/" + accountID + "/diagnostics/traceroute"
diagnosticsPayload := DiagnosticsTracerouteConfiguration{
Targets: targets,
Colos: colos,
Options: tracerouteOptions,
}
res, err := api.makeRequest(http.MethodPost, uri, diagnosticsPayload)
if err != nil {
return []DiagnosticsTracerouteResponseResult{}, errors.Wrap(err, errMakeRequestError)
}
var diagnosticsResponse DiagnosticsTracerouteResponse
err = json.Unmarshal(res, &diagnosticsResponse)
if err != nil {
return []DiagnosticsTracerouteResponseResult{}, errors.Wrap(err, errUnmarshalError)
}
return diagnosticsResponse.Result, nil
}