diff --git a/lib/fog/aws/auto_scaling.rb b/lib/fog/aws/auto_scaling.rb index 1385f80af..314e1d468 100644 --- a/lib/fog/aws/auto_scaling.rb +++ b/lib/fog/aws/auto_scaling.rb @@ -35,6 +35,8 @@ module Fog request :describe_tags request :describe_termination_policy_types request :detach_load_balancers + request :detach_instances + request :attach_instances request :disable_metrics_collection request :enable_metrics_collection request :execute_policy diff --git a/lib/fog/aws/models/auto_scaling/group.rb b/lib/fog/aws/models/auto_scaling/group.rb index 3ca94f206..547893975 100644 --- a/lib/fog/aws/models/auto_scaling/group.rb +++ b/lib/fog/aws/models/auto_scaling/group.rb @@ -61,6 +61,18 @@ module Fog reload end + def detach_instances(*instance_ids) + requires :id + service.detach_instances(id, 'InstanceIds' => instance_ids) + reload + end + + def attach_instances(*instance_ids) + requires :id + service.attach_instances(id, 'InstanceIds' => instance_ids) + reload + end + def disable_metrics_collection(metrics = {}) requires :id service.disable_metrics_collection(id, 'Metrics' => metrics) diff --git a/lib/fog/aws/requests/auto_scaling/attach_instances.rb b/lib/fog/aws/requests/auto_scaling/attach_instances.rb new file mode 100644 index 000000000..3fcf9498b --- /dev/null +++ b/lib/fog/aws/requests/auto_scaling/attach_instances.rb @@ -0,0 +1,59 @@ +module Fog + module AWS + class AutoScaling + class Real + require 'fog/aws/parsers/auto_scaling/basic' + + # Removes one or more instances from the specified Auto Scaling group. + # + # cli equiv: + # `aws autoscaling attach-instances --instance-ids i-93633f9b --auto-scaling-group-name my-auto-scaling-group` + # + # ==== Parameters + # + # * AutoScalingGroupName<~String> - The name of the Auto Scaling group`` + # * 'InstanceIds'<~Array> - The list of Auto Scaling instances to detach. + # + # ==== See Also + # + # http://docs.aws.amazon.com/cli/latest/reference/autoscaling/attach-instances.html + + ExpectedOptions[:asg_name] = %w[AutoScalingGroupName] + + def attach_instances(auto_scaling_group_name, options = {}) + + if instance_ids = options.delete('InstanceIds') + options.merge!(AWS.indexed_param('InstanceIds.member.%d', [*instance_ids])) + end + + request({ + 'Action' => 'AttachInstances', + 'AutoScalingGroupName' => auto_scaling_group_name, + :parser => Fog::Parsers::AWS::AutoScaling::Basic.new + }.merge!(options)) + end + end + + class Mock + def attach_instances(auto_scaling_group_name, options = {}) + unexpected_options = options.keys - ExpectedOptions[:asg_name] + + unless unexpected_options.empty? + raise Fog::AWS::AutoScaling::ValidationError.new("Options #{unexpected_options.join(',')} should not be included in request") + end + + unless self.data[:auto_scaling_groups].key?(auto_scaling_group_name) + raise Fog::AWS::AutoScaling::ValidationError.new('AutoScalingGroup name not found - null') + end + + response = Excon::Response.new + response.status = 200 + response.body = { + 'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id } + } + response + end + end + end + end +end diff --git a/lib/fog/aws/requests/auto_scaling/detach_instances.rb b/lib/fog/aws/requests/auto_scaling/detach_instances.rb new file mode 100644 index 000000000..a021c5eac --- /dev/null +++ b/lib/fog/aws/requests/auto_scaling/detach_instances.rb @@ -0,0 +1,69 @@ +module Fog + module AWS + class AutoScaling + class Real + require 'fog/aws/parsers/auto_scaling/basic' + + # Removes one or more instances from the specified Auto Scaling group. + # + # cli equiv: + # `aws autoscaling detach-instances --instance-ids i-2a2d8978 --auto-scaling-group-name my-asg --should-decrement-desired-capacity` + # + # ==== Parameters + # + # * AutoScalingGroupName<~String> - The name of the Auto Scaling group`` + # * 'InstanceIds'<~Array> - The list of Auto Scaling instances to detach. + # + # * options<~Hash>: + # 'shouldDecrementDesiredCapacity'<~Boolean> - decrement the asg capacity or not (it will boot another if an instance id detached) + # + # ==== See Also + # http://docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/detach-instance-asg.html + # + + ExpectedOptions[:asg_name] = %w[AutoScalingGroupName] + ExpectedOptions[:instance_ids] = %w[AutoScalingGroupName] + + def detach_instances(auto_scaling_group_name, options = {}) + + if should_decrement_desired_capacity = options.delete('ShouldDecrementDesiredCapacity') + options.merge!('ShouldDecrementDesiredCapacity' => true.to_s) + else + options.merge!('ShouldDecrementDesiredCapacity' => false.to_s) + end + + if instance_ids = options.delete('InstanceIds') + options.merge!(AWS.indexed_param('InstanceIds.member.%d', [*instance_ids])) + end + + request({ + 'Action' => 'DetachInstances', + 'AutoScalingGroupName' => auto_scaling_group_name, + :parser => Fog::Parsers::AWS::AutoScaling::Basic.new + }.merge!(options)) + end + end + + class Mock + def detach_instances(auto_scaling_group_name, options = {}) + unexpected_options = options.keys - ExpectedOptions[:asg_name] - ExpectedOptions[:instance_ids] + + unless unexpected_options.empty? + raise Fog::AWS::AutoScaling::ValidationError.new("Options #{unexpected_options.join(',')} should not be included in request") + end + + unless self.data[:auto_scaling_groups].key?(auto_scaling_group_name) + raise Fog::AWS::AutoScaling::ValidationError.new('AutoScalingGroup name not found - null') + end + + response = Excon::Response.new + response.status = 200 + response.body = { + 'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id } + } + response + end + end + end + end +end diff --git a/tests/requests/auto_scaling/auto_scaling_tests.rb b/tests/requests/auto_scaling/auto_scaling_tests.rb index f3998392c..a7743959e 100644 --- a/tests/requests/auto_scaling/auto_scaling_tests.rb +++ b/tests/requests/auto_scaling/auto_scaling_tests.rb @@ -35,7 +35,15 @@ Shindo.tests('AWS::AutoScaling | auto_scaling_tests', ['aws', 'auto_scaling']) d Fog::AWS[:auto_scaling].detach_load_balancers(@asg_name, 'LoadBalancerNames' => 'elb-test-fog').body end - tests("#describe_auto_scaling_groups").formats(AWS::AutoScaling::Formats::DESCRIBE_AUTO_SCALING_GROUPS) do + tests("#detach_instances").formats(AWS::AutoScaling::Formats::BASIC) do + Fog::AWS[:auto_scaling].detach_instances(@asg_name, 'InstanceIds' => 'i-deadbeef').body + end + + tests("#attach_instances").formats(AWS::AutoScaling::Formats::BASIC) do + Fog::AWS[:auto_scaling].attach_instances(@asg_name, 'InstanceIds' => 'i-deadbeef').body + end + + tests("#describe_auto_scaling_groups").formats(AWS::AutoScaling::Formats::DESCRIBE_AUTO_SCALING_GROUPS) do Fog::AWS[:auto_scaling].describe_auto_scaling_groups().body end tests("#describe_auto_scaling_groups").formats(AWS::AutoScaling::Formats::DESCRIBE_AUTO_SCALING_GROUPS) do