Merge "Add retrieve_bay_uuid in conductor_utils"

This commit is contained in:
Jenkins 2015-12-11 04:45:45 +00:00 committed by Gerrit Code Review
commit 8072d9c590
3 changed files with 43 additions and 60 deletions

View File

@ -72,11 +72,7 @@ class Handler(object):
# Since bay identifier is specified verify whether its a UUID
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
if not utils.is_uuid_like(bay_ident):
bay = objects.Bay.get_by_name(context, bay_ident)
bay_ident = bay.uuid
bay_uuid = bay_ident
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_service(context, bay_uuid)
if utils.is_uuid_like(service_ident):
service = objects.Service.get_by_uuid(context,
@ -127,11 +123,7 @@ class Handler(object):
# Since bay identifier is specified verify whether its a UUID
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
if not utils.is_uuid_like(bay_ident):
bay = objects.Bay.get_by_name(context, bay_ident)
bay_ident = bay.uuid
bay_uuid = bay_ident
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_service(context, bay_uuid)
if utils.is_uuid_like(service_ident):
service = objects.Service.get_by_uuid(context, service_ident,
@ -155,11 +147,7 @@ class Handler(object):
# Since bay identifier is specified verify whether its a UUID
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
if not utils.is_uuid_like(bay_ident):
bay = objects.Bay.get_by_name(context, bay_ident)
bay_ident = bay.uuid
bay_uuid = bay_ident
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_service(context, bay_uuid)
if utils.is_uuid_like(service_ident):
service = objects.Service.get_by_uuid(context, service_ident,
@ -174,11 +162,7 @@ class Handler(object):
# Since bay identifier is specified verify whether its a UUID
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
if not utils.is_uuid_like(bay_ident):
bay = objects.Bay.get_by_name(context, bay_ident)
bay_ident = bay.uuid
bay_uuid = bay_ident
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_service(context, bay_uuid)
try:
resp = self.k8s_api.list_namespaced_service(namespace='default')
@ -247,11 +231,7 @@ class Handler(object):
# Since bay identifier is specified verify whether its a UUID
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
if not utils.is_uuid_like(bay_ident):
bay = objects.Bay.get_by_name(context, bay_ident)
bay_ident = bay.uuid
bay_uuid = bay_ident
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_pod(context, bay_uuid)
if utils.is_uuid_like(pod_ident):
pod = objects.Pod.get_by_uuid(context, pod_ident,
@ -290,11 +270,7 @@ class Handler(object):
# Since bay identifier is specified verify whether its a UUID
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
if not utils.is_uuid_like(bay_ident):
bay = objects.Bay.get_by_name(context, bay_ident)
bay_ident = bay.uuid
bay_uuid = bay_ident
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_pod(context, bay_uuid)
if utils.is_uuid_like(pod_ident):
pod = objects.Pod.get_by_uuid(context, pod_ident,
@ -317,11 +293,7 @@ class Handler(object):
# Since bay identifier is specified verify whether its a UUID
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
if not utils.is_uuid_like(bay_ident):
bay = objects.Bay.get_by_name(context, bay_ident)
bay_ident = bay.uuid
bay_uuid = bay_ident
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_pod(context, bay_uuid)
if utils.is_uuid_like(pod_ident):
pod = objects.Pod.get_by_uuid(context, pod_ident,
@ -336,11 +308,7 @@ class Handler(object):
# Since bay identifier is specified verify whether its a UUID
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
if not utils.is_uuid_like(bay_ident):
bay = objects.Bay.get_by_name(context, bay_ident)
bay_ident = bay.uuid
bay_uuid = bay_ident
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_pod(context, bay_uuid)
try:
resp = self.k8s_api.list_namespaced_pod(namespace='default')
@ -399,11 +367,7 @@ class Handler(object):
# Since bay identifier is specified verify whether its a UUID
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
if not utils.is_uuid_like(bay_ident):
bay = objects.Bay.get_by_name(context, bay_ident)
bay_ident = bay.uuid
bay_uuid = bay_ident
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_rc(context, bay_uuid)
if utils.is_uuid_like(rc_ident):
rc = objects.ReplicationController.get_by_uuid(context, rc_ident,
@ -440,11 +404,7 @@ class Handler(object):
# Since bay identifier is specified verify whether its a UUID
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
if not utils.is_uuid_like(bay_ident):
bay = objects.Bay.get_by_name(context, bay_ident)
bay_ident = bay.uuid
bay_uuid = bay_ident
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_rc(context, bay_uuid)
if utils.is_uuid_like(rc_ident):
rc = objects.ReplicationController.get_by_uuid(context, rc_ident,
@ -470,11 +430,7 @@ class Handler(object):
# Since bay identifier is specified verify whether its a UUID
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
if not utils.is_uuid_like(bay_ident):
bay = objects.Bay.get_by_name(context, bay_ident)
bay_ident = bay.uuid
bay_uuid = bay_ident
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_rc(context, bay_uuid)
if utils.is_uuid_like(rc_ident):
rc = objects.ReplicationController.get_by_uuid(context, rc_ident,
@ -491,11 +447,7 @@ class Handler(object):
# Since bay identifier is specified verify whether its a UUID
# or Name. If name is specified as bay identifier need to extract
# the bay uuid since its needed to get the k8s_api object.
if not utils.is_uuid_like(bay_ident):
bay = objects.Bay.get_by_name(context, bay_ident)
bay_ident = bay.uuid
bay_uuid = bay_ident
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
self.k8s_api = k8s.create_k8s_api_rc(context, bay_uuid)
try:
resp = self.k8s_api.list_namespaced_replication_controller(

View File

@ -13,6 +13,7 @@
# limitations under the License.
from magnum.common import clients
from magnum.common import utils
from magnum.objects import bay
from magnum.objects import baymodel
@ -25,6 +26,14 @@ def retrieve_baymodel(context, bay):
return baymodel.BayModel.get_by_uuid(context, bay.baymodel_id)
def retrieve_bay_uuid(context, bay_ident):
if not utils.is_uuid_like(bay_ident):
bay_obj = bay.Bay.get_by_name(context, bay_ident)
return bay_obj.uuid
else:
return bay_ident
def object_has_stack(context, bay_uuid):
osc = clients.OpenStackClients(context)
obj = retrieve_bay(context, bay_uuid)

View File

@ -69,3 +69,25 @@ class TestConductorUtils(base.TestCase):
mock_baymodel_get_by_uuid.assert_called_once_with(
expected_context,
expected_baymodel_uuid)
@patch('magnum.common.utils.is_uuid_like')
@patch('magnum.objects.Bay.get_by_name')
def test_retrieve_bay_uuid_from_name(self, mock_bay_get_by_name,
mock_uuid_like):
bay = objects.Bay(uuid=1)
mock_uuid_like.return_value = False
mock_bay_get_by_name.return_value = bay
bay_uuid = utils.retrieve_bay_uuid('context', 'fake_name')
self.assertEqual('1', bay_uuid)
mock_uuid_like.assert_called_once_with('fake_name')
mock_bay_get_by_name.assert_called_once_with('context', 'fake_name')
@patch('magnum.common.utils.is_uuid_like')
@patch('magnum.objects.Bay.get_by_name')
def test_retrieve_bay_uuid_from_uuid(self, mock_bay_get_by_name,
mock_uuid_like):
bay_uuid = utils.retrieve_bay_uuid('context', '1')
self.assertEqual('1', bay_uuid)
mock_uuid_like.return_value = True
mock_bay_get_by_name.assert_not_called()