Added default flavor. Made flavors to be retrieved once in describe.
Change-Id: I17379179b433b1ea77b19676f54a122304695621
This commit is contained in:
parent
91ad95ce4b
commit
ae298a3a26
|
@ -41,6 +41,9 @@ ec2_opts = [
|
|||
default=False,
|
||||
help='Return the IP address as private dns hostname in '
|
||||
'describe instances'),
|
||||
cfg.StrOpt('default_flavor',
|
||||
default='m1.small',
|
||||
help='A flavor to use as a default instance type'),
|
||||
]
|
||||
|
||||
CONF = cfg.CONF
|
||||
|
@ -92,6 +95,8 @@ def run_instances(context, image_id, min_count, max_count,
|
|||
|
||||
nova = clients.nova(context)
|
||||
try:
|
||||
if instance_type is None:
|
||||
instance_type = CONF.default_flavor
|
||||
os_flavor = next(f for f in nova.flavors.list()
|
||||
if f.name == instance_type)
|
||||
except StopIteration:
|
||||
|
@ -282,7 +287,8 @@ class InstanceDescriber(common.TaggableItemsDescriber):
|
|||
formatted_instance = _format_instance(
|
||||
self.context, instance, os_instance,
|
||||
self.ec2_network_interfaces.get(instance['id']),
|
||||
self.image_ids, self.volumes, self.os_volumes)
|
||||
self.image_ids, self.volumes, self.os_volumes,
|
||||
self.os_flavors)
|
||||
|
||||
reservation_id = instance['reservation_id']
|
||||
if reservation_id in self.reservations:
|
||||
|
@ -315,6 +321,7 @@ class InstanceDescriber(common.TaggableItemsDescriber):
|
|||
|
||||
def get_os_items(self):
|
||||
self.os_volumes = _get_os_volumes(self.context)
|
||||
self.os_flavors = _get_os_flavors(self.context)
|
||||
nova = clients.nova(ec2_context.get_os_admin_context())
|
||||
return nova.servers.list(
|
||||
search_opts={'all_tenants': True,
|
||||
|
@ -511,7 +518,8 @@ def _format_reservation(context, reservation, formatted_instances, os_groups):
|
|||
|
||||
|
||||
def _format_instance(context, instance, os_instance, ec2_network_interfaces,
|
||||
image_ids, volumes=None, os_volumes=None):
|
||||
image_ids, volumes=None, os_volumes=None,
|
||||
os_flavors=None):
|
||||
ec2_instance = {
|
||||
'amiLaunchIndex': instance['launch_index'],
|
||||
'imageId': (ec2utils.os_id_to_ec2_id(context, 'ami',
|
||||
|
@ -519,7 +527,7 @@ def _format_instance(context, instance, os_instance, ec2_network_interfaces,
|
|||
ids_by_os_id=image_ids)
|
||||
if os_instance.image else None),
|
||||
'instanceId': instance['id'],
|
||||
'instanceType': _cloud_format_instance_type(context, os_instance),
|
||||
'instanceType': os_flavors.get(os_instance.flavor['id'], 'unknown'),
|
||||
'keyName': os_instance.key_name,
|
||||
'launchTime': os_instance.created,
|
||||
'placement': {
|
||||
|
@ -745,6 +753,11 @@ def _get_os_instances_by_instances(context, instances, exactly=False,
|
|||
return os_instances
|
||||
|
||||
|
||||
def _get_os_flavors(context):
|
||||
os_flavors = clients.nova(context).flavors.list()
|
||||
return dict((f.id, f.name) for f in os_flavors)
|
||||
|
||||
|
||||
def _get_os_volumes(context):
|
||||
search_opts = ({'all_tenants': True,
|
||||
'project_id': context.project_id}
|
||||
|
@ -1163,7 +1176,6 @@ def _cloud_format_ramdisk_id(context, os_instance, image_ids=None):
|
|||
|
||||
|
||||
def _cloud_format_instance_type(context, os_instance):
|
||||
# TODO(ft): cache flavors
|
||||
return clients.nova(context).flavors.get(os_instance.flavor['id']).name
|
||||
|
||||
|
||||
|
|
|
@ -75,7 +75,8 @@ class InstanceTestCase(base.ApiTestCase):
|
|||
format_security_groups_ids_names.return_value = {}
|
||||
|
||||
self.fake_flavor = mock.Mock()
|
||||
self.fake_flavor.configure_mock(name='fake_flavor')
|
||||
self.fake_flavor.configure_mock(name='fake_flavor',
|
||||
id='fakeFlavorId')
|
||||
self.nova.flavors.get.return_value = self.fake_flavor
|
||||
self.nova.flavors.list.return_value = [self.fake_flavor]
|
||||
|
||||
|
@ -1424,9 +1425,7 @@ class InstancePrivateTestCase(test_base.BaseTestCase):
|
|||
def test_format_instance(self, db_api, nova, cinder):
|
||||
nova = nova.return_value
|
||||
fake_context = mock.Mock(service_catalog=[{'type': 'fake'}])
|
||||
fake_flavor = mock.Mock()
|
||||
fake_flavor.configure_mock(name='fake_flavor')
|
||||
nova.flavors.get.return_value = fake_flavor
|
||||
fake_flavors = {'fakeFlavorId': 'fake_flavor'}
|
||||
|
||||
instance = {'id': fakes.random_ec2_id('i'),
|
||||
'os_id': fakes.random_os_id(),
|
||||
|
@ -1437,13 +1436,15 @@ class InstancePrivateTestCase(test_base.BaseTestCase):
|
|||
# NOTE(ft): check instance state formatting
|
||||
setattr(os_instance, 'OS-EXT-STS:vm_state', 'active')
|
||||
formatted_instance = instance_api._format_instance(
|
||||
fake_context, instance, os_instance, [], {})
|
||||
fake_context, instance, os_instance, [], {},
|
||||
os_flavors=fake_flavors)
|
||||
self.assertEqual({'name': 'running', 'code': 16},
|
||||
formatted_instance['instanceState'])
|
||||
|
||||
setattr(os_instance, 'OS-EXT-STS:vm_state', 'stopped')
|
||||
formatted_instance = instance_api._format_instance(
|
||||
fake_context, instance, os_instance, [], {})
|
||||
fake_context, instance, os_instance, [], {},
|
||||
os_flavors=fake_flavors)
|
||||
self.assertEqual({'name': 'stopped', 'code': 80},
|
||||
formatted_instance['instanceState'])
|
||||
|
||||
|
@ -1454,7 +1455,8 @@ class InstancePrivateTestCase(test_base.BaseTestCase):
|
|||
setattr(os_instance, 'OS-EXT-SRV-ATTR:kernel_id', kernel_id)
|
||||
setattr(os_instance, 'OS-EXT-SRV-ATTR:ramdisk_id', ramdisk_id)
|
||||
formatted_instance = instance_api._format_instance(
|
||||
fake_context, instance, os_instance, [], {})
|
||||
fake_context, instance, os_instance, [], {},
|
||||
os_flavors=fake_flavors)
|
||||
db_api.add_item_id.assert_has_calls(
|
||||
[mock.call(mock.ANY, 'ami', os_instance.image['id']),
|
||||
mock.call(mock.ANY, 'aki', kernel_id),
|
||||
|
|
Loading…
Reference in New Issue