Updated Branches:
  refs/heads/trunk 08a325ac7 -> 0f69cd3fe

LIBCLOUD-335: EC2 IAM Profile are refactored for py2~3 and tested

Signed-off-by: Tomaz Muraus <[email protected]>


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

Branch: refs/heads/trunk
Commit: dcd1ac028af16b43959cb8a53f15a30eab920f32
Parents: df3eb01
Author: Xavier Barbosa <[email protected]>
Authored: Wed Sep 25 11:12:10 2013 +0200
Committer: Tomaz Muraus <[email protected]>
Committed: Wed Sep 25 15:37:50 2013 +0200

----------------------------------------------------------------------
 libcloud/compute/drivers/ec2.py                 | 17 +++++-----
 .../fixtures/ec2/run_instances_iam_profile.xml  | 35 ++++++++++++++++++++
 libcloud/test/compute/test_ec2.py               | 29 ++++++++++++++++
 3 files changed, 72 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/dcd1ac02/libcloud/compute/drivers/ec2.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/drivers/ec2.py b/libcloud/compute/drivers/ec2.py
index 3be281e..58471aa 100644
--- a/libcloud/compute/drivers/ec2.py
+++ b/libcloud/compute/drivers/ec2.py
@@ -26,7 +26,7 @@ import copy
 
 from xml.etree import ElementTree as ET
 
-from libcloud.utils.py3 import b
+from libcloud.utils.py3 import b, basestring
 
 from libcloud.utils.xml import fixxpath, findtext, findattr, findall
 from libcloud.utils.publickey import get_pubkey_ssh2_fingerprint
@@ -1418,14 +1418,13 @@ class BaseEC2NodeDriver(NodeDriver):
                     params['BlockDeviceMapping.%d.%s' % (idx, k)] = str(v)
 
         if 'ex_iamprofile' in kwargs:
-            try:
-                if kwargs['ex_iamprofile'].startswith('arn:aws:iam:'):
-                    params['IamInstanceProfile.Arn'] = kwargs['ex_iamprofile']
-                else:
-                    params['IamInstanceProfile.Name'] = kwargs['ex_iamprofile']
-            except AttributeError as exception:
-                raise AttributeError(
-                    'ex_iamprofile not string')
+            if not isinstance(kwargs['ex_iamprofile'], basestring):
+                raise AttributeError('ex_iamprofile not string')
+
+            if kwargs['ex_iamprofile'].startswith('arn:aws:iam:'):
+                params['IamInstanceProfile.Arn'] = kwargs['ex_iamprofile']
+            else:
+                params['IamInstanceProfile.Name'] = kwargs['ex_iamprofile']
 
         object = self.connection.request(self.path, params=params).object
         nodes = self._to_nodes(object, 'instancesSet/item')

http://git-wip-us.apache.org/repos/asf/libcloud/blob/dcd1ac02/libcloud/test/compute/fixtures/ec2/run_instances_iam_profile.xml
----------------------------------------------------------------------
diff --git a/libcloud/test/compute/fixtures/ec2/run_instances_iam_profile.xml 
b/libcloud/test/compute/fixtures/ec2/run_instances_iam_profile.xml
new file mode 100644
index 0000000..51a89a1
--- /dev/null
+++ b/libcloud/test/compute/fixtures/ec2/run_instances_iam_profile.xml
@@ -0,0 +1,35 @@
+<RunInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2010-08-31/";>
+  <reservationId>r-47a5403e</reservationId>
+  <ownerId>AIDADH4IGTRXXKCD</ownerId>
+  <groupSet>
+    <item>
+      <groupId>default</groupId>
+    </item>
+  </groupSet>
+  <instancesSet>
+    <item>
+      <instanceId>i-2ba64343</instanceId>
+      <imageId>ami-be3adfd7</imageId>
+      <instanceState>
+        <code>0</code>
+        <name>pending</name>
+      </instanceState>
+      <privateDnsName></privateDnsName>
+      <dnsName></dnsName>
+      <keyName>example-key-name</keyName>
+      <amiLaunchIndex>0</amiLaunchIndex>
+      <instanceType>m1.small</instanceType>
+      <launchTime>2007-08-07T11:51:50.000Z</launchTime>
+      <placement>
+        <availabilityZone>us-east-1b</availabilityZone>
+      </placement>
+      <monitoring>
+        <enabled>true</enabled>
+      </monitoring>
+      <iamInstanceProfile>
+        <id>AIDGPMS9RO4H3FEXAMPLE</id>
+        
<arn>arn:aws:iam::123456789012:instance-profile/ExampleInstanceProfile</arn>
+      </iamInstanceProfile>
+    </item>
+  </instancesSet>
+</RunInstancesResponse>

http://git-wip-us.apache.org/repos/asf/libcloud/blob/dcd1ac02/libcloud/test/compute/test_ec2.py
----------------------------------------------------------------------
diff --git a/libcloud/test/compute/test_ec2.py 
b/libcloud/test/compute/test_ec2.py
index 840a786..c51dc5a 100644
--- a/libcloud/test/compute/test_ec2.py
+++ b/libcloud/test/compute/test_ec2.py
@@ -287,6 +287,31 @@ class EC2Tests(LibcloudTestCase, TestCaseMixin):
 
         self.driver.region_name = region_old
 
+    def test_ex_create_node_with_ex_iam_profile(self):
+        iamProfile = {
+            'id': 'AIDGPMS9RO4H3FEXAMPLE',
+            'name': 'Foo',
+            'arn': 'arn:aws:iam:...'
+        }
+
+        image = NodeImage(id='ami-be3adfd7',
+                          name=self.image_name,
+                          driver=self.driver)
+        size = NodeSize('m1.small', 'Small Instance', None, None, None, None,
+                        driver=self.driver)
+
+        EC2MockHttp.type = None
+        node1 = self.driver.create_node(name='foo', image=image, size=size)
+        EC2MockHttp.type = 'ex_iam_profile'
+        node2 = self.driver.create_node(name='bar', image=image, size=size,
+                                        ex_iam_profile=iamProfile['name'])
+        node3 = self.driver.create_node(name='bar', image=image, size=size,
+                                        ex_iam_profile=iamProfile['arn'])
+
+        self.assertFalse(node1.extra['iam_profile'])
+        self.assertEqual(node2.extra['iam_profile'], iamProfile['id'])
+        self.assertEqual(node3.extra['iam_profile'], iamProfile['id'])
+
     def test_list_images(self):
         images = self.driver.list_images()
         image = images[0]
@@ -640,6 +665,10 @@ class EC2MockHttp(MockHttpTestCase):
         body = self.fixtures.load('run_instances_idem_mismatch.xml')
         return (httplib.BAD_REQUEST, body, {}, 
httplib.responses[httplib.BAD_REQUEST])
 
+    def _ex_iam_profile_RunInstances(self, method, url, body, headers):
+        body = self.fixtures.load('run_instances_iam_profile.xml')
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
     def _TerminateInstances(self, method, url, body, headers):
         body = self.fixtures.load('terminate_instances.xml')
         return (httplib.OK, body, {}, httplib.responses[httplib.OK])

Reply via email to