blib/lib/VM/EC2/REST/autoscaling.pm untested

filter: Statementstmt 0.0% 0 / 118 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 47 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 211 CC 8 SCAR 18.1
CC
8
Cov
0%
create_launch_configuration
12.0
create_autoscaling_group
12.0
autoscaling_tags
12.0
CRAP 6.1
1 package VM::EC2::REST::autoscaling;
2  
3 use strict;
4 use VM::EC2 ''; # important not to import anything!
5 package VM::EC2; # add methods to VM::EC2
6  
7 VM::EC2::Dispatch->register(
8 DescribeLaunchConfigurations => 'fetch_members,LaunchConfigurations,VM::EC2::LaunchConfiguration',
9 DescribeAutoScalingGroups => 'fetch_members,AutoScalingGroups,VM::EC2::ASG',
10 DescribePolicies => 'fetch_members,ScalingPolicies,VM::EC2::ScalingPolicy',
11 );
12  
13 sub asg_call {
14 my $self = shift;
15 (my $endpoint = $self->{endpoint}) =~ s/ec2/autoscaling/;
16 local $self->{endpoint} = $endpoint;
17 local $self->{version} = '2011-01-01';
18 $self->call(@_);
19 }
20  
21 =head1 NAME VM::EC2::REST::autoscaling
22  
23 =head1 SYNOPSIS
24  
25 use VM::EC2 ':autoscaling';
26  
27 =head1 METHODS
28  
29 This module provides VM::EC2 methods for autoscaling groups and launch
30 configurations. Not all of the Amazon API is implemented, but the
31 most common functions are available.
32  
33 Implemented:
34 CreateAutoScalingGroup
35 CreateLaunchConfiguration
36 DeleteAutoScalingGroup
37 DeleteLaunchConfiguration
38 DeletePolicy
39 DescribeAutoScalingGroups
40 DescribeLaunchConfigurations
41 DescribePolicies
42 ExecutePolicy
43 PutScalingPolicy
44 ResumeProcesses
45 SuspendProcesses
46 UpdateAutoScalingGroup
47  
48 Unimplemented:
49 CreateOrUpdateTags
50 DeleteNotificationConfiguration
51 DeleteScheduledAction
52 DeleteTags
53 DescribeAdjustmentTypes
54 DescribeAutoScalingInstances
55 DescribeAutoScalingNotificationTypes
56 DescribeMetricCollectionTypes
57 DescribeNotificationConfigurations
58 DescribeScalingActivities
59 DescribeScalingProcessTypes
60 DescribeScheduledActions
61 DescribeTags
62 DescribeTerminationPolicyTypes
63 DisableMetricsCollection
64 EnableMetricsCollection
65 PutNotificationConfiguration
66 PutScheduledUpdateGroupAction
67 SetDesiredCapacity
68 SetInstanceHealth
69 TerminateInstanceInAutoScalingGroup
70  
71 =head2 @lc = $ec2->describe_launch_configurations(-names => \@names);
72  
73 =head2 @lc = $ec->describe_launch_configurations(@names);
74  
75 Provides detailed information for the specified launch configuration(s).
76  
77 Optional parameters are:
78  
79 -launch_configuration_names Name of the Launch config.
80 This can be a string scalar or an arrayref.
81  
82 -name Alias for -launch_configuration_names
83  
84 Returns a series of L<VM::EC2::LaunchConfiguration> objects.
85  
86 =cut
87  
88 sub describe_launch_configurations {
89 my $self = shift;
90 my %args = $self->args('-launch_configuration_names',@_);
91 $args{-launch_configuration_names} ||= $args{-names};
92 my @params = $self->list_parm('LaunchConfigurationNames',\%args);
93 return $self->asg_call('DescribeLaunchConfigurations', @params);
94 }
95  
96 =head2 $success = $ec2->create_launch_configuration(%args);
97  
98 Creates a new launch configuration.
99  
100 Required arguments:
101  
102 -name -- scalar, name for the Launch config.
103 -image_id -- scalar, AMI id which this launch config will use
104 -instance_type -- scalar, instance type of the Amazon EC2 instance.
105  
106 Optional arguments:
107  
108 -block_device_mappings -- list of hashref
109 -ebs_optimized -- scalar (boolean). false by default
110 -iam_instance_profile -- scalar
111 -instance_monitoring -- scalar (boolean). true by default
112 -kernel_id -- scalar
113 -key_name -- scalar
114 -ramdisk -- scalar
115 -security_groups -- list of scalars
116 -spot_price -- scalar
117 -user_data -- scalar
118  
119 Returns true on successful execution.
120  
121 =cut
122  
123 sub create_launch_configuration {
124 my $self = shift;
125 my %args = @_;
126 my $name = $args{-name} or croak "-name argument is required";
127 my $imageid = $args{-image_id} or croak "-image_id argument is required";
128 my $itype = $args{-instance_type} or croak "-instance_type argument is required";
129  
130 my @params = (ImageId => $imageid, InstanceType => $itype, LaunchConfigurationName => $name);
131 push @params, $self->member_list_parm('BlockDeviceMappings',\%args);
132 push @params, $self->member_list_parm('SecurityGroups',\%args);
133 push @params, $self->boolean_parm('EbsOptimized', \%args);
134 push @params, ('UserData' =>encode_base64($args{-user_data},'')) if $args{-user_data};
135 push @params, ('InstanceMonitoring.Enabled' => 'false')
136 if (exists $args{-instance_monitoring} and not $args{-instance_monitoring});
137  
138 my @p = map {$self->single_parm($_,\%args) }
139 qw(IamInstanceProfile KernelId KeyName RamdiskId SpotPrice);
140 push @params, @p;
141  
142 return $self->asg_call('CreateLaunchConfiguration',@params);
143 }
144  
145 =head2 $success = $ec2->delete_launch_configuration(-name => $name);
146  
147 Deletes a launch config.
148  
149 -name Required. Name of the launch config to delete
150  
151 Returns true on success.
152  
153 =cut
154  
155 sub delete_launch_configuration {
156 my $self = shift;
157 my %args = @_;
158 my $name = $args{-name} or croak "-name argument is required";
159 my @params = (LaunchConfigurationName => $name);
160 return $self->asg_call('DeleteLaunchConfiguration', @params);
161 }
162  
163 =head2 @asg = $ec2->describe_autoscaling_groups(-auto_scaling_group_names => \@names);
164  
165 Returns information about autoscaling groups
166  
167 -auto_scaling_group_names List of auto scaling groups to describe
168 -names Alias of -auto_scaling_group_names
169  
170 Returns a list of L<VM::EC2::ASG>.
171  
172 =cut
173  
174 sub describe_autoscaling_groups {
175 my ($self, %args) = @_;
176 $args{-auto_scaling_group_names} ||= $args{-names};
177 my @params = $self->member_list_parm('AutoScalingGroupNames',\%args);
178 return $self->asg_call('DescribeAutoScalingGroups', @params);
179 }
180  
181 =head2 $success = $ec2->create_autoscaling_group(-name => $name,
182 -launch_config => $lc,
183 -max_size => $max_size,
184 -min_size => $min_size);
185  
186 Creates a new autoscaling group.
187  
188 Required arguments:
189  
190 -name Name for the autoscaling group
191 -launch_config Name of the launch configuration to be used
192 -max_size Max number of instances to be run at once
193 -min_size Min number of instances
194  
195 Optional arguments:
196  
197 -availability_zones List of availability zone names
198 -load_balancer_names List of ELB names
199 -tags List of tags to apply to the instances run
200 -termination_policies List of policy names
201 -default_cooldown Time in seconds between autoscaling activities
202 -desired_capacity Number of instances to be run after creation
203 -health_check_type One of "ELB" or "EC2"
204 -health_check_grace_period Mandatory for health check type ELB. Number of
205 seconds between an instance is started and the
206 autoscaling group starts checking its health
207 -placement_group Physical location of your cluster placement group
208 -vpc_zone_identifier Strinc containing a comma-separated list of subnet
209 identifiers
210  
211 Returns true on success.
212  
213 =cut
214  
215 sub create_autoscaling_group {
216 my $self = shift;
217 my %args = @_;
218 my $name = $args{-name} or croak "-name argument is required";
219 my $lconfig = $args{-launch_config} or croak "-launch_config argument is required";
220 my $max = $args{-max_size};
221 croak "-max_size argument is required" if (not defined $max);
222 my $min = $args{-min_size};
223 croak "-min_size argument is required" if (not defined $min);
224  
225 my @params = (AutoScalingGroupName => $name, LaunchConfigurationName => $lconfig, MaxSize => $max,
226 MinSize => $max);
227 push @params, $self->member_list_parm('AvailabilityZones',\%args);
228 push @params, $self->member_list_parm('LoadBalancerNames',\%args);
229 push @params, $self->member_list_parm('TerminationPolicies',\%args);
230 push @params, $self->autoscaling_tags('Tags', \%args);
231  
232 my @p = map {$self->single_parm($_,\%args) }
233 qw( DefaultCooldown DesiredCapacity HealthCheckGracePeriod HealthCheckType PlacementGroup
234 VPCZoneIdentifier);
235 push @params, @p;
236  
237 return $self->asg_call('CreateAutoScalingGroup',@params);
238 }
239  
240 =head2 $success = $ec2->delete_autoscaling_group(-name => $name)
241  
242 Deletes an autoscaling group.
243  
244 -name Name of the autoscaling group to delete
245  
246 Returns true on success.
247  
248 =cut
249  
250 sub delete_autoscaling_group {
251 my $self = shift;
252 my %args = @_;
253 my $name = $args{-name} or croak "-name argument is required";
254 my @params = (AutoScalingGroupName => $name);
255 push @params, $self->single_parm('ForceDelete',\%args);
256 return $self->asg_call('DeleteAutoScalingGroup', @params);
257 }
258  
259 =head2 $success = $ec2->update_autoscaling_group(-name => $name);
260  
261 Updates an autoscaling group. Only required parameter is C<-name>
262  
263 Optional arguments:
264  
265 -availability_zones List of AZ's
266 -termination_policies List of policy names
267 -default_cooldown
268 -desired_capacity
269 -health_check_type
270 -health_check_grace_period
271 -launch_configuration_name
272 -placement_group
273 -vpc_zone_identifier
274 -max_size
275 -min_size
276  
277 Returns true on success;
278  
279 =cut
280  
281 sub update_autoscaling_group {
282 my $self = shift;
283 my %args = @_;
284  
285 my $name = $args{-name} or croak "-name argument is required";
286 my @params = (AutoScalingGroupName => $name);
287  
288 push @params, $self->member_list_parm('AvailabilityZones',\%args);
289 push @params, $self->member_list_parm('TerminationPolicies',\%args);
290  
291 my @p = map {$self->single_parm($_,\%args) }
292 qw( DefaultCooldown DesiredCapacity HealthCheckGracePeriod
293 HealthCheckType LaunchConfigurationName PlacementGroup
294 VPCZoneIdentifier MaxSize MinSize );
295 push @params, @p;
296  
297 return $self->asg_call('UpdateAutoScalingGroup',@params);
298 }
299  
300 =head2 $success = $ec2->suspend_processes(-name => $asg_name,
301 -scaling_processes => \@procs);
302  
303 Suspend the requested autoscaling processes.
304  
305 -name Name of the autoscaling group
306 -scaling_processes List of process names to suspend. Valid processes are:
307 Launch
308 Terminate
309 HealthCheck
310 ReplaceUnhealty
311 AZRebalance
312 AlarmNotification
313 ScheduledActions
314 AddToLoadBalancer
315  
316 Returns true on success.
317  
318 =cut
319  
320 sub suspend_processes {
321 my ($self, %args) = @_;
322 my $name = $args{-name} or croak "-name argument is required";
323 my @params = (AutoScalingGroupName => $name);
324 push @params, $self->member_list_parm('ScalingProcesses', \%args);
325 return $self->asg_call('SuspendProcesses', @params);
326 }
327  
328 =head2 $success = $ec2->resume_processes(-name => $asg_name,
329 -scaling_processes => \@procs);
330  
331 Resumes the requested autoscaling processes. It accepts the same arguments than
332 C<suspend_processes>.
333  
334 Returns true on success.
335  
336 =cut
337  
338 sub resume_processes {
339 my ($self, %args) = @_;
340 my $name = $args{-name} or croak "-name argument is required";
341 my @params = (AutoScalingGroupName => $name);
342 push @params, $self->member_list_parm('ScalingProcesses', \%args);
343 return $self->asg_call('ResumeProcesses', @params);
344 }
345  
346 =head2 @arguments = $ec2->autoscaling_tags($argname, \%args)
347  
348 =cut
349  
350 sub autoscaling_tags {
351 my $self = shift;
352 my ($argname, $args) = @_;
353  
354 my $name = $self->canonicalize($argname);
355 my @params;
356 if (my $a = $args->{$name}||$args->{"-$argname"}) {
357 my $c = 1;
358 for my $tag (ref $a && ref $a eq 'ARRAY' ? @$a : $a) {
359 my $prefix = "$argname.member." . $c++;
360 while (my ($k, $v) = each %$tag) {
361 push @params, ("$prefix.$k" => $v);
362 }
363 }
364 }
365  
366 return @params;
367 }
368  
369 =head2 @asg = $ec2->describe_policies(-auto_scaling_group_name => $name);
370  
371 Returns information about autoscaling policies
372  
373 -auto_scaling_group_name The name of the Auto Scaling group
374 -policy_names An array of policy names or policy ARNs to be described. If this list is omitted, all policy names are described. If an auto scaling group name is provided, the results are limited to that group. The list of requested policy names cannot contain more than 50 items. If unknown policy names are requested, they are ignored with no error.
375 -names Alias of -auto_scaling_group_names
376  
377 Returns a list of L<VM::EC2::ScalingPolicy>.
378  
379 =cut
380  
381 sub describe_policies {
382 my ($self, %args) = @_;
383 $args{-auto_scaling_group_name} ||= $args{-name};
384 my @params = $self->member_list_parm('PolicyNames',\%args);
385 push @params, ('AutoScalingGroupName', $args{-auto_scaling_group_name})
386 if ($args{-auto_scaling_group_name});
387 return $self->asg_call('DescribePolicies', @params);
388 }
389  
390  
391 =head2 $success = $ec2->put_scaling_policy
392  
393 Creates or updates a policy for an Auto Scaling group.
394  
395 Required arguments:
396  
397 -policy_name The name of the policy to update or create.
398 -name Alias for -policy_name
399 -auto_scaling_group_name The name or ARN of the Auto Scaling group.
400 -scaling_adjustment Number of instances by which to scale.
401 -adjustment_type Specifies wheter -scaling_adjustment is an absolute
402 number or a percentage of the current capacity.
403 Valid values are:
404 ChangeInCapacity
405 ExactCapacity
406 PercentChangeInCapacity
407  
408 Optional arguments:
409  
410 -cooldown The amount of time, in seconds, after a scaling
411 activity completes and before the next scaling acitvity
412 can start.
413 -min_adjustment_step Used with PercentChangeInCapacity as -adjustment_type.
414  
415 Returns true on success
416  
417 =cut
418  
419 sub put_scaling_policy {
420 my ($self, %args) = @_;
421 $args{-policy_name} ||= $args{-name};
422 my @params = map {$self->single_parm($_, \%args) }
423 qw( AdjustmentType AutoScalingGroupName Cooldown MinAdjustmentStep
424 PolicyName ScalingAdjustment );
425  
426 return $self->asg_call('PutScalingPolicy', @params);
427 }
428  
429 =head2 $success = $ec2->delete_policy(-policy_name => $name)
430  
431 Deletes a policy
432  
433 Required arguments:
434  
435 -policy_name Name or ARN of the policy
436 -name Alias for -policy_name
437 -auto_scaling_group_name Name of the Auto Scaling Group, required when
438 specifying policy by name (not by ARN)
439  
440 Returns true on success
441  
442 =cut
443  
444 sub delete_policy {
445 my ($self, %args) = @_;
446 $args{-policy_name} ||= $args{-name};
447 my @params = map { $self->single_parm($_, \%args) }
448 qw( AutoScalingGroupName PolicyName );
449  
450 return $self->asg_call('DeletePolicy', @params);
451 }
452  
453 =head2 $success = $ec2->execute_policy(-policy_name => $name)
454  
455 Runs a policy
456  
457 Required arguments:
458  
459 -policy_name Name or ARN of the policy
460 -name Alias for -policy_name
461 -auto_scaling_group_name Name of the Auto Scaling Group, required when
462 specifying policy by name (not by ARN)
463  
464 Optional arguments:
465  
466 -honor_cooldown Set to true if you want AutoScaling to reject
467 the request when it is in cooldown
468  
469 Returns true on success
470  
471 =cut
472  
473 sub execute_policy {
474 my ($self, %args) = @_;
475 $args{-policy_name} ||= $args{-name};
476 my @params = map { $self->single_parm($_, \%args) }
477 qw( AutoScalingGroupName HonorCooldown PolicyName );
478  
479 return $self->asg_call('ExecutePolicy', @params);
480 }
481  
482 =head1 SEE ALSO
483  
484 L<VM::EC2>
485  
486 =head1 AUTHOR
487  
488 Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>.
489  
490 Copyright (c) 2011 Ontario Institute for Cancer Research
491  
492 This package and its accompanying libraries is free software; you can
493 redistribute it and/or modify it under the terms of the GPL (either
494 version 1, or at your option, any later version) or the Artistic
495 License 2.0. Refer to LICENSE for the full license text. In addition,
496 please see DISCLAIMER.txt for disclaimers of warranty.
497  
498 =cut
499  
500  
501 1;