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

Commit 64f00dd

Browse files
committed
Allow setting ingress rules for default security groups in VPC
Due to default security groups all being named default we couldn't reference them previously due to unique resouce naming conflicts. This patch allows for a composite namevar only in the case of the default group. Note that the composite name populates the VPC field automatically, so you don't have to duplicate the information in a separate property.
1 parent 722ccfa commit 64f00dd

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

examples/vpc-example/init.pp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@
1414
}],
1515
}
1616

17+
ec2_securitygroup { 'sample-vpc::default':
18+
ensure => present,
19+
region => 'sa-east-1',
20+
description => 'default VPC security group',
21+
ingress => [{
22+
protocol => 'tcp',
23+
port => 22,
24+
cidr => '0.0.0.0/0'
25+
},{
26+
security_group => 'default',
27+
}],
28+
}
29+
1730
ec2_vpc_subnet { 'sample-subnet':
1831
ensure => present,
1932
region => 'sa-east-1',

lib/puppet/provider/ec2_securitygroup/v2.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,12 @@ def self.security_group_to_hash(region, group)
6969
vpc_name_tag ? vpc_name_tag.value : nil
7070
end
7171
end
72+
name = group[:group_name]
73+
name = "#{vpc_name}::#{name}" if vpc_name && name == 'default'
7274
{
7375
id: group.group_id,
74-
name: group[:group_name],
76+
name: name,
77+
group_name: group[:group_name],
7578
id: group[:group_id],
7679
description: group[:description],
7780
ensure: :present,

lib/puppet/type/ec2_securitygroup.rb

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55

66
ensurable
77

8-
newparam(:name, namevar: true) do
9-
desc 'the name of the security group'
8+
newparam(:name) do
9+
desc 'the name of the security group resource'
10+
isnamevar
1011
validate do |value|
1112
fail Puppet::Error, 'Security groups must have a name' if value == ''
1213
end
1314
end
1415

16+
newparam(:group_name) do
17+
desc 'the name of the security group'
18+
isnamevar
19+
end
20+
1521
newproperty(:region) do
1622
desc 'the region in which to launch the security group'
1723
validate do |value|
@@ -53,6 +59,7 @@ def stringify_values(rules)
5359

5460
newproperty(:vpc) do
5561
desc 'A VPC to which the group should be associated'
62+
isnamevar
5663
end
5764

5865
def should_autorequire?(rule)
@@ -70,4 +77,25 @@ def should_autorequire?(rule)
7077
autorequire(:ec2_vpc) do
7178
self[:vpc]
7279
end
80+
81+
# When you create a VPC you automatically get a security group called default. You can't change the name.
82+
# This lack of uniqueness makes managing these default security groups difficult. Enter a composite namevar.
83+
# We support two name formats:
84+
#
85+
# 1. {some-security-group}
86+
# 2. {some-vpc-name}::default
87+
#
88+
# Note that we only support prefixing a security group name with the vpc name for the default security group
89+
# at this point. This avoids the issue of otherwise needing to store the resources in two places for non-default
90+
# VPC secueity groups.
91+
#
92+
# In the case of a a default security group, we maintain the full name (including the VPC name) in the name property
93+
# as otherwise it won't be unique and uniqueness and composite namevars are fun.
94+
def self.title_patterns
95+
[
96+
[ /^(([\w\-]+)::(default))$/, [ [ :name, lambda {|x| x} ], [ :vpc, lambda {|x| x} ], [ :group_name, lambda {|x| x} ] ] ],
97+
[ /^(([\w\-]+))$/, [ [ :name, lambda {|x| x} ], [ :group_name, lambda {|x| x} ] ] ]
98+
]
99+
end
100+
73101
end

0 commit comments

Comments
 (0)