-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cloudwatch): add static helper function to grant metric writing … (
#258) * feat(cloudwatch): add static helper function to grant metric writing permissions This fixes #214. * Address review comments * Update changelog
- Loading branch information
Showing
7 changed files
with
108 additions
and
53 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
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,49 @@ | ||
import { Statistic } from "./metric"; | ||
|
||
export interface SimpleStatistic { | ||
type: 'simple'; | ||
statistic: Statistic; | ||
} | ||
|
||
export interface PercentileStatistic { | ||
type: 'percentile'; | ||
percentile: number; | ||
} | ||
/** | ||
* Parse a statistic, returning the type of metric that was used (simple or percentile) | ||
*/ | ||
export function parseStatistic(stat: string): SimpleStatistic | PercentileStatistic { | ||
const lowerStat = stat.toLowerCase(); | ||
|
||
// Simple statistics | ||
const statMap: {[k: string]: Statistic} = { | ||
average: Statistic.Average, | ||
avg: Statistic.Average, | ||
minimum: Statistic.Minimum, | ||
min: Statistic.Minimum, | ||
maximum: Statistic.Maximum, | ||
max: Statistic.Maximum, | ||
samplecount: Statistic.SampleCount, | ||
n: Statistic.SampleCount, | ||
sum: Statistic.Sum, | ||
}; | ||
|
||
if (lowerStat in statMap) { | ||
return { | ||
type: 'simple', | ||
statistic: statMap[lowerStat] | ||
}; | ||
} | ||
|
||
// Percentile statistics | ||
const re = /^p([\d.]+)$/; | ||
const m = re.exec(lowerStat); | ||
if (m) { | ||
return { | ||
type: 'percentile', | ||
percentile: parseFloat(m[1]) | ||
}; | ||
} | ||
|
||
throw new Error(`Not a valid statistic: '${stat}', must be one of Average | Minimum | Maximum | SampleCount | Sum | pNN.NN`); | ||
} |
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,33 @@ | ||
import { expect, haveResource } from '@aws-cdk/assert'; | ||
import { Anyone, Stack } from '@aws-cdk/core'; | ||
import { Role } from '@aws-cdk/iam'; | ||
import { Test } from 'nodeunit'; | ||
import { Metric } from '../lib'; | ||
|
||
export = { | ||
'metric grant'(test: Test) { | ||
// GIVEN | ||
const stack = new Stack(); | ||
const role = new Role(stack, 'SomeRole', { | ||
assumedBy: new Anyone() | ||
}); | ||
|
||
// WHEN | ||
Metric.grantPutMetricData(role); | ||
|
||
// THEN | ||
expect(stack).to(haveResource('AWS::IAM::Policy', { | ||
PolicyDocument: { | ||
Statement: [ | ||
{ | ||
Action: "cloudwatch:PutMetricData", | ||
Effect: "Allow", | ||
Resource: "*" | ||
} | ||
], | ||
}, | ||
})); | ||
|
||
test.done(); | ||
} | ||
}; |