Display trove instance hostname if available

Trove can now be integrated with designate, the instance will
have a hostname instead of ip. If the integration is not enabled
the ip would be available in the instance info.

Change-Id: I7173f4437e78dd45f8f99d5c9dd80ab86869e943
Closes-Bug: #1262777
This commit is contained in:
lin-hua-cheng 2014-02-07 22:57:12 -08:00
parent 0c4cf9aacf
commit 6b6cbe932d
5 changed files with 61 additions and 20 deletions

View File

@ -126,13 +126,6 @@ class UpdateRow(tables.Row):
return instance
def get_ips(instance):
if hasattr(instance, "ip"):
if len(instance.ip):
return instance.ip[0]
return _("Not Assigned")
def get_size(instance):
if hasattr(instance, "full_flavor"):
size_string = _("%(name)s | %(RAM)s RAM")
@ -161,7 +154,7 @@ class InstancesTable(tables.DataTable):
name = tables.Column("name",
link=("horizon:project:databases:detail"),
verbose_name=_("Database Name"))
ip = tables.Column(get_ips, verbose_name=_("IP Address"))
host = tables.Column("host", verbose_name=_("Host"))
size = tables.Column(get_size,
verbose_name=_("Size"),
attrs={'data-type': 'size'})

View File

@ -21,14 +21,14 @@
<h4>{% trans "Connection Info" %}</h4>
<hr class="header_rule">
<dl>
{% with instance.ip.0 as ipaddress %}
<dt>{% trans "Instance IP Address" %}</dt>
<dd>{{ ipaddress }}</dd>
{% with instance.host as host %}
<dt>{% trans "Host" %}</dt>
<dd>{{ host }}</dd>
<dt>{% trans "Database Port" %}</dt>
<dd>3306</dd> {# TODO: This should be a config #}
<dt>{% trans "Connection Examples" %}</dt>
<dd>mysql -h {{ ipaddress }} -u USERNAME -p</dd>
<dd>mysql://USERNAME:PASSWORD@{{ ipaddress }}:3306/DATABASE</dd>
<dd>mysql -h {{ host }} -u USERNAME -p</dd>
<dd>mysql://USERNAME:PASSWORD@{{ host }}:3306/DATABASE</dd>
{% endwith %}
</dl>
</div>

View File

@ -46,6 +46,9 @@ class DatabaseTests(test.TestCase):
self.mox.ReplayAll()
res = self.client.get(INDEX_URL)
self.assertTemplateUsed(res, 'project/databases/index.html')
# Check the Host column displaying ip or hostname
self.assertContains(res, '10.0.0.3')
self.assertContains(res, 'trove.instance-2.com')
@test.create_stubs(
{api.trove: ('instance_list', 'flavor_list')})
@ -79,7 +82,9 @@ class DatabaseTests(test.TestCase):
{api.trove: ('instance_list', 'flavor_list')})
def test_index_pagination(self):
# Mock database instances
databases = common.Paginated(self.databases.list(),
databases = self.databases.list()
last_record = databases[-1]
databases = common.Paginated(databases,
next_marker="foo")
api.trove.instance_list(IsA(http.HttpRequest), marker=None)\
.AndReturn(databases)
@ -91,7 +96,7 @@ class DatabaseTests(test.TestCase):
res = self.client.get(INDEX_URL)
self.assertTemplateUsed(res, 'project/databases/index.html')
self.assertContains(
res, 'marker=6ddc36d9-73db-4e23-b52e-368937d72719')
res, 'marker=' + last_record.id)
@test.create_stubs(
{api.trove: ('instance_list', 'flavor_list')})
@ -182,12 +187,24 @@ class DatabaseTests(test.TestCase):
@test.create_stubs(
{api.trove: ('instance_get', 'flavor_get',)})
def test_details(self):
def _test_details(self, database, with_designate=False):
api.trove.instance_get(IsA(http.HttpRequest), IsA(unicode))\
.AndReturn(self.databases.first())
.AndReturn(database)
api.trove.flavor_get(IsA(http.HttpRequest), IsA(str))\
.AndReturn(self.flavors.first())
self.mox.ReplayAll()
res = self.client.get(DETAILS_URL)
self.assertTemplateUsed(res, 'project/databases/detail.html')
if with_designate:
self.assertContains(res, database.hostname)
else:
self.assertContains(res, database.ip[0])
def test_details_with_ip(self):
database = self.databases.first()
self._test_details(database, with_designate=False)
def test_details_with_hostname(self):
database = self.databases.list()[1]
self._test_details(database, with_designate=True)

View File

@ -38,6 +38,14 @@ from openstack_dashboard.dashboards.project.databases import workflows
LOG = logging.getLogger(__name__)
def get_host(instance):
if hasattr(instance, "hostname"):
return instance.hostname
elif hasattr(instance, "ip") and instance.ip:
return instance.ip[0]
return _("Not Assigned")
class IndexView(horizon_tables.DataTableView):
table_class = tables.InstancesTable
template_name = 'project/databases/index.html'
@ -59,6 +67,7 @@ class IndexView(horizon_tables.DataTableView):
flavor = self.get_flavors().get(instance.flavor["id"])
if flavor is not None:
instance.full_flavor = flavor
instance.host = get_host(instance)
return instance
def get_data(self):
@ -103,6 +112,7 @@ class DetailView(horizon_tabs.TabbedTableView):
LOG.info("Obtaining instance for detailed view ")
instance_id = self.kwargs['instance_id']
instance = api.trove.instance_get(self.request, instance_id)
instance.host = get_host(instance)
except Exception:
redirect = reverse('horizon:project:databases:index')
msg = _('Unable to retrieve details '

View File

@ -20,7 +20,7 @@ from troveclient.v1 import instances
from openstack_dashboard.test.test_data import utils
DATABASE_DATA = {
DATABASE_DATA_ONE = {
"status": "ACTIVE",
"updated": "2013-08-12T22:00:09",
"name": "Test Database",
@ -40,6 +40,23 @@ DATABASE_DATA = {
"id": "6ddc36d9-73db-4e23-b52e-368937d72719"
}
DATABASE_DATA_TWO = {
"status": "ACTIVE",
"updated": "2013-08-12T22:00:09",
"name": "Test Database With DNS",
"links": [],
"created": "2013-08-12T22:00:03",
"hostname": "trove.instance-2.com",
"volume": {
"used": 0.13,
"size": 1
},
"flavor": {
"id": "1",
"links": []
},
"id": "4d7b3f57-44f5-41d2-8e86-36b88cad572a"
}
BACKUP_ONE = {
"instance_id": "6ddc36d9-73db-4e23-b52e-368937d72719",
@ -68,12 +85,16 @@ BACKUP_TWO = {
def data(TEST):
database = instances.Instance(instances.Instances(None), DATABASE_DATA)
database1 = instances.Instance(instances.Instances(None),
DATABASE_DATA_ONE)
database2 = instances.Instance(instances.Instances(None),
DATABASE_DATA_TWO)
bkup1 = backups.Backup(backups.Backups(None), BACKUP_ONE)
bkup2 = backups.Backup(backups.Backups(None), BACKUP_TWO)
TEST.databases = utils.TestDataContainer()
TEST.database_backups = utils.TestDataContainer()
TEST.databases.add(database)
TEST.databases.add(database1)
TEST.databases.add(database2)
TEST.database_backups.add(bkup1)
TEST.database_backups.add(bkup2)