Working with the object oriented APIs

To make it easier for the end user, Libcloud components expose a fullyobject-oriented API.

This means that besides the driver object you also work with NodeImage,and NodeSize object in the compute API, Container and Objectobject in the Storage API, Zone and Record object in the DNS APIand so on.

Methods which operate on those resources usually require you to pass in aninstance of the resource you want to manipulate or work with and not just anid.

To obtain a reference to this resource, Libcloud providers corresponding getand / or list methods.

A couple of examples are shown below.

Example 1 - listing records for a zone with a known id

  1. from libcloud.dns.providers import get_driver
  2. from libcloud.dns.types import Provider
  3.  
  4. CREDENTIALS_ZERIGO = ('email', 'api key')
  5. ZONE_ID = 'example.myzone.com'
  6.  
  7. Cls = get_driver(Provider.ZERIGO)
  8. driver = Cls(*CREDENTIALS_ZERIGO)
  9.  
  10. zone = driver.get_zone(zone_id=ZONE_ID)
  11. records = driver.list_records(zone=zone)

In this example, the driver.get_zone() method call results in an HTTPcall.

Example 2 - creating an EC2 instance with a known NodeSize and NodeImage id

  1. from libcloud.compute.types import Provider
  2. from libcloud.compute.providers import get_driver
  3.  
  4. ACCESS_ID = 'your access id'
  5. SECRET_KEY = 'your secret key'
  6.  
  7. IMAGE_ID = 'ami-c8052d8d'
  8. SIZE_ID = 't1.micro'
  9.  
  10. cls = get_driver(Provider.EC2)
  11. driver = cls(ACCESS_ID, SECRET_KEY, region="us-west-1")
  12.  
  13. # Here we select size and image
  14. sizes = driver.list_sizes()
  15. images = driver.list_images()
  16.  
  17. size = [s for s in sizes if s.id == SIZE_ID][0]
  18. image = [i for i in images if i.id == IMAGE_ID][0]
  19.  
  20. node = driver.create_node(name='test-node', image=image, size=size)

In this example, both the driver.list_sizes() anddriver.list_images() method calls result in HTTP calls.

As you can see above, most of those getter methods retrieve extra informationabout the resource from the provider API and result in an HTTP request.

There are some cases when you might not want this:

  • You don’t care if a resource doesn’t exist
  • You don’t care about the extra attributes
  • You want to avoid an extra HTTP request
  • You want to avoid holding a reference to the resource object

If that is true for you, you can directly instantiate a resource with a knownid. You can see how to do this in the examples below.

Example 1 - listing records for a zone with a known id

  1. from libcloud.dns.base import Zone
  2. from libcloud.dns.providers import get_driver
  3. from libcloud.dns.types import Provider
  4.  
  5. CREDENTIALS_ZERIGO = ('email', 'api key')
  6. ZONE_ID = 'example.myzone.com'
  7.  
  8. Cls = get_driver(Provider.ZERIGO)
  9. driver = Cls(*CREDENTIALS_ZERIGO)
  10.  
  11. zone = Zone(ZONE_ID, domain=None, type=None, ttl=None,
  12. driver=driver)
  13. records = driver.list_records(zone=zone)

Example 2 - creating an EC2 instance with a known NodeSize and NodeImage id

  1. from libcloud.compute.types import Provider
  2. from libcloud.compute.providers import get_driver
  3. from libcloud.compute.base import NodeSize, NodeImage
  4.  
  5. ACCESS_ID = 'your access id'
  6. SECRET_KEY = 'your secret key'
  7.  
  8. IMAGE_ID = 'ami-c8052d8d'
  9. SIZE_ID = 't1.micro'
  10.  
  11. cls = get_driver(Provider.EC2)
  12. driver = cls(ACCESS_ID, SECRET_KEY, region="us-west-1")
  13.  
  14. size = NodeSize(id=SIZE_ID, name=None, ram=None, disk=None, bandwidth=None,
  15. price=None, driver=driver)
  16. image = NodeImage(id=IMAGE_ID, name=None, driver=driver)
  17.  
  18. node = driver.create_node(name='test-node', image=image, size=size)

Example 3 - creating an EC2 instance with an IAM profile

  1. from libcloud.compute.types import Provider
  2. from libcloud.compute.providers import get_driver
  3.  
  4. ACCESS_ID = 'your access id'
  5. SECRET_KEY = 'your secret key'
  6. IAM_PROFILE = 'your IAM profile arn or IAM profile name'
  7.  
  8. IMAGE_ID = 'ami-c8052d8d'
  9. SIZE_ID = 't1.micro'
  10. cls = get_driver(Provider.EC2)
  11. driver = cls(ACCESS_ID, SECRET_KEY, region="us-west-1")
  12.  
  13. # Here we select size and image
  14. sizes = driver.list_sizes()
  15. images = driver.list_images()
  16.  
  17. size = [s for s in sizes if s.id == SIZE_ID][0]
  18. image = [i for i in images if i.id == IMAGE_ID][0]
  19.  
  20. node = driver.create_node(name='test-node', image=image, size=size,
  21. ex_iamprofile=IAM_PROFILE)