Added key_name support to instances

This commit is contained in:
Chris St. Pierre 2014-05-07 08:54:27 -04:00
parent 4ba4dd5e26
commit b244457c4f
3 changed files with 17 additions and 3 deletions

View File

@ -46,6 +46,7 @@ class Instance(BotoInstance):
self.security_groups = security_groups
self.instance_type = kwargs.get("instance_type", "m1.small")
self.subnet_id = kwargs.get("subnet_id")
self.key_name = kwargs.get("key_name")
@classmethod
def create_from_cloudformation_json(cls, resource_name, cloudformation_json):
@ -60,7 +61,8 @@ class Instance(BotoInstance):
count=1,
security_group_names=group_names,
instance_type=properties.get("InstanceType", "m1.small"),
subnet_id=properties.get("SubnetId")
subnet_id=properties.get("SubnetId"),
key_name=properties.get("KeyName"),
)
return reservation.instances[0]

View File

@ -35,9 +35,11 @@ class InstanceResponse(BaseResponse):
security_group_names = self._get_multi_param('SecurityGroup')
instance_type = self.querystring.get("InstanceType", ["m1.small"])[0]
subnet_id = self.querystring.get("SubnetId", [None])[0]
key_name = self.querystring.get("KeyName", [None])[0]
new_reservation = ec2_backend.add_instances(
image_id, min_count, user_data, security_group_names,
instance_type=instance_type, subnet_id=subnet_id)
instance_type=instance_type, subnet_id=subnet_id,
key_name=key_name)
template = Template(EC2_RUN_INSTANCES)
return template.render(reservation=new_reservation)
@ -114,6 +116,7 @@ EC2_RUN_INSTANCES = """<RunInstancesResponse xmlns="http://ec2.amazonaws.com/doc
<privateDnsName/>
<dnsName/>
<reason/>
<keyName>{{ instance.key_name }}</keyName>
<amiLaunchIndex>0</amiLaunchIndex>
<instanceType>{{ instance.instance_type }}</instanceType>
<launchTime>2007-08-07T11:51:50.000Z</launchTime>
@ -164,7 +167,7 @@ EC2_DESCRIBE_INSTANCES = """<DescribeInstancesResponse xmlns='http://ec2.amazona
<privateDnsName>ip-10.0.0.12.ec2.internal</privateDnsName>
<dnsName>ec2-46.51.219.63.compute-1.amazonaws.com</dnsName>
<reason/>
<keyName>gsg-keypair</keyName>
<keyName>{{ instance.key_name }}</keyName>
<amiLaunchIndex>0</amiLaunchIndex>
<productCodes/>
<instanceType>{{ instance.instance_type }}</instanceType>

View File

@ -201,3 +201,12 @@ def test_run_instance_with_subnet():
instance = reservation.instances[0]
instance.subnet_id.should.equal("subnet-abcd1234")
@mock_ec2
def test_run_instance_with_keypair():
conn = boto.connect_ec2('the_key', 'the_secret')
reservation = conn.run_instances('ami-1234abcd', key_name="keypair_name")
instance = reservation.instances[0]
instance.key_name.should.equal("keypair_name")