Sha256: 5f623e21a1d211fe9b4159461a2427e3b961e8a8b70929faffbfb4e7532426e8

Contents?: true

Size: 1.94 KB

Versions: 3

Compression:

Stored size: 1.94 KB

Contents

module Awspec::Generator
  module Spec
    class Ec2
      include Awspec::Helper::Finder
      def generate_from_vpc(vpc_id)
        describes = %w(
          instance_id image_id private_dns_name public_dns_name
          instance_type private_ip_address public_ip_address
        )
        vpc = find_vpc(vpc_id)
        fail 'Not Found VPC' unless vpc
        @vpc_id = vpc[:vpc_id]
        @vpc_tag_name = vpc.tag_name
        instances = select_ec2_by_vpc_id(@vpc_id)
        specs = instances.map do |instance|
          instance_id = instance[:instance_id]
          instance_tag_name = instance.tag_name
          subnet = find_subnet(instance.subnet_id)
          subnet_tag_name = subnet.tag_name
          eips = select_eip_by_instance_id(instance_id)
          content = ERB.new(ec2_spec_template, nil, '-').result(binding).gsub(/^\n/, '')
        end
        specs.join("\n")
      end

      # rubocop:disable all
      def ec2_spec_template
        template = <<-'EOF'
<%- if instance_tag_name -%>
describe ec2('<%= instance_tag_name %>') do
<%- else -%>
describe ec2('<%= instance_id %>') do
<%- end -%>
  it { should exist }
  it { should be_<%= instance.state.name %> }
<% describes.each do |describe| %>
<%- if instance.key?(describe) -%>
<%- if instance[describe].is_a?(TrueClass) || instance[describe].is_a?(FalseClass) -%>
  its(:<%= describe %>) { should eq <%= instance[describe] %> }
<%- else -%>
  its(:<%= describe %>) { should eq '<%= instance[describe] %>' }
<%- end -%>
<%- end -%>
<% end %>
<% instance.security_groups.each do |sg| %>
  it { should have_security_group('<%= sg.group_name %>') }
<% end %>
<%- if @vpc_tag_name -%>
  it { should belong_to_vpc('<%= @vpc_tag_name %>') }
<%- else -%>
  it { should belong_to_vpc('<%= @vpc_id %>') }
<%- end -%>
  it { should belong_to_subnet('<%= subnet_tag_name %>') }
<% eips.each do |eip| %>
  it { should have_eip('<%= eip.public_ip %>') }
<% end %>
end
EOF
        template
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
awspec-0.2.2 lib/awspec/generator/spec/ec2.rb
awspec-0.2.1 lib/awspec/generator/spec/ec2.rb
awspec-0.2.0 lib/awspec/generator/spec/ec2.rb