From 3e392b07bf3c71756cbe0a334db13236b6c2ebe1 Mon Sep 17 00:00:00 2001
From: licanwei
Date: Thu, 10 Nov 2016 19:17:46 +0800
Subject: [PATCH] optimized 'find_instance()'
The function nova.servers.list() will return a huge context in the
large infrastructure. We can use nova.servers.get() instead of it.
Change-Id: If922c31e8aff27bfbc475c63083ee561ee7f1f67
---
watcher/common/nova_helper.py | 10 +---------
watcher/tests/common/test_nova_helper.py | 4 +++-
2 files changed, 4 insertions(+), 10 deletions(-)
diff --git a/watcher/common/nova_helper.py b/watcher/common/nova_helper.py
index c8aee3066..792e150fb 100644
--- a/watcher/common/nova_helper.py
+++ b/watcher/common/nova_helper.py
@@ -73,15 +73,7 @@ class NovaHelper(object):
return self.nova.availability_zones.list(detailed=True)
def find_instance(self, instance_id):
- search_opts = {'all_tenants': True}
- instances = self.nova.servers.list(detailed=True,
- search_opts=search_opts)
- instance = None
- for _instance in instances:
- if _instance.id == instance_id:
- instance = _instance
- break
- return instance
+ return self.nova.servers.get(instance_id)
def wait_for_volume_status(self, volume, status, timeout=60,
poll_interval=1):
diff --git a/watcher/tests/common/test_nova_helper.py b/watcher/tests/common/test_nova_helper.py
index 38bda9837..b7ad8d8ea 100644
--- a/watcher/tests/common/test_nova_helper.py
+++ b/watcher/tests/common/test_nova_helper.py
@@ -49,7 +49,7 @@ class TestNovaHelper(base.TestCase):
@staticmethod
def fake_nova_find_list(nova_util, find=None, list=None):
- nova_util.nova.servers.find.return_value = find
+ nova_util.nova.servers.get.return_value = find
if list is None:
nova_util.nova.servers.list.return_value = []
else:
@@ -102,6 +102,8 @@ class TestNovaHelper(base.TestCase):
)
self.assertTrue(is_success)
+ setattr(server, 'OS-EXT-SRV-ATTR:host',
+ self.source_node)
self.fake_nova_find_list(nova_util, find=server, list=None)
is_success = nova_util.live_migrate_instance(
self.instance_uuid, self.destination_node