From 62801d714b61bdea3f51c858ca0ff99ced00cb75 Mon Sep 17 00:00:00 2001 From: zhangyanxian Date: Thu, 18 May 2017 06:16:05 +0000 Subject: [PATCH] Optimizing some logic in the code when creating pod 1. What is the problem? There are so many if branches for az_name in pod.py, this will lead to confusion. 2. What is the solution to the problem? We can optimize them to keep the logic more clearly 3. What features need to be implemented to the Tricircle to realize the solution? None Closes-Bug: #1691653 Change-Id: I1f31ff685de2e516d36027388a7b0eacc1a76f5e --- tricircle/api/controllers/pod.py | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/tricircle/api/controllers/pod.py b/tricircle/api/controllers/pod.py index 409fa6db..3273997b 100644 --- a/tricircle/api/controllers/pod.py +++ b/tricircle/api/controllers/pod.py @@ -59,31 +59,30 @@ class PodsController(rest.RestController): dc_name = pod.get('dc_name', '').strip() az_name = pod.get('az_name', '').strip() _uuid = uuidutils.generate_uuid() + top_region_name = self._get_top_region(context) - if az_name == '' and region_name == '': - return Response(_('Valid region_name is required for top region'), - 422) + if az_name == '': + if region_name == '': + return Response( + _('Valid region_name is required for top region'), + 422) - if az_name != '' and region_name == '': - return Response(_('Valid region_name is required for pod'), 422) - - if pod.get('az_name') is None: - if self._get_top_region(context) != '': + if top_region_name != '': return Response(_('Top region already exists'), 409) + # to create the top region, make the pod_az_name to null value + pod_az_name = '' - # if az_name is not null, then the pod region name should not - # be same as that the top region if az_name != '': - if (self._get_top_region(context) == region_name and - region_name != ''): + if region_name == '': + return Response( + _('Valid region_name is required for pod'), 422) + # region_name != '' + # then the pod region name should not be same as the top region + if top_region_name == region_name: return Response( _('Pod region name duplicated with the top region name'), 409) - # to create the top region, make the pod_az_name to null value - if az_name == '': - pod_az_name = '' - try: with context.session.begin(): new_pod = core.create_resource(