|
| 1 | +import { Template, Match } from '@aws-cdk/assertions'; |
| 2 | +import * as cloudwatch from '@aws-cdk/aws-cloudwatch'; |
| 3 | +import * as iam from '@aws-cdk/aws-iam'; |
| 4 | +import * as iot from '@aws-cdk/aws-iot'; |
| 5 | +import * as cdk from '@aws-cdk/core'; |
| 6 | +import { CloudWatchAlarmAction, CloudWatchAlarmActionProps } from '../../lib/cloudwatch-alarm-action'; |
| 7 | + |
| 8 | +test('Default cloudwatch alarm action', () => { |
| 9 | + // Given |
| 10 | + const stack = new cdk.Stack(); |
| 11 | + const topicRule = new iot.TopicRule(stack, 'MyTopicRule', { |
| 12 | + sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, stateReason, stateValue FROM 'device/+/data'"), |
| 13 | + }); |
| 14 | + const alarmArn = 'arn:aws:cloudwatch:us-east-1:123456789012:alarm:MyAlarm'; |
| 15 | + const alarm = cloudwatch.Alarm.fromAlarmArn(stack, 'MyAlarm', alarmArn); |
| 16 | + const cloudWatchAlarmActionProps: CloudWatchAlarmActionProps = { |
| 17 | + stateReason: '${stateReason}', |
| 18 | + stateValue: '${stateValue}', |
| 19 | + }; |
| 20 | + |
| 21 | + // When |
| 22 | + topicRule.addAction(new CloudWatchAlarmAction(alarm, cloudWatchAlarmActionProps)); |
| 23 | + |
| 24 | + // Then |
| 25 | + Template.fromStack(stack).hasResourceProperties('AWS::IoT::TopicRule', { |
| 26 | + TopicRulePayload: { |
| 27 | + Actions: [ |
| 28 | + { |
| 29 | + CloudwatchAlarm: { |
| 30 | + AlarmName: 'MyAlarm', |
| 31 | + RoleArn: { |
| 32 | + 'Fn::GetAtt': ['MyTopicRuleTopicRuleActionRoleCE2D05DA', 'Arn'], |
| 33 | + }, |
| 34 | + StateReason: '${stateReason}', |
| 35 | + StateValue: '${stateValue}', |
| 36 | + }, |
| 37 | + }, |
| 38 | + ], |
| 39 | + }, |
| 40 | + }); |
| 41 | + |
| 42 | + Template.fromStack(stack).hasResourceProperties('AWS::IAM::Role', { |
| 43 | + AssumeRolePolicyDocument: { |
| 44 | + Statement: [ |
| 45 | + { |
| 46 | + Action: 'sts:AssumeRole', |
| 47 | + Effect: 'Allow', |
| 48 | + Principal: { |
| 49 | + Service: 'iot.amazonaws.com', |
| 50 | + }, |
| 51 | + }, |
| 52 | + ], |
| 53 | + Version: '2012-10-17', |
| 54 | + }, |
| 55 | + }); |
| 56 | + |
| 57 | + Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', { |
| 58 | + PolicyDocument: { |
| 59 | + Statement: [ |
| 60 | + { |
| 61 | + Action: 'cloudwatch:SetAlarmState', |
| 62 | + Effect: 'Allow', |
| 63 | + Resource: alarmArn, |
| 64 | + }, |
| 65 | + ], |
| 66 | + Version: '2012-10-17', |
| 67 | + }, |
| 68 | + PolicyName: 'MyTopicRuleTopicRuleActionRoleDefaultPolicy54A701F7', |
| 69 | + Roles: [{ Ref: 'MyTopicRuleTopicRuleActionRoleCE2D05DA' }], |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +test('can set stateReason', () => { |
| 74 | + // Given |
| 75 | + const stack = new cdk.Stack(); |
| 76 | + const topicRule = new iot.TopicRule(stack, 'MyTopicRule', { |
| 77 | + sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, stateReason, stateValue FROM 'device/+/data'"), |
| 78 | + }); |
| 79 | + const alarmArn = 'arn:aws:cloudwatch:us-east-1:123456789012:alarm:MyAlarm'; |
| 80 | + const alarm = cloudwatch.Alarm.fromAlarmArn(stack, 'MyAlarm', alarmArn); |
| 81 | + const cloudWatchAlarmActionProps: CloudWatchAlarmActionProps = { |
| 82 | + stateReason: 'Test SetAlarmState', |
| 83 | + stateValue: '${stateValue}', |
| 84 | + }; |
| 85 | + |
| 86 | + // When |
| 87 | + topicRule.addAction(new CloudWatchAlarmAction(alarm, cloudWatchAlarmActionProps)); |
| 88 | + |
| 89 | + // Then |
| 90 | + Template.fromStack(stack).hasResourceProperties('AWS::IoT::TopicRule', { |
| 91 | + TopicRulePayload: { |
| 92 | + Actions: [ |
| 93 | + Match.objectLike({ CloudwatchAlarm: { StateReason: 'Test SetAlarmState' } }), |
| 94 | + ], |
| 95 | + }, |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +test('can set stateValue', () => { |
| 100 | + // Given |
| 101 | + const stack = new cdk.Stack(); |
| 102 | + const topicRule = new iot.TopicRule(stack, 'MyTopicRule', { |
| 103 | + sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, stateReason, stateValue FROM 'device/+/data'"), |
| 104 | + }); |
| 105 | + const alarmArn = 'arn:aws:cloudwatch:us-east-1:123456789012:alarm:MyAlarm'; |
| 106 | + const alarm = cloudwatch.Alarm.fromAlarmArn(stack, 'MyAlarm', alarmArn); |
| 107 | + const cloudWatchAlarmActionProps: CloudWatchAlarmActionProps = { |
| 108 | + stateReason: '${stateReason}', |
| 109 | + stateValue: 'ALARM', |
| 110 | + }; |
| 111 | + |
| 112 | + // When |
| 113 | + topicRule.addAction(new CloudWatchAlarmAction(alarm, cloudWatchAlarmActionProps)); |
| 114 | + |
| 115 | + // Then |
| 116 | + Template.fromStack(stack).hasResourceProperties('AWS::IoT::TopicRule', { |
| 117 | + TopicRulePayload: { |
| 118 | + Actions: [ |
| 119 | + Match.objectLike({ CloudwatchAlarm: { StateValue: 'ALARM' } }), |
| 120 | + ], |
| 121 | + }, |
| 122 | + }); |
| 123 | +}); |
| 124 | + |
| 125 | +test('can set role', () => { |
| 126 | + // Given |
| 127 | + const stack = new cdk.Stack(); |
| 128 | + const topicRule = new iot.TopicRule(stack, 'MyTopicRule', { |
| 129 | + sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, stateReason, stateValue FROM 'device/+/data'"), |
| 130 | + }); |
| 131 | + const alarmArn = 'arn:aws:cloudwatch:us-east-1:123456789012:alarm:MyAlarm'; |
| 132 | + const alarm = cloudwatch.Alarm.fromAlarmArn(stack, 'MyAlarm', alarmArn); |
| 133 | + const role = iam.Role.fromRoleArn(stack, 'MyRole', 'arn:aws:iam::123456789012:role/ForTest'); |
| 134 | + const cloudWatchAlarmActionProps: CloudWatchAlarmActionProps = { |
| 135 | + stateReason: '${stateReason}', |
| 136 | + stateValue: '${stateValue}', |
| 137 | + role: role, |
| 138 | + }; |
| 139 | + |
| 140 | + // When |
| 141 | + topicRule.addAction(new CloudWatchAlarmAction(alarm, cloudWatchAlarmActionProps)); |
| 142 | + |
| 143 | + // Then |
| 144 | + Template.fromStack(stack).hasResourceProperties('AWS::IoT::TopicRule', { |
| 145 | + TopicRulePayload: { |
| 146 | + Actions: [ |
| 147 | + Match.objectLike({ CloudwatchAlarm: { RoleArn: 'arn:aws:iam::123456789012:role/ForTest' } }), |
| 148 | + ], |
| 149 | + }, |
| 150 | + }); |
| 151 | + |
| 152 | + Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', { |
| 153 | + PolicyName: 'MyRolePolicy64AB00A5', |
| 154 | + Roles: ['ForTest'], |
| 155 | + }); |
| 156 | +}); |
0 commit comments