forked from wavefrontHQ/go-wavefront-management-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
derivedmetric.go
120 lines (104 loc) · 3.82 KB
/
derivedmetric.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
package wavefront
import (
"fmt"
"strconv"
)
type DerivedMetric struct {
ID *string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Query string `json:"query,omitempty"`
Minutes int `json:"minutes,omitempty"`
Tags WFTags `json:"tags,omitempty"`
Status []string `json:"status,omitempty"`
InTrash bool `json:"inTrash,omitempty"`
QueryFailing bool `json:"queryFailing,omitempty"`
LastFailedTime int `json:"lastFailedTime,omitempty"`
LastErrorMessage string `json:"lastErrorMessage,omitempty"`
AdditionalInformation string `json:"additionalInformation,omitempty"`
HostsUsed []string `json:"hostsUsed,omitempty"`
UpdateUserId string `json:"updateUserId,omitempty"`
CreateUserId string `json:"createUserId,omitempty"`
LastProcessedMillis int `json:"lastProcessedMillis,omitempty"`
ProcessRateMinutes int `json:"processRateMinutes,omitempty"`
PointsScannedAtLastQuery int `json:"pointsScannedAtLastQuery,omitempty"`
IncludeObsoleteMetrics bool `json:"includeObsoleteMetrics,omitempty"`
LastQueryTime int `json:"lastQueryTime,omitempty"`
MetricsUsed []string `json:"metricsUsed,omitempty"`
QueryQBEnabled bool `json:"queryQBEnabled,omitempty"`
UpdatedEpochMillis int `json:"updatedEpochMillis,omitempty"`
CreatedEpochMillis int `json:"createdEpochMillis,omitempty"`
Deleted bool `json:"deleted,omitempty"`
}
type DerivedMetrics struct {
client Wavefronter
}
type WFTags struct {
CustomerTags []string `json:"customerTags"`
}
const baseDerivedMetricsPath = "/api/v2/derivedmetric"
func (c *Client) DerivedMetrics() *DerivedMetrics {
return &DerivedMetrics{client: c}
}
// Get is used to retrieve an existing DerivedMetric by ID.
// The ID field must be specified
func (dm DerivedMetrics) Get(metric *DerivedMetric) error {
if metric.ID == nil || *metric.ID == "" {
return fmt.Errorf("id must be specified")
}
return doRest(
"GET",
fmt.Sprintf("%s/%s", baseDerivedMetricsPath, *metric.ID),
dm.client,
doResponse(metric))
}
// Find returns all DerivedMetrics filtered by the given search conditions.
// If filter is nil, all DerivedMetrics are returned.
func (dm DerivedMetrics) Find(filter []*SearchCondition) (
results []*DerivedMetric, err error) {
err = doSearch(filter, "derivedmetric", dm.client, &results)
return
}
// Create a DerivedMetric, name, query, and minutes are required
func (dm DerivedMetrics) Create(metric *DerivedMetric) error {
if metric.Name == "" || metric.Query == "" || metric.Minutes == 0 {
return fmt.Errorf("name, query, and minutes must be specified to create a derived metric")
}
return doRest(
"POST",
baseDerivedMetricsPath,
dm.client,
doPayload(metric),
doResponse(metric))
}
// Update a DerivedMetric all fields are optional except for ID
func (dm DerivedMetrics) Update(metric *DerivedMetric) error {
if metric.ID == nil || *metric.ID == "" {
return fmt.Errorf("id must be specified")
}
return doRest(
"PUT",
fmt.Sprintf("%s/%s", baseDerivedMetricsPath, *metric.ID),
dm.client,
doPayload(metric),
doResponse(metric))
}
// Delete a DerivedMetric all fields are optional except for ID
func (dm DerivedMetrics) Delete(metric *DerivedMetric, skipTrash bool) error {
if metric.ID == nil || *metric.ID == "" {
return fmt.Errorf("id must be specified")
}
params := map[string]string{
"skipTrash": strconv.FormatBool(skipTrash),
}
err := doRest(
"DELETE",
fmt.Sprintf("%s/%s", baseDerivedMetricsPath, *metric.ID),
dm.client,
doParams(params))
if err != nil {
return err
}
empty := ""
metric.ID = &empty
return nil
}