From 0f4a7087b9eea9bb0ede88512a7d0d7d10dcd580 Mon Sep 17 00:00:00 2001 From: Ivan Kusalic Date: Fri, 23 Aug 2013 16:58:11 +0200 Subject: [PATCH] Add example for OpenStack's floating IPs --- docs/compute/examples.rst | 8 ++++++ docs/examples/compute/openstack_floating_ips.py | 33 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 docs/examples/compute/openstack_floating_ips.py diff --git a/docs/compute/examples.rst b/docs/compute/examples.rst index 681c041..a9418aa 100644 --- a/docs/compute/examples.rst +++ b/docs/compute/examples.rst @@ -111,3 +111,11 @@ Created node also gets added to the provided security groups. As noted in the example, you use `extra_args` argument to provide a dictionary that contains the keypair and list of securitygroups. Note that the list of securitygroups is passed as a string not as a python list. + +Create flaoting IP and attach it to a node using a local OpenStack provider +--------------------------------------------------------------------------- + +This example demonstrates how to use OpenStack's floating IPs. + +.. literalinclude:: /examples/compute/openstack_floating_ips.py + :language: python diff --git a/docs/examples/compute/openstack_floating_ips.py b/docs/examples/compute/openstack_floating_ips.py new file mode 100644 index 0000000..620326e --- /dev/null +++ b/docs/examples/compute/openstack_floating_ips.py @@ -0,0 +1,33 @@ +from libcloud.compute.types import Provider +from libcloud.compute.providers import get_driver + +import libcloud.security + +# This assumes you don't have SSL set up. +# Note: Code like this poses a security risk (MITM attack) and +# that's the reason why you should never use it for anything else +# besides testing. You have been warned. +libcloud.security.VERIFY_SSL_CERT = False + +OpenStack = get_driver(Provider.OPENSTACK) +driver = OpenStack('your_auth_username', 'your_auth_password', + ex_force_auth_url='http://10.0.4.1:5000/v2.0', + ex_force_auth_version='2.0_password', ex_tenant_name='your_tenant') + +# get the first pool - public by default +pool, = driver.ex_list_floating_ip_pools() + +# create an ip in the pool +floating_ip = pool.create_floating_ip() + +# get the node, note: change the node id to the some id you have +node = driver.ex_get_node_details('922a4381-a18c-487f-b816-cc31c9060853') + +# attach the ip to the node +driver.ex_attach_floating_ip_to_node(node, floating_ip) + +# remove it from the node +driver.ex_detach_floating_ip_from_node(node, floating_ip) + +# delete the ip +floating_ip.delete() -- 1.8.2.3