# File lib/rudy/aws/ec2/instance.rb, line 190
190:       def list_as_hash(state=nil, inst_ids=[], &each_inst)
191:         state &&= state.to_sym
192:         state = nil if state == :any
193:         raise "Unknown state: #{state}" if state && !Instances.known_state?(state)
194:         state = 'shutting-down''shutting-down' if state == :shutting_down # EC2 uses a dash
195: 
196:         # If we got Instance objects, we want just the IDs.
197:         # This method always returns an Array.
198:         inst_ids = objects_to_instance_ids(inst_ids)
199:       
200:         response = Rudy::AWS::EC2.execute_request({}) {
201:           @@ec2.describe_instances(:instance_id => inst_ids)
202:         }
203:       
204:         # requestId: c16878ac-28e4-4859-9878-ef93af45789c
205:         # reservationSet: 
206:         #   item: 
207:         #   - reservationId: r-e493148d
208:         #     groupSet: 
209:         #       item: 
210:         #       - groupId: default
211:         #     instancesSet: 
212:         #       item:
213:         return nil unless response['reservationSet'].is_a?(Hash)  # No instances 
214:       
215:         resids = []
216:         instances = {}
217:         response['reservationSet']['item'].each do |res|      
218:           resids << res['reservationId']
219:           groups = res['groupSet']['item'].collect { |g| g['groupId'] }
220:           # And each reservation can have 1 or more instances
221:           next unless res['instancesSet'].is_a?(Hash)
222:           res['instancesSet']['item'].each do |props|
223:             inst = Instances.from_hash(props)
224:             next if state && inst.state != state.to_s
225:             inst.groups = groups
226:             #puts "STATE: #{inst.state} #{state}"
227:             instances[inst.awsid] = inst
228:           end
229:         end
230:         
231:         instances.each_value { |inst| each_inst.call(inst) } if each_inst
232:         
233:         instances = nil if instances.empty? # Don't return an empty hash
234:         instances
235:       end