saltstack/salt — pull request #10685 by bretep (2014-02-24 06:49:03+00:00)
Title: Optimize salt-cloud providers when map_providers_parallel is called.

@techhat I think this is a cleaner way to solve the needing to specify a profile when destroying an instance. Instead of working around the problem of querying the same region multiple times with the same queries, I'v created an optimizer for the ec2 provider. Any provider can add in an optimization function specific to that provider, if needed. If a provider does not have an optimization function, no worries, that provider will be added as is and proccessed as normal.

I've done extensive testing with this code adding multiple providers and testing to make sure I didn't break current fuctionality. However, it'd be great to have a second pair of eyes on this and maybe even suggest where I can clean up some code as I whiped this up today pretty fast.

What this does is:
1. Check to see if your provider can optimize before querying. This is important because each provider may need to optimize differently.
2. Build an optimized providers list. This will add both optimized and non-optimized providers to the list.
3. Returns list of providers.

### ec2 provider
Optimization is based on region and credentails (id and key). If all three values match, then we remove the duplicate provider. Both providers have the same access since they use the same credentials and access the same region. There is no need to query both providers as this will lead to duplicates.

---------------------
As a side note, this sped up my queries significantly. I have 25 providers configured across 9 regions. I went from querying 25 times to 9, once per region. That is optimization baby! Better yet, the destroy function now works great without a profile specified. Same with tagging and any other action.