Working with the object oriented APIs

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

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

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

To obtain a reference to this resource, Libcloud providers corresponding get and / 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. CREDENTIALS_ZERIGO = ('email', 'api key')
  4. ZONE_ID = 'example.myzone.com'
  5. Cls = get_driver(Provider.ZERIGO)
  6. driver = Cls(*CREDENTIALS_ZERIGO)
  7. zone = driver.get_zone(zone_id=ZONE_ID)
  8. records = driver.list_records(zone=zone)

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

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. ACCESS_ID = 'your access id'
  4. SECRET_KEY = 'your secret key'
  5. IMAGE_ID = 'ami-c8052d8d'
  6. SIZE_ID = 't1.micro'
  7. cls = get_driver(Provider.EC2)
  8. driver = cls(ACCESS_ID, SECRET_KEY, region="us-west-1")
  9. # Here we select size and image
  10. sizes = driver.list_sizes()
  11. images = driver.list_images()
  12. size = [s for s in sizes if s.id == SIZE_ID][0]
  13. image = [i for i in images if i.id == IMAGE_ID][0]
  14. node = driver.create_node(name='test-node', image=image, size=size)

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

As you can see above, most of those getter methods retrieve extra information about 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 known id. 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. CREDENTIALS_ZERIGO = ('email', 'api key')
  5. ZONE_ID = 'example.myzone.com'
  6. Cls = get_driver(Provider.ZERIGO)
  7. driver = Cls(*CREDENTIALS_ZERIGO)
  8. zone = Zone(ZONE_ID, domain=None, type=None, ttl=None,
  9. driver=driver)
  10. 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. ACCESS_ID = 'your access id'
  5. SECRET_KEY = 'your secret key'
  6. IMAGE_ID = 'ami-c8052d8d'
  7. SIZE_ID = 't1.micro'
  8. cls = get_driver(Provider.EC2)
  9. driver = cls(ACCESS_ID, SECRET_KEY, region="us-west-1")
  10. size = NodeSize(id=SIZE_ID, name=None, ram=None, disk=None, bandwidth=None,
  11. price=None, driver=driver)
  12. image = NodeImage(id=IMAGE_ID, name=None, driver=driver)
  13. 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. ACCESS_ID = 'your access id'
  4. SECRET_KEY = 'your secret key'
  5. IAM_PROFILE = 'your IAM profile arn or IAM profile name'
  6. IMAGE_ID = 'ami-c8052d8d'
  7. SIZE_ID = 't1.micro'
  8. cls = get_driver(Provider.EC2)
  9. driver = cls(ACCESS_ID, SECRET_KEY, region="us-west-1")
  10. # Here we select size and image
  11. sizes = driver.list_sizes()
  12. images = driver.list_images()
  13. size = [s for s in sizes if s.id == SIZE_ID][0]
  14. image = [i for i in images if i.id == IMAGE_ID][0]
  15. node = driver.create_node(name='test-node', image=image, size=size,
  16. ex_iamprofile=IAM_PROFILE)