[GCE] Use instance name instead of instance id for instance mapping
1. Currently we use GCE id for instance mapping from GCE to Openstack. Since we use GCE name as primary identifier in all GCE operations, we should use GCE name for mapping. 2. Add Openstack Project ID and User ID in GCE instance metadata 3. Some pep8 fixes in nova code Change-Id: Id26a674be0f96e2ff5bf89c5ffcca120bd52e0fa Closes-Bug: #1717889
This commit is contained in:
parent
38f171bdea
commit
c547b5523a
|
@ -12,11 +12,12 @@ License for the specific language governing permissions and limitations
|
|||
under the License.
|
||||
"""
|
||||
|
||||
import boto
|
||||
import mock
|
||||
|
||||
from moto import mock_ec2_deprecated
|
||||
from nova import test
|
||||
from nova.virt.ec2.keypair import KeyPairNotifications
|
||||
import boto
|
||||
import mock
|
||||
|
||||
|
||||
class KeyPairNotificationsTestCase(test.NoDBTestCase):
|
||||
|
@ -80,4 +81,3 @@ class KeyPairNotificationsTestCase(test.NoDBTestCase):
|
|||
aws_keypairs = self.fake_aws_conn.get_all_key_pairs()
|
||||
self.assertEqual(len(aws_keypairs), 1)
|
||||
self.assertEqual(aws_keypairs[0].name, fake_key_name_2)
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ class GCENovaTestCase(test.TestCase):
|
|||
self.context = context.get_admin_context()
|
||||
self.instance = fake_instance.fake_instance_obj(self.context)
|
||||
self.instance.system_metadata = {'image_gce_link': 'fake_link'}
|
||||
self.instance.metadata = {'gce_id': "instance-1"}
|
||||
self.instance.metadata = {'gce_name': "instance-1"}
|
||||
self.instance.display_name = "fake_instance"
|
||||
self.instance.flavor.name = "n1-standard-1"
|
||||
self._driver.init_host(None)
|
||||
|
@ -51,7 +51,7 @@ class GCENovaTestCase(test.TestCase):
|
|||
mock_list_instances.side_effect = gce_mock.list_instances
|
||||
mock_get_metadata.side_effect = gce_mock.get_instances_metadata_key
|
||||
instances_list = self._driver.list_instances()
|
||||
self.assertTrue(isinstance(instances_list, list))
|
||||
self.assertIsInstance(instances_list, list)
|
||||
self.assertEqual(["instance-1", "instance-2"], instances_list)
|
||||
|
||||
@mock.patch('nova.virt.gce.driver.gceutils.get_instances_metadata_key')
|
||||
|
@ -60,7 +60,7 @@ class GCENovaTestCase(test.TestCase):
|
|||
mock_list_instances.side_effect = gce_mock.list_instances
|
||||
mock_get_metadata.side_effect = gce_mock.get_instances_metadata_key
|
||||
instances_list = self._driver.list_instance_uuids()
|
||||
self.assertTrue(isinstance(instances_list, list))
|
||||
self.assertIsInstance(instances_list, list)
|
||||
self.assertEqual(2, len(instances_list))
|
||||
|
||||
@mock.patch('nova.virt.gce.driver.gceutils.set_instance_metadata')
|
||||
|
@ -153,7 +153,7 @@ class GCENovaTestCase(test.TestCase):
|
|||
data=disk_data), instance=self.instance, mountpoint="/dev/sda")
|
||||
mock_attach.assert_called_once_with(
|
||||
self._driver.gce_svc, self._driver.gce_project,
|
||||
self._driver.gce_zone, self.instance.metadata['gce_id'],
|
||||
self._driver.gce_zone, self.instance.metadata['gce_name'],
|
||||
disk_data['name'], disk_data['selfLink'])
|
||||
mock_wait.assert_called_once_with(self._driver.gce_svc,
|
||||
self._driver.gce_project,
|
||||
|
@ -171,7 +171,7 @@ class GCENovaTestCase(test.TestCase):
|
|||
data=disk_data), instance=self.instance, mountpoint="/dev/sda")
|
||||
mock_detach.assert_called_once_with(
|
||||
self._driver.gce_svc, self._driver.gce_project,
|
||||
self._driver.gce_zone, self.instance.metadata['gce_id'],
|
||||
self._driver.gce_zone, self.instance.metadata['gce_name'],
|
||||
disk_data['name'])
|
||||
mock_wait.assert_called_once_with(self._driver.gce_svc,
|
||||
self._driver.gce_project,
|
||||
|
@ -186,7 +186,7 @@ class GCENovaTestCase(test.TestCase):
|
|||
update_task_state=gce_mock.update_task_state)
|
||||
mock_get_instance.assert_called_once_with(
|
||||
self._driver.gce_svc, self._driver.gce_project,
|
||||
self._driver.gce_zone, self.instance.metadata['gce_id'])
|
||||
self._driver.gce_zone, self.instance.metadata['gce_name'])
|
||||
|
||||
@mock.patch('nova.virt.gce.driver.gceutils.delete_snapshot')
|
||||
@mock.patch('nova.virt.gce.driver.gceutils.delete_disk')
|
||||
|
|
|
@ -380,7 +380,8 @@ class EC2Driver(driver.ComputeDriver):
|
|||
instance['metadata'].update({'ec2_id': ec2_id})
|
||||
ec2_instance_obj.add_tag("Name", instance['display_name'])
|
||||
ec2_instance_obj.add_tag("openstack_id", instance['uuid'])
|
||||
ec2_instance_obj.add_tag("openstack_project_id", context.project_id)
|
||||
ec2_instance_obj.add_tag(
|
||||
"openstack_project_id", context.project_id)
|
||||
ec2_instance_obj.add_tag("openstack_user_id", context.user_id)
|
||||
self._uuid_to_ec2_instance[instance.uuid] = ec2_instance_obj
|
||||
|
||||
|
|
|
@ -15,10 +15,12 @@ under the License.
|
|||
|
||||
import eventlet
|
||||
eventlet.monkey_patch()
|
||||
|
||||
from kombu import Connection
|
||||
from kombu import Exchange
|
||||
from kombu import Queue
|
||||
from kombu.mixins import ConsumerMixin
|
||||
from kombu import Queue
|
||||
|
||||
from oslo_config import cfg
|
||||
from oslo_log import log as logging
|
||||
|
||||
|
@ -66,5 +68,5 @@ class KeyPairNotifications(ConsumerMixin):
|
|||
try:
|
||||
LOG.info('Deleting %s keypair', key_name)
|
||||
self.ec2_conn.delete_key_pair(key_name)
|
||||
except:
|
||||
except Exception:
|
||||
LOG.exception('Could not delete %s', key_name)
|
||||
|
|
|
@ -125,9 +125,9 @@ class GCEDriver(driver.ComputeDriver):
|
|||
m.update(gce_id)
|
||||
return str(uuid.UUID(bytes=m.digest(), version=4))
|
||||
|
||||
def _get_gce_id_from_instance(self, instance):
|
||||
if 'gce_id' in instance.metadata and instance.metadata['gce_id']:
|
||||
return instance.metadata['gce_id']
|
||||
def _get_gce_name_from_instance(self, instance):
|
||||
if 'gce_name' in instance.metadata and instance.metadata['gce_name']:
|
||||
return instance.metadata['gce_name']
|
||||
elif instance.uuid in self._uuid_to_gce_instance:
|
||||
return self._uuid_to_gce_instance[instance.uuid]['name']
|
||||
# if none of the conditions are met we cannot map OpenStack UUID to
|
||||
|
@ -244,12 +244,21 @@ class GCEDriver(driver.ComputeDriver):
|
|||
gce_instance = gceutils.get_instance(compute, project, zone,
|
||||
gce_instance_name)
|
||||
# Update GCE info in openstack instance metadata
|
||||
instance.metadata.update({'gce_id': gce_instance['name']})
|
||||
instance.metadata.update({'gce_id': gce_instance['id']})
|
||||
instance.metadata.update({'gce_name': gce_instance['name']})
|
||||
gce_metadata = [
|
||||
{
|
||||
'key': 'openstack_id',
|
||||
'value': instance.uuid
|
||||
},
|
||||
{
|
||||
'key': 'openstack_project_id',
|
||||
'value': context.project_id
|
||||
},
|
||||
{
|
||||
'key': 'openstack_user_id',
|
||||
'value': context.user_id
|
||||
}
|
||||
]
|
||||
ssh_keys = self._process_ssh_keys(instance)
|
||||
if ssh_keys:
|
||||
|
@ -287,7 +296,7 @@ class GCEDriver(driver.ComputeDriver):
|
|||
compute, project, zone = self.gce_svc, self.gce_project, self.gce_zone
|
||||
|
||||
try:
|
||||
gce_id = self._get_gce_id_from_instance(instance)
|
||||
gce_id = self._get_gce_name_from_instance(instance)
|
||||
LOG.info("Taking snapshot of instance %s" % instance.uuid)
|
||||
try:
|
||||
boot_disk = gceutils.get_instance_boot_disk(
|
||||
|
@ -378,7 +387,7 @@ class GCEDriver(driver.ComputeDriver):
|
|||
LOG.info("Completed snapshot for instance %s" % instance.uuid)
|
||||
|
||||
except Exception as e:
|
||||
LOG.exception("An error occurred during image creation: %s" % e)
|
||||
LOG.exception("An error occured during image creation: %s" % e)
|
||||
if instance_stopped:
|
||||
operation = gceutils.start_instance(compute, project, zone,
|
||||
gce_id)
|
||||
|
@ -430,7 +439,7 @@ class GCEDriver(driver.ComputeDriver):
|
|||
def _soft_reboot(self, context, instance, network_info,
|
||||
block_device_info=None):
|
||||
compute, project, zone = self.gce_svc, self.gce_project, self.gce_zone
|
||||
gce_id = self._get_gce_id_from_instance(instance)
|
||||
gce_id = self._get_gce_name_from_instance(instance)
|
||||
LOG.info('Stopping instance %s' % instance.uuid)
|
||||
operation = gceutils.stop_instance(compute, project, zone, gce_id)
|
||||
gceutils.wait_for_operation(compute, project, operation)
|
||||
|
@ -442,7 +451,7 @@ class GCEDriver(driver.ComputeDriver):
|
|||
def _hard_reboot(self, context, instance, network_info,
|
||||
block_device_info=None):
|
||||
compute, project, zone = self.gce_svc, self.gce_project, self.gce_zone
|
||||
gce_id = self._get_gce_id_from_instance(instance)
|
||||
gce_id = self._get_gce_name_from_instance(instance)
|
||||
LOG.info('Resetting instance %s' % instance.uuid)
|
||||
operation = gceutils.reset_instance(compute, project, zone, gce_id)
|
||||
gceutils.wait_for_operation(compute, project, operation)
|
||||
|
@ -497,7 +506,7 @@ class GCEDriver(driver.ComputeDriver):
|
|||
waiting for it to shutdown
|
||||
"""
|
||||
compute, project, zone = self.gce_svc, self.gce_project, self.gce_zone
|
||||
gce_id = self._get_gce_id_from_instance(instance)
|
||||
gce_id = self._get_gce_name_from_instance(instance)
|
||||
LOG.info('Stopping instance %s' % instance.uuid)
|
||||
operation = gceutils.stop_instance(compute, project, zone, gce_id)
|
||||
gceutils.wait_for_operation(compute, project, operation)
|
||||
|
@ -506,7 +515,7 @@ class GCEDriver(driver.ComputeDriver):
|
|||
def power_on(self, context, instance, network_info, block_device_info):
|
||||
"""Power on the specified instance."""
|
||||
compute, project, zone = self.gce_svc, self.gce_project, self.gce_zone
|
||||
gce_id = self._get_gce_id_from_instance(instance)
|
||||
gce_id = self._get_gce_name_from_instance(instance)
|
||||
LOG.info('Starting instance %s' % instance.uuid)
|
||||
operation = gceutils.start_instance(compute, project, zone, gce_id)
|
||||
gceutils.wait_for_operation(compute, project, operation)
|
||||
|
@ -584,7 +593,7 @@ class GCEDriver(driver.ComputeDriver):
|
|||
compute, project, zone = self.gce_svc, self.gce_project, self.gce_zone
|
||||
LOG.info('Deleting instance %s' % instance.uuid)
|
||||
try:
|
||||
gce_id = self._get_gce_id_from_instance(instance)
|
||||
gce_id = self._get_gce_name_from_instance(instance)
|
||||
except exception.InstanceNotFound:
|
||||
LOG.error("Unable to find GCE mapping for instance %s" %
|
||||
instance.uuid)
|
||||
|
@ -606,7 +615,7 @@ class GCEDriver(driver.ComputeDriver):
|
|||
disk_bus=None, device_type=None, encryption=None):
|
||||
"""Attach the disk to the instance at mountpoint using info."""
|
||||
compute, project, zone = self.gce_svc, self.gce_project, self.gce_zone
|
||||
gce_id = self._get_gce_id_from_instance(instance)
|
||||
gce_id = self._get_gce_name_from_instance(instance)
|
||||
gce_volume = connection_info['data']
|
||||
disk_name = gce_volume['name']
|
||||
disk_link = gce_volume['selfLink']
|
||||
|
@ -620,7 +629,7 @@ class GCEDriver(driver.ComputeDriver):
|
|||
encryption=None):
|
||||
"""Detach the disk attached to the instance."""
|
||||
compute, project, zone = self.gce_svc, self.gce_project, self.gce_zone
|
||||
gce_id = self._get_gce_id_from_instance(instance)
|
||||
gce_id = self._get_gce_name_from_instance(instance)
|
||||
gce_volume = connection_info['data']
|
||||
disk_name = gce_volume['name']
|
||||
operation = gceutils.detach_disk(compute, project, zone, gce_id,
|
||||
|
@ -642,7 +651,7 @@ class GCEDriver(driver.ComputeDriver):
|
|||
|
||||
def get_info(self, instance):
|
||||
compute, project, zone = self.gce_svc, self.gce_project, self.gce_zone
|
||||
gce_id = self._get_gce_id_from_instance(instance)
|
||||
gce_id = self._get_gce_name_from_instance(instance)
|
||||
gce_instance = gceutils.get_instance(compute, project, zone, gce_id)
|
||||
power_state = GCE_STATE_MAP[gce_instance['status']]
|
||||
return hardware.InstanceInfo(state=power_state)
|
||||
|
|
Loading…
Reference in New Issue