Rewrites the ClusterView.build_instances
There is a lot of similar code in build_instances versions of normal and mgmt views for mongodb & vertica cluster strategies. Rewrote the entire code to create a method ClusterView._build_instances which could be used by both versions views(normal & mgmt) and by both datastores viz. mongodb & vertica. Change-Id: I8a63399af3d443de3e77af1202f227f71070635d
This commit is contained in:
parent
7c300679d5
commit
50ffa17837
|
@ -13,12 +13,14 @@
|
||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
from trove.common import cfg
|
||||||
from trove.common.strategies.cluster import strategy
|
from trove.common.strategies.cluster import strategy
|
||||||
from trove.common.views import create_links
|
from trove.common.views import create_links
|
||||||
from trove.instance.views import InstanceDetailView
|
from trove.instance.views import InstanceDetailView
|
||||||
from trove.openstack.common import log as logging
|
from trove.openstack.common import log as logging
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
CONF = cfg.CONF
|
||||||
|
|
||||||
|
|
||||||
class ClusterView(object):
|
class ClusterView(object):
|
||||||
|
@ -51,6 +53,39 @@ class ClusterView(object):
|
||||||
def _build_links(self):
|
def _build_links(self):
|
||||||
return create_links("clusters", self.req, self.cluster.id)
|
return create_links("clusters", self.req, self.cluster.id)
|
||||||
|
|
||||||
|
def _build_instances(self, ip_to_be_published_for=[],
|
||||||
|
instance_dict_to_be_published_for=[]):
|
||||||
|
instances = []
|
||||||
|
ip_list = []
|
||||||
|
if self.load_servers:
|
||||||
|
cluster_instances = self.cluster.instances
|
||||||
|
else:
|
||||||
|
cluster_instances = self.cluster.instances_without_server
|
||||||
|
for instance in cluster_instances:
|
||||||
|
instance_dict = {
|
||||||
|
"id": instance.id,
|
||||||
|
"name": instance.name,
|
||||||
|
"type": instance.type,
|
||||||
|
"links": create_links("instances", self.req, instance.id)
|
||||||
|
}
|
||||||
|
if instance.shard_id:
|
||||||
|
instance_dict["shard_id"] = instance.shard_id
|
||||||
|
if self.load_servers:
|
||||||
|
instance_dict["status"] = instance.status
|
||||||
|
if CONF.get(instance.datastore_version.manager).volume_support:
|
||||||
|
instance_dict["volume"] = {"size": instance.volume_size}
|
||||||
|
instance_dict["flavor"] = self._build_flavor_info(
|
||||||
|
instance.flavor_id)
|
||||||
|
instance_ips = instance.get_visible_ip_addresses()
|
||||||
|
if self.load_servers and instance_ips:
|
||||||
|
instance_dict["ip"] = instance_ips
|
||||||
|
if instance.type in ip_to_be_published_for:
|
||||||
|
ip_list.append(instance_ips[0])
|
||||||
|
if instance.type in instance_dict_to_be_published_for:
|
||||||
|
instances.append(instance_dict)
|
||||||
|
ip_list.sort()
|
||||||
|
return instances, ip_list
|
||||||
|
|
||||||
def build_instances(self):
|
def build_instances(self):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,6 @@ from trove.common import exception
|
||||||
from trove.common import remote
|
from trove.common import remote
|
||||||
from trove.common.strategies.cluster import base
|
from trove.common.strategies.cluster import base
|
||||||
from trove.common import utils
|
from trove.common import utils
|
||||||
from trove.common.views import create_links
|
|
||||||
from trove.common import wsgi
|
from trove.common import wsgi
|
||||||
from trove.datastore import models as datastore_models
|
from trove.datastore import models as datastore_models
|
||||||
from trove.extensions.mgmt.clusters.views import MgmtClusterView
|
from trove.extensions.mgmt.clusters.views import MgmtClusterView
|
||||||
|
@ -226,66 +225,13 @@ class MongoDbCluster(models.Cluster):
|
||||||
class MongoDbClusterView(ClusterView):
|
class MongoDbClusterView(ClusterView):
|
||||||
|
|
||||||
def build_instances(self):
|
def build_instances(self):
|
||||||
instances = []
|
return self._build_instances(['query_router'], ['member'])
|
||||||
ip_list = []
|
|
||||||
if self.load_servers:
|
|
||||||
cluster_instances = self.cluster.instances
|
|
||||||
else:
|
|
||||||
cluster_instances = self.cluster.instances_without_server
|
|
||||||
for instance in cluster_instances:
|
|
||||||
if self.load_servers and instance.type == 'query_router':
|
|
||||||
ip = instance.get_visible_ip_addresses()
|
|
||||||
if ip:
|
|
||||||
ip_list.append(ip[0])
|
|
||||||
if instance.type != 'member':
|
|
||||||
continue
|
|
||||||
instance_dict = {
|
|
||||||
"id": instance.id,
|
|
||||||
"name": instance.name,
|
|
||||||
"links": create_links("instances", self.req, instance.id)
|
|
||||||
}
|
|
||||||
if instance.shard_id:
|
|
||||||
instance_dict["shard_id"] = instance.shard_id
|
|
||||||
if self.load_servers:
|
|
||||||
instance_dict["status"] = instance.status
|
|
||||||
if CONF.get(instance.datastore_version.manager).volume_support:
|
|
||||||
instance_dict["volume"] = {"size": instance.volume_size}
|
|
||||||
instance_dict["flavor"] = self._build_flavor_info(
|
|
||||||
instance.flavor_id)
|
|
||||||
instances.append(instance_dict)
|
|
||||||
ip_list.sort()
|
|
||||||
return instances, ip_list
|
|
||||||
|
|
||||||
|
|
||||||
class MongoDbMgmtClusterView(MgmtClusterView):
|
class MongoDbMgmtClusterView(MgmtClusterView):
|
||||||
|
|
||||||
def build_instances(self):
|
def build_instances(self):
|
||||||
instances = []
|
return self._build_instances(['query_router'],
|
||||||
ip_list = []
|
['config_server',
|
||||||
if self.load_servers:
|
'member',
|
||||||
cluster_instances = self.cluster.instances
|
'query_router'])
|
||||||
else:
|
|
||||||
cluster_instances = self.cluster.instances_without_server
|
|
||||||
for instance in cluster_instances:
|
|
||||||
instance_dict = {
|
|
||||||
"id": instance.id,
|
|
||||||
"name": instance.name,
|
|
||||||
"type": instance.type,
|
|
||||||
"links": create_links("instances", self.req, instance.id)
|
|
||||||
}
|
|
||||||
instance_ips = instance.get_visible_ip_addresses()
|
|
||||||
if self.load_servers and instance_ips:
|
|
||||||
instance_dict["ip"] = instance_ips
|
|
||||||
if instance.type == 'query_router':
|
|
||||||
ip_list.append(instance_ips[0])
|
|
||||||
if instance.shard_id:
|
|
||||||
instance_dict["shard_id"] = instance.shard_id
|
|
||||||
if self.load_servers:
|
|
||||||
instance_dict["status"] = instance.status
|
|
||||||
if CONF.get(instance.datastore_version.manager).volume_support:
|
|
||||||
instance_dict["volume"] = {"size": instance.volume_size}
|
|
||||||
instance_dict["flavor"] = self._build_flavor_info(
|
|
||||||
instance.flavor_id)
|
|
||||||
instances.append(instance_dict)
|
|
||||||
ip_list.sort()
|
|
||||||
return instances, ip_list
|
|
||||||
|
|
|
@ -20,7 +20,6 @@ from trove.common import cfg
|
||||||
from trove.common import exception
|
from trove.common import exception
|
||||||
from trove.common import remote
|
from trove.common import remote
|
||||||
from trove.common.strategies.cluster import base
|
from trove.common.strategies.cluster import base
|
||||||
from trove.common.views import create_links
|
|
||||||
from trove.extensions.mgmt.clusters.views import MgmtClusterView
|
from trove.extensions.mgmt.clusters.views import MgmtClusterView
|
||||||
from trove.instance import models as inst_models
|
from trove.instance import models as inst_models
|
||||||
from trove.openstack.common import log as logging
|
from trove.openstack.common import log as logging
|
||||||
|
@ -130,60 +129,10 @@ class VerticaCluster(models.Cluster):
|
||||||
class VerticaClusterView(ClusterView):
|
class VerticaClusterView(ClusterView):
|
||||||
|
|
||||||
def build_instances(self):
|
def build_instances(self):
|
||||||
instances = []
|
return self._build_instances(['member'], ['member'])
|
||||||
ip_list = []
|
|
||||||
if self.load_servers:
|
|
||||||
cluster_instances = self.cluster.instances
|
|
||||||
else:
|
|
||||||
cluster_instances = self.cluster.instances_without_server
|
|
||||||
for instance in cluster_instances:
|
|
||||||
if instance.type != 'member':
|
|
||||||
continue
|
|
||||||
instance_dict = {
|
|
||||||
"id": instance.id,
|
|
||||||
"name": instance.name,
|
|
||||||
"links": create_links("instances", self.req, instance.id)
|
|
||||||
}
|
|
||||||
if self.load_servers:
|
|
||||||
instance_dict["status"] = instance.status
|
|
||||||
if CONF.get(instance.datastore_version.manager).volume_support:
|
|
||||||
instance_dict["volume"] = {"size": instance.volume_size}
|
|
||||||
instance_dict["flavor"] = self._build_flavor_info(
|
|
||||||
instance.flavor_id)
|
|
||||||
instance_ips = instance.get_visible_ip_addresses()
|
|
||||||
if instance_ips:
|
|
||||||
instance_dict["ip"] = instance_ips
|
|
||||||
ip_list.append(instance_ips[0])
|
|
||||||
instances.append(instance_dict)
|
|
||||||
ip_list.sort()
|
|
||||||
return instances, ip_list
|
|
||||||
|
|
||||||
|
|
||||||
class VerticaMgmtClusterView(MgmtClusterView):
|
class VerticaMgmtClusterView(MgmtClusterView):
|
||||||
|
|
||||||
def build_instances(self):
|
def build_instances(self):
|
||||||
instances = []
|
return self._build_instances(['member'], ['member'])
|
||||||
ip_list = []
|
|
||||||
if self.load_servers:
|
|
||||||
cluster_instances = self.cluster.instances
|
|
||||||
else:
|
|
||||||
cluster_instances = self.cluster.instances_without_server
|
|
||||||
for instance in cluster_instances:
|
|
||||||
instance_dict = {
|
|
||||||
"id": instance.id,
|
|
||||||
"name": instance.name,
|
|
||||||
"type": instance.type,
|
|
||||||
"links": create_links("instances", self.req, instance.id)
|
|
||||||
}
|
|
||||||
instance_ips = instance.get_visible_ip_addresses()
|
|
||||||
if self.load_servers and instance_ips:
|
|
||||||
instance_dict["ip"] = instance_ips
|
|
||||||
if self.load_servers:
|
|
||||||
instance_dict["status"] = instance.status
|
|
||||||
if CONF.get(instance.datastore_version.manager).volume_support:
|
|
||||||
instance_dict["volume"] = {"size": instance.volume_size}
|
|
||||||
instance_dict["flavor"] = self._build_flavor_info(
|
|
||||||
instance.flavor_id)
|
|
||||||
instances.append(instance_dict)
|
|
||||||
ip_list.sort()
|
|
||||||
return instances, ip_list
|
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
from mock import Mock
|
from mock import Mock
|
||||||
|
from mock import MagicMock
|
||||||
from mock import patch
|
from mock import patch
|
||||||
from testtools import TestCase
|
from testtools import TestCase
|
||||||
from trove.cluster.views import ClusterInstanceDetailView
|
from trove.cluster.views import ClusterInstanceDetailView
|
||||||
|
@ -23,7 +24,6 @@ from trove.cluster.views import load_view
|
||||||
from trove.common import cfg
|
from trove.common import cfg
|
||||||
from trove.common.strategies.cluster.experimental.mongodb.api import (
|
from trove.common.strategies.cluster.experimental.mongodb.api import (
|
||||||
MongoDbClusterView)
|
MongoDbClusterView)
|
||||||
|
|
||||||
CONF = cfg.CONF
|
CONF = cfg.CONF
|
||||||
|
|
||||||
|
|
||||||
|
@ -73,6 +73,40 @@ class ClusterViewTest(TestCase):
|
||||||
view = load_view(cluster, Mock())
|
view = load_view(cluster, Mock())
|
||||||
self.assertTrue(isinstance(view, MongoDbClusterView))
|
self.assertTrue(isinstance(view, MongoDbClusterView))
|
||||||
|
|
||||||
|
def test__build_instances(self, *args):
|
||||||
|
cluster = Mock()
|
||||||
|
cluster.instances = []
|
||||||
|
cluster.instances.append(Mock())
|
||||||
|
cluster.instances.append(Mock())
|
||||||
|
cluster.instances.append(Mock())
|
||||||
|
cluster.instances[0].type = 'configsvr'
|
||||||
|
cluster.instances[0].get_visible_ip_addresses = lambda: ['1.2.3.4']
|
||||||
|
cluster.instances[0].datastore_version.manager = 'mongodb'
|
||||||
|
cluster.instances[1].type = 'query_router'
|
||||||
|
cluster.instances[1].get_visible_ip_addresses = lambda: ['1.2.3.4']
|
||||||
|
cluster.instances[1].datastore_version.manager = 'mongodb'
|
||||||
|
cluster.instances[2].type = 'member'
|
||||||
|
cluster.instances[2].get_visible_ip_addresses = lambda: ['1.2.3.4']
|
||||||
|
cluster.instances[2].datastore_version.manager = 'mongodb'
|
||||||
|
|
||||||
|
def test_case(ip_to_be_published_for,
|
||||||
|
instance_dict_to_be_published_for,
|
||||||
|
number_of_ip_published,
|
||||||
|
number_of_instance_dict_published):
|
||||||
|
view = ClusterView(cluster, MagicMock())
|
||||||
|
instances, ip_list = view._build_instances(
|
||||||
|
ip_to_be_published_for, instance_dict_to_be_published_for)
|
||||||
|
|
||||||
|
self.assertEqual(len(ip_list), number_of_ip_published)
|
||||||
|
self.assertEqual(len(instances), number_of_instance_dict_published)
|
||||||
|
|
||||||
|
test_case([], [], 0, 0)
|
||||||
|
test_case(['abc'], ['def'], 0, 0)
|
||||||
|
test_case(['query_router'], ['member'], 1, 1)
|
||||||
|
test_case(['query_router'], ['query_router', 'configsvr', 'member'],
|
||||||
|
1, 3)
|
||||||
|
test_case(['query_router', 'member'], ['member'], 2, 1)
|
||||||
|
|
||||||
|
|
||||||
class ClusterInstanceDetailViewTest(TestCase):
|
class ClusterInstanceDetailViewTest(TestCase):
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue