Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
a21f53cf
Commit
a21f53cf
authored
Sep 26, 2017
by
Mitsuhiro Tanda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor cloudwatch code
parent
59cdd4d8
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
58 additions
and
96 deletions
+58
-96
pkg/tsdb/cloudwatch/annotation_query.go
+2
-9
pkg/tsdb/cloudwatch/cloudwatch.go
+11
-42
pkg/tsdb/cloudwatch/credentials.go
+42
-12
pkg/tsdb/cloudwatch/metric_find_query.go
+3
-33
No files found.
pkg/tsdb/cloudwatch/annotation_query.go
View file @
a21f53cf
...
...
@@ -6,7 +6,6 @@ import (
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/tsdb"
...
...
@@ -34,16 +33,10 @@ func (e *CloudWatchExecutor) executeAnnotationQuery(ctx context.Context, queryCo
actionPrefix
:=
parameters
.
Get
(
"actionPrefix"
)
.
MustString
(
""
)
alarmNamePrefix
:=
parameters
.
Get
(
"alarmNamePrefix"
)
.
MustString
(
""
)
dsInfo
:=
e
.
getDsInfo
(
region
)
cfg
,
err
:=
getAwsConfig
(
dsInfo
)
svc
,
err
:=
e
.
getClient
(
region
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Failed to call cloudwatch:ListMetrics"
)
}
sess
,
err
:=
session
.
NewSession
(
cfg
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Failed to call cloudwatch:ListMetrics"
)
return
nil
,
err
}
svc
:=
cloudwatch
.
New
(
sess
,
cfg
)
var
alarmNames
[]
*
string
if
usePrefixMatch
{
...
...
pkg/tsdb/cloudwatch/cloudwatch.go
View file @
a21f53cf
...
...
@@ -15,7 +15,6 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/grafana/grafana/pkg/components/null"
"github.com/grafana/grafana/pkg/components/simplejson"
...
...
@@ -26,6 +25,17 @@ type CloudWatchExecutor struct {
*
models
.
DataSource
}
type
DatasourceInfo
struct
{
Profile
string
Region
string
AuthType
string
AssumeRoleArn
string
Namespace
string
AccessKey
string
SecretKey
string
}
func
NewCloudWatchExecutor
(
dsInfo
*
models
.
DataSource
)
(
tsdb
.
TsdbQueryEndpoint
,
error
)
{
return
&
CloudWatchExecutor
{},
nil
}
...
...
@@ -113,47 +123,6 @@ func (e *CloudWatchExecutor) executeTimeSeriesQuery(ctx context.Context, queryCo
return
result
,
nil
}
func
(
e
*
CloudWatchExecutor
)
getClient
(
region
string
)
(
*
cloudwatch
.
CloudWatch
,
error
)
{
assumeRoleArn
:=
e
.
DataSource
.
JsonData
.
Get
(
"assumeRoleArn"
)
.
MustString
()
accessKey
:=
""
secretKey
:=
""
for
key
,
value
:=
range
e
.
DataSource
.
SecureJsonData
.
Decrypt
()
{
if
key
==
"accessKey"
{
accessKey
=
value
}
if
key
==
"secretKey"
{
secretKey
=
value
}
}
datasourceInfo
:=
&
DatasourceInfo
{
Region
:
region
,
Profile
:
e
.
DataSource
.
Database
,
AssumeRoleArn
:
assumeRoleArn
,
AccessKey
:
accessKey
,
SecretKey
:
secretKey
,
}
credentials
,
err
:=
GetCredentials
(
datasourceInfo
)
if
err
!=
nil
{
return
nil
,
err
}
cfg
:=
&
aws
.
Config
{
Region
:
aws
.
String
(
region
),
Credentials
:
credentials
,
}
sess
,
err
:=
session
.
NewSession
(
cfg
)
if
err
!=
nil
{
return
nil
,
err
}
client
:=
cloudwatch
.
New
(
sess
,
cfg
)
return
client
,
nil
}
func
(
e
*
CloudWatchExecutor
)
executeQuery
(
ctx
context
.
Context
,
parameters
*
simplejson
.
Json
,
queryContext
*
tsdb
.
TsdbQuery
)
(
*
tsdb
.
QueryResult
,
error
)
{
query
,
err
:=
parseQuery
(
parameters
)
if
err
!=
nil
{
...
...
pkg/tsdb/cloudwatch/credentials.go
View file @
a21f53cf
...
...
@@ -13,20 +13,10 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials/endpointcreds"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/sts"
)
type
DatasourceInfo
struct
{
Profile
string
Region
string
AuthType
string
AssumeRoleArn
string
Namespace
string
AccessKey
string
SecretKey
string
}
type
cache
struct
{
credential
*
credentials
.
Credentials
expiration
*
time
.
Time
...
...
@@ -150,7 +140,31 @@ func ec2RoleProvider(sess *session.Session) credentials.Provider {
return
&
ec2rolecreds
.
EC2RoleProvider
{
Client
:
ec2metadata
.
New
(
sess
),
ExpiryWindow
:
5
*
time
.
Minute
}
}
func
getAwsConfig
(
dsInfo
*
DatasourceInfo
)
(
*
aws
.
Config
,
error
)
{
func
(
e
*
CloudWatchExecutor
)
getDsInfo
(
region
string
)
*
DatasourceInfo
{
assumeRoleArn
:=
e
.
DataSource
.
JsonData
.
Get
(
"assumeRoleArn"
)
.
MustString
()
accessKey
:=
""
secretKey
:=
""
for
key
,
value
:=
range
e
.
DataSource
.
SecureJsonData
.
Decrypt
()
{
if
key
==
"accessKey"
{
accessKey
=
value
}
if
key
==
"secretKey"
{
secretKey
=
value
}
}
datasourceInfo
:=
&
DatasourceInfo
{
Region
:
region
,
Profile
:
e
.
DataSource
.
Database
,
AssumeRoleArn
:
assumeRoleArn
,
AccessKey
:
accessKey
,
SecretKey
:
secretKey
,
}
return
datasourceInfo
}
func
(
e
*
CloudWatchExecutor
)
getAwsConfig
(
dsInfo
*
DatasourceInfo
)
(
*
aws
.
Config
,
error
)
{
creds
,
err
:=
GetCredentials
(
dsInfo
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -162,3 +176,19 @@ func getAwsConfig(dsInfo *DatasourceInfo) (*aws.Config, error) {
}
return
cfg
,
nil
}
func
(
e
*
CloudWatchExecutor
)
getClient
(
region
string
)
(
*
cloudwatch
.
CloudWatch
,
error
)
{
datasourceInfo
:=
e
.
getDsInfo
(
region
)
cfg
,
err
:=
e
.
getAwsConfig
(
datasourceInfo
)
if
err
!=
nil
{
return
nil
,
err
}
sess
,
err
:=
session
.
NewSession
(
cfg
)
if
err
!=
nil
{
return
nil
,
err
}
client
:=
cloudwatch
.
New
(
sess
,
cfg
)
return
client
,
nil
}
pkg/tsdb/cloudwatch/metric_find_query.go
View file @
a21f53cf
...
...
@@ -210,30 +210,6 @@ func transformToTable(data []suggestData, result *tsdb.QueryResult) {
result
.
Meta
.
Set
(
"rowCount"
,
len
(
data
))
}
func
(
e
*
CloudWatchExecutor
)
getDsInfo
(
region
string
)
*
DatasourceInfo
{
assumeRoleArn
:=
e
.
DataSource
.
JsonData
.
Get
(
"assumeRoleArn"
)
.
MustString
()
accessKey
:=
""
secretKey
:=
""
for
key
,
value
:=
range
e
.
DataSource
.
SecureJsonData
.
Decrypt
()
{
if
key
==
"accessKey"
{
accessKey
=
value
}
if
key
==
"secretKey"
{
secretKey
=
value
}
}
datasourceInfo
:=
&
DatasourceInfo
{
Region
:
region
,
Profile
:
e
.
DataSource
.
Database
,
AssumeRoleArn
:
assumeRoleArn
,
AccessKey
:
accessKey
,
SecretKey
:
secretKey
,
}
return
datasourceInfo
}
// Whenever this list is updated, frontend list should also be updated.
// Please update the region list in public/app/plugins/datasource/cloudwatch/partials/config.html
func
(
e
*
CloudWatchExecutor
)
handleGetRegions
(
ctx
context
.
Context
,
parameters
*
simplejson
.
Json
,
queryContext
*
tsdb
.
TsdbQuery
)
([]
suggestData
,
error
)
{
...
...
@@ -460,16 +436,10 @@ func (e *CloudWatchExecutor) handleGetEc2InstanceAttribute(ctx context.Context,
}
func
(
e
*
CloudWatchExecutor
)
cloudwatchListMetrics
(
region
string
,
namespace
string
,
metricName
string
,
dimensions
[]
*
cloudwatch
.
DimensionFilter
)
(
*
cloudwatch
.
ListMetricsOutput
,
error
)
{
dsInfo
:=
e
.
getDsInfo
(
region
)
cfg
,
err
:=
getAwsConfig
(
dsInfo
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Failed to call cloudwatch:ListMetrics"
)
}
sess
,
err
:=
session
.
NewSession
(
cfg
)
svc
,
err
:=
e
.
getClient
(
region
)
if
err
!=
nil
{
return
nil
,
err
ors
.
New
(
"Failed to call cloudwatch:ListMetrics"
)
return
nil
,
err
}
svc
:=
cloudwatch
.
New
(
sess
,
cfg
)
params
:=
&
cloudwatch
.
ListMetricsInput
{
Namespace
:
aws
.
String
(
namespace
),
...
...
@@ -496,7 +466,7 @@ func (e *CloudWatchExecutor) cloudwatchListMetrics(region string, namespace stri
func
(
e
*
CloudWatchExecutor
)
ec2DescribeInstances
(
region
string
,
filters
[]
*
ec2
.
Filter
,
instanceIds
[]
*
string
)
(
*
ec2
.
DescribeInstancesOutput
,
error
)
{
dsInfo
:=
e
.
getDsInfo
(
region
)
cfg
,
err
:=
getAwsConfig
(
dsInfo
)
cfg
,
err
:=
e
.
getAwsConfig
(
dsInfo
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Failed to call ec2:DescribeInstances"
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment