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

Commit 40ae3b2

Browse files
committed
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.
1 parent 0e2ff12 commit 40ae3b2

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

lib/puppet/provider/ec2_securitygroup/v2.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def self.instances
2424
def self.prefetch(resources)
2525
instances.each do |prov|
2626
if resource = resources[prov.name] # rubocop:disable Lint/AssignmentInCondition
27-
resource.provider = prov if resource[:region] == prov.region
27+
resource.provider = prov if (resource[:region] || ENV['AWS_REGION']) == prov.region
2828
end
2929
end
3030
end
@@ -96,14 +96,13 @@ def self.security_group_to_hash(region, group)
9696
end
9797

9898
def exists?
99-
dest_region = resource[:region] if resource
100-
Puppet.info("Checking if security group #{name} exists in region #{dest_region || region}")
99+
Puppet.info("Checking if security group #{name} exists in region #{target_region}")
101100
@property_hash[:ensure] == :present
102101
end
103102

104103
def create
105-
Puppet.info("Creating security group #{name} in region #{resource[:region]}")
106-
ec2 = ec2_client(resource[:region])
104+
Puppet.info("Creating security group #{name} in region #{target_region}")
105+
ec2 = ec2_client(target_region)
107106
config = {
108107
group_name: name,
109108
description: resource[:description]
@@ -207,8 +206,8 @@ def ingress=(value)
207206
end
208207

209208
def destroy
210-
Puppet.info("Deleting security group #{name} in region #{resource[:region]}")
211-
ec2_client(resource[:region]).delete_security_group(
209+
Puppet.info("Deleting security group #{name} in region #{target_region}")
210+
ec2_client(target_region).delete_security_group(
212211
group_id: @property_hash[:id]
213212
)
214213
@property_hash[:ensure] = :absent

lib/puppet/type/ec2_securitygroup.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require_relative '../../puppet_x/puppetlabs/property/tag.rb'
2+
require_relative '../../puppet_x/puppetlabs/property/region'
23
require_relative '../../puppet_x/puppetlabs/aws_ingress_rules_parser'
34

45
Puppet::Type.newtype(:ec2_securitygroup) do
@@ -13,11 +14,8 @@
1314
end
1415
end
1516

16-
newproperty(:region) do
17+
newproperty(:region, :parent => PuppetX::Property::AwsRegion) do
1718
desc 'the region in which to launch the security group'
18-
validate do |value|
19-
fail 'region should not contains spaces' if value =~ /\s/
20-
end
2119
end
2220

2321
newproperty(:ingress, :array_matching => :all) do

lib/puppet_x/puppetlabs/aws.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ def self.tags_for(item)
9696
tags
9797
end
9898

99+
def target_region
100+
target = resource ? resource[:region] || region : region
101+
target = nil if target == :absent
102+
target || ENV['AWS_REGION']
103+
end
104+
99105
def tags=(value)
100106
Puppet.info("Updating tags for #{name} in region #{region}")
101107
ec2 = ec2_client(resource[:region])
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module PuppetX
2+
module Property
3+
class AwsRegion < Puppet::Property
4+
validate do |value|
5+
name = resource[:name]
6+
fail "region for #{name} should not contains spaces" if value =~ /\s/
7+
if !ENV['AWS_REGION'].nil? && ENV['AWS_REGION'] != value
8+
fail "if using AWS_REGION environment variable it must match the specified region value for #{name}"
9+
end
10+
end
11+
end
12+
end
13+
end

0 commit comments

Comments
 (0)