2017-11-22 19:04:13 +00:00
|
|
|
class AwsCloudwatchAlarm < Inspec.resource(1)
|
|
|
|
name 'aws_cloudwatch_alarm'
|
|
|
|
desc <<-EOD
|
|
|
|
# Look for a specific alarm
|
|
|
|
aws_cloudwatch_alarm(
|
|
|
|
metric: 'my-metric-name',
|
|
|
|
metric_namespace: 'my-metric-namespace',
|
|
|
|
) do
|
|
|
|
it { should exist }
|
|
|
|
end
|
|
|
|
EOD
|
2018-02-08 04:26:37 +00:00
|
|
|
supports platform: 'aws'
|
2017-11-22 19:04:13 +00:00
|
|
|
|
2018-02-08 04:26:37 +00:00
|
|
|
include AwsSingularResourceMixin
|
|
|
|
attr_reader :alarm_actions, :alarm_name, :metric_name, :metric_namespace
|
2017-11-22 19:04:13 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-12-04 18:32:13 +00:00
|
|
|
def validate_params(raw_params)
|
|
|
|
recognized_params = check_resource_param_names(
|
|
|
|
raw_params: raw_params,
|
|
|
|
allowed_params: [:metric_name, :metric_namespace],
|
|
|
|
)
|
2017-11-22 19:04:13 +00:00
|
|
|
validated_params = {}
|
|
|
|
# Currently you must specify exactly metric_name and metric_namespace
|
|
|
|
[:metric_name, :metric_namespace].each do |param|
|
2017-12-04 18:32:13 +00:00
|
|
|
raise ArgumentError, "Missing resource param #{param}" unless recognized_params.key?(param)
|
|
|
|
validated_params[param] = recognized_params.delete(param)
|
2017-11-22 19:04:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
validated_params
|
|
|
|
end
|
|
|
|
|
2018-02-08 04:26:37 +00:00
|
|
|
def fetch_from_api
|
|
|
|
aws_alarms = BackendFactory.create(inspec_runner).describe_alarms_for_metric(
|
2017-11-22 19:04:13 +00:00
|
|
|
metric_name: @metric_name,
|
|
|
|
namespace: @metric_namespace,
|
|
|
|
)
|
|
|
|
if aws_alarms.metric_alarms.empty?
|
|
|
|
@exists = false
|
|
|
|
elsif aws_alarms.metric_alarms.count > 1
|
|
|
|
alarms = aws_alarms.metric_alarms.map(&:alarm_name)
|
|
|
|
raise 'More than one Cloudwatch Alarm was matched. Try using ' \
|
|
|
|
"more specific resource parameters. Alarms matched: #{alarms.join(', ')}"
|
|
|
|
else
|
|
|
|
@alarm_actions = aws_alarms.metric_alarms.first.alarm_actions
|
|
|
|
@alarm_name = aws_alarms.metric_alarms.first.alarm_name
|
|
|
|
@exists = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Backend
|
2018-02-08 04:26:37 +00:00
|
|
|
class AwsClientApi < AwsBackendBase
|
|
|
|
AwsCloudwatchAlarm::BackendFactory.set_default_backend(self)
|
|
|
|
self.aws_client_class = Aws::CloudWatch::Client
|
|
|
|
|
|
|
|
def describe_alarms_for_metric(query)
|
|
|
|
aws_service_client.describe_alarms_for_metric(query)
|
2017-11-22 19:04:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|