Issue LIBCLOUD-335: Add IAM Profile on node creation for ec2 based providers.
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/df3eb017 Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/df3eb017 Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/df3eb017 Branch: refs/heads/trunk Commit: df3eb0179ff0c179e2433981f6917a61cf682fba Parents: 08a325a Author: Xavier Barbosa <[email protected]> Authored: Tue Sep 24 14:21:09 2013 +0200 Committer: Tomaz Muraus <[email protected]> Committed: Wed Sep 25 15:37:50 2013 +0200 ---------------------------------------------------------------------- docs/compute/examples.rst | 11 +++++++++++ docs/examples/compute/create_ec2_node_iam.py | 20 ++++++++++++++++++++ docs/other/working-with-oo-apis.rst | 6 ++++++ libcloud/compute/drivers/ec2.py | 19 +++++++++++++++++-- 4 files changed, 54 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/df3eb017/docs/compute/examples.rst ---------------------------------------------------------------------- diff --git a/docs/compute/examples.rst b/docs/compute/examples.rst index 1176795..0892d6d 100644 --- a/docs/compute/examples.rst +++ b/docs/compute/examples.rst @@ -93,6 +93,17 @@ supported providers and provider constants, see .. literalinclude:: /examples/compute/create_ec2_node_custom_ami.py :language: python +Create EC2 node using an IAM Profile +------------------------------------ + +.. note:: + + This example assumes the IAM profile already exists. If the key pair + doesn't exist yet, you must create it manually. + +.. literalinclude:: /examples/compute/create_ec2_node_iam.py + :language: python + Create a node on a CloudStack provider using a provided key pair and security groups ------------------------------------------------------------------------------------ http://git-wip-us.apache.org/repos/asf/libcloud/blob/df3eb017/docs/examples/compute/create_ec2_node_iam.py ---------------------------------------------------------------------- diff --git a/docs/examples/compute/create_ec2_node_iam.py b/docs/examples/compute/create_ec2_node_iam.py new file mode 100644 index 0000000..952ef2b --- /dev/null +++ b/docs/examples/compute/create_ec2_node_iam.py @@ -0,0 +1,20 @@ +from libcloud.compute.types import Provider +from libcloud.compute.providers import get_driver + +ACCESS_ID = 'your access id' +SECRET_KEY = 'your secret key' +IAM_PROFILE = 'your IAM profile arn or IAM profile name' + +IMAGE_ID = 'ami-c8052d8d' +SIZE_ID = 't1.micro' +cls = get_driver(Provider.EC2_US_WEST) +driver = cls(ACCESS_ID, SECRET_KEY) + +# Here we select size and image +sizes = driver.list_sizes() +images = driver.list_images() + +size = [s for s in sizes if s.id == SIZE_ID][0] +image = [i for i in images if i.id == IMAGE_ID][0] + +node = driver.create_node(name='test-node', image=image, size=size, ex_iamprofile=IAM_PROFILE) http://git-wip-us.apache.org/repos/asf/libcloud/blob/df3eb017/docs/other/working-with-oo-apis.rst ---------------------------------------------------------------------- diff --git a/docs/other/working-with-oo-apis.rst b/docs/other/working-with-oo-apis.rst index 45fb45a..96b29b6 100644 --- a/docs/other/working-with-oo-apis.rst +++ b/docs/other/working-with-oo-apis.rst @@ -59,3 +59,9 @@ Example 2 - creating an EC2 instance with a known ``NodeSize`` and ``NodeImage`` .. literalinclude:: /examples/compute/create_ec2_node_manual_instantiation.py :language: python + +Example 3 - creating an EC2 instance with an IAM profile +-------------------------------------------------------- + +.. literalinclude:: /examples/compute/create_ec2_node_iam.py + :language: python http://git-wip-us.apache.org/repos/asf/libcloud/blob/df3eb017/libcloud/compute/drivers/ec2.py ---------------------------------------------------------------------- diff --git a/libcloud/compute/drivers/ec2.py b/libcloud/compute/drivers/ec2.py index 3ddb3e5..3be281e 100644 --- a/libcloud/compute/drivers/ec2.py +++ b/libcloud/compute/drivers/ec2.py @@ -543,7 +543,9 @@ class BaseEC2NodeDriver(NodeDriver): 'clienttoken': findattr(element=element, xpath="clientToken", namespace=NAMESPACE), 'groups': groups, - 'tags': tags + 'tags': tags, + 'iam_profile': findattr(element, xpath="iamInstanceProfile/id", + namespace=NAMESPACE) } ) return n @@ -1325,7 +1327,7 @@ class BaseEC2NodeDriver(NodeDriver): @keyword ex_maxcount: Maximum number of instances to launch @type ex_maxcount: C{int} - @keyword ex_security_groups: A list of namees of security groups to + @keyword ex_security_groups: A list of names of security groups to assign to the node. @type ex_security_groups: C{list} @@ -1343,6 +1345,9 @@ class BaseEC2NodeDriver(NodeDriver): [{'DeviceName': '/dev/sda1', 'Ebs.VolumeSize': 10}, {'DeviceName': '/dev/sdb', 'VirtualName': 'ephemeral0'}] @type ex_blockdevicemappings: C{list} of C{dict} + + @keyword ex_iamprofile: Name or ARN of IAM profile + @type ex_iamprofile: C{str} """ image = kwargs["image"] size = kwargs["size"] @@ -1412,6 +1417,16 @@ class BaseEC2NodeDriver(NodeDriver): for k, v in mapping.items(): 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') + object = self.connection.request(self.path, params=params).object nodes = self._to_nodes(object, 'instancesSet/item')
