Skip to content

Commit

Permalink
Specify Period and EvaluationPeriods (#10)
Browse files Browse the repository at this point in the history
* Specify Period and EvaluationPeriods #7

* fix linting

* add namespace to properties
  • Loading branch information
gcphost authored and sbstjn committed May 5, 2018
1 parent 2726bd3 commit 0918825
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,27 @@ class Alarm {
}
}

resourceProperties (value) {
if (value instanceof Object) {
return value
}

return {
value
}
}

ressources () {
return this.thresholds.map(
(value, i) => {
(props, i) => {
const properties = this.resourceProperties(props)

const config = {
[this.formatAlarmName(value)]: {
[this.formatAlarmName(properties.value)]: {
Type: 'AWS::CloudWatch::Alarm',
Properties: {
AlarmDescription: util.format('Alarm if queue contains more than %s messages', value),
Namespace: 'AWS/SQS',
AlarmDescription: util.format('Alarm if queue contains more than %s messages', properties.value),
Namespace: properties.namespace || 'AWS/SQS',
MetricName: 'ApproximateNumberOfMessagesVisible',
Dimensions: [
{
Expand All @@ -51,9 +63,9 @@ class Alarm {
}
],
Statistic: 'Sum',
Period: 60,
EvaluationPeriods: 1,
Threshold: value,
Period: properties.period || 60,
EvaluationPeriods: properties.evaluationPeriods || 1,
Threshold: properties.value,
ComparisonOperator: 'GreaterThanOrEqualToThreshold',
AlarmActions: [
{ 'Fn::Join': [ '', [ 'arn:aws:sns:' + this.region + ':', { 'Ref': 'AWS::AccountId' }, ':' + this.topic ] ] }
Expand All @@ -66,13 +78,13 @@ class Alarm {
}

if (this.name) {
config[this.formatAlarmName(value)].Properties.AlarmName = util.format('%s-%s-%d', this.name, this.queue, value)
config[this.formatAlarmName(properties.value)].Properties.AlarmName = util.format('%s-%s-%d', this.name, this.queue, properties.value)
}

if (this.treatMissingData) {
let treatMissing = this.resolveTreatMissingData(i)
if (treatMissing) {
config[this.formatAlarmName(value)].Properties.TreatMissingData = treatMissing
config[this.formatAlarmName(properties.value)].Properties.TreatMissingData = treatMissing
}
}
return config
Expand Down
53 changes: 53 additions & 0 deletions test/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,56 @@ describe('alarm treatMissingData', () => {
})
})
})

it('creates CloudFormation configuration with custom thresholds', () => {
let config = {
getProvider: () => ({ getRegion: () => 'test-region' }),
service: {
custom: {
'sqs-alarms': [
{
queue: 'test-queue',
topic: 'test-topic',
thresholds: [
{
value: 1,
period: 5,
evaluationPeriods: 1
},
{
value: 2,
period: 5,
evaluationPeriods: 1
},
{
value: 3,
period: 5,
evaluationPeriods: 1,
namespace: 'test'
}
]
}
]
},
provider: {
compiledCloudFormationTemplate: {
Resources: {}
}
}
}
}

const test = new Plugin(config)
test.beforeDeployResources()

const data = config.service.provider.compiledCloudFormationTemplate.Resources

expect(data).toHaveProperty('testqueueMessageAlarm3')
expect(data).toHaveProperty('testqueueMessageAlarm3.Type', 'AWS::CloudWatch::Alarm')
expect(data).toHaveProperty('testqueueMessageAlarm3.Properties')
expect(data).toHaveProperty('testqueueMessageAlarm3.Properties.AlarmDescription', 'Alarm if queue contains more than 3 messages')
expect(data).toHaveProperty('testqueueMessageAlarm3.Properties.Threshold', 3)
expect(data).toHaveProperty('testqueueMessageAlarm3.Properties.EvaluationPeriods', 1)
expect(data).toHaveProperty('testqueueMessageAlarm3.Properties.Period', 5)
expect(data).toHaveProperty('testqueueMessageAlarm3.Properties.Namespace', 'test')
})

0 comments on commit 0918825

Please sign in to comment.