Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Adds Href to metric resources in the REST API
Browse files Browse the repository at this point in the history
This does work with '*' in the path
  • Loading branch information
lynxbat committed Nov 21, 2015
1 parent d51b85b commit 79007dd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
13 changes: 10 additions & 3 deletions mgmt/rest/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ limitations under the License.
package rest

import (
"fmt"
"net/http"
"sort"
"strconv"
Expand All @@ -36,7 +37,7 @@ func (s *Server) getMetrics(w http.ResponseWriter, r *http.Request, _ httprouter
respond(500, rbody.FromError(err), w)
return
}
respondWithMetrics(mets, w)
respondWithMetrics(r.Host, mets, w)
}

func (s *Server) getMetricsFromTree(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
Expand Down Expand Up @@ -64,7 +65,7 @@ func (s *Server) getMetricsFromTree(w http.ResponseWriter, r *http.Request, para
respond(404, rbody.FromError(err), w)
return
}
respondWithMetrics(mets, w)
respondWithMetrics(r.Host, mets, w)
return
}

Expand All @@ -79,6 +80,7 @@ func (s *Server) getMetricsFromTree(w http.ResponseWriter, r *http.Request, para
Namespace: core.JoinNamespace(mt.Namespace()),
Version: mt.Version(),
LastAdvertisedTimestamp: mt.LastAdvertisedTime().Unix(),
Href: catalogedMetricURI(r.Host, mt),
}
rt := mt.Policy().RulesAsTable()
policies := make([]rbody.PolicyTable, 0, len(rt))
Expand All @@ -97,7 +99,7 @@ func (s *Server) getMetricsFromTree(w http.ResponseWriter, r *http.Request, para
respond(200, b, w)
}

func respondWithMetrics(mets []core.CatalogedMetric, w http.ResponseWriter) {
func respondWithMetrics(host string, mets []core.CatalogedMetric, w http.ResponseWriter) {
b := rbody.NewMetricsReturned()

for _, met := range mets {
Expand All @@ -118,8 +120,13 @@ func respondWithMetrics(mets []core.CatalogedMetric, w http.ResponseWriter) {
Version: met.Version(),
LastAdvertisedTimestamp: met.LastAdvertisedTime().Unix(),
Policy: policies,
Href: catalogedMetricURI(host, met),
})
}
sort.Sort(b)
respond(200, b, w)
}

func catalogedMetricURI(host string, mt core.CatalogedMetric) string {
return fmt.Sprintf("%s://%s/v1/metrics%s", protocolPrefix, host, core.JoinNamespace(mt.Namespace()))
}
1 change: 1 addition & 0 deletions mgmt/rest/rbody/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Metric struct {
Namespace string `json:"namespace,omitempty"`
Version int `json:"version,omitempty"`
Policy []PolicyTable `json:"policy,omitempty"`
Href string `json:"href"`
}

type MetricReturned struct {
Expand Down

0 comments on commit 79007dd

Please sign in to comment.