From 8a86e1f4874abc7bc8adffa3f6608b0fe1dc292a Mon Sep 17 00:00:00 2001 From: Gareth Rushgrove Date: Wed, 18 Mar 2015 09:42:14 +0000 Subject: [PATCH] Make the region param and ENV var interchangable This allows for creating and deleteing resources from puppet resource by passing the environment variable (which is already used for scoping). Prior to this change you would have to pass the information twice. If you do pass both we maintain the same approach as previous, giving the param a higher presendence. This also allows for not specifying the region in the DSL, and instead using the AWS_REGION variable. This matches the standard behaviour of other AWS tools, and means in the case of not providing anything the correct error bubbles through. --- lib/puppet/provider/ec2_securitygroup/v2.rb | 16 +++++++--------- lib/puppet_x/puppetlabs/aws.rb | 6 ++++++ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/puppet/provider/ec2_securitygroup/v2.rb b/lib/puppet/provider/ec2_securitygroup/v2.rb index 414167e5..a6c57968 100644 --- a/lib/puppet/provider/ec2_securitygroup/v2.rb +++ b/lib/puppet/provider/ec2_securitygroup/v2.rb @@ -23,7 +23,7 @@ def self.instances def self.prefetch(resources) instances.each do |prov| if resource = resources[prov.name] # rubocop:disable Lint/AssignmentInCondition - resource.provider = prov if resource[:region] == prov.region + resource.provider = prov if (resource[:region] || ENV['AWS_REGION']) == prov.region end end end @@ -83,14 +83,13 @@ def self.security_group_to_hash(region, group) end def exists? - dest_region = resource[:region] if resource - Puppet.info("Checking if security group #{name} exists in region #{dest_region || region}") + Puppet.info("Checking if security group #{name} exists in region #{target_region}") @property_hash[:ensure] == :present end def create - Puppet.info("Creating security group #{name} in region #{resource[:region]}") - ec2 = ec2_client(resource[:region]) + Puppet.info("Creating security group #{name} in region #{target_region}") + ec2 = ec2_client(target_region) config = { group_name: name, description: resource[:description] @@ -121,13 +120,12 @@ def create end def authorize_ingress(new_rules, existing_rules=[]) - ec2 = ec2_client(resource[:region]) + ec2 = ec2_client(target_region) new_rules = [new_rules] unless new_rules.is_a?(Array) to_create = new_rules - existing_rules to_delete = existing_rules - new_rules - to_create.reject(&:nil?).each do |rule| if rule.key? 'security_group' source_group_name = rule['security_group'] @@ -201,8 +199,8 @@ def ingress=(value) end def destroy - Puppet.info("Deleting security group #{name} in region #{resource[:region]}") - ec2_client(resource[:region]).delete_security_group( + Puppet.info("Deleting security group #{name} in region #{target_region}") + ec2_client(target_region).delete_security_group( group_id: @property_hash[:id] ) @property_hash[:ensure] = :absent diff --git a/lib/puppet_x/puppetlabs/aws.rb b/lib/puppet_x/puppetlabs/aws.rb index 56d4be00..93823295 100644 --- a/lib/puppet_x/puppetlabs/aws.rb +++ b/lib/puppet_x/puppetlabs/aws.rb @@ -98,6 +98,12 @@ def self.tags_for(item) tags end + def target_region + target = resource ? resource[:region] || region : region + target = nil if target == :absent + target || ENV['AWS_REGION'] + end + def tags=(value) Puppet.info("Updating tags for #{name} in region #{region}") ec2 = ec2_client(resource[:region])