diff --git a/lib/awspec/command/generate.rb b/lib/awspec/command/generate.rb index e58d8119d..d11539da0 100644 --- a/lib/awspec/command/generate.rb +++ b/lib/awspec/command/generate.rb @@ -16,16 +16,30 @@ class Generate < Thor end end + types_for_generate_all = %w( + iam_policy cloudwatch_alarm + ) + desc 'route53_hosted_zone [example.com.]', 'Generate route53_hosted_zone spec from Domain name' def route53_hosted_zone(hosted_zone) load_secrets puts Awspec::Generator::Spec::Route53HostedZone.new.generate_by_domain_name(hosted_zone) end - desc 'iam_policy', 'Generate attached iam_policy spec' - def iam_policy - load_secrets - puts Awspec::Generator::Spec::IamPolicy.new.generate_all + types_for_generate_all = %w( + iam_policy cloudwatch_alarm + ) + + types_for_generate_all.each do |type| + if type == 'iam_policy' + desc 'iam_policy', 'Generate attached iam_policy spec' + else + desc type, "Generate #{type} spec" + end + define_method type do + load_secrets + eval "puts Awspec::Generator::Spec::#{type.camelize}.new.generate_all" + end end no_commands do diff --git a/lib/awspec/generator.rb b/lib/awspec/generator.rb index b210a64f2..b253ac4ba 100644 --- a/lib/awspec/generator.rb +++ b/lib/awspec/generator.rb @@ -8,6 +8,7 @@ require 'awspec/generator/spec/route53_hosted_zone' require 'awspec/generator/spec/elb' require 'awspec/generator/spec/iam_policy' +require 'awspec/generator/spec/cloudwatch_alarm' # Doc require 'awspec/generator/doc/type' diff --git a/lib/awspec/generator/spec/cloudwatch_alarm.rb b/lib/awspec/generator/spec/cloudwatch_alarm.rb new file mode 100644 index 000000000..bec17aa40 --- /dev/null +++ b/lib/awspec/generator/spec/cloudwatch_alarm.rb @@ -0,0 +1,40 @@ +module Awspec::Generator + module Spec + class CloudwatchAlarm + include Awspec::Helper::Finder + def generate_all + alarms = select_all_cloudwatch_alarms + alarms.empty? && fail('Not Found alarm') + ERB.new(alarm_spec_template, nil, '-').result(binding).chomp + end + + def alarm_spec_template + template = <<-'EOF' +<% alarms.each do |alarm| %> +describe cloudwatch_alarm('<%= alarm.alarm_name %>') do + it { should exist } +<%- alarm.ok_actions.each do |action| -%> + it { should have_ok_action('<%= action %>') } +<%- end -%> +<%- alarm.alarm_actions.each do |action| -%> + it { should have_alarm_action('<%= action %>') } +<%- end -%> +<%- alarm.insufficient_data_actions.each do |action| -%> + it { should have_insufficient_data_action('<%= action %>') } +<%- end -%> + it { should belong_to_metric('<%= alarm.metric_name %>').namespace('<%= alarm.namespace %>') } + its(:state_value) { should eq '<%= alarm.state_value %>' } + its(:statistic) { should eq '<%= alarm.statistic %>' } + its(:period) { should eq <%= alarm.period %> } + its(:unit) { should eq '<%= alarm.unit %>' } + its(:evaluation_periods) { should eq <%= alarm.evaluation_periods %> } + its(:threshold) { should eq <%= alarm.threshold %> } + its(:comparison_operator) { should eq '<%= alarm.comparison_operator %>' } +end +<% end %> +EOF + template + end + end + end +end diff --git a/lib/awspec/helper/finder/cloudwatch.rb b/lib/awspec/helper/finder/cloudwatch.rb index 1fe31e074..4d7e7a96a 100644 --- a/lib/awspec/helper/finder/cloudwatch.rb +++ b/lib/awspec/helper/finder/cloudwatch.rb @@ -12,6 +12,18 @@ def find_cloudwatch_alarm(id) alarm[:alarm_arn] == id end end + + def select_all_cloudwatch_alarms + selected = [] + res = @cloudwatch_client.describe_alarms + + loop do + selected += res.metric_alarms + (res.next_page? && res = res.next_page) || break + end + + selected + end end end end diff --git a/lib/awspec/stub/cloudwatch_alarm.rb b/lib/awspec/stub/cloudwatch_alarm.rb index 244f5a0db..51c91792d 100644 --- a/lib/awspec/stub/cloudwatch_alarm.rb +++ b/lib/awspec/stub/cloudwatch_alarm.rb @@ -28,7 +28,7 @@ } ], period: 300, - unit: nil, + unit: 'Seconds', evaluation_periods: 1, threshold: 5.0, comparison_operator: 'LessThanOrEqualToThreshold' diff --git a/spec/generator/spec/cloudwatch_alarm_spec.rb b/spec/generator/spec/cloudwatch_alarm_spec.rb new file mode 100644 index 000000000..aaeaee1c8 --- /dev/null +++ b/spec/generator/spec/cloudwatch_alarm_spec.rb @@ -0,0 +1,28 @@ +require 'spec_helper' + +describe 'Awspec::Generator::Spec::CloudwatchAlarm' do + before do + Awspec::Stub.load 'cloudwatch_alarm' + end + let(:cloudwatch_alarm) { Awspec::Generator::Spec::CloudwatchAlarm.new } + it 'generate_all generate spec' do + spec = <<-'EOF' + +describe cloudwatch_alarm('my-cloudwatch-alarm') do + it { should exist } + it { should have_ok_action('arn:aws:sns:ap-northeast-1:1234567890:sns_alert') } + it { should have_alarm_action('arn:aws:sns:ap-northeast-1:1234567890:sns_alert') } + it { should have_insufficient_data_action('arn:aws:sns:ap-northeast-1:1234567890:sns_alert') } + it { should belong_to_metric('NumberOfProcesses').namespace('my-cloudwatch-namespace') } + its(:state_value) { should eq 'OK' } + its(:statistic) { should eq 'Average' } + its(:period) { should eq 300 } + its(:unit) { should eq 'Seconds' } + its(:evaluation_periods) { should eq 1 } + its(:threshold) { should eq 5.0 } + its(:comparison_operator) { should eq 'LessThanOrEqualToThreshold' } +end +EOF + expect(cloudwatch_alarm.generate_all.to_s.gsub(/\n/, "\n")).to eq spec + end +end