From a0f7273b867281ea06de2bc48af238785437efea Mon Sep 17 00:00:00 2001
From: Dirk Mueller <dirk@dmllr.de>
Date: Tue, 1 Jul 2014 00:24:15 +0200
Subject: [PATCH] Fix host listing in live migration

The live migration selector needs to offer a list
of hosts, not a list of hypervisors.

Change-Id: I73c1175a5f87b0f91e36083f05467c26feca5d51
Closes-Bug: #1335999
---
 .../dashboards/admin/instances/forms.py            |  7 ++--
 .../dashboards/admin/instances/tests.py            | 40 +++++++++++-----------
 .../dashboards/admin/instances/views.py            |  4 +--
 openstack_dashboard/test/test_data/nova_data.py    | 10 ++++++
 4 files changed, 36 insertions(+), 25 deletions(-)

diff --git a/openstack_dashboard/dashboards/admin/instances/forms.py b/openstack_dashboard/dashboards/admin/instances/forms.py
index b13dce6..86da601 100644
--- a/openstack_dashboard/dashboards/admin/instances/forms.py
+++ b/openstack_dashboard/dashboards/admin/instances/forms.py
@@ -50,10 +50,11 @@ class LiveMigrateForm(forms.SelfHandlingForm):
     def populate_host_choices(self, request, initial):
         hosts = initial.get('hosts')
         current_host = initial.get('current_host')
-        host_list = [(host.hypervisor_hostname,
-                      host.hypervisor_hostname)
+        host_list = [(host.host_name,
+                      host.host_name)
                      for host in hosts
-                     if host.service['host'] != current_host]
+                     if host.service.startswith('compute') and
+                         host.host_name != current_host]
         if host_list:
             host_list.insert(0, ("", _("Select a new host")))
         else:
diff --git a/openstack_dashboard/dashboards/admin/instances/tests.py b/openstack_dashboard/dashboards/admin/instances/tests.py
index abb98af..77c5042 100644
--- a/openstack_dashboard/dashboards/admin/instances/tests.py
+++ b/openstack_dashboard/dashboards/admin/instances/tests.py
@@ -221,14 +221,14 @@ class InstanceViewTest(test.BaseAdminViewTests):
         self.assertContains(res, "instances__revert")
         self.assertNotContains(res, "instances__migrate")
 
-    @test.create_stubs({api.nova: ('hypervisor_list',
+    @test.create_stubs({api.nova: ('host_list',
                                    'server_get',)})
     def test_instance_live_migrate_get(self):
         server = self.servers.first()
         api.nova.server_get(IsA(http.HttpRequest), server.id) \
                 .AndReturn(server)
-        api.nova.hypervisor_list(IsA(http.HttpRequest)) \
-                .AndReturn(self.hypervisors.list())
+        api.nova.host_list(IsA(http.HttpRequest)) \
+                .AndReturn(self.hosts.list())
 
         self.mox.ReplayAll()
 
@@ -252,13 +252,13 @@ class InstanceViewTest(test.BaseAdminViewTests):
 
         self.assertRedirectsNoFollow(res, INDEX_URL)
 
-    @test.create_stubs({api.nova: ('hypervisor_list',
+    @test.create_stubs({api.nova: ('host_list',
                                    'server_get',)})
     def test_instance_live_migrate_list_hypervisor_get_exception(self):
         server = self.servers.first()
         api.nova.server_get(IsA(http.HttpRequest), server.id) \
                 .AndReturn(server)
-        api.nova.hypervisor_list(IsA(http.HttpRequest)) \
+        api.nova.host_list(IsA(http.HttpRequest)) \
                 .AndRaise(self.exceptions.nova)
 
         self.mox.ReplayAll()
@@ -268,14 +268,14 @@ class InstanceViewTest(test.BaseAdminViewTests):
 
         self.assertRedirectsNoFollow(res, INDEX_URL)
 
-    @test.create_stubs({api.nova: ('hypervisor_list',
+    @test.create_stubs({api.nova: ('host_list',
                                    'server_get',)})
     def test_instance_live_migrate_list_hypervisor_without_current(self):
         server = self.servers.first()
         api.nova.server_get(IsA(http.HttpRequest), server.id) \
                 .AndReturn(server)
-        api.nova.hypervisor_list(IsA(http.HttpRequest)) \
-                .AndReturn(self.hypervisors.list())
+        api.nova.host_list(IsA(http.HttpRequest)) \
+                .AndReturn(self.hosts.list())
 
         self.mox.ReplayAll()
 
@@ -283,24 +283,25 @@ class InstanceViewTest(test.BaseAdminViewTests):
                       args=[server.id])
         res = self.client.get(url)
         self.assertNotContains(
-            res, "<option value=\"devstack003\">devstack003</option>")
+            res, "<option value=\"instance-host\">instance-host</option>")
         self.assertContains(
             res, "<option value=\"devstack001\">devstack001</option>")
-        self.assertContains(
+        self.assertNotContains(
             res, "<option value=\"devstack002\">devstack002</option>")
+        self.assertContains(
+            res, "<option value=\"devstack003\">devstack003</option>")
 
-    @test.create_stubs({api.nova: ('hypervisor_list',
+    @test.create_stubs({api.nova: ('host_list',
                                    'server_get',
                                    'server_live_migrate',)})
     def test_instance_live_migrate_post(self):
         server = self.servers.first()
-        hypervisor = self.hypervisors.first()
-        host = hypervisor.hypervisor_hostname
+        host = self.hosts.first().host_name
 
         api.nova.server_get(IsA(http.HttpRequest), server.id) \
                 .AndReturn(server)
-        api.nova.hypervisor_list(IsA(http.HttpRequest)) \
-                .AndReturn(self.hypervisors.list())
+        api.nova.host_list(IsA(http.HttpRequest)) \
+                .AndReturn(self.hosts.list())
         api.nova.server_live_migrate(IsA(http.HttpRequest), server.id, host,
                                      block_migration=False,
                                      disk_over_commit=False) \
@@ -314,18 +315,17 @@ class InstanceViewTest(test.BaseAdminViewTests):
         self.assertNoFormErrors(res)
         self.assertRedirectsNoFollow(res, INDEX_URL)
 
-    @test.create_stubs({api.nova: ('hypervisor_list',
+    @test.create_stubs({api.nova: ('host_list',
                                    'server_get',
                                    'server_live_migrate',)})
     def test_instance_live_migrate_post_api_exception(self):
         server = self.servers.first()
-        hypervisor = self.hypervisors.first()
-        host = hypervisor.hypervisor_hostname
+        host = self.hosts.first().host_name
 
         api.nova.server_get(IsA(http.HttpRequest), server.id) \
                 .AndReturn(server)
-        api.nova.hypervisor_list(IsA(http.HttpRequest)) \
-                .AndReturn(self.hypervisors.list())
+        api.nova.host_list(IsA(http.HttpRequest)) \
+                .AndReturn(self.hosts.list())
         api.nova.server_live_migrate(IsA(http.HttpRequest), server.id, host,
                                      block_migration=False,
                                      disk_over_commit=False) \
diff --git a/openstack_dashboard/dashboards/admin/instances/views.py b/openstack_dashboard/dashboards/admin/instances/views.py
index 54f41ce..4d2358d 100644
--- a/openstack_dashboard/dashboards/admin/instances/views.py
+++ b/openstack_dashboard/dashboards/admin/instances/views.py
@@ -143,10 +143,10 @@ class LiveMigrateView(forms.ModalFormView):
     @memoized.memoized_method
     def get_hosts(self, *args, **kwargs):
         try:
-            return api.nova.hypervisor_list(self.request)
+            return api.nova.host_list(self.request)
         except Exception:
             redirect = reverse("horizon:admin:instances:index")
-            msg = _('Unable to retrieve hypervisor information.')
+            msg = _('Unable to retrieve host information.')
             exceptions.handle(self.request, msg, redirect=redirect)
 
     @memoized.memoized_method
diff --git a/openstack_dashboard/test/test_data/nova_data.py b/openstack_dashboard/test/test_data/nova_data.py
index 56cd698..0f54f33 100644
--- a/openstack_dashboard/test/test_data/nova_data.py
+++ b/openstack_dashboard/test/test_data/nova_data.py
@@ -730,6 +730,16 @@ def data(TEST):
             "zone": "testing"
         }
     )
+
+    host4 = hosts.Host(hosts.HostManager(None),
+        {
+            "host_name": "instance-host",
+            "service": "compute",
+            "zone": "testing"
+        }
+    )
+
     TEST.hosts.add(host1)
     TEST.hosts.add(host2)
     TEST.hosts.add(host3)
+    TEST.hosts.add(host4)
-- 
1.9.0

