Skip to content

Commit

Permalink
Using rescue to hold AWS::SNS::Client exception
Browse files Browse the repository at this point in the history
That will allow to evaluate the "should exist" statement and raise the Awspec::NoExistingResource anywhere else.
  • Loading branch information
Alceu Rodrigues de Freitas Junior committed Feb 9, 2019
1 parent 5c02ff9 commit 11b06f4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion doc/resource_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -2551,7 +2551,7 @@ end
```


### its(:vpc_id), its(:db_instance_identifier), its(:db_instance_class), its(:engine), its(:db_instance_status), its(:master_username), its(:db_name), its(:endpoint), its(:allocated_storage), its(:instance_create_time), its(:preferred_backup_window), its(:backup_retention_period), its(:db_security_groups), its(:availability_zone), its(:preferred_maintenance_window), its(:pending_modified_values), its(:latest_restorable_time), its(:multi_az), its(:engine_version), its(:auto_minor_version_upgrade), its(:read_replica_source_db_instance_identifier), its(:read_replica_db_instance_identifiers), its(:read_replica_db_cluster_identifiers), its(:license_model), its(:iops), its(:character_set_name), its(:secondary_availability_zone), its(:publicly_accessible), its(:status_infos), its(:storage_type), its(:tde_credential_arn), its(:db_instance_port), its(:db_cluster_identifier), its(:storage_encrypted), its(:kms_key_id), its(:dbi_resource_id), its(:ca_certificate_identifier), its(:domain_memberships), its(:copy_tags_to_snapshot), its(:monitoring_interval), its(:enhanced_monitoring_resource_arn), its(:monitoring_role_arn), its(:promotion_tier), its(:db_instance_arn), its(:timezone), its(:iam_database_authentication_enabled), its(:performance_insights_enabled), its(:performance_insights_kms_key_id), its(:performance_insights_retention_period), its(:enabled_cloudwatch_logs_exports), its(:processor_features), its(:deletion_protection), its(:listener_endpoint)
### its(:vpc_id), its(:db_instance_identifier), its(:db_instance_class), its(:engine), its(:db_instance_status), its(:master_username), its(:db_name), its(:endpoint), its(:allocated_storage), its(:instance_create_time), its(:preferred_backup_window), its(:backup_retention_period), its(:db_security_groups), its(:availability_zone), its(:preferred_maintenance_window), its(:pending_modified_values), its(:latest_restorable_time), its(:multi_az), its(:engine_version), its(:auto_minor_version_upgrade), its(:read_replica_source_db_instance_identifier), its(:read_replica_db_instance_identifiers), its(:read_replica_db_cluster_identifiers), its(:license_model), its(:iops), its(:character_set_name), its(:secondary_availability_zone), its(:publicly_accessible), its(:status_infos), its(:storage_type), its(:tde_credential_arn), its(:db_instance_port), its(:db_cluster_identifier), its(:storage_encrypted), its(:kms_key_id), its(:dbi_resource_id), its(:ca_certificate_identifier), its(:domain_memberships), its(:copy_tags_to_snapshot), its(:monitoring_interval), its(:enhanced_monitoring_resource_arn), its(:monitoring_role_arn), its(:promotion_tier), its(:db_instance_arn), its(:timezone), its(:iam_database_authentication_enabled), its(:performance_insights_enabled), its(:performance_insights_kms_key_id), its(:performance_insights_retention_period), its(:enabled_cloudwatch_logs_exports), its(:processor_features), its(:deletion_protection), its(:associated_roles), its(:listener_endpoint)
### :unlock: Advanced use

`rds` can use `Aws::RDS::DBInstance` resource (see http://docs.aws.amazon.com/sdkforruby/api/Aws/RDS/DBInstance.html).
Expand Down
48 changes: 42 additions & 6 deletions lib/awspec/helper/finder/sns_topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,52 @@ def to_s
end

def find_sns_topic(topic_arn)
response = sns_client.get_topic_attributes({ topic_arn: topic_arn })
SnsTopic.new(response.attributes)
# Find a SNS topic by searching from it's ARN.
# Expects as parameter the SNS topic ARN.
# Returns a Awspec::Helper::Finder::SNSTopic::SnsTopic if the topic is
# found, nil otherwise.
# If the SNS topic does not exist, AWS SDK throws an exception
# which is acceptable in some cases, but in others will generate a
# NoMethodFound during tests execution.
# In order to avoid that and follow a uniform interface, the exception
# will be captured and nil will be returned instead.
# By convention, the returned object will be checked (see check_existence
# method from Awspec::Type::Base class for an example) and an exception
# Awspec::NoExistingResource will be raised.
begin
response = sns_client.get_topic_attributes({ topic_arn: topic_arn })
puts response
topic = SnsTopic.new(response.attributes)
rescue Aws::SNS::Errors::NotFound
topic = nil
end

topic
end

def find_sns_topic_subs(topic_arn)
response = sns_client.list_subscriptions_by_topic({ topic_arn: topic_arn })
subscriptions = {}
response.subscriptions.each do |subscribed|
subscriptions[subscribed['subscription_arn'].to_sym] = subscribed
# Find a SNS topic subscribers by searching from it's ARN.
# Expects as parameter the SNS topic ARN.
# Returns a map (with keys as the subscribed ARN and the value instances
# of respective objects) if the topic is found, nil otherwise.
# If the SNS topic does not exist, AWS SDK throws an exception
# which is acceptable in some cases, but in others will generate a
# NoMethodFound during tests execution.
# In order to avoid that and follow a uniform interface, the exception
# will be captured and nil will be returned instead.
# By convention, the returned object will be checked (see check_existence
# method from Awspec::Type::Base class for an example) and an exception
# Awspec::NoExistingResource will be raised.
begin
response = sns_client.list_subscriptions_by_topic({ topic_arn: topic_arn })
subscriptions = {}
response.subscriptions.each do |subscribed|
subscriptions[subscribed['subscription_arn'].to_sym] = subscribed
end
rescue Aws::SNS::Errors::NotFound
subscriptions = nil
end

subscriptions
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/awspec/type/sns_topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def subscribed(subscribed_arn)
end

def method_missing(method_name)
check_existence
# delegates the method invocation to Awspec::Helper::Finder::SnsTopic::SnsTopic class
@resource_via_client.send method_name
end
Expand All @@ -42,6 +43,7 @@ def method_missing(method_name)

def fetch_subscriptions
@subscriptions = find_sns_topic_subs(@topic_arn) if @subscriptions.nil?
raise Awspec::NoExistingResource.new(self.class, @display_name) if @subscriptions.nil?
@subscriptions
end
end
Expand Down

0 comments on commit 11b06f4

Please sign in to comment.