|
| 1 | +package cloudwatch |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "io/ioutil" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go/aws" |
| 10 | + "github.com/aws/aws-sdk-go/service/cloudwatch" |
| 11 | + "github.com/aws/aws-sdk-go/service/ec2" |
| 12 | + "github.com/grafana/grafana/pkg/middleware" |
| 13 | + "github.com/grafana/grafana/pkg/util" |
| 14 | +) |
| 15 | + |
| 16 | +type actionHandler func(*cwRequest, *middleware.Context) |
| 17 | + |
| 18 | +var actionHandlers map[string]actionHandler |
| 19 | + |
| 20 | +type cwRequest struct { |
| 21 | + Region string `json:"region"` |
| 22 | + Action string `json:"action"` |
| 23 | + Body []byte `json:"-"` |
| 24 | +} |
| 25 | + |
| 26 | +func init() { |
| 27 | + actionHandlers = map[string]actionHandler{ |
| 28 | + "GetMetricStatistics": handleGetMetricStatistics, |
| 29 | + "ListMetrics": handleListMetrics, |
| 30 | + "DescribeInstances": handleDescribeInstances, |
| 31 | + "__GetRegions": handleGetRegions, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func handleGetMetricStatistics(req *cwRequest, c *middleware.Context) { |
| 36 | + svc := cloudwatch.New(&aws.Config{Region: aws.String(req.Region)}) |
| 37 | + |
| 38 | + reqParam := &struct { |
| 39 | + Parameters struct { |
| 40 | + Namespace string `json:"namespace"` |
| 41 | + MetricName string `json:"metricName"` |
| 42 | + Dimensions []*cloudwatch.Dimension `json:"dimensions"` |
| 43 | + Statistics []*string `json:"statistics"` |
| 44 | + StartTime int64 `json:"startTime"` |
| 45 | + EndTime int64 `json:"endTime"` |
| 46 | + Period int64 `json:"period"` |
| 47 | + } `json:"parameters"` |
| 48 | + }{} |
| 49 | + json.Unmarshal(req.Body, reqParam) |
| 50 | + |
| 51 | + params := &cloudwatch.GetMetricStatisticsInput{ |
| 52 | + Namespace: aws.String(reqParam.Parameters.Namespace), |
| 53 | + MetricName: aws.String(reqParam.Parameters.MetricName), |
| 54 | + Dimensions: reqParam.Parameters.Dimensions, |
| 55 | + Statistics: reqParam.Parameters.Statistics, |
| 56 | + StartTime: aws.Time(time.Unix(reqParam.Parameters.StartTime, 0)), |
| 57 | + EndTime: aws.Time(time.Unix(reqParam.Parameters.EndTime, 0)), |
| 58 | + Period: aws.Int64(reqParam.Parameters.Period), |
| 59 | + } |
| 60 | + |
| 61 | + resp, err := svc.GetMetricStatistics(params) |
| 62 | + if err != nil { |
| 63 | + c.JsonApiErr(500, "Unable to call AWS API", err) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + c.JSON(200, resp) |
| 68 | +} |
| 69 | + |
| 70 | +func handleListMetrics(req *cwRequest, c *middleware.Context) { |
| 71 | + svc := cloudwatch.New(&aws.Config{Region: aws.String(req.Region)}) |
| 72 | + reqParam := &struct { |
| 73 | + Parameters struct { |
| 74 | + Namespace string `json:"namespace"` |
| 75 | + MetricName string `json:"metricName"` |
| 76 | + Dimensions []*cloudwatch.DimensionFilter `json:"dimensions"` |
| 77 | + } `json:"parameters"` |
| 78 | + }{} |
| 79 | + |
| 80 | + json.Unmarshal(req.Body, reqParam) |
| 81 | + |
| 82 | + params := &cloudwatch.ListMetricsInput{ |
| 83 | + Namespace: aws.String(reqParam.Parameters.Namespace), |
| 84 | + MetricName: aws.String(reqParam.Parameters.MetricName), |
| 85 | + Dimensions: reqParam.Parameters.Dimensions, |
| 86 | + } |
| 87 | + |
| 88 | + resp, err := svc.ListMetrics(params) |
| 89 | + if err != nil { |
| 90 | + c.JsonApiErr(500, "Unable to call AWS API", err) |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + c.JSON(200, resp) |
| 95 | +} |
| 96 | + |
| 97 | +func handleDescribeInstances(req *cwRequest, c *middleware.Context) { |
| 98 | + svc := ec2.New(&aws.Config{Region: aws.String(req.Region)}) |
| 99 | + |
| 100 | + reqParam := &struct { |
| 101 | + Parameters struct { |
| 102 | + Filters []*ec2.Filter `json:"filters"` |
| 103 | + InstanceIds []*string `json:"instanceIds"` |
| 104 | + } `json:"parameters"` |
| 105 | + }{} |
| 106 | + json.Unmarshal(req.Body, reqParam) |
| 107 | + |
| 108 | + params := &ec2.DescribeInstancesInput{} |
| 109 | + if len(reqParam.Parameters.Filters) > 0 { |
| 110 | + params.Filters = reqParam.Parameters.Filters |
| 111 | + } |
| 112 | + if len(reqParam.Parameters.InstanceIds) > 0 { |
| 113 | + params.InstanceIDs = reqParam.Parameters.InstanceIds |
| 114 | + } |
| 115 | + |
| 116 | + resp, err := svc.DescribeInstances(params) |
| 117 | + if err != nil { |
| 118 | + c.JsonApiErr(500, "Unable to call AWS API", err) |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + c.JSON(200, resp) |
| 123 | +} |
| 124 | + |
| 125 | +func handleGetRegions(req *cwRequest, c *middleware.Context) { |
| 126 | + regions := []string{ |
| 127 | + "us-west-2", "us-west-1", "eu-west-1", "eu-central-1", "ap-southeast-1", |
| 128 | + "ap-southeast-2", "ap-northeast-1", "sa-east-1", |
| 129 | + } |
| 130 | + |
| 131 | + result := []interface{}{} |
| 132 | + for _, region := range regions { |
| 133 | + result = append(result, util.DynMap{"text": region, "value": region}) |
| 134 | + } |
| 135 | + |
| 136 | + c.JSON(200, result) |
| 137 | +} |
| 138 | + |
| 139 | +func HandleRequest(c *middleware.Context) { |
| 140 | + var req cwRequest |
| 141 | + req.Body, _ = ioutil.ReadAll(c.Req.Request.Body) |
| 142 | + json.Unmarshal(req.Body, &req) |
| 143 | + |
| 144 | + if handler, found := actionHandlers[req.Action]; !found { |
| 145 | + c.JsonApiErr(500, "Unexpected AWS Action", errors.New(req.Action)) |
| 146 | + return |
| 147 | + } else { |
| 148 | + handler(&req, c) |
| 149 | + } |
| 150 | +} |
0 commit comments