Repository: chaostoolkit-incubator/chaostoolkit-aws

Issue:
asg.actions.detach_random_instances: also return IDs of detached instances
Referring to [asg.actions.detach_random_instances](https://github.com/chaostoolkit-incubator/chaostoolkit-aws/blob/0.16.0/chaosaws/asg/actions.py#L351), expected similar behavior to other `.*_random_.*` actions that return IDs of the randomly selected resources, such as:
- [asg.actions.terminate_random_instances](https://github.com/chaostoolkit-incubator/chaostoolkit-aws/blob/0.16.0/chaosaws/asg/actions.py#L95)
- [asg.actions.stop_random_instances](https://github.com/chaostoolkit-incubator/chaostoolkit-aws/blob/0.16.0/chaosaws/asg/actions.py#L180)
- [asg.actions.detach_random_volume](https://github.com/chaostoolkit-incubator/chaostoolkit-aws/blob/0.16.0/chaosaws/asg/actions.py#L391)
- [ecs.actions.stop_random_tasks](https://github.com/chaostoolkit-incubator/chaostoolkit-aws/blob/0.16.0/chaosaws/ecs/actions.py#L70)

Discussion:
anukul: cc @Lawouach 
anukul: @Lawouach sure, updated.

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9751079..94f6fe7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@
 - adding put_metric_data to cloudwatch actions
 
 ### Changed
+- update return value of asg action detach_random_instances to include instance IDs
 
 ## [0.16.0][]
 [0.16.0]: https://github.com/chaostoolkit-incubator/chaostoolkit-aws/compare/0.15.1...0.16.0
diff --git a/chaosaws/asg/actions.py b/chaosaws/asg/actions.py
index 5fe275c..8de365f 100644
--- a/chaosaws/asg/actions.py
+++ b/chaosaws/asg/actions.py
@@ -343,12 +343,17 @@ def detach_random_instances(asg_names: List[str] = None,
                 'than exist on asg %s' % (a['AutoScalingGroupName']))
 
         instances = random.sample(instances, instance_count)
+        instances = sorted(instances)
 
         response = client.detach_instances(
             AutoScalingGroupName=a['AutoScalingGroupName'],
-            InstanceIds=sorted(instances),
+            InstanceIds=instances,
             ShouldDecrementDesiredCapacity=decrement_capacity)
         results.setdefault('Activities', []).extend(response['Activities'])
+        results.setdefault('DetachingInstances', []).append({
+            'AutoScalingGroupName': a['AutoScalingGroupName'],
+            'InstanceIds': instances
+        })
     return results
 
 


Test patch:
diff --git a/tests/asg/test_asg_actions.py b/tests/asg/test_asg_actions.py
index ad2a682..2a9b6c9 100644
--- a/tests/asg/test_asg_actions.py
+++ b/tests/asg/test_asg_actions.py
@@ -584,7 +584,7 @@ def test_detach_instances_count(aws_client):
             }
         ]
     }
-    detach_random_instances(asg_names, instance_count=2)
+    result = detach_random_instances(asg_names, instance_count=2)
 
     instance_calls = [
         ['i-00000000000000001', 'i-00000000000000002'],
@@ -592,15 +592,25 @@ def test_detach_instances_count(aws_client):
         ['i-00000000000000002', 'i-00000000000000003']]
 
     ex = None
+    call_found = False
     for i in instance_calls:
         try:
+            call_found = False
+            instance_ids = sorted(i)
             client.detach_instances.assert_called_with(
                 AutoScalingGroupName=asg_names[0],
-                InstanceIds=sorted(i),
+                InstanceIds=instance_ids,
                 ShouldDecrementDesiredCapacity=False)
+            call_found = True
+            assert result['DetachingInstances'] == [{
+                'AutoScalingGroupName': asg_names[0],
+                'InstanceIds': instance_ids
+            }]
             return True
         except AssertionError as e:
             ex = str(e.args)
+            if call_found:
+                break
     raise AssertionError(ex)