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

Commit

Permalink
Adds support for passing the query as a parameter
Browse files Browse the repository at this point in the history
Returns all version if no version is specified, updates snapctl help
and updates the href to use a parameter for the namespace query.
  • Loading branch information
jcooklin committed Jul 19, 2016
1 parent 3ae67ba commit 30478b7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/snapctl/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ var (
},
{
Name: "get",
Usage: "get details on a single metric",
Usage: "get details on metric(s)",
Action: getMetric,
Flags: []cli.Flag{
flMetricVersion,
Expand Down
2 changes: 1 addition & 1 deletion cmd/snapctl/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ var (
// metric
flMetricVersion = cli.IntFlag{
Name: "metric-version, v",
Usage: "The metric version. Default (0) is latest",
Usage: "The metric version. 0 (default) returns all. -1 returns latest.",
}
flMetricNamespace = cli.StringFlag{
Name: "metric-namespace, m",
Expand Down
11 changes: 6 additions & 5 deletions mgmt/rest/client/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (c *Client) GetMetricCatalog() *GetMetricsResult {
// It returns the corresponding metric catalog if succeeded. Otherwise, an error is returned.
func (c *Client) FetchMetrics(ns string, ver int) *GetMetricsResult {
r := &GetMetricsResult{}
q := fmt.Sprintf("/metrics%s?ver=%d", ns, ver)
q := fmt.Sprintf("/metrics?ns=%s&ver=%d", ns, ver)
resp, err := c.do("GET", q, ContentTypeJSON)
if err != nil {
return &GetMetricsResult{Err: err}
Expand All @@ -80,7 +80,7 @@ func (c *Client) FetchMetrics(ns string, ver int) *GetMetricsResult {
// GetMetricVersions retrieves all versions of a metric at a given namespace.
func (c *Client) GetMetricVersions(ns string) *GetMetricsResult {
r := &GetMetricsResult{}
q := fmt.Sprintf("/metrics%s", ns)
q := fmt.Sprintf("/metrics?ns=%s", ns)
resp, err := c.do("GET", q, ContentTypeJSON)
if err != nil {
return &GetMetricsResult{Err: err}
Expand All @@ -100,11 +100,12 @@ func (c *Client) GetMetricVersions(ns string) *GetMetricsResult {

// GetMetric retrieves a metric at a given namespace and version.
// If the version is < 1, the latest version is returned.
// Now returns an interface as several return types are possible
// (array of metrics)
// Returns an interface as several return types are possible
// (array of metrics).
func (c *Client) GetMetric(ns string, ver int) interface{} {
r := &GetMetricResult{}
q := fmt.Sprintf("/metrics%s?ver=%d", ns, ver)

q := fmt.Sprintf("/metrics?ns=%s&ver=%d", ns, ver)
resp, err := c.do("GET", q, ContentTypeJSON)
if err != nil {
return &GetMetricResult{Err: err}
Expand Down
44 changes: 39 additions & 5 deletions mgmt/rest/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ package rest
import (
"fmt"
"net/http"
"net/url"
"sort"
"strconv"
"strings"

"github.com/julienschmidt/httprouter"

Expand All @@ -32,6 +34,38 @@ import (
)

func (s *Server) getMetrics(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
ver := 0 // 0: get all metrics

// If we are provided a parameter with the name 'ns' we need to
// perform a query
q := r.URL.Query()
v := q.Get("ver")
ns_query := q.Get("ns")
if ns_query != "" {
ver = 0 // 0: get all versions
if v != "" {
var err error
ver, err = strconv.Atoi(v)
if err != nil {
respond(400, rbody.FromError(err), w)
return
}
}
// strip the leading '/' and split on the remaining '/'
ns := strings.Split(strings.TrimLeft(ns_query, "/"), "/")
if ns[len(ns)-1] == "*" {
ns = ns[:len(ns)-1]
}

mets, err := s.mm.FetchMetrics(core.NewNamespace(ns...), ver)
if err != nil {
respond(404, rbody.FromError(err), w)
return
}
respondWithMetrics(r.Host, mets, w)
return
}

mets, err := s.mm.MetricCatalog()
if err != nil {
respond(500, rbody.FromError(err), w)
Expand Down Expand Up @@ -63,7 +97,7 @@ func (s *Server) getMetricsFromTree(w http.ResponseWriter, r *http.Request, para

if ns[len(ns)-1] == "*" {
if v == "" {
ver = -1
ver = 0 //return all metrics
} else {
ver, err = strconv.Atoi(v)
if err != nil {
Expand All @@ -81,14 +115,14 @@ func (s *Server) getMetricsFromTree(w http.ResponseWriter, r *http.Request, para
return
}

// If no version was given, get all that fall at this namespace.
// If no version was given, get all version that fall at this namespace.
if v == "" {
mts, err := s.mm.GetMetricVersions(core.NewNamespace(ns...))
mets, err := s.mm.FetchMetrics(core.NewNamespace(ns...), 0)
if err != nil {
respond(404, rbody.FromError(err), w)
return
}
respondWithMetrics(r.Host, mts, w)
respondWithMetrics(r.Host, mets, w)
return
}

Expand Down Expand Up @@ -175,7 +209,7 @@ func respondWithMetrics(host string, mets []core.CatalogedMetric, w http.Respons
}

func catalogedMetricURI(host string, mt core.CatalogedMetric) string {
return fmt.Sprintf("%s://%s/v1/metrics%s?ver=%d", protocolPrefix, host, mt.Namespace().String(), mt.Version())
return fmt.Sprintf("%s://%s/v1/metrics?ns=%s&ver=%d", protocolPrefix, host, url.QueryEscape(mt.Namespace().String()), mt.Version())
}

func getDynamicElements(ns core.Namespace, indexes []int) []rbody.DynamicElement {
Expand Down
2 changes: 2 additions & 0 deletions mgmt/rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ limitations under the License.
package rest

import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
Expand Down Expand Up @@ -510,6 +511,7 @@ func respond(code int, b rbody.Body, w http.ResponseWriter) {
if err != nil {
panic(err)
}
j = bytes.Replace(j, []byte("\\u0026"), []byte("&"), -1)
fmt.Fprint(w, string(j))
}

Expand Down

0 comments on commit 30478b7

Please sign in to comment.