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

Commit

Permalink
Fix for issue #1048: Invalid API response returned by mock collector
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Bourdon committed Jul 18, 2016
1 parent b97e97b commit 3ae67ba
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
43 changes: 36 additions & 7 deletions cmd/snapctl/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"time"

"github.com/codegangsta/cli"
"github.com/intelsdi-x/snap/mgmt/rest/client"
"github.com/intelsdi-x/snap/mgmt/rest/rbody"
)

Expand Down Expand Up @@ -94,13 +95,7 @@ func listMetrics(ctx *cli.Context) error {
return nil
}

func getMetric(ctx *cli.Context) error {
if !ctx.IsSet("metric-namespace") {
return newUsageError("namespace is required\n\n", ctx)
}
ns := ctx.String("metric-namespace")
ver := ctx.Int("metric-version")
metric := pClient.GetMetric(ns, ver)
func printMetric(metric *client.GetMetricResult, idx int) error {
if metric.Err != nil {
return fmt.Errorf("%v", metric.Err)
}
Expand All @@ -119,6 +114,9 @@ func getMetric(ctx *cli.Context) error {

namespace := getNamespace(metric.Metric)

if idx > 0 {
fmt.Printf("\n")
}
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
printFields(w, false, 0, "NAMESPACE", "VERSION", "UNIT", "LAST ADVERTISED TIME", "DESCRIPTION")
printFields(w, false, 0, namespace, metric.Metric.Version, metric.Metric.Unit, time.Unix(metric.Metric.LastAdvertisedTimestamp, 0).Format(time.RFC1123), metric.Metric.Description)
Expand Down Expand Up @@ -150,7 +148,38 @@ func getMetric(ctx *cli.Context) error {
printFields(w, true, 6, rule.Name, rule.Type, rule.Default, rule.Required, rule.Minimum, rule.Maximum)
}
w.Flush()
return nil
}

func getMetric(ctx *cli.Context) error {
if !ctx.IsSet("metric-namespace") {
return newUsageError("namespace is required\n\n", ctx)
}
ns := ctx.String("metric-namespace")
ver := ctx.Int("metric-version")
metric := pClient.GetMetric(ns, ver)
switch mtype := metric.(type) {
case []*client.GetMetricResult:
// Multiple metrics
var merr error
for i, m := range metric.([]*client.GetMetricResult) {
err := printMetric(m, i)
if err != nil {
merr = err
}
}
if merr != nil {
return merr
}
case *client.GetMetricResult:
// Single metric
err := printMetric(metric.(*client.GetMetricResult), 0)
if err != nil {
return err
}
default:
return fmt.Errorf("Unexpected response type %T\n", mtype)
}
return nil
}

Expand Down
17 changes: 16 additions & 1 deletion mgmt/rest/client/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ 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.
func (c *Client) GetMetric(ns string, ver int) *GetMetricResult {
// Now 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)
resp, err := c.do("GET", q, ContentTypeJSON)
Expand All @@ -112,6 +114,10 @@ func (c *Client) GetMetric(ns string, ver int) *GetMetricResult {
case rbody.MetricReturnedType:
mc := resp.Body.(*rbody.MetricReturned)
r.Metric = mc.Metric
case rbody.MetricsReturnedType:
mc := resp.Body.(*rbody.MetricsReturned)
r := convertMetrics(mc)
return r
case rbody.ErrorType:
r.Err = resp.Body.(*rbody.Error)
default:
Expand Down Expand Up @@ -144,3 +150,12 @@ func convertCatalog(c *rbody.MetricsReturned) []*rbody.Metric {
}
return mci
}

func convertMetrics(c *rbody.MetricsReturned) []*GetMetricResult {
mci := make([]*GetMetricResult, len(*c))
for i, _ := range *c {
r := &GetMetricResult{Metric: &(*c)[i]}
mci[i] = r
}
return mci
}
2 changes: 1 addition & 1 deletion mgmt/rest/rbody/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func NewMetricsReturned() MetricsReturned {
}

func (m MetricsReturned) ResponseBodyMessage() string {
return "Metric"
return "Metrics returned"
}

func (m MetricsReturned) ResponseBodyType() string {
Expand Down

0 comments on commit 3ae67ba

Please sign in to comment.