Update go-librato
This commit is contained in:
parent
3481d1bf6e
commit
5316739ba1
|
@ -0,0 +1,110 @@
|
|||
package librato
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// AlertsService handles communication with the Librato API methods related to
|
||||
// alerts.
|
||||
type AlertsService struct {
|
||||
client *Client
|
||||
}
|
||||
|
||||
// Alert represents a Librato Alert.
|
||||
type Alert struct {
|
||||
Name *string `json:"name"`
|
||||
ID *uint `json:"id,omitempty"`
|
||||
Conditions []AlertCondition `json:"conditions,omitempty"`
|
||||
// These are interface{} because the Librato API asks for integers
|
||||
// on Create and returns hashes on Get
|
||||
Services interface{} `json:"services,omitempty"`
|
||||
Attributes *AlertAttributes `json:"attributes,omitempty"`
|
||||
Description *string `json:"description,omitempty"`
|
||||
Active *bool `json:"active,omitempty"`
|
||||
RearmSeconds *uint `json:"rearm_seconds,omitempty"`
|
||||
}
|
||||
|
||||
func (a Alert) String() string {
|
||||
return Stringify(a)
|
||||
}
|
||||
|
||||
// AlertCondition represents an alert trigger condition.
|
||||
type AlertCondition struct {
|
||||
Type *string `json:"type,omitempty"`
|
||||
MetricName *string `json:"metric_name,omitempty"`
|
||||
Source *string `json:"source,omitempty"`
|
||||
DetectReset *bool `json:"detect_reset,omitempty"`
|
||||
Threshold *float64 `json:"threshold,omitempty"`
|
||||
SummaryFunction *string `json:"summary_function,omitempty"`
|
||||
Duration *uint `json:"duration,omitempty"`
|
||||
}
|
||||
|
||||
// AlertAttributes represents the attributes of an alert.
|
||||
type AlertAttributes struct {
|
||||
RunbookURL *string `json:"runbook_url,omitempty"`
|
||||
}
|
||||
|
||||
// Get an alert by ID
|
||||
//
|
||||
// Librato API docs: https://www.librato.com/docs/api/#retrieve-alert-by-id
|
||||
func (a *AlertsService) Get(id uint) (*Alert, *http.Response, error) {
|
||||
urlStr := fmt.Sprintf("alerts/%d", id)
|
||||
|
||||
req, err := a.client.NewRequest("GET", urlStr, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
alert := new(Alert)
|
||||
resp, err := a.client.Do(req, alert)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
return alert, resp, err
|
||||
}
|
||||
|
||||
// Create an alert
|
||||
//
|
||||
// Librato API docs: https://www.librato.com/docs/api/?shell#create-an-alert
|
||||
func (a *AlertsService) Create(alert *Alert) (*Alert, *http.Response, error) {
|
||||
req, err := a.client.NewRequest("POST", "alerts", alert)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
al := new(Alert)
|
||||
resp, err := a.client.Do(req, al)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
return al, resp, err
|
||||
}
|
||||
|
||||
// Edit an alert.
|
||||
//
|
||||
// Librato API docs: https://www.librato.com/docs/api/?shell#update-alert
|
||||
func (a *AlertsService) Edit(alertID uint, alert *Alert) (*http.Response, error) {
|
||||
u := fmt.Sprintf("alerts/%d", alertID)
|
||||
req, err := a.client.NewRequest("PUT", u, alert)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return a.client.Do(req, nil)
|
||||
}
|
||||
|
||||
// Delete an alert
|
||||
//
|
||||
// Librato API docs: https://www.librato.com/docs/api/?shell#delete-alert
|
||||
func (a *AlertsService) Delete(id uint) (*http.Response, error) {
|
||||
u := fmt.Sprintf("alerts/%d", id)
|
||||
req, err := a.client.NewRequest("DELETE", u, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return a.client.Do(req, nil)
|
||||
}
|
|
@ -44,7 +44,10 @@ type Client struct {
|
|||
UserAgent string
|
||||
|
||||
// Services used to manipulate API entities.
|
||||
Spaces *SpacesService
|
||||
Spaces *SpacesService
|
||||
Metrics *MetricsService
|
||||
Alerts *AlertsService
|
||||
Services *ServicesService
|
||||
}
|
||||
|
||||
// NewClient returns a new Librato API client bound to the public Librato API.
|
||||
|
@ -74,6 +77,9 @@ func NewClientWithBaseURL(baseURL *url.URL, email, token string) *Client {
|
|||
}
|
||||
|
||||
c.Spaces = &SpacesService{client: c}
|
||||
c.Metrics = &MetricsService{client: c}
|
||||
c.Alerts = &AlertsService{client: c}
|
||||
c.Services = &ServicesService{client: c}
|
||||
|
||||
return c
|
||||
}
|
||||
|
@ -89,7 +95,6 @@ func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Requ
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
u := c.BaseURL.ResolveReference(rel)
|
||||
|
||||
var buf io.ReadWriter
|
||||
|
|
|
@ -0,0 +1,163 @@
|
|||
package librato
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// MetricsService handles communication with the Librato API methods related to
|
||||
// metrics.
|
||||
type MetricsService struct {
|
||||
client *Client
|
||||
}
|
||||
|
||||
// Metric represents a Librato Metric.
|
||||
type Metric struct {
|
||||
Name *string `json:"name"`
|
||||
Period *uint `json:"period,omitempty"`
|
||||
DisplayName *string `json:"display_name,omitempty"`
|
||||
Attributes *MetricAttributes `json:"attributes,omitempty"`
|
||||
}
|
||||
|
||||
type MetricAttributes struct {
|
||||
Color *string `json:"color"`
|
||||
// These are interface{} because sometimes the Librato API
|
||||
// returns strings, and sometimes it returns integers
|
||||
DisplayMax interface{} `json:"display_max"`
|
||||
DisplayMin interface{} `json:"display_min"`
|
||||
DisplayUnitsShort string `json:"display_units_short"`
|
||||
DisplayStacked bool `json:"display_stacked"`
|
||||
DisplayTransform string `json:"display_transform"`
|
||||
}
|
||||
|
||||
type ListMetricsOptions struct {
|
||||
*PaginationMeta
|
||||
Name string `url:"name,omitempty"`
|
||||
}
|
||||
|
||||
// Advance to the specified page in result set, while retaining
|
||||
// the filtering options.
|
||||
func (l *ListMetricsOptions) AdvancePage(next *PaginationMeta) ListMetricsOptions {
|
||||
return ListMetricsOptions{
|
||||
PaginationMeta: next,
|
||||
Name: l.Name,
|
||||
}
|
||||
}
|
||||
|
||||
type ListMetricsResponse struct {
|
||||
ThisPage *PaginationResponseMeta
|
||||
NextPage *PaginationMeta
|
||||
}
|
||||
|
||||
// List metrics using the provided options.
|
||||
//
|
||||
// Librato API docs: https://www.librato.com/docs/api/#retrieve-metrics
|
||||
func (m *MetricsService) List(opts *ListMetricsOptions) ([]Metric, *ListMetricsResponse, error) {
|
||||
u, err := urlWithOptions("metrics", opts)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
req, err := m.client.NewRequest("GET", u, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var metricsResponse struct {
|
||||
Query PaginationResponseMeta
|
||||
Metrics []Metric
|
||||
}
|
||||
|
||||
_, err = m.client.Do(req, &metricsResponse)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return metricsResponse.Metrics,
|
||||
&ListMetricsResponse{
|
||||
ThisPage: &metricsResponse.Query,
|
||||
NextPage: metricsResponse.Query.nextPage(opts.PaginationMeta),
|
||||
},
|
||||
nil
|
||||
}
|
||||
|
||||
// Get a metric by name
|
||||
//
|
||||
// Librato API docs: https://www.librato.com/docs/api/#retrieve-metric-by-name
|
||||
func (m *MetricsService) Get(name string) (*Metric, *http.Response, error) {
|
||||
u := fmt.Sprintf("metrics/%s", name)
|
||||
req, err := m.client.NewRequest("GET", u, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
metric := new(Metric)
|
||||
resp, err := m.client.Do(req, metric)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
return metric, resp, err
|
||||
}
|
||||
|
||||
type MeasurementSubmission struct {
|
||||
MeasureTime *uint `json:"measure_time,omitempty"`
|
||||
Source *string `json:"source,omitempty"`
|
||||
Gauges []*GaugeMeasurement `json:"gauges,omitempty"`
|
||||
Counters []*Measurement `json:"counters,omitempty"`
|
||||
}
|
||||
|
||||
type Measurement struct {
|
||||
Name string `json:"name"`
|
||||
Value *float64 `json:"value,omitempty"`
|
||||
MeasureTime *uint `json:"measure_time,omitempty"`
|
||||
Source *string `json:"source,omitempty"`
|
||||
}
|
||||
|
||||
type GaugeMeasurement struct {
|
||||
*Measurement
|
||||
Count *uint `json:"count,omitempty"`
|
||||
Sum *float64 `json:"sum,omitempty"`
|
||||
Max *float64 `json:"max,omitempty"`
|
||||
Min *float64 `json:"min,omitempty"`
|
||||
SumSquares *float64 `json:"sum_squares,omitempty"`
|
||||
}
|
||||
|
||||
// Submit metrics
|
||||
//
|
||||
// Librato API docs: https://www.librato.com/docs/api/#submit-metrics
|
||||
func (m *MetricsService) Submit(measurements *MeasurementSubmission) (*http.Response, error) {
|
||||
req, err := m.client.NewRequest("POST", "/metrics", measurements)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return m.client.Do(req, nil)
|
||||
}
|
||||
|
||||
// Edit a metric.
|
||||
//
|
||||
// Librato API docs: https://www.librato.com/docs/api/#update-metric-by-name
|
||||
func (m *MetricsService) Edit(metric *Metric) (*http.Response, error) {
|
||||
u := fmt.Sprintf("metrics/%s", *metric.Name)
|
||||
|
||||
req, err := m.client.NewRequest("PUT", u, metric)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return m.client.Do(req, nil)
|
||||
}
|
||||
|
||||
// Delete a metric.
|
||||
//
|
||||
// Librato API docs: https://www.librato.com/docs/api/#delete-metric-by-name
|
||||
func (m *MetricsService) Delete(name string) (*http.Response, error) {
|
||||
u := fmt.Sprintf("metrics/%s", name)
|
||||
req, err := m.client.NewRequest("DELETE", u, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return m.client.Do(req, nil)
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package librato
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// PaginationResponseMeta contains pagination metadata from Librato API
|
||||
// responses.
|
||||
type PaginationResponseMeta struct {
|
||||
Offset uint `json:"offset"`
|
||||
Length uint `json:"length"`
|
||||
Total uint `json:"total"`
|
||||
Found uint `json:"found"`
|
||||
}
|
||||
|
||||
// Calculate the pagination metadata for the next page of the result set.
|
||||
// Takes the metadata used to request the current page so that it can use the
|
||||
// same sort/orderby options
|
||||
func (p *PaginationResponseMeta) nextPage(originalQuery *PaginationMeta) (next *PaginationMeta) {
|
||||
nextOffset := p.Offset + p.Length
|
||||
|
||||
if nextOffset >= p.Found {
|
||||
return nil
|
||||
}
|
||||
|
||||
next = &PaginationMeta{}
|
||||
next.Offset = nextOffset
|
||||
next.Length = p.Length
|
||||
|
||||
if originalQuery != nil {
|
||||
next.OrderBy = originalQuery.OrderBy
|
||||
next.Sort = originalQuery.Sort
|
||||
}
|
||||
|
||||
return next
|
||||
}
|
||||
|
||||
// PaginationMeta contains metadata that the Librato API requires for pagination
|
||||
// http://dev.librato.com/v1/pagination
|
||||
type PaginationMeta struct {
|
||||
Offset uint `url:"offset,omitempty"`
|
||||
Length uint `url:"length,omitempty"`
|
||||
OrderBy string `url:"orderby,omitempty"`
|
||||
Sort string `url:"sort,omitempty"`
|
||||
}
|
||||
|
||||
// EncodeValues is implemented to allow other strucs to embed PaginationMeta and
|
||||
// still use github.com/google/go-querystring/query to encode the struct. It
|
||||
// makes PaginationMeta implement query.Encoder.
|
||||
func (m *PaginationMeta) EncodeValues(name string, values *url.Values) error {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if m.Offset != 0 {
|
||||
values.Set("offset", fmt.Sprintf("%d", m.Offset))
|
||||
}
|
||||
if m.Length != 0 {
|
||||
values.Set("length", fmt.Sprintf("%d", m.Length))
|
||||
}
|
||||
if m.OrderBy != "" {
|
||||
values.Set("orderby", m.OrderBy)
|
||||
}
|
||||
if m.Sort != "" {
|
||||
values.Set("sort", m.Sort)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
package librato
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// ServicesService handles communication with the Librato API methods related to
|
||||
// notification services.
|
||||
type ServicesService struct {
|
||||
client *Client
|
||||
}
|
||||
|
||||
// Service represents a Librato Service.
|
||||
type Service struct {
|
||||
ID *uint `json:"id,omitempty"`
|
||||
Type *string `json:"type,omitempty"`
|
||||
Title *string `json:"title,omitempty"`
|
||||
// This is an interface{} because it's a hash of settings
|
||||
// specific to each service.
|
||||
Settings map[string]string `json:"settings,omitempty"`
|
||||
}
|
||||
|
||||
func (a Service) String() string {
|
||||
return Stringify(a)
|
||||
}
|
||||
|
||||
// Get a service by ID
|
||||
//
|
||||
// Librato API docs: https://www.librato.com/docs/api/#retrieve-specific-service
|
||||
func (s *ServicesService) Get(id uint) (*Service, *http.Response, error) {
|
||||
urlStr := fmt.Sprintf("services/%d", id)
|
||||
|
||||
req, err := s.client.NewRequest("GET", urlStr, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
service := new(Service)
|
||||
resp, err := s.client.Do(req, service)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
return service, resp, err
|
||||
}
|
||||
|
||||
// Create a service
|
||||
//
|
||||
// Librato API docs: https://www.librato.com/docs/api/#create-a-service
|
||||
func (s *ServicesService) Create(service *Service) (*Service, *http.Response, error) {
|
||||
req, err := s.client.NewRequest("POST", "services", service)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
sv := new(Service)
|
||||
resp, err := s.client.Do(req, sv)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
return sv, resp, err
|
||||
}
|
||||
|
||||
// Edit a service.
|
||||
//
|
||||
// Librato API docs: https://www.librato.com/docs/api/#update-a-service
|
||||
func (s *ServicesService) Edit(serviceID uint, service *Service) (*http.Response, error) {
|
||||
u := fmt.Sprintf("services/%d", serviceID)
|
||||
req, err := s.client.NewRequest("PUT", u, service)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.client.Do(req, nil)
|
||||
}
|
||||
|
||||
// Delete a service
|
||||
//
|
||||
// Librato API docs: https://www.librato.com/docs/api/#delete-a-service
|
||||
func (s *ServicesService) Delete(id uint) (*http.Response, error) {
|
||||
u := fmt.Sprintf("services/%d", id)
|
||||
req, err := s.client.NewRequest("DELETE", u, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.client.Do(req, nil)
|
||||
}
|
|
@ -1215,8 +1215,11 @@
|
|||
"revision": "df949784da9ed028ee76df44652e42d37a09d7e4"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "jq2E42bB0kwKaerHXwJslUea4eM=",
|
||||
"origin": "github.com/hashicorp/terraform/vendor/github.com/henrikhodne/go-librato/librato",
|
||||
"path": "github.com/henrikhodne/go-librato/librato",
|
||||
"revision": "613abdebf4922c4d9d46bcb4bcf14ee18c08d7de"
|
||||
"revision": "6e9aa4b1a8a8b735ad14b4f1c9542ef183e82dc2",
|
||||
"revisionTime": "2016-08-11T07:26:26Z"
|
||||
},
|
||||
{
|
||||
"comment": "v0.0.2-37-g5cd82f0",
|
||||
|
|
Loading…
Reference in New Issue