Skip to content

Commit

Permalink
Merge pull request #57 from k1LoW/add-command-generate-cloudwatch_alarm
Browse files Browse the repository at this point in the history
Add command `awspec generate cloudwatch_alarm`
  • Loading branch information
k1LoW committed Oct 20, 2015
2 parents 887a11c + 7b45d2f commit 2a16fdc
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 5 deletions.
22 changes: 18 additions & 4 deletions lib/awspec/command/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/awspec/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
40 changes: 40 additions & 0 deletions lib/awspec/generator/spec/cloudwatch_alarm.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions lib/awspec/helper/finder/cloudwatch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/awspec/stub/cloudwatch_alarm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
}
],
period: 300,
unit: nil,
unit: 'Seconds',
evaluation_periods: 1,
threshold: 5.0,
comparison_operator: 'LessThanOrEqualToThreshold'
Expand Down
28 changes: 28 additions & 0 deletions spec/generator/spec/cloudwatch_alarm_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2a16fdc

Please sign in to comment.