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

Commit

Permalink
Make the region param and ENV var interchangable
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
garethr committed Mar 18, 2015
1 parent 722ccfa commit 8a86e1f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
16 changes: 7 additions & 9 deletions lib/puppet/provider/ec2_securitygroup/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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']
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lib/puppet_x/puppetlabs/aws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit 8a86e1f

Please sign in to comment.