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

Commit db1c767

Browse files
authored
Merge pull request #383 from ctidigital/feature/autoscale-update
Feature/autoscale update
2 parents 7ae7ef1 + 60d91fd commit db1c767

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,15 @@ back- end instances. Accepts a hash with the following keys:
601601
##### `load_balancers`
602602
*Optional* A list of load balancer names that should be attached to this autoscaling group.
603603

604+
##### `target_groups`
605+
*Optional* A list of ELBv2 Target Group names that should be attached to this autoscaling group.
606+
604607
##### `subnets`
605608
*Optional* The subnets to associate with the autoscaling group.
606609

610+
##### `termination_policies`
611+
*Optional* A list of termination policies to use when scaling in instances. For valid termination policies, see [Controlling Which Instances Auto Scaling Terminates During Scale In](http://docs.aws.amazon.com/autoscaling/latest/userguide/as-instance-termination.html).
612+
607613
#####`tags`
608614
*Optional* The tags to assign to the autoscaling group. Accepts a 'key => value' hash of tags. The tags are not propagated to launched instances.
609615

lib/puppet/provider/ec2_autoscalinggroup/v2.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def self.group_to_hash(region, group)
5959
health_check_grace_period: group.health_check_grace_period,
6060
new_instances_protected_from_scale_in: group.new_instances_protected_from_scale_in,
6161
load_balancers: fetch_load_balancers(autoscaling_client(region), group.auto_scaling_group_name),
62+
target_groups: fetch_target_groups(region, group.auto_scaling_group_name),
63+
termination_policies: group.termination_policies,
6264
instance_count: group.instances.count,
6365
ensure: :present,
6466
subnets: subnet_names,
@@ -72,6 +74,17 @@ def self.fetch_load_balancers(client, name)
7274
response.load_balancers.collect { |lb| lb.load_balancer_name }
7375
end
7476

77+
def self.fetch_target_groups(region, name)
78+
response = autoscaling_client(region).describe_load_balancer_target_groups(auto_scaling_group_name: name)
79+
response.load_balancer_target_groups.collect { |tg| fetch_target_group_name(region, tg.load_balancer_target_group_arn) }.flatten
80+
end
81+
82+
def self.fetch_target_group_name(region, value)
83+
arn = value.is_a?(Array) ? value : [value]
84+
response = elbv2_client(region).describe_target_groups(target_group_arns: arn)
85+
response.target_groups.collect { |tg| tg.target_group_name }
86+
end
87+
7588
def exists?
7689
Puppet.debug("Checking if auto scaling group #{name} exists in region #{target_region}")
7790
@property_hash[:ensure] == :present
@@ -177,6 +190,14 @@ def subnets=(value)
177190
)
178191
end
179192

193+
def termination_policies=(value)
194+
policies = value.is_a?(Array) ? value : [value]
195+
autoscaling_client(target_region).update_auto_scaling_group(
196+
auto_scaling_group_name: name,
197+
termination_policies: policies,
198+
)
199+
end
200+
180201
def availability_zones=(value)
181202
zones = value.is_a?(Array) ? value : [value]
182203
autoscaling_client(target_region).update_auto_scaling_group(
@@ -185,6 +206,29 @@ def availability_zones=(value)
185206
)
186207
end
187208

209+
def target_groups=(value)
210+
should_names = value.is_a?(Array) ? value : [value]
211+
212+
response = elbv2_client(target_region).describe_target_groups(names: should_names)
213+
should = (response.target_groups.collect { |tg| tg.target_group_arn }).to_set
214+
215+
response = elbv2_client(target_region).describe_target_groups(names: target_groups)
216+
is = (response.target_groups.collect { |tg| tg.target_group_arn }).to_set
217+
218+
to_delete = is - should
219+
to_add = should - is
220+
221+
autoscaling_client(target_region).attach_load_balancer_target_groups(
222+
auto_scaling_group_name: name,
223+
target_group_arns: to_add,
224+
)
225+
autoscaling_client(target_region).detach_load_balancer_target_groups(
226+
auto_scaling_group_name: name,
227+
target_group_arns: to_delete,
228+
)
229+
230+
end
231+
188232
def load_balancers=(value)
189233
should = (value.is_a?(Array) ? value : [value]).to_set
190234
is = fetch_load_balancers(autoscaling_client(target_region), name).to_set

lib/puppet/provider/ec2_launchconfiguration/v2.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ def self.config_to_hash(region, config)
6262
spot_price: config.spot_price,
6363
ebs_optimized: config.ebs_optimized,
6464
}
65-
config[:block_device_mappings] = devices unless devices.empty?
65+
if devices.empty?
66+
config[:block_device_mappings] = [ ]
67+
else
68+
config[:block_device_mappings] = devices
69+
end
6670
config
6771
end
6872

lib/puppet/type/ec2_autoscalinggroup.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,24 @@ def insync?(is)
135135
end
136136
end
137137

138+
newproperty(:target_groups, :array_matching => :all) do
139+
desc 'The target groups attached to this group.'
140+
validate do |value|
141+
fail 'target_groups cannot be blank' if value == ''
142+
fail 'target_groups should be a String' unless value.is_a?(String)
143+
end
144+
def insync?(is)
145+
is.to_set == should.to_set
146+
end
147+
end
148+
149+
newproperty(:termination_policies, :array_matching => :all) do
150+
desc 'The termination policies attached to this group.'
151+
def insync?(is)
152+
is.to_set == should.to_set
153+
end
154+
end
155+
138156
newproperty(:subnets, :array_matching => :all) do
139157
desc 'The subnets to associate the autoscaling group.'
140158
validate do |value|

lib/puppet_x/puppetlabs/aws.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ def elb_client(region = default_region)
162162
self.class.elb_client(region)
163163
end
164164

165+
def self.elbv2_client(region = default_region)
166+
::Aws::ElasticLoadBalancingV2::Client.new(client_config(region))
167+
end
168+
169+
def elbv2_client(region = default_region)
170+
self.class.elbv2_client(region)
171+
end
172+
165173
def self.autoscaling_client(region = default_region)
166174
::Aws::AutoScaling::Client.new(client_config(region))
167175
end

0 commit comments

Comments
 (0)