Skip to content
This repository has been archived by the owner on Jun 5, 2020. It is now read-only.

Commit

Permalink
Revert "Merge pull request #99 from garethr/avoid-exceptions-in-prefe…
Browse files Browse the repository at this point in the history
…tch"

This reverts commit 5e48527, reversing
changes made to c4b2d2a.
  • Loading branch information
Iristyle committed Mar 17, 2015
1 parent 29aa029 commit 074c0cc
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 61 deletions.
4 changes: 1 addition & 3 deletions lib/puppet/provider/ec2_autoscalinggroup/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ def self.prefetch(resources)
def self.group_to_hash(region, group)
subnet_names = []
unless group.vpc_zone_identifier.nil? || group.vpc_zone_identifier.empty?
response = ec2_client(region).describe_subnets(filters: [
{name: 'subnet-id', values: group.vpc_zone_identifier.to_s.split(',')}
])
response = ec2_client(region).describe_subnets(subnet_ids: group.vpc_zone_identifier.to_s.split(','))
subnet_names = response.data.subnets.collect do |subnet|
subnet_name_tag = subnet.tags.detect { |tag| tag.key == 'Name' }
subnet_name_tag ? subnet_name_tag.value : nil
Expand Down
7 changes: 3 additions & 4 deletions lib/puppet/provider/ec2_instance/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ def self.instance_to_hash(region, instance)
end
subnet_name = nil
if instance.subnet_id
subnet_response = ec2_client(region).describe_subnets(filters: [
{name: 'subnet-id', values: [instance.subnet_id]}
])
subnet_name = subnet_response.data.subnets.empty? ? nil : name_from_tag(subnet_response.data.subnets.first)
subnet_response = ec2_client(region).describe_subnets(subnet_ids: [instance.subnet_id])
subnet_name_tag = subnet_response.data.subnets.first.tags.detect { |tag| tag.key == 'Name' }
end
subnet_name = subnet_name_tag ? subnet_name_tag.value : nil

devices = instance.block_device_mappings.collect do |mapping|
{
Expand Down
12 changes: 8 additions & 4 deletions lib/puppet/provider/ec2_launchconfiguration/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ def self.prefetch(resources)
read_only(:region, :image_id, :instance_type, :key_name, :security_groups)

def self.config_to_hash(region, config)
group_response = ec2_client(region).describe_security_groups(filters: [
{name: 'group-id', values: config.security_groups}
])
security_group_names = group_response.data.security_groups.collect(&:group_name)
# It appears possible to get launch configurations manually to a state where
# they return the identifier of an invalid or a non-existent security groups
security_group_names = begin
group_response = ec2_client(region).describe_security_groups(group_ids: config.security_groups)
group_response.data.security_groups.collect(&:group_name)
rescue Aws::EC2::Errors::InvalidGroupIdMalformed, Aws::EC2::Errors::InvalidGroupNotFound
[]
end
{
name: config.launch_configuration_name,
security_groups: security_group_names,
Expand Down
27 changes: 11 additions & 16 deletions lib/puppet/provider/ec2_securitygroup/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,26 @@ def self.format_ingress_rules(client, group)
rule.user_id_group_pairs.collect do |security_group|
name = security_group.group_name
if name.nil?
group_response = client.describe_security_groups(filters: [
{name: 'group-id', values: [security_group.group_id]}
])
groups = group_response.data.security_groups
name = groups.empty? ? nil : groups.first.group_name
end
if name
{
'security_group' => name
}
else
nil
group_response = client.describe_security_groups(
group_ids: [security_group.group_id]
)
name = group_response.data.security_groups.first.group_name
end
{
'security_group' => name
}
end
end
end.flatten.uniq.compact
end.flatten.uniq
end

def self.security_group_to_hash(region, group)
ec2 = ec2_client(region)
vpc_name = nil
if group.vpc_id
vpc_response = ec2.describe_vpcs(filters: [
{name: 'vpc-id', values: [group.vpc_id]}
])
vpc_response = ec2.describe_vpcs(
vpc_ids: [group.vpc_id]
)
vpc_name = if vpc_response.data.vpcs.empty?
nil
elsif vpc_response.data.vpcs.first.to_hash.keys.include?(:group_name)
Expand Down
8 changes: 4 additions & 4 deletions lib/puppet/provider/ec2_vpc/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ def self.prefetch(resources)

def self.vpc_to_hash(region, vpc)
options_name = unless vpc.dhcp_options_id.nil? || vpc.dhcp_options_id.empty?
response = ec2_client(region).describe_dhcp_options(filters: [
{name: 'dhcp-options-id', values: [vpc.dhcp_options_id]}
])
response.data.dhcp_options.empty? ? nil : name_from_tag(response.data.dhcp_options.first)
response = ec2_client(region).describe_dhcp_options(
dhcp_options_ids: [vpc.dhcp_options_id]
)
name_from_tag(response.dhcp_options.first)
else
nil
end
Expand Down
5 changes: 2 additions & 3 deletions lib/puppet/provider/ec2_vpc_internet_gateway/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,15 @@ def self.gateway_to_hash(region, gateway)
vpc_id = nil
attachments = gateway.attachments.map(&:vpc_id)
if assigned_name and ! attachments.empty?
vpc_response = ec2_client(region).describe_vpcs(filters: [
{name: 'vpc-id', values: attachments}
])
vpc_response = ec2_client(region).describe_vpcs(vpc_ids: attachments)
vpc = vpc_response.data.vpcs.first
vpc_name_tag = vpc.tags.detect { |tag| tag.key == 'Name' }
if vpc_name_tag
vpc_name = vpc_name_tag.value
vpc_id = vpc.vpc_id
end
end

{
name: assigned_name,
vpc: vpc_name,
Expand Down
9 changes: 3 additions & 6 deletions lib/puppet/provider/ec2_vpc_routetable/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,8 @@ def self.route_to_hash(region, route)

def self.route_table_to_hash(region, table)
ec2 = ec2_client(region)
vpc_response = ec2.describe_vpcs(filters: [
{name: 'vpc-id', values: [table.vpc_id]}
])

vpc_name = vpc_response.data.vpcs.empty? ? nil : name_from_tag(vpc_response.data.vpcs.first)
vpc_response = ec2.describe_vpcs(vpc_ids: [table.vpc_id])
vpc_name_tag = vpc_response.data.vpcs.first.tags.detect { |tag| tag.key == 'Name' }

routes = table.routes.collect do |route|
route_to_hash(region, route)
Expand All @@ -68,7 +65,7 @@ def self.route_table_to_hash(region, table)
{
name: name_from_tag(table),
id: table.route_table_id,
vpc: vpc_name,
vpc: vpc_name_tag ? vpc_name_tag.value : nil,
ensure: :present,
routes: routes.reject(&:nil?),
region: region,
Expand Down
14 changes: 6 additions & 8 deletions lib/puppet/provider/ec2_vpc_subnet/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,23 @@ def self.prefetch(resources)

def self.subnet_to_hash(region, subnet)
ec2 = ec2_client(region)
vpc_response = ec2.describe_vpcs(filters: [
{name: 'vpc-id', values: [subnet.vpc_id]}
])

vpc_name = vpc_response.data.vpcs.empty? ? nil : name_from_tag(vpc_response.data.vpcs.first)
vpc_response = ec2.describe_vpcs(vpc_ids: [subnet.vpc_id])
vpc_name_tag = vpc_response.data.vpcs.first.tags.detect { |tag| tag.key == 'Name' }

table_response = ec2.describe_route_tables(filters: [
{name: 'association.subnet-id', values: [subnet.subnet_id]},
{name: 'vpc-id', values: [subnet.vpc_id]},
])
table_name = table_response.data.route_tables.empty? ? nil : name_from_tag(table_response.data.route_tables.first)
tables = table_response.data.route_tables
table_name_tag = tables.empty? ? nil : tables.first.tags.detect { |tag| tag.key == 'Name' }

{
name: name_from_tag(subnet),
route_table: table_name,
route_table: table_name_tag ? table_name_tag.value : nil,
id: subnet.subnet_id,
cidr_block: subnet.cidr_block,
availability_zone: subnet.availability_zone,
vpc: vpc_name,
vpc: vpc_name_tag ? vpc_name_tag.value : nil,
ensure: :present,
region: region,
tags: tags_for(subnet),
Expand Down
12 changes: 6 additions & 6 deletions lib/puppet/provider/ec2_vpc_vpn/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def self.prefetch(resources)
def self.connection_to_hash(region, connection)
ec2 = ec2_client(region)

customer_response = ec2.describe_customer_gateways(filters: [
{name: 'customer-gateway-id', values: [connection.customer_gateway_id]}
])
customer_response = ec2.describe_customer_gateways(
customer_gateway_ids: [connection.customer_gateway_id]
)

customer_gateways = customer_response.data.customer_gateways
customer_gateway_name = unless customer_gateways.empty?
Expand All @@ -47,9 +47,9 @@ def self.connection_to_hash(region, connection)
nil
end

vpn_response = ec2.describe_vpn_gateways(filters: [
{name: 'vpn-gateway-id', values: [connection.vpn_gateway_id]}
])
vpn_response = ec2.describe_vpn_gateways(
vpn_gateway_ids: [connection.vpn_gateway_id]
)

vpn_gateways = vpn_response.data.vpn_gateways
vpn_gateway_name = unless vpn_gateways.empty?
Expand Down
10 changes: 6 additions & 4 deletions lib/puppet/provider/ec2_vpc_vpn_gateway/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ def self.gateway_to_hash(region, gateway)
attached = gateway.vpc_attachments.detect { |vpc| vpc.state == 'attached' }
if attached
vpc_id = attached.vpc_id
vpc_response = ec2_client(region).describe_vpcs(filters: [
{name: 'vpc-id', values: [vpc_id]}
])
vpc_name = vpc_response.data.vpcs.empty? ? nil : name_from_tag(vpc_response.data.vpcs.first)
vpc_response = ec2_client(region).describe_vpcs(
vpc_ids: [vpc_id]
)
vpc = vpc_response.data.vpcs.first
vpc_name_tag = vpc.tags.detect { |tag| tag.key == 'Name' }
vpc_name = vpc_name_tag ? vpc_name_tag.value : nil
else
vpc_name = nil
vpc_id = nil
Expand Down
4 changes: 1 addition & 3 deletions lib/puppet/provider/elb_loadbalancer/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ def self.load_balancer_to_hash(region, load_balancer)
instance_ids = load_balancer.instances.map(&:instance_id)
instance_names = []
unless instance_ids.empty?
instances = ec2_client(region).describe_instances(filters: [
{name: 'instance-id', values: instance_ids}
]).collect do |response|
instances = ec2_client(region).describe_instances(instance_ids: instance_ids).collect do |response|
response.data.reservations.collect do |reservation|
reservation.instances.collect do |instance|
instance
Expand Down

0 comments on commit 074c0cc

Please sign in to comment.