This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created V2 API routes for Metric and Tasks:
- V2 metrics routes now have a metrics section in the body of the response. - Fixed text case in v2/tests from "ScheduledTasks" to "scheduled_tasks" to be consistent with other responses. - Added V2 folders in rest and in rbody to hold v2 methods
- Loading branch information
Showing
8 changed files
with
220 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package v2 | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/intelsdi-x/snap/mgmt/rest/rbody" | ||
) | ||
|
||
type MetricList struct { | ||
Metrics []rbody.Metric `json:"metrics,omitempty"` | ||
} | ||
|
||
type MetricReturned struct { | ||
Metric *rbody.Metric `json:"metric,omitempty"` | ||
} | ||
|
||
func (m *MetricReturned) ResponseBodyMessage() string { | ||
return "Metric returned" | ||
} | ||
|
||
func (m *MetricReturned) ResponseBodyType() string { | ||
return "metric_returned" | ||
} | ||
|
||
type MetricsReturned MetricList | ||
|
||
func (m MetricsReturned) Len() int { | ||
return len(m.Metrics) | ||
} | ||
|
||
func (m MetricsReturned) Less(i, j int) bool { | ||
return (fmt.Sprintf("%s:%d", m.Metrics[i].Namespace, m.Metrics[i].Version)) < (fmt.Sprintf("%s:%d", m.Metrics[j].Namespace, m.Metrics[j].Version)) | ||
} | ||
|
||
func (m MetricsReturned) Swap(i, j int) { | ||
m.Metrics[i], m.Metrics[j] = m.Metrics[j], m.Metrics[i] | ||
} | ||
|
||
func NewMetricsReturned() MetricsReturned { | ||
return MetricsReturned{Metrics: []rbody.Metric{}} | ||
} | ||
|
||
func (m MetricsReturned) ResponseBodyMessage() string { | ||
return "Metrics returned" | ||
} | ||
|
||
func (m MetricsReturned) ResponseBodyType() string { | ||
return rbody.MetricsReturnedType | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package v2 | ||
|
||
import "github.com/intelsdi-x/snap/mgmt/rest/rbody" | ||
|
||
type ScheduledTaskListReturned struct { | ||
ScheduledTasks []rbody.ScheduledTask `json:"scheduled_task,omitempty"` | ||
} | ||
|
||
func (s *ScheduledTaskListReturned) Len() int { | ||
return len(s.ScheduledTasks) | ||
} | ||
|
||
func (s *ScheduledTaskListReturned) Less(i, j int) bool { | ||
return s.ScheduledTasks[j].CreationTime().After(s.ScheduledTasks[i].CreationTime()) | ||
} | ||
|
||
func (s *ScheduledTaskListReturned) Swap(i, j int) { | ||
s.ScheduledTasks[i], s.ScheduledTasks[j] = s.ScheduledTasks[j], s.ScheduledTasks[i] | ||
} | ||
|
||
func (s *ScheduledTaskListReturned) ResponseBodyMessage() string { | ||
return "Scheduled tasks retrieved" | ||
} | ||
|
||
func (s *ScheduledTaskListReturned) ResponseBodyType() string { | ||
return "scheduled_task_list_returned" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package v2 | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"sort" | ||
|
||
"github.com/codegangsta/negroni" | ||
"github.com/intelsdi-x/snap/core" | ||
"github.com/intelsdi-x/snap/mgmt/rest/rbody" | ||
"github.com/intelsdi-x/snap/mgmt/rest/rbody/v2" | ||
) | ||
|
||
func RespondWithMetrics(host string, mets []core.CatalogedMetric, w http.ResponseWriter) { | ||
b := v2.NewMetricsReturned() | ||
|
||
for _, met := range mets { | ||
rt := met.Policy().RulesAsTable() | ||
policies := make([]rbody.PolicyTable, 0, len(rt)) | ||
for _, r := range rt { | ||
policies = append(policies, rbody.PolicyTable{ | ||
Name: r.Name, | ||
Type: r.Type, | ||
Default: r.Default, | ||
Required: r.Required, | ||
Minimum: r.Minimum, | ||
Maximum: r.Maximum, | ||
}) | ||
} | ||
dyn, indexes := met.Namespace().IsDynamic() | ||
var dynamicElements []rbody.DynamicElement | ||
if dyn { | ||
dynamicElements = getDynamicElements(met.Namespace(), indexes) | ||
} | ||
|
||
b.Metrics = append(b.Metrics, rbody.Metric{ | ||
Namespace: met.Namespace().String(), | ||
Version: met.Version(), | ||
LastAdvertisedTimestamp: met.LastAdvertisedTime().Unix(), | ||
Description: met.Description(), | ||
Dynamic: dyn, | ||
DynamicElements: dynamicElements, | ||
Unit: met.Unit(), | ||
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?ns=%s&ver=%d", "http", host, url.QueryEscape(mt.Namespace().String()), mt.Version()) | ||
} | ||
|
||
func getDynamicElements(ns core.Namespace, indexes []int) []rbody.DynamicElement { | ||
elements := make([]rbody.DynamicElement, 0, len(indexes)) | ||
for _, v := range indexes { | ||
e := ns.Element(v) | ||
elements = append(elements, rbody.DynamicElement{ | ||
Index: v, | ||
Name: e.Name, | ||
Description: e.Description, | ||
}) | ||
} | ||
return elements | ||
} | ||
|
||
func respond(code int, b rbody.Body, w http.ResponseWriter) { | ||
resp := &rbody.APIResponse{ | ||
Meta: &rbody.APIResponseMeta{ | ||
Code: code, | ||
Message: b.ResponseBodyMessage(), | ||
Type: b.ResponseBodyType(), | ||
Version: 2, | ||
}, | ||
Body: b, | ||
} | ||
if !w.(negroni.ResponseWriter).Written() { | ||
w.WriteHeader(code) | ||
} | ||
|
||
j, err := json.MarshalIndent(resp, "", " ") | ||
if err != nil { | ||
panic(err) | ||
} | ||
j = bytes.Replace(j, []byte("\\u0026"), []byte("&"), -1) | ||
fmt.Fprint(w, string(j)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package v2 | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"sort" | ||
|
||
"github.com/intelsdi-x/snap/core" | ||
"github.com/intelsdi-x/snap/mgmt/rest/rbody" | ||
"github.com/intelsdi-x/snap/mgmt/rest/rbody/v2" | ||
) | ||
|
||
func GetTasks(w http.ResponseWriter, r *http.Request, sts map[string]core.Task) { | ||
tasks := &v2.ScheduledTaskListReturned{} | ||
tasks.ScheduledTasks = make([]rbody.ScheduledTask, len(sts)) | ||
|
||
i := 0 | ||
for _, t := range sts { | ||
tasks.ScheduledTasks[i] = *rbody.SchedulerTaskFromTask(t) | ||
tasks.ScheduledTasks[i].Href = taskURI(r.Host, t) | ||
i++ | ||
} | ||
sort.Sort(tasks) | ||
respond(200, tasks, w) | ||
} | ||
|
||
func taskURI(host string, t core.Task) string { | ||
return fmt.Sprintf("%s://%s/v1/tasks/%s", "http", host, t.ID()) | ||
} |