mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
add ebs_volume_ids() for templating
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/cloudwatch"
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/grafana/grafana/pkg/middleware"
|
||||
)
|
||||
|
||||
@@ -22,86 +23,123 @@ func ProxyCloudWatchDataSourceRequest(c *middleware.Context) {
|
||||
}{}
|
||||
json.Unmarshal([]byte(body), reqInfo)
|
||||
|
||||
svc := cloudwatch.New(&aws.Config{Region: aws.String(reqInfo.Region)})
|
||||
switch reqInfo.Service {
|
||||
case "CloudWatch":
|
||||
svc := cloudwatch.New(&aws.Config{Region: aws.String(reqInfo.Region)})
|
||||
|
||||
switch reqInfo.Action {
|
||||
case "GetMetricStatistics":
|
||||
reqParam := &struct {
|
||||
Parameters struct {
|
||||
Namespace string `json:"Namespace"`
|
||||
MetricName string `json:"MetricName"`
|
||||
Dimensions []map[string]string `json:"Dimensions"`
|
||||
Statistics []string `json:"Statistics"`
|
||||
StartTime int64 `json:"StartTime"`
|
||||
EndTime int64 `json:"EndTime"`
|
||||
Period int64 `json:"Period"`
|
||||
} `json:"parameters"`
|
||||
}{}
|
||||
json.Unmarshal([]byte(body), reqParam)
|
||||
switch reqInfo.Action {
|
||||
case "GetMetricStatistics":
|
||||
reqParam := &struct {
|
||||
Parameters struct {
|
||||
Namespace string `json:"Namespace"`
|
||||
MetricName string `json:"MetricName"`
|
||||
Dimensions []map[string]string `json:"Dimensions"`
|
||||
Statistics []string `json:"Statistics"`
|
||||
StartTime int64 `json:"StartTime"`
|
||||
EndTime int64 `json:"EndTime"`
|
||||
Period int64 `json:"Period"`
|
||||
} `json:"parameters"`
|
||||
}{}
|
||||
json.Unmarshal([]byte(body), reqParam)
|
||||
|
||||
statistics := make([]*string, 0)
|
||||
for k := range reqParam.Parameters.Statistics {
|
||||
statistics = append(statistics, &reqParam.Parameters.Statistics[k])
|
||||
statistics := make([]*string, 0)
|
||||
for k := range reqParam.Parameters.Statistics {
|
||||
statistics = append(statistics, &reqParam.Parameters.Statistics[k])
|
||||
}
|
||||
dimensions := make([]*cloudwatch.Dimension, 0)
|
||||
for _, d := range reqParam.Parameters.Dimensions {
|
||||
dimensions = append(dimensions, &cloudwatch.Dimension{
|
||||
Name: aws.String(d["Name"]),
|
||||
Value: aws.String(d["Value"]),
|
||||
})
|
||||
}
|
||||
|
||||
params := &cloudwatch.GetMetricStatisticsInput{
|
||||
Namespace: aws.String(reqParam.Parameters.Namespace),
|
||||
MetricName: aws.String(reqParam.Parameters.MetricName),
|
||||
Dimensions: dimensions,
|
||||
Statistics: statistics,
|
||||
StartTime: aws.Time(time.Unix(reqParam.Parameters.StartTime, 0)),
|
||||
EndTime: aws.Time(time.Unix(reqParam.Parameters.EndTime, 0)),
|
||||
Period: aws.Int64(reqParam.Parameters.Period),
|
||||
}
|
||||
|
||||
resp, err := svc.GetMetricStatistics(params)
|
||||
if err != nil {
|
||||
c.JsonApiErr(500, "Unable to call AWS API", err)
|
||||
return
|
||||
}
|
||||
|
||||
respJson, _ := json.Marshal(resp)
|
||||
fmt.Fprint(c.RW(), string(respJson))
|
||||
case "ListMetrics":
|
||||
reqParam := &struct {
|
||||
Parameters struct {
|
||||
Namespace string `json:"Namespace"`
|
||||
MetricName string `json:"MetricName"`
|
||||
Dimensions []map[string]string `json:"Dimensions"`
|
||||
} `json:"parameters"`
|
||||
}{}
|
||||
json.Unmarshal([]byte(body), reqParam)
|
||||
|
||||
dimensions := make([]*cloudwatch.DimensionFilter, 0)
|
||||
for _, d := range reqParam.Parameters.Dimensions {
|
||||
dimensions = append(dimensions, &cloudwatch.DimensionFilter{
|
||||
Name: aws.String(d["Name"]),
|
||||
Value: aws.String(d["Value"]),
|
||||
})
|
||||
}
|
||||
|
||||
params := &cloudwatch.ListMetricsInput{
|
||||
Namespace: aws.String(reqParam.Parameters.Namespace),
|
||||
MetricName: aws.String(reqParam.Parameters.MetricName),
|
||||
Dimensions: dimensions,
|
||||
}
|
||||
|
||||
resp, err := svc.ListMetrics(params)
|
||||
if err != nil {
|
||||
c.JsonApiErr(500, "Unable to call AWS API", err)
|
||||
return
|
||||
}
|
||||
|
||||
respJson, _ := json.Marshal(resp)
|
||||
fmt.Fprint(c.RW(), string(respJson))
|
||||
default:
|
||||
c.JsonApiErr(500, "Unexpected CloudWatch action", errors.New(reqInfo.Action))
|
||||
}
|
||||
dimensions := make([]*cloudwatch.Dimension, 0)
|
||||
for _, d := range reqParam.Parameters.Dimensions {
|
||||
dimensions = append(dimensions, &cloudwatch.Dimension{
|
||||
Name: aws.String(d["Name"]),
|
||||
Value: aws.String(d["Value"]),
|
||||
})
|
||||
case "EC2":
|
||||
svc := ec2.New(&aws.Config{Region: aws.String(reqInfo.Region)})
|
||||
|
||||
switch reqInfo.Action {
|
||||
case "DescribeInstances":
|
||||
reqParam := &struct {
|
||||
Parameters struct {
|
||||
Filters []*ec2.Filter `json:"Filters"`
|
||||
InstanceIds []*string `json:"InstanceIds"`
|
||||
} `json:"parameters"`
|
||||
}{}
|
||||
json.Unmarshal([]byte(body), reqParam)
|
||||
|
||||
params := &ec2.DescribeInstancesInput{}
|
||||
if len(reqParam.Parameters.Filters) > 0 {
|
||||
params.Filters = reqParam.Parameters.Filters
|
||||
}
|
||||
if len(reqParam.Parameters.InstanceIds) > 0 {
|
||||
params.InstanceIDs = reqParam.Parameters.InstanceIds
|
||||
}
|
||||
|
||||
resp, err := svc.DescribeInstances(params)
|
||||
if err != nil {
|
||||
c.JsonApiErr(500, "Unable to call AWS API", err)
|
||||
return
|
||||
}
|
||||
|
||||
respJson, _ := json.Marshal(resp)
|
||||
fmt.Fprint(c.RW(), string(respJson))
|
||||
default:
|
||||
c.JsonApiErr(500, "Unexpected EC2 action", errors.New(reqInfo.Action))
|
||||
}
|
||||
|
||||
params := &cloudwatch.GetMetricStatisticsInput{
|
||||
Namespace: aws.String(reqParam.Parameters.Namespace),
|
||||
MetricName: aws.String(reqParam.Parameters.MetricName),
|
||||
Dimensions: dimensions,
|
||||
Statistics: statistics,
|
||||
StartTime: aws.Time(time.Unix(reqParam.Parameters.StartTime, 0)),
|
||||
EndTime: aws.Time(time.Unix(reqParam.Parameters.EndTime, 0)),
|
||||
Period: aws.Int64(reqParam.Parameters.Period),
|
||||
}
|
||||
|
||||
resp, err := svc.GetMetricStatistics(params)
|
||||
if err != nil {
|
||||
c.JsonApiErr(500, "Unable to call AWS API", err)
|
||||
return
|
||||
}
|
||||
|
||||
respJson, _ := json.Marshal(resp)
|
||||
fmt.Fprint(c.RW(), string(respJson))
|
||||
case "ListMetrics":
|
||||
reqParam := &struct {
|
||||
Parameters struct {
|
||||
Namespace string `json:"Namespace"`
|
||||
MetricName string `json:"MetricName"`
|
||||
Dimensions []map[string]string `json:"Dimensions"`
|
||||
} `json:"parameters"`
|
||||
}{}
|
||||
json.Unmarshal([]byte(body), reqParam)
|
||||
|
||||
dimensions := make([]*cloudwatch.DimensionFilter, 0)
|
||||
for _, d := range reqParam.Parameters.Dimensions {
|
||||
dimensions = append(dimensions, &cloudwatch.DimensionFilter{
|
||||
Name: aws.String(d["Name"]),
|
||||
Value: aws.String(d["Value"]),
|
||||
})
|
||||
}
|
||||
|
||||
params := &cloudwatch.ListMetricsInput{
|
||||
Namespace: aws.String(reqParam.Parameters.Namespace),
|
||||
MetricName: aws.String(reqParam.Parameters.MetricName),
|
||||
Dimensions: dimensions,
|
||||
}
|
||||
|
||||
resp, err := svc.ListMetrics(params)
|
||||
if err != nil {
|
||||
c.JsonApiErr(500, "Unable to call AWS API", err)
|
||||
return
|
||||
}
|
||||
|
||||
respJson, _ := json.Marshal(resp)
|
||||
fmt.Fprint(c.RW(), string(respJson))
|
||||
default:
|
||||
c.JsonApiErr(500, "Unexpected CloudWatch action", errors.New(reqInfo.Action))
|
||||
c.JsonApiErr(500, "Unexpected service", errors.New(reqInfo.Service))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user