lib/resources/aws/aws_ec2_instance.rb in inspec-4.3.2 vs lib/resources/aws/aws_ec2_instance.rb in inspec-4.6.3
- old
+ new
@@ -1,9 +1,12 @@
-# author: Christoph Hartmann
+require "resource_support/aws/aws_singular_resource_mixin"
+require "resource_support/aws/aws_backend_base"
+require "aws-sdk-ec2"
+
class AwsEc2Instance < Inspec.resource(1)
- name 'aws_ec2_instance'
- desc 'Verifies settings for an EC2 instance'
+ name "aws_ec2_instance"
+ desc "Verifies settings for an EC2 instance"
example <<~EXAMPLE
describe aws_ec2_instance('i-123456') do
it { should be_running }
it { should have_roles }
@@ -12,11 +15,11 @@
describe aws_ec2_instance(name: 'my-instance') do
it { should be_running }
it { should have_roles }
end
EXAMPLE
- supports platform: 'aws'
+ supports platform: "aws"
# TODO: rewrite to avoid direct injection, match other resources, use AwsSingularResourceMixin
def initialize(opts, conn = nil)
@opts = opts
@opts.is_a?(Hash) ? @display_name = @opts[:name] : @display_name = opts
@@ -31,11 +34,11 @@
yield
rescue Aws::Errors::MissingCredentialsError
# The AWS error here is unhelpful:
# "unable to sign request without credentials set"
Inspec::Log.error "It appears that you have not set your AWS credentials. You may set them using environment variables, or using the 'aws://region/aws_credentials_profile' target. See https://www.inspec.io/docs/reference/platforms for details."
- fail_resource('No AWS credentials available')
+ fail_resource("No AWS credentials available")
rescue Aws::Errors::ServiceError => e
fail_resource e.message
end
# TODO: DRY up, see https://github.com/chef/inspec/issues/2633
@@ -55,14 +58,14 @@
catch_aws_errors do
if @opts.is_a?(Hash)
first = @ec2_resource.instances(
{
filters: [{
- name: 'tag:Name',
+ name: "tag:Name",
values: [@opts[:name]],
}],
- },
+ }
).first
# catch case where the instance is not known
@instance_id = first.id unless first.nil?
else
@instance_id = @opts
@@ -86,11 +89,11 @@
# helper methods for each state
%w{
pending running shutting-down
terminated stopping stopped unknown
}.each do |state_name|
- define_method state_name.tr('-', '_') + '?' do
+ define_method state_name.tr("-", "_") + "?" do
state == state_name
end
end
# attributes that we want to expose
@@ -109,13 +112,13 @@
# Don't document this - it's a bit hard to use. Our current doctrine
# is to use dumb things, like arrays of strings - use security_group_ids instead.
def security_groups
catch_aws_errors do
- @security_groups ||= instance.security_groups.map { |sg|
+ @security_groups ||= instance.security_groups.map do |sg|
{ id: sg.group_id, name: sg.group_name }
- }
+ end
end
end
def security_group_ids
catch_aws_errors do
@@ -137,10 +140,10 @@
catch_aws_errors do
instance_profile = instance.iam_instance_profile
if instance_profile
roles = @iam_resource.instance_profile(
- instance_profile.arn.gsub(%r{^.*\/}, ''),
+ instance_profile.arn.gsub(%r{^.*\/}, "")
).roles
else
roles = nil
end