From 79007dd260fc5e7de9bb87a6534d42c66fae48c7 Mon Sep 17 00:00:00 2001 From: Nicholas Weaver Date: Sat, 21 Nov 2015 12:00:06 -0800 Subject: [PATCH] Adds Href to metric resources in the REST API This does work with '*' in the path --- mgmt/rest/metric.go | 13 ++++++++++--- mgmt/rest/rbody/metric.go | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/mgmt/rest/metric.go b/mgmt/rest/metric.go index a72a984fd..205d4b2e7 100644 --- a/mgmt/rest/metric.go +++ b/mgmt/rest/metric.go @@ -20,6 +20,7 @@ limitations under the License. package rest import ( + "fmt" "net/http" "sort" "strconv" @@ -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) { @@ -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 } @@ -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)) @@ -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 { @@ -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())) +} diff --git a/mgmt/rest/rbody/metric.go b/mgmt/rest/rbody/metric.go index c3253c8ca..88fc6d641 100644 --- a/mgmt/rest/rbody/metric.go +++ b/mgmt/rest/rbody/metric.go @@ -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 {