inspec/lib/resources/aws/aws_billing_report.rb
Lynn Frank 2857d07151 Fixes resource examples
The opening and closing mechanic varied between all the various
resources. This changes them all to use a HEREDOC with a tilde
to remove leading whitespace. This removes the need for the
special method to trim the `#print_example` method from shell.

Signed-off-by: Franklin Webber <franklin.webber@gmail.com>
2019-03-19 11:25:41 -05:00

100 lines
2.7 KiB
Ruby

class AwsBillingReport < Inspec.resource(1)
name 'aws_billing_report'
supports platform: 'aws'
desc 'Verifies settings for AWS Cost and Billing Reports.'
example <<~EXAMPLE
describe aws_billing_report('inspec1') do
its('report_name') { should cmp 'inspec1' }
its('time_unit') { should cmp 'hourly' }
end
describe aws_billing_report(report: 'inspec1') do
it { should exist }
end
EXAMPLE
include AwsSingularResourceMixin
attr_reader :report_name, :time_unit, :format, :compression, :s3_bucket,
:s3_prefix, :s3_region
def to_s
"AWS Billing Report #{report_name}"
end
def hourly?
exists? ? time_unit.eql?('hourly') : nil
end
def daily?
exists? ? time_unit.eql?('daily') : nil
end
def zip?
exists? ? compression.eql?('zip') : nil
end
def gzip?
exists? ? compression.eql?('gzip') : nil
end
private
def validate_params(raw_params)
validated_params = check_resource_param_names(
raw_params: raw_params,
allowed_params: [:report_name],
allowed_scalar_name: :report_name,
allowed_scalar_type: String,
)
if validated_params.empty?
raise ArgumentError, "You must provide the parameter 'report_name' to aws_billing_report."
end
validated_params
end
def fetch_from_api
report = find_report(report_name)
@exists = !report.nil?
if exists?
@time_unit = report.time_unit.downcase
@format = report.format.downcase
@compression = report.compression.downcase
@s3_bucket = report.s3_bucket
@s3_prefix = report.s3_prefix
@s3_region = report.s3_region
end
end
def find_report(report_name)
pagination_opts = {}
found_report_def = nil
while found_report_def.nil?
api_result = backend.describe_report_definitions(pagination_opts)
next_token = api_result.next_token
found_report_def = api_result.report_definitions.find { |report_def| report_def.report_name == report_name }
pagination_opts = { next_token: next_token }
next if found_report_def.nil? && next_token # Loop again: didn't find it, but there are more results
break if found_report_def.nil? && next_token.nil? # Give up: didn't find it, no more results
end
found_report_def
end
def backend
@backend ||= BackendFactory.create(inspec_runner)
end
class Backend
class AwsClientApi < AwsBackendBase
AwsBillingReport::BackendFactory.set_default_backend(self)
self.aws_client_class = Aws::CostandUsageReportService::Client
def describe_report_definitions(query = {})
aws_service_client.describe_report_definitions(query)
end
end
end
end