Configurable network labels used for showing IPs in instance details

The ip shown in the instance detail was hard coded to be obtained from
a nova netwok label 'private' or 'usenet'. With this change, the labels
is configurable based on a regex pattern.

Fixes bug 1206303

Change-Id: I73d896e6b85bdebe342a777219b9b60d6954f8b6
This commit is contained in:
Steve Leon 2013-07-31 09:12:32 -07:00
parent 4307d36e2d
commit 7e57dbddcd
5 changed files with 74 additions and 11 deletions

View File

@ -47,6 +47,7 @@ swift_url = http://localhost:8080/v1/AUTH_
# Config option for showing the IP address that nova doles out
add_addresses = True
network_label_regex = ^private$
# Config options for enabling volume service
trove_volume_support = True

View File

@ -68,7 +68,8 @@ nova_service_type = compute
nova_service_name = Compute Service
# Config option for showing the IP address that nova doles out
add_addresses = False
add_addresses = True
network_label_regex = ^private$
# Config options for enabling volume service
trove_volume_support = True

View File

@ -172,7 +172,8 @@ common_opts = [
cfg.StrOpt('nova_proxy_admin_pass', default='',
help="Admin password used to connect to Nova"),
cfg.StrOpt('nova_proxy_admin_tenant_name', default='',
help="Admin tenant used to connect to Nova")
help="Admin tenant used to connect to Nova"),
cfg.StrOpt('network_label_regex', default='^private$'),
]

View File

@ -15,9 +15,9 @@
# License for the specific language governing permissions and limitations
# under the License.
import re
from trove.openstack.common import log as logging
from trove.common import cfg
from trove.common import utils
from trove.common.views import create_links
from trove.instance import models
@ -27,14 +27,14 @@ CONF = cfg.CONF
def get_ip_address(addresses):
if (addresses is not None and
addresses.get('private') is not None and
len(addresses['private']) > 0):
return [addr.get('addr') for addr in addresses['private']]
if (addresses is not None and
addresses.get('usernet') is not None and
len(addresses['usernet']) > 0):
return [addr.get('addr') for addr in addresses['usernet']]
if addresses is None:
return None
IPs = []
for label in addresses:
if (re.search(CONF.network_label_regex, label) and
len(addresses[label]) > 0):
IPs.extend([addr.get('addr') for addr in addresses[label]])
return IPs
class InstanceView(object):

View File

@ -0,0 +1,60 @@
# Copyright 2013 Hewlett-Packard Development Company, L.P.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
from testtools import TestCase
from trove.common import cfg
from trove.instance.views import get_ip_address
CONF = cfg.CONF
class InstanceViewsTest(TestCase):
def setUp(self):
super(InstanceViewsTest, self).setUp()
self.addresses = {"private": [{"addr": "123.123.123.123"}],
"internal": [{"addr": "10.123.123.123"}],
"public": [{"addr": "15.123.123.123"}]}
self.orig_conf = CONF.network_label_regex
def tearDown(self):
super(InstanceViewsTest, self).tearDown()
CONF.network_label_regex = self.orig_conf
def test_one_network_label_exact(self):
CONF.network_label_regex = '^internal$'
ip = get_ip_address(self.addresses)
self.assertEqual(['10.123.123.123'], ip)
def test_one_network_label(self):
CONF.network_label_regex = 'public'
ip = get_ip_address(self.addresses)
self.assertEqual(['15.123.123.123'], ip)
def test_two_network_labels(self):
CONF.network_label_regex = '^(private|public)$'
ip = get_ip_address(self.addresses)
self.assertTrue(len(ip) == 2)
self.assertTrue('123.123.123.123' in ip)
self.assertTrue('15.123.123.123' in ip)
def test_all_network_labels(self):
CONF.network_label_regex = '.*'
ip = get_ip_address(self.addresses)
self.assertTrue(len(ip) == 3)
self.assertTrue('10.123.123.123' in ip)
self.assertTrue('123.123.123.123' in ip)
self.assertTrue('15.123.123.123' in ip)