You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by to...@apache.org on 2014/03/03 16:55:33 UTC

[2/5] git commit: Added ex_create_balancer_policy() and ex_destroy_balancer_policy() method

Added ex_create_balancer_policy() and ex_destroy_balancer_policy() method

Added test suite for ex_create_balancer_policy() and ex_destroy_balancer_policy() method

Added ex_set_balancer_policies_listener(self, balancer) method

added all the methods required for adding & deleting policies

corrected the docstring and positioning of methods

added testsuits for extension methods

fixture for ex_create_balancer_policy() method

fixture for ex_list_balancer_policies() method

fixture for ex_list_balancer_policy_types() method

fixture for ex_set_balancer_policies_backend_server() method

fixture for ex_set_balancer_policies_listener() method

removed basestring for python 3 support

PEP8 guidelines fixture

added the documentation for the extension methods

corrected various docstrings

corrected various docstrings and lint

correct the lint and added argument names

pep8 fixtures

pep8 fixtures

made changes in the doc

python code of creating connection for the doc

made changes in the doc

python code for creating policy for doc ref

python code for creating load balancer for doc ref

python code for creating balancer listeners for doc ref

python code for deleting balancer policy for doc ref

python code for listing  balancer policies for doc ref

python code for listing  balancer policy types for doc ref

python code for set balancer policy types for backend server

python code for set balancer policy types for listeners

list all the load balancers

all method implemetation of ELB driver

improved the documentation

pep8 correction

removed indendation error

update the docstrings

update the docstrings

removed invalid syntax

removed sphinx build errors

Signed-off-by: Tomaz Muraus <to...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/96efa263
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/96efa263
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/96efa263

Branch: refs/heads/trunk
Commit: 96efa263d35820be7756af8bdd04f6ba18c36139
Parents: a2b3aee
Author: Rahul Ranjan <rr...@localhost.localdomain>
Authored: Sat Feb 22 22:46:35 2014 +0530
Committer: Tomaz Muraus <to...@apache.org>
Committed: Mon Mar 3 13:15:57 2014 +0100

----------------------------------------------------------------------
 docs/examples/loadbalancer/elb/complete_tut.py  |  60 +++++
 .../elb/create_lb_connection_for_aws.py         |   8 +
 .../loadbalancer/elb/create_lb_policy.py        |  17 ++
 .../loadbalancer/elb/create_load_balancer.py    |  23 ++
 .../elb/ex_create_balancer_listeners.py         |  15 ++
 .../elb/ex_delete_balancer_policy.py            |  14 ++
 .../elb/ex_list_balancer_policies.py            |  12 +
 .../elb/ex_list_balancer_policy_types.py        |  12 +
 .../ex_set_balancer_policies_backend_server.py  |  15 ++
 .../elb/ex_set_balancer_policies_listener.py    |  15 ++
 .../loadbalancer/elb/list_load_balancer.py      |  10 +
 docs/loadbalancer/drivers/elb.rst               | 238 +++++++++++++++++++
 libcloud/loadbalancer/drivers/elb.py            | 188 +++++++++++++++
 .../elb/create_load_balancer_policy.xml         |   3 +
 .../elb/describe_load_balancer_policies.xml     |  29 +++
 .../describe_load_balancers_policy_types.xml    |  21 ++
 ...oad_balancer_policies_for_backend_server.xml |   6 +
 .../set_load_balancer_policies_of_listener.xml  |   4 +
 libcloud/test/loadbalancer/test_elb.py          |  79 +++++-
 19 files changed, 767 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/96efa263/docs/examples/loadbalancer/elb/complete_tut.py
----------------------------------------------------------------------
diff --git a/docs/examples/loadbalancer/elb/complete_tut.py b/docs/examples/loadbalancer/elb/complete_tut.py
new file mode 100644
index 0000000..7907bca
--- /dev/null
+++ b/docs/examples/loadbalancer/elb/complete_tut.py
@@ -0,0 +1,60 @@
+from libcloud.loadbalancer.base import Member, Algorithm
+from libcloud.loadbalancer.types import Provider
+from libcloud.loadbalancer.providers import get_driver
+
+
+ACCESS_ID = 'your access id'
+SECRET_KEY = 'your secret key'
+
+
+def main():
+    cls = get_driver(Provider.ELB)
+    driver = cls(key=ACCESS_ID, secret=SECRET_KEY, region='')
+
+    print driver.list_balancers()
+
+    # members associated with the load balancer
+    members = (Member(None, '192.168.88.1', 8000),
+               Member(None, '192.168.88.2', 8080))
+    # creates a new balancer named 'MyLB'
+    new_balancer = driver.create_balancer(
+        name='MyLB',
+        algorithm=Algorithm.ROUND_ROBIN,
+        port=80,
+        protocol='http',
+        members=members)
+    print new_balancer
+
+    # create load balancer policy
+    print driver.ex_create_balancer_policy(
+        name='MyLB',
+        policy_name='EnableProxyProtocol',
+        policy_type='ProxyProtocolPolicyType',
+        policy_attributes={'ProxyProtocol': 'true'})
+
+    # delete load balancer policy
+    print driver.ex_delete_balancer_policy(
+        name='MyLB',
+        policy_name='EnableProxyProtocol')
+
+    # set load balancer policies for backend server
+    print driver.ex_set_balancer_policies_backend_server(
+        name='MyLB',
+        port=80,
+        policies=['MyDurationStickyPolicy'])
+
+    # create the listeners for the balancers
+    print driver.ex_create_balancer_listeners(
+        name='MyLB',
+        listeners=[[1024, 65533, 'HTTPS',
+                   'arn:aws:iam::123456789012:server-certificate/servercert']])
+
+    # set the listeners policies for the balancers
+    print driver.ex_set_balancer_policies_listener(
+        name='MyLB',
+        port=80,
+        policies=['MyDurationStickyPolicy'])
+
+
+if __name__ == '__main__':
+    main()

http://git-wip-us.apache.org/repos/asf/libcloud/blob/96efa263/docs/examples/loadbalancer/elb/create_lb_connection_for_aws.py
----------------------------------------------------------------------
diff --git a/docs/examples/loadbalancer/elb/create_lb_connection_for_aws.py b/docs/examples/loadbalancer/elb/create_lb_connection_for_aws.py
new file mode 100644
index 0000000..19ad70f
--- /dev/null
+++ b/docs/examples/loadbalancer/elb/create_lb_connection_for_aws.py
@@ -0,0 +1,8 @@
+from libcloud.compute.types import Provider
+from libcloud.compute.providers import get_driver
+
+ACCESS_ID = 'your access id'
+SECRET_KEY = 'your secret key'
+
+cls = get_driver(Provider.ELB)
+driver = cls(key=ACCESS_ID, secret=SECRET_KEY, region='')

http://git-wip-us.apache.org/repos/asf/libcloud/blob/96efa263/docs/examples/loadbalancer/elb/create_lb_policy.py
----------------------------------------------------------------------
diff --git a/docs/examples/loadbalancer/elb/create_lb_policy.py b/docs/examples/loadbalancer/elb/create_lb_policy.py
new file mode 100644
index 0000000..806f31e
--- /dev/null
+++ b/docs/examples/loadbalancer/elb/create_lb_policy.py
@@ -0,0 +1,17 @@
+from libcloud.compute.types import Provider
+from libcloud.compute.providers import get_driver
+
+ACCESS_ID = 'your access id'
+SECRET_KEY = 'your secret key'
+
+cls = get_driver(Provider.ELB)
+driver = cls(key=ACCESS_ID, secret=SECRET_KEY, region='')
+
+print driver.list_balancers()
+
+# create load balancer policy
+driver.ex_create_balancer_policy(
+    name='MyLB',
+    policy_name='EnableProxyProtocol',
+    policy_type='ProxyProtocolPolicyType',
+    policy_attributes={'ProxyProtocol': 'true'})

http://git-wip-us.apache.org/repos/asf/libcloud/blob/96efa263/docs/examples/loadbalancer/elb/create_load_balancer.py
----------------------------------------------------------------------
diff --git a/docs/examples/loadbalancer/elb/create_load_balancer.py b/docs/examples/loadbalancer/elb/create_load_balancer.py
new file mode 100644
index 0000000..4866004
--- /dev/null
+++ b/docs/examples/loadbalancer/elb/create_load_balancer.py
@@ -0,0 +1,23 @@
+from libcloud.loadbalancer.base import Member, Algorithm
+from libcloud.compute.types import Provider
+from libcloud.compute.providers import get_driver
+
+ACCESS_ID = 'your access id'
+SECRET_KEY = 'your secret key'
+
+cls = get_driver(Provider.ELB)
+driver = cls(key=ACCESS_ID, secret=SECRET_KEY, region='')
+
+print driver.list_balancers()
+
+# members associated with the load balancer
+members = (Member(None, '192.168.88.1', 8000),
+           Member(None, '192.168.88.2', 8080))
+new_balancer = driver.create_balancer(
+    name='MyLB',
+    algorithm=Algorithm.ROUND_ROBIN,
+    port=80,
+    protocol='http',
+    members=members)
+
+print new_balancer

http://git-wip-us.apache.org/repos/asf/libcloud/blob/96efa263/docs/examples/loadbalancer/elb/ex_create_balancer_listeners.py
----------------------------------------------------------------------
diff --git a/docs/examples/loadbalancer/elb/ex_create_balancer_listeners.py b/docs/examples/loadbalancer/elb/ex_create_balancer_listeners.py
new file mode 100644
index 0000000..040034c
--- /dev/null
+++ b/docs/examples/loadbalancer/elb/ex_create_balancer_listeners.py
@@ -0,0 +1,15 @@
+from libcloud.compute.types import Provider
+from libcloud.compute.providers import get_driver
+
+ACCESS_ID = 'your access id'
+SECRET_KEY = 'your secret key'
+
+cls = get_driver(Provider.ELB)
+driver = cls(key=ACCESS_ID, secret=SECRET_KEY, region='')
+
+print driver.list_balancers()
+
+driver.ex_create_balancer_listeners(
+    name='MyLB',
+    listeners=[[1024, 65533, 'HTTPS',
+               'arn:aws:iam::123456789012:server-certificate/servercert']])

http://git-wip-us.apache.org/repos/asf/libcloud/blob/96efa263/docs/examples/loadbalancer/elb/ex_delete_balancer_policy.py
----------------------------------------------------------------------
diff --git a/docs/examples/loadbalancer/elb/ex_delete_balancer_policy.py b/docs/examples/loadbalancer/elb/ex_delete_balancer_policy.py
new file mode 100644
index 0000000..e8f95c9
--- /dev/null
+++ b/docs/examples/loadbalancer/elb/ex_delete_balancer_policy.py
@@ -0,0 +1,14 @@
+from libcloud.compute.types import Provider
+from libcloud.compute.providers import get_driver
+
+ACCESS_ID = 'your access id'
+SECRET_KEY = 'your secret key'
+
+cls = get_driver(Provider.ELB)
+driver = cls(key=ACCESS_ID, secret=SECRET_KEY, region='')
+
+print driver.list_balancers()
+
+driver.ex_delete_balancer_policy(
+    name='MyLB',
+    policy_name='EnableProxyProtocol')

http://git-wip-us.apache.org/repos/asf/libcloud/blob/96efa263/docs/examples/loadbalancer/elb/ex_list_balancer_policies.py
----------------------------------------------------------------------
diff --git a/docs/examples/loadbalancer/elb/ex_list_balancer_policies.py b/docs/examples/loadbalancer/elb/ex_list_balancer_policies.py
new file mode 100644
index 0000000..b91a29f
--- /dev/null
+++ b/docs/examples/loadbalancer/elb/ex_list_balancer_policies.py
@@ -0,0 +1,12 @@
+from libcloud.compute.types import Provider
+from libcloud.compute.providers import get_driver
+
+ACCESS_ID = 'your access id'
+SECRET_KEY = 'your secret key'
+
+cls = get_driver(Provider.ELB)
+driver = cls(key=ACCESS_ID, secret=SECRET_KEY, region='')
+
+print driver.list_balancers()
+
+print driver.ex_list_balancer_policies()

http://git-wip-us.apache.org/repos/asf/libcloud/blob/96efa263/docs/examples/loadbalancer/elb/ex_list_balancer_policy_types.py
----------------------------------------------------------------------
diff --git a/docs/examples/loadbalancer/elb/ex_list_balancer_policy_types.py b/docs/examples/loadbalancer/elb/ex_list_balancer_policy_types.py
new file mode 100644
index 0000000..f5bb5e5
--- /dev/null
+++ b/docs/examples/loadbalancer/elb/ex_list_balancer_policy_types.py
@@ -0,0 +1,12 @@
+from libcloud.compute.types import Provider
+from libcloud.compute.providers import get_driver
+
+ACCESS_ID = 'your access id'
+SECRET_KEY = 'your secret key'
+
+cls = get_driver(Provider.ELB)
+driver = cls(key=ACCESS_ID, secret=SECRET_KEY, region='')
+
+print driver.list_balancers()
+
+print driver.ex_list_balancer_policy_types()

http://git-wip-us.apache.org/repos/asf/libcloud/blob/96efa263/docs/examples/loadbalancer/elb/ex_set_balancer_policies_backend_server.py
----------------------------------------------------------------------
diff --git a/docs/examples/loadbalancer/elb/ex_set_balancer_policies_backend_server.py b/docs/examples/loadbalancer/elb/ex_set_balancer_policies_backend_server.py
new file mode 100644
index 0000000..9a4386c
--- /dev/null
+++ b/docs/examples/loadbalancer/elb/ex_set_balancer_policies_backend_server.py
@@ -0,0 +1,15 @@
+from libcloud.compute.types import Provider
+from libcloud.compute.providers import get_driver
+
+ACCESS_ID = 'your access id'
+SECRET_KEY = 'your secret key'
+
+cls = get_driver(Provider.ELB)
+driver = cls(key=ACCESS_ID, secret=SECRET_KEY, region='')
+
+print driver.list_balancers()
+
+driver.ex_set_balancer_policies_backend_server(
+    name='MyLB',
+    port=80,
+    policies=['MyDurationStickyPolicy'])

http://git-wip-us.apache.org/repos/asf/libcloud/blob/96efa263/docs/examples/loadbalancer/elb/ex_set_balancer_policies_listener.py
----------------------------------------------------------------------
diff --git a/docs/examples/loadbalancer/elb/ex_set_balancer_policies_listener.py b/docs/examples/loadbalancer/elb/ex_set_balancer_policies_listener.py
new file mode 100644
index 0000000..4f52e98
--- /dev/null
+++ b/docs/examples/loadbalancer/elb/ex_set_balancer_policies_listener.py
@@ -0,0 +1,15 @@
+from libcloud.compute.types import Provider
+from libcloud.compute.providers import get_driver
+
+ACCESS_ID = 'your access id'
+SECRET_KEY = 'your secret key'
+
+cls = get_driver(Provider.ELB)
+driver = cls(key=ACCESS_ID, secret=SECRET_KEY, region='')
+
+print driver.list_balancers()
+
+driver.ex_set_balancer_policies_listener(
+    name='MyLB',
+    port=80,
+    policies=['MyDurationStickyPolicy'])

http://git-wip-us.apache.org/repos/asf/libcloud/blob/96efa263/docs/examples/loadbalancer/elb/list_load_balancer.py
----------------------------------------------------------------------
diff --git a/docs/examples/loadbalancer/elb/list_load_balancer.py b/docs/examples/loadbalancer/elb/list_load_balancer.py
new file mode 100644
index 0000000..0f8dcd1
--- /dev/null
+++ b/docs/examples/loadbalancer/elb/list_load_balancer.py
@@ -0,0 +1,10 @@
+from libcloud.compute.types import Provider
+from libcloud.compute.providers import get_driver
+
+ACCESS_ID = 'your access id'
+SECRET_KEY = 'your secret key'
+
+cls = get_driver(Provider.ELB)
+driver = cls(key=ACCESS_ID, secret=SECRET_KEY, region='')
+
+print driver.list_balancers()

http://git-wip-us.apache.org/repos/asf/libcloud/blob/96efa263/docs/loadbalancer/drivers/elb.rst
----------------------------------------------------------------------
diff --git a/docs/loadbalancer/drivers/elb.rst b/docs/loadbalancer/drivers/elb.rst
new file mode 100644
index 0000000..219454f
--- /dev/null
+++ b/docs/loadbalancer/drivers/elb.rst
@@ -0,0 +1,238 @@
+Elastic Load Balancing interface for AWS
+========================================
+
+What is Elastic Load Balancing?
+-------------------------------
+
+Amazon Web Services (AWS) provides Elastic Load Balancing to automatically
+distribute incoming web traffic across multiple Amazon Elastic Compute Cloud
+(Amazon EC2) instances. With Elastic Load Balancing, you can add and remove
+EC2 instances as your needs change without disrupting the overall flow of
+information. If one EC2 instance fails, Elastic Load Balancing automatically
+reroutes the traffic to the remaining running EC2 instances. If the failed
+EC2 instance is restored, Elastic Load Balancing restores the traffic to
+that instance. Elastic Load Balancing offers clients a single point of
+contact, and it can also serve as the first line of defense against attacks
+on your network. You can offload the work of encryption and decryption to
+Elastic Load Balancing, so your servers can focus on their main task.
+
+
+Ok Now Tell me some advantages of using Elastic Load Balancing
+---------------------------------------------------------------
+
+* Distribution of requests to Amazon EC2 instances (servers)in multiple
+  vailability Zones so that the risk of overloading one single instance
+  is minimized.
+* Continuous monitoring of the health of Amazon EC2 instances registered
+  with the load balancer.
+* Support for end-to-end traffic encryption on those networks that use
+  secure (HTTPS/SSL) connections.
+* Support for the sticky session feature, which is the ability to "stick"
+  user sessions to specific Amazon EC2 instances.
+* Association of the load balancer with your domain name.
+* Supports use of both the Internet Protocol version 4 (IPv4) and
+  Internet Protocol version 6 (IPv6).
+
+"How it works?" would be nice to share
+--------------------------------------
+
+Elastic Load Balancing consists of two components: the load balancers and
+the controller service. The load balancers monitor the traffic and handle
+requests that come in through the Internet. The controller service monitors
+the load balancers, adding and removing load balancers as needed and
+verifying that the load balancers are functioning properly.
+
+* The client sends a URL request to DNS servers to access your application.
+  For example, myLB-1234567890.us-east-1.elb.amazonaws.com.
+* Then The client looks for the resolution of the DNS name sent by the
+  DNS server.
+* The client then opens a connection to the machine at the provided IP address.
+  The instance at this address is the load balancer you created.
+* The load balancer checks the health states of all the registered EC2 application
+  instances within the selected Availability Zones
+* Then load balancer routes the client request to the healthy EC2 application
+  instance identified in the previous step.
+
+That's not enough, you want to learn more about ELB, then Refer `AWS site
+<http://docs.aws.amazon.com/ElasticLoadBalancing/latest/APIReference/Welcome.html/>`_.
+
+
+Now lets dive into the tutorial which will focus on libcloud Elastic
+Load Balancing interface for AWS.
+
+1. Creating a Connection
+-------------------------
+
+The first step in accessing ELB is to create a connection to the service.
+
+So, when you instantiate a ELB driver you need to pass the following arguments
+to the driver constructor:
+
+* ``key`` - Your AWS API key
+* ``secret`` - Your AWS secret key
+* ``region`` - The region of your AWS instance host point
+  (e.g ``us-west-2`` for ``US West (Oregon) Region``)
+
+Typically this will lead to:
+
+.. literalinclude:: /examples/loadbalancer/elb/create_lb_connection_for_aws.py
+   :language: python
+
+if everything gone well; means if your console has not yelled any error then
+your connection has been established.
+
+by the way if you have difficulty in getting your 'access key' and 'secret key'
+id's, look at security credentials page of AWS management console.
+
+
+The base `libcloud` ELB API allows you to:
+
+* list, create, attach, detach, delete load balancer
+* list protocols related to load balancer
+
+Non-standard functionality and extension methods
+-------------------------------------------------
+
+The AWS ELB driver exposes a lot of `libcloud` non-standard
+functionalities through extension methods and arguments.
+
+These functionalities include:
+
+* list laod balancer policy
+* list load balancer policy types
+* create, delete load balancer policy
+* create listeners for load balancer
+* enable/disable policies on backend server & listeners
+
+2. Getting Existing Load Balancers
+-----------------------------------
+
+To retrieve any exiting load balancers available
+
+.. literalinclude:: /examples/loadbalancer/elb/list_load_balancer.py
+   :language: python
+
+this will return you a list of load balancers
+``[<LoadBalancer: id=balancer_id, name=balancer_name, state=balancer_state>]``
+
+3. Creating New Load Balancers
+-------------------------------
+
+To create new load balancer initialise some members for the load balancer
+first
+
+.. literalinclude:: /examples/loadbalancer/elb/create_load_balancer.py
+   :language: python
+
+Ok if everything is fine; you will see this on your python shell screen
+``[<LoadBalancer: id='MyLB', name='MyLB', state=1]``
+
+note: ``state`` value may differ
+
+4. Creating Load Balancer Policy
+--------------------------------
+
+To creates a new policy for a load balancer that contains the necessary
+attributes depending on the policy type
+
+.. literalinclude:: /examples/loadbalancer/elb/create_lb_policy.py
+   :language: python
+
+If you get ``True``, then congratulation you have succesfully created
+the load balancer policy.
+
+Now there are some extension methods to look on
+To get all policy associated with the load balancer
+
+.. literalinclude:: /examples/loadbalancer/elb/ex_list_balancer_policies.py
+   :language: python
+
+you will get output something like this
+ ``['EnableProxyProtocol']``
+
+To get all the policy types available
+
+.. literalinclude:: /examples/loadbalancer/elb/ex_list_balancer_policy_types.py
+   :language: python
+
+It will return a list of available policy types
+ ``['EnableProxyProtocolType']``
+
+To delete a policy associated with the load balancer
+
+.. literalinclude:: /examples/loadbalancer/elb/ex_delete_balancer_policy.py
+   :language: python
+
+Will return ``True`` if it deletes the policy successfully.
+
+5. Enable/Disable Policy on Backend server
+-------------------------------------------
+
+Wait! first tell me about Policies
+----------------------------------
+
+A security policy is a combination of SSL Protocols, SSL Ciphers, and
+the Server Order Preference option. For more information about
+configuring SSL connection for your load balancer
+
+Two Types
+
+* Predefined Security Policy—A list of predefined SSL negotiation
+  configurations with enabled ciphers and protocols
+* Custom Security Policy—A list of ciphers and protocols that you
+  specify to create a custom negotiation configuration
+
+To enable the policies on the server we need to call
+"SetLoadBalancerPoliciesForBackendServer" action.
+
+.. literalinclude:: /examples/loadbalancer/elb/ex_set_balancer_policies_backend_server.py
+   :language: python
+
+Will return ``True`` if it sets the policies successfully on backend server.
+
+To disable the policy you just need to pass the policies parameter as empty
+list
+
+6. Enable/Diable Policy on Listeners
+-------------------------------------
+
+I don't have any idea about ``listeners``?
+------------------------------------------
+
+A listener is a process that listens for connection requests.
+It is configured with a protocol and a port number for front-end
+(client to load balancer) and back-end (load balancer to back-end instance)
+connections.
+
+Elastic Load Balancing supports the load balancing of applications
+using HTTP, HTTPS (secure HTTP), TCP, and SSL (secure TCP) protocols.
+The HTTPS uses the SSL protocol to establish secure connections over
+the HTTP layer. You can also use SSL protocol to establish secure
+connections over the TCP layer.
+
+To create one or more listeners on a load balancer for the specified port
+
+.. literalinclude:: /examples/loadbalancer/elb/ex_create_balancer_listeners.py
+   :language: python
+
+Will return ``True`` if it creates load balancer listeners successfully.
+
+As mentioned above for backend Server, to enable the policies on the listeners,
+need to call ``SetLoadBalancerPoliciesOfListener`` action
+
+.. literalinclude:: /examples/loadbalancer/elb/ex_set_balancer_policies_listener.py
+   :language: python
+
+Will return ``True`` if it sets load balancer policies for listeners successfully.
+
+To disable the policy you just need to pass the policies parameter as empty
+list
+
+Wrapping up
+-----------
+
+So now we have come to the end of tutorial. This last program file implements all
+the above attributes of ELB driver that we have discussed.
+
+.. literalinclude:: /examples/loadbalancer/elb/complete_tut.py
+   :language: python

http://git-wip-us.apache.org/repos/asf/libcloud/blob/96efa263/libcloud/loadbalancer/drivers/elb.py
----------------------------------------------------------------------
diff --git a/libcloud/loadbalancer/drivers/elb.py b/libcloud/loadbalancer/drivers/elb.py
index f57ad9c..73a3c72 100644
--- a/libcloud/loadbalancer/drivers/elb.py
+++ b/libcloud/loadbalancer/drivers/elb.py
@@ -18,6 +18,7 @@ __all__ = [
 ]
 
 
+from libcloud.utils.py3 import httplib
 from libcloud.utils.xml import findtext, findall
 from libcloud.loadbalancer.types import State
 from libcloud.loadbalancer.base import Driver, LoadBalancer, Member
@@ -132,6 +133,183 @@ class ElasticLBDriver(Driver):
     def balancer_list_members(self, balancer):
         return balancer._members
 
+    def ex_list_balancer_policies(self, balancer):
+        """
+        Return a list of policy description string.
+
+        :rtype: ``list`` of ``str``
+        """
+        params = {
+            'Action': 'DescribeLoadBalancerPolicies',
+            'LoadBalancerName': balancer.id
+        }
+
+        data = self.connection.request(ROOT, params=params).object
+        return self._to_policies(data)
+
+    def ex_list_balancer_policy_types(self):
+        """
+        Return a list of policy type description string.
+
+        :rtype: ``list`` of ``str``
+        """
+        params = {'Action': 'DescribeLoadBalancerPolicyTypes'}
+
+        data = self.connection.request(ROOT, params=params).object
+        return self._to_policy_types(data)
+
+    def ex_create_balancer_policy(self, name, policy_name, policy_type,
+                                  policy_attributes=None):
+        """
+        Create a new load balancer policy
+
+        :param name: Balancer name to create the policy for
+        :type  name: ``str``
+
+        :param policy_name: policy to be created
+        :type  policy_name: ``str``
+
+        :param policy_type: policy type being used to create policy.
+        :type  policy_type: ``str``
+
+        :param policy_attributes: Each list contain values, ['AttributeName',
+                                                             'value']
+        :type  policy_attributes: ``PolicyAttribute list``
+        """
+        params = {
+            'Action': 'CreateLoadBalancerPolicy',
+            'LoadBalancerName': name,
+            'PolicyName': policy_name,
+            'PolicyTypeName': policy_type
+        }
+
+        if policy_attributes is not None:
+            for index, (name, value) in enumerate(
+                    policy_attributes.iteritems(), 1):
+                params['PolicyAttributes.member.%d. \
+                        AttributeName' % (index)] = name
+                params['PolicyAttributes.member.%d. \
+                        AttributeValue' % (index)] = value
+
+        response = self.connection.request(ROOT, params=params)
+        return response.status == httplib.OK
+
+    def ex_delete_balancer_policy(self, name, policy_name):
+        """
+        Delete a load balancer policy
+
+        :param name: balancer name for which policy will be deleted
+        :type  name: ``str``
+
+        :param policy_name: The Mnemonic name for the policy being deleted
+        :type  policy_name: ``str``
+        """
+        params = {
+            'Action': 'DeleteLoadBalancerPolicy',
+            'LoadBalancerName': name,
+            'PolicyName': policy_name
+        }
+
+        response = self.connection.request(ROOT, params=params)
+        return response.status == httplib.OK
+
+    def ex_set_balancer_policies_listener(self, name, port, policies):
+        """
+        Associates, updates, or disables a policy with a listener on
+        the load balancer
+
+        :param name: balancer name to set policies for listerner
+        :type  name: ``str``
+
+        :param port: port to use
+        :type  port: ``str``
+
+        :param policies: List of policies to be associated with the balancer
+        :type  policies: ``string list``
+        """
+        params = {
+            'Action': 'SetLoadBalancerPoliciesOfListener',
+            'LoadBalancerName': name,
+            'LoadBalancerPort': str(port)
+        }
+
+        if policies:
+            params = self._create_list_params(params, policies,
+                                              'PolicyNames.member.%d')
+
+        response = self.connection.request(ROOT, params=params)
+        return response.status == httplib.OK
+
+    def ex_set_balancer_policies_backend_server(self, name, instance_port,
+                                                policies):
+        """
+        Replaces the current set of policies associated with a port on
+        which the back-end server is listening with a new set of policies
+
+        :param name: balancer name to set policies of backend server
+        :type  name: ``str``
+
+        :param instance_port: Instance Port
+        :type  instance_port: ``int``
+
+        :param policies: List of policies to be associated with the balancer
+        :type  policies: ``string list`
+        """
+        params = {
+            'Action': 'SetLoadBalancerPoliciesForBackendServer',
+            'LoadBalancerName': name,
+            'InstancePort': str(instance_port)
+        }
+
+        if policies:
+            params = self._create_list_params(params, policies,
+                                              'PolicyNames.member.%d')
+
+        response = self.connection.request(ROOT, params=params)
+        return response.status == httplib.OK
+
+    def ex_create_balancer_listeners(self, name, listeners=None):
+        """
+        Creates one or more listeners on a load balancer for the specified port
+
+        :param name: The mnemonic name associated with the load balancer
+        :type  name: ``str``
+
+        :param listeners: Each tuple contain values, (LoadBalancerPortNumber,
+                          InstancePortNumber, Protocol,[SSLCertificateId])
+        :type  listeners: ``list of tuple`
+        """
+        params = {
+            'Action': 'CreateLoadBalancerListeners',
+            'LoadBalancerName': name
+        }
+
+        for index, listener in enumerate(listeners):
+            i = index + 1
+            protocol = listener[2].upper()
+            params['Listeners.member.%d.LoadBalancerPort' % i] = listener[0]
+            params['Listeners.member.%d.InstancePort' % i] = listener[1]
+            params['Listeners.member.%d.Protocol' % i] = listener[2]
+            if protocol == 'HTTPS' or protocol == 'SSL':
+                params['Listeners.member.%d.   \
+                        SSLCertificateId' % i] = listener[3]
+        else:
+            return False
+
+        response = self.connection.request(ROOT, params=params)
+        return response.status == httplib.OK
+
+    def _to_policies(self, data):
+        xpath = 'DescribeLoadBalancerPoliciesResult/PolicyDescriptions/member'
+        return [findtext(element=el, xpath='PolicyName', namespace=NS)
+                for el in findall(element=data, xpath=xpath, namespace=NS)]
+
+    def _to_policy_types(self, data):
+        xpath = 'DescribeLoadBalancerPolicyTypesResult/'
+        xpath += 'PolicyTypeDescriptions/member'
+        return [findtext(element=el, xpath='PolicyTypeName', namespace=NS)
+                for el in findall(element=data, xpath=xpath, namespace=NS)]
+
     def _to_balancers(self, data):
         xpath = 'DescribeLoadBalancersResult/LoadBalancerDescriptions/member'
         return [self._to_balancer(el)
@@ -160,3 +338,13 @@ class ElasticLBDriver(Driver):
                                             balancer=balancer))
 
         return balancer
+
+    def _create_list_params(self, params, items, label):
+        """
+        return parameter list
+        """
+        if isinstance(items, str):
+            items = [items]
+        for index, item in enumerate(items):
+            params[label % (index + 1)] = item
+        return params

http://git-wip-us.apache.org/repos/asf/libcloud/blob/96efa263/libcloud/test/loadbalancer/fixtures/elb/create_load_balancer_policy.xml
----------------------------------------------------------------------
diff --git a/libcloud/test/loadbalancer/fixtures/elb/create_load_balancer_policy.xml b/libcloud/test/loadbalancer/fixtures/elb/create_load_balancer_policy.xml
new file mode 100644
index 0000000..8c0b4c8
--- /dev/null
+++ b/libcloud/test/loadbalancer/fixtures/elb/create_load_balancer_policy.xml
@@ -0,0 +1,3 @@
+<CreateLoadBalancerPolicyResult xmlns="http://elasticloadbalancing.amazonaws.com/doc/2012-06-01/">
+	<DNSName>tests.example.com</DNSName>
+</CreateLoadBalancerPolicyResult>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/libcloud/blob/96efa263/libcloud/test/loadbalancer/fixtures/elb/describe_load_balancer_policies.xml
----------------------------------------------------------------------
diff --git a/libcloud/test/loadbalancer/fixtures/elb/describe_load_balancer_policies.xml b/libcloud/test/loadbalancer/fixtures/elb/describe_load_balancer_policies.xml
new file mode 100644
index 0000000..188af76
--- /dev/null
+++ b/libcloud/test/loadbalancer/fixtures/elb/describe_load_balancer_policies.xml
@@ -0,0 +1,29 @@
+<DescribeLoadBalancerPoliciesResponse xmlns="http://elasticloadbalancing.amazonaws.com/doc/2012-06-01/">
+  <DescribeLoadBalancerPoliciesResult>
+    <PolicyDescriptions>
+      <member>
+        <PolicyName>MyDurationStickyPolicy</PolicyName>
+        <PolicyTypeName>LBCookieStickinessPolicyType</PolicyTypeName>
+        <PolicyAttributeDescriptions>
+          <member>
+            <AttributeName>CookieExpirationPeriod</AttributeName>
+            <AttributeValue>60</AttributeValue>
+          </member>
+        </PolicyAttributeDescriptions>
+      </member>
+      <member>
+        <PolicyName>MyAppStickyPolicy</PolicyName>
+        <PolicyTypeName>AppCookieStickinessPolicyType</PolicyTypeName>
+        <PolicyAttributeDescriptions>
+          <member>
+            <AttributeName>CookieName</AttributeName>
+            <AttributeValue>MyAppCookie</AttributeValue>
+          </member>
+        </PolicyAttributeDescriptions>
+      </member>
+    </PolicyDescriptions>
+  </DescribeLoadBalancerPoliciesResult>
+<ResponseMetadata>
+    <RequestId>83c88b9d-12b7-11e3-8b82-87b12EXAMPLE</RequestId>
+</ResponseMetadata>
+</DescribeLoadBalancerPoliciesResponse>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/libcloud/blob/96efa263/libcloud/test/loadbalancer/fixtures/elb/describe_load_balancers_policy_types.xml
----------------------------------------------------------------------
diff --git a/libcloud/test/loadbalancer/fixtures/elb/describe_load_balancers_policy_types.xml b/libcloud/test/loadbalancer/fixtures/elb/describe_load_balancers_policy_types.xml
new file mode 100644
index 0000000..05635f3
--- /dev/null
+++ b/libcloud/test/loadbalancer/fixtures/elb/describe_load_balancers_policy_types.xml
@@ -0,0 +1,21 @@
+<DescribeLoadBalancerPolicyTypesResponse xmlns="http://elasticloadbalancing.amazonaws.com/doc/2012-06-01/">
+  <DescribeLoadBalancerPolicyTypesResult>
+    <PolicyTypeDescriptions>
+      <member>
+        <PolicyAttributeTypeDescriptions>
+          <member>
+            <AttributeName>ProxyProtocol</AttributeName>
+            <AttributeType>Boolean</AttributeType>
+            <Cardinality>ONE</Cardinality>
+          </member>
+        </PolicyAttributeTypeDescriptions>
+        <PolicyTypeName>ProxyProtocolPolicyType</PolicyTypeName>
+        <Description>Policy that controls whether to include the IP address and port of the originating request for TCP messages.
+        This policy operates on TCP/SSL listeners only</Description>
+      </member>
+    </PolicyTypeDescriptions>
+  </DescribeLoadBalancerPolicyTypesResult>
+  <ResponseMetadata>
+    <RequestId>1549581b-12b7-11e3-895e-1334aEXAMPLE</RequestId>
+  </ResponseMetadata>
+</DescribeLoadBalancerPolicyTypesResponse>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/libcloud/blob/96efa263/libcloud/test/loadbalancer/fixtures/elb/set_load_balancer_policies_for_backend_server.xml
----------------------------------------------------------------------
diff --git a/libcloud/test/loadbalancer/fixtures/elb/set_load_balancer_policies_for_backend_server.xml b/libcloud/test/loadbalancer/fixtures/elb/set_load_balancer_policies_for_backend_server.xml
new file mode 100644
index 0000000..39cfc3d
--- /dev/null
+++ b/libcloud/test/loadbalancer/fixtures/elb/set_load_balancer_policies_for_backend_server.xml
@@ -0,0 +1,6 @@
+<SetLoadBalancerPoliciesForBackendServerResponse xmlns="http://elasticloadbalancing.amazonaws.com/doc/2012-06-01/">
+  <SetLoadBalancerPoliciesForBackendServerResult/>
+  <ResponseMetadata>
+    <RequestId>0eb9b381-dde0-11e2-8d78-6ddbaEXAMPLE</RequestId>
+  </ResponseMetadata>
+</SetLoadBalancerPoliciesForBackendServerResponse>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/libcloud/blob/96efa263/libcloud/test/loadbalancer/fixtures/elb/set_load_balancer_policies_of_listener.xml
----------------------------------------------------------------------
diff --git a/libcloud/test/loadbalancer/fixtures/elb/set_load_balancer_policies_of_listener.xml b/libcloud/test/loadbalancer/fixtures/elb/set_load_balancer_policies_of_listener.xml
new file mode 100644
index 0000000..699350b
--- /dev/null
+++ b/libcloud/test/loadbalancer/fixtures/elb/set_load_balancer_policies_of_listener.xml
@@ -0,0 +1,4 @@
+<SetLoadBalancerPoliciesOfListenerResult xmlns="http://elasticloadbalancing.amazonaws.com/doc/2012-06-01/">
+  <Instances>
+  </Instances>
+</SetLoadBalancerPoliciesOfListenerResult>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/libcloud/blob/96efa263/libcloud/test/loadbalancer/test_elb.py
----------------------------------------------------------------------
diff --git a/libcloud/test/loadbalancer/test_elb.py b/libcloud/test/loadbalancer/test_elb.py
index 12ecb5b..88f5555 100644
--- a/libcloud/test/loadbalancer/test_elb.py
+++ b/libcloud/test/loadbalancer/test_elb.py
@@ -88,6 +88,50 @@ class ElasticLBTests(unittest.TestCase):
 
         self.assertTrue(balancer.detach_member(member))
 
+    def test_ex_list_balancer_policies(self):
+        balancer = self.driver.get_balancer(balancer_id='tests')
+        policies = self.driver.ex_list_balancer_policies(balancer)
+
+        self.assertTrue('MyDurationStickyPolicy' in policies)
+
+    def test_ex_list_balancer_policy_types(self):
+        policy_types = self.driver.ex_list_balancer_policy_types()
+
+        self.assertTrue('ProxyProtocolPolicyType' in policy_types)
+
+    def test_ex_create_balancer_policy(self):
+        self.assertTrue(
+            self.driver.ex_create_balancer_policy(
+                name='tests',
+                policy_name='MyDurationProxyPolicy',
+                policy_type='ProxyProtocolPolicyType'))
+
+    def test_ex_delete_balancer_policy(self):
+        self.assertTrue(
+            self.driver.ex_delete_balancer_policy(
+                name='tests',
+                policy_name='MyDurationProxyPolicy'))
+
+    def test_ex_set_balancer_policies_listener(self):
+        self.assertTrue(
+            self.driver.ex_set_balancer_policies_listener(
+                name='tests',
+                port=80,
+                policies=['MyDurationStickyPolicy']))
+
+    def test_ex_set_balancer_policies_backend_server(self):
+        self.assertTrue(
+            self.driver.ex_set_balancer_policies_backend_server(
+                name='tests',
+                instance_port=80,
+                policies=['MyDurationProxyPolicy']))
+
+    def text_ex_create_balancer_listeners(self):
+        self.assertTrue(
+            self.driver.ex_create_balancer_listeners(
+                name='tests',
+                listeners=[[1024, 65533, 'HTTP']]))
+
 
 class ElasticLBMockHttp(MockHttpTestCase):
     fixtures = LoadBalancerFileFixtures('elb')
@@ -100,14 +144,45 @@ class ElasticLBMockHttp(MockHttpTestCase):
         body = self.fixtures.load('create_load_balancer.xml')
         return (httplib.OK, body, {}, httplib.responses[httplib.OK])
 
-    def _2012_06_01_DeregisterInstancesFromLoadBalancer(self, method, url, body, headers):
-        body = self.fixtures.load('deregister_instances_from_load_balancer.xml')
+    def _2012_06_01_DeregisterInstancesFromLoadBalancer(self, method, url,
+                                                        body, headers):
+        body = self.fixtures.load(
+            'deregister_instances_from_load_balancer.xml')
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
+    def _2012_06_01_CreateLoadBalancerPolicy(self, method, url, body, headers):
+        body = self.fixtures.load('create_load_balancer_policy.xml')
         return (httplib.OK, body, {}, httplib.responses[httplib.OK])
 
     def _2012_06_01_DeleteLoadBalancer(self, method, url, body, headers):
         body = ''
         return (httplib.OK, body, {}, httplib.responses[httplib.OK])
 
+    def _2012_06_01_DescribeLoadBalancerPolicies(self, method, url, body,
+                                                 headers):
+        body = self.fixtures.load('describe_load_balancer_policies.xml')
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
+    def _2012_06_01_DescribeLoadBalancerPolicyTypes(self, method, url, body,
+                                                    headers):
+        body = self.fixtures.load('describe_load_balancers_policy_types.xml')
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
+    def _2012_06_01_DeleteLoadBalancerPolicy(self, method, url, body, headers):
+        body = ''
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
+    def _2012_06_01_SetLoadBalancerPoliciesOfListener(self, method, url, body,
+                                                      headers):
+        body = self.fixtures.load('set_load_balancer_policies_of_listener.xml')
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
+    def _2012_06_01_SetLoadBalancerPoliciesForBackendServer(self, method, url,
+                                                            body, headers):
+        body = self.fixtures.load(
+            'set_load_balancer_policies_for_backend_server.xml')
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
 
 if __name__ == "__main__":
     sys.exit(unittest.main())