Module: AwsResourceMixin
- Included in:
- AwsPluralResourceMixin, AwsSingularResourceMixin
- Defined in:
- lib/resource_support/aws/aws_resource_mixin.rb
Instance Method Summary collapse
-
#catch_aws_errors ⇒ Object
Intercept AWS exceptions.
- #check_resource_param_names(raw_params: {}, allowed_params: [], allowed_scalar_name: nil, allowed_scalar_type: nil) ⇒ Object
- #initialize(resource_params = {}) ⇒ Object
- #inspec_runner ⇒ Object
-
#validate_params(resource_params) ⇒ Object
Default implementation of validate params accepts everything.
Instance Method Details
#catch_aws_errors ⇒ Object
Intercept AWS exceptions
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/resource_support/aws/aws_resource_mixin.rb', line 56 def catch_aws_errors 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') rescue Aws::Errors::ServiceError => e fail_resource e. end |
#check_resource_param_names(raw_params: {}, allowed_params: [], allowed_scalar_name: nil, allowed_scalar_type: nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/resource_support/aws/aws_resource_mixin.rb', line 20 def check_resource_param_names(raw_params: {}, allowed_params: [], allowed_scalar_name: nil, allowed_scalar_type: nil) # Some resources allow passing in a single ID value. Check and convert to hash if so. if allowed_scalar_name && !raw_params.is_a?(Hash) value_seen = raw_params if value_seen.is_a?(allowed_scalar_type) raw_params = { allowed_scalar_name => value_seen } else raise ArgumentError, 'If you pass a single value to the resource, it must ' \ "be a #{allowed_scalar_type}, not an #{value_seen.class}." end end # Remove all expected params from the raw param hash recognized_params = {} allowed_params.each do |expected_param| recognized_params[expected_param] = raw_params.delete(expected_param) if raw_params.key?(expected_param) end # Any leftovers are unwelcome unless raw_params.empty? raise ArgumentError, "Unrecognized resource param '#{raw_params.keys.first}'. Expected parameters: #{allowed_params.join(', ')}" end recognized_params end |
#initialize(resource_params = {}) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/resource_support/aws/aws_resource_mixin.rb', line 2 def initialize(resource_params = {}) validate_params(resource_params).each do |param, value| instance_variable_set(:"@#{param}", value) end catch_aws_errors do fetch_from_api end rescue ArgumentError => e # continue with ArgumentError if testing raise unless respond_to?(:inspec) raise Inspec::Exceptions::ResourceFailed, e. end |
#inspec_runner ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/resource_support/aws/aws_resource_mixin.rb', line 46 def inspec_runner # When running under inspec-cli, we have an 'inspec' method that # returns the runner. When running under unit tests, we don't # have that, but we still have to call this to pass something # (nil is OK) to the backend. # TODO: remove with https://github.com/chef/inspec-aws/issues/216 inspec if respond_to?(:inspec) end |
#validate_params(resource_params) ⇒ Object
Default implementation of validate params accepts everything.
16 17 18 |
# File 'lib/resource_support/aws/aws_resource_mixin.rb', line 16 def validate_params(resource_params) resource_params end |