spulec/moto — pull request #139 by kouk (2014-06-18 07:59:30+00:00)
Title: general support for access to backend models

This is one idea about providing users with a way to access the backend's
models in a general way. Trying to avoid extending moto's API too much I
thought that it would be logical simply to give a user access to the `boto`
objects that moto creates in it's backend models. These objects support whatever
API `boto` says they support and a user can access them using the boto class
name (for example `SpotInstanceRequest`). In effect the only addition to
`moto`'s API is a method `get_model` which takes a class name and returns an
array of objects of that type.

With this implementation one could for example do the following (usefull [here](https://github.com/buildbot/buildbot/pull/1076)):

```
import boto
from moto import mock_ec2
from moto.backends import get_model

@mock_ec2
def test_request_spot_instances_fulfilled():
"""
Test that moto correctly fullfills a spot instance request
"""
conn = boto.connect_ec2()

request = conn.request_spot_instances(
price=0.5, image_id='ami-abcd1234',
)

requests = conn.get_all_spot_instance_requests()
requests.should.have.length_of(1)
request = requests[0]

request.state.should.equal("open")

get_model('SpotInstanceRequest')[0].state = 'active'

requests = conn.get_all_spot_instance_requests()
requests.should.have.length_of(1)
request = requests[0]

request.state.should.equal("active")
```