Skip to content

Commit

Permalink
Merge "config-exception: Handle dbe_read raising NoIdError"
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuul authored and opencontrail-ci-admin committed Jun 25, 2015
2 parents d814f35 + c874418 commit 0781f26
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 88 deletions.
2 changes: 1 addition & 1 deletion src/config/api-server/tests/test_crud_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ def create_vns():
class TestVncCfgApiServerRequests(test_case.ApiServerTestCase):
""" Tests to verify the max_requests config parameter of api-server."""
def __init__(self, *args, **kwargs):
super(TestVncCfgApiServerConnection, self).__init__(*args, **kwargs)
super(TestVncCfgApiServerRequests, self).__init__(*args, **kwargs)
self._config_knobs.extend([('DEFAULTS', 'max_requests', 10),])


Expand Down
2 changes: 1 addition & 1 deletion src/config/api-server/tests/test_ip_alloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ def __call__(self, *args, **kwargs):
raise Exception(
"Instance IP was persisted more than once")

if args[0].startswith('/api-server/subnets'):
if args[1].startswith('/api-server/subnets'):
self._invoked += 1
return self._orig_method(args, kwargs)
# end SpyCreateNode
Expand Down
2 changes: 1 addition & 1 deletion src/config/api-server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def parse_args(args_str):
'logging_conf': '',
'logger_class': None,
'multi_tenancy': True,
'disc_server_ip': None,
'disc_server_ip': '127.0.0.1',
'disc_server_port': '5998',
'zk_server_ip': '127.0.0.1:2181',
'worker_id': '0',
Expand Down
60 changes: 47 additions & 13 deletions src/config/api-server/vnc_addr_mgmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pprint import pformat
from cfgm_common import jsonutils as json
import cfgm_common.exceptions
import cfgm_common.utils
try:
#python2.7
from collections import OrderedDict
Expand Down Expand Up @@ -389,6 +390,7 @@ def _get_subnet_dicts(self, vn_fq_name, vn_dict=None):
obj_type='virtual-network',
obj_ids={'uuid': vn_uuid},
obj_fields=['network_ipam_refs'])

if not ok:
raise VncError(result)

Expand Down Expand Up @@ -760,11 +762,18 @@ def net_check_subnet_delete(self, db_vn_dict, req_vn_dict):
'instance-ip', {'uuid': ref['uuid']})
except cfgm_common.exceptions.NoIdError:
continue

if not ok:
self.config_log(
"Error in subnet delete instance-ip check: %s" %(result),
level=SandeshLevel.SYS_ERR)
return False, result

inst_ip = result.get('instance_ip_address')
if not inst_ip:
self.config_log(
"Error in subnet delete ip null: %s" %(ref['uuid']),
level=SandeshLevel.SYS_ERR)
continue

inst_ip = result.get('instance_ip_address', None)
if not all_matching_cidrs(inst_ip, requested_subnets):
return False,\
"Cannot Delete IP Block, IP(%s) is in use"\
Expand All @@ -777,9 +786,12 @@ def net_check_subnet_delete(self, db_vn_dict, req_vn_dict):
'floating-ip-pool', {'uuid': ref['uuid']})
except cfgm_common.exceptions.NoIdError:
continue

if not ok:
continue
self.config_log(
"Error in subnet delete floating-ip-pool check: %s"
%(result),
level=SandeshLevel.SYS_ERR)
return False, result

floating_ips = result.get('floating_ips', [])
for floating_ip in floating_ips:
Expand All @@ -790,11 +802,20 @@ def net_check_subnet_delete(self, db_vn_dict, req_vn_dict):
'floating-ip', {'uuid': floating_ip['uuid']})
except cfgm_common.exceptions.NoIdError:
continue

if not ok:
if not read_ok:
self.config_log(
"Error in subnet delete floating-ip check: %s"
%(read_result),
level=SandeshLevel.SYS_ERR)
return False, result

fip_addr = read_result.get('floating_ip_address')
if not fip_addr:
self.config_log(
"Error in subnet delete fip null: %s"
%(floating_ip['uuid']),
level=SandeshLevel.SYS_ERR)
continue

fip_addr = read_result.get('floating_ip_address', None)
if not all_matching_cidrs(fip_addr, requested_subnets):
return False,\
"Cannot Delete IP Block, Floating IP(%s) is in use"\
Expand Down Expand Up @@ -878,7 +899,11 @@ def ip_alloc_req(self, vn_fq_name, vn_dict=None, sub=None, asked_ip_addr=None,

def ip_alloc_notify(self, ip_addr, vn_fq_name):
vn_fq_name_str = ':'.join(vn_fq_name)
subnet_dicts = self._get_subnet_dicts(vn_fq_name)
try:
subnet_dicts = self._get_subnet_dicts(vn_fq_name)
except cfgm_common.exceptions.NoIdError:
return

for subnet_name in subnet_dicts:
# create subnet_obj internally if it was created by some other
# api-server before
Expand Down Expand Up @@ -1004,14 +1029,23 @@ def ip_count(self, obj_dict, subnet=None):
try:
(ok, result) = db_conn.dbe_read(
'instance-ip', {'uuid': uuid})
inst_ip = result.get('instance_ip_address')
if not inst_ip:
self.config_log(
"Error in ip_count, ip null: %s" %(uuid),
level=SandeshLevel.SYS_ERR)
continue
if IPAddress(inst_ip) in IPNetwork(subnet):
addr_num += 1
except cfgm_common.exceptions.NoIdError:
continue
except Exception as e:
ok = False
result = cfgm_common.utils.detailed_traceback()
if not ok:
self.config_log(result, level=SandeshLevel.SYS_ERR)
continue

inst_ip = result.get('instance_ip_address', None)
if IPAddress(inst_ip) in IPNetwork(subnet):
addr_num += 1

return addr_num
# end ip_count
Expand Down
68 changes: 49 additions & 19 deletions src/config/api-server/vnc_cfg_api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,23 +474,26 @@ def set_admin_role(*args, **kwargs):
# end __init__

def _extensions_transform_request(self, request):
if not self._extension_mgrs['resourceApi'].names():
extensions = self._extension_mgrs.get('resourceApi')
if not extensions or not extensions.names():
return None
return self._extension_mgrs['resourceApi'].map_method(
return extensions.map_method(
'transform_request', request)
# end _extensions_transform_request

def _extensions_validate_request(self, request):
if not self._extension_mgrs['resourceApi'].names():
extensions = self._extension_mgrs.get('resourceApi')
if not extensions or not extensions.names():
return None
return self._extension_mgrs['resourceApi'].map_method(
return extensions.map_method(
'validate_request', request)
# end _extensions_validate_request

def _extensions_transform_response(self, request, response):
if not self._extension_mgrs['resourceApi'].names():
extensions = self._extension_mgrs.get('resourceApi')
if not extensions or not extensions.names():
return None
return self._extension_mgrs['resourceApi'].map_method(
return extensions.map_method(
'transform_response', request, response)
# end _extensions_transform_response

Expand Down Expand Up @@ -643,17 +646,32 @@ def documentation_http_get(self, filename):

def ref_update_http_post(self):
self._post_common(bottle.request, None, None)
obj_type = bottle.request.json['type']
obj_uuid = bottle.request.json['uuid']
ref_type = bottle.request.json['ref-type'].replace('-', '_')
operation = bottle.request.json['operation']
# grab fields
obj_type = bottle.request.json.get('type')
obj_uuid = bottle.request.json.get('uuid')
ref_type = bottle.request.json.get('ref-type')
operation = bottle.request.json.get('operation')
ref_uuid = bottle.request.json.get('ref-uuid')
ref_fq_name = bottle.request.json.get('ref-fq-name')
attr = bottle.request.json.get('attr')

# validate fields
if None in (obj_type, obj_uuid, ref_type, operation):
err_msg = 'Bad Request: type/uuid/ref-type/operation is null: '
err_msg += '%s, %s, %s, %s.' \
%(obj_type, obj_uuid, ref_type, operation)
bottle.abort(400, err_msg)

if operation.upper() not in ['ADD', 'DELETE']:
err_msg = 'Bad Request: operation should be add or delete: %s' \
%(operation)
bottle.abort(400, err_msg)

if not ref_uuid and not ref_fq_name:
bottle.abort(404, 'Either ref-uuid or ref-fq-name must be specified')
err_msg = 'Bad Request: Either ref-uuid or ref-fq-name must be specified'
bottle.abort(400, err_msg)

ref_type = ref_type.replace('-', '_')
if not ref_uuid:
try:
ref_uuid = self._db_conn.fq_name_to_uuid(ref_type, ref_fq_name)
Expand All @@ -667,11 +685,17 @@ def ref_update_http_post(self):
fq_name = self._db_conn.uuid_to_fq_name(obj_uuid)
except NoIdError:
bottle.abort(404, 'UUID ' + obj_uuid + ' not found')
(read_ok, read_result) = self._db_conn.dbe_read(
obj_type, bottle.request.json)
try:
(read_ok, read_result) = self._db_conn.dbe_read(
obj_type, bottle.request.json)
except NoIdError:
bottle.abort(404, 'Object Not Found: ' + obj_uuid)
except Exception as e:
read_ok = False
read_result = cfgm_common.utils.detailed_traceback()
if not read_ok:
self.config_object_error(obj_uuid, None, obj_type, 'ref_update', read_result)
bottle.abort(404, read_result)
bottle.abort(500, read_result)

obj_dict = read_result
if operation == 'ADD':
Expand All @@ -683,10 +707,6 @@ def ref_update_http_post(self):
if old_ref['to'] == ref_fq_name or old_ref['uuid'] == ref_uuid:
obj_dict[ref_type+'_refs'].remove(old_ref)
break
else:
msg = 'Unknown operation ' + operation
self.config_object_error(obj_uuid, None, obj_type, 'ref_update', msg)
bottle.abort(409, msg)

(ok, put_result) = r_class.http_put(obj_uuid, fq_name, obj_dict, self._db_conn)
if not ok:
Expand Down Expand Up @@ -1485,7 +1505,17 @@ def vn_subnet_ip_count_http_post(self, id):

# expected format {"subnet" : ["2.1.1.0/24", "1.1.1.0/24"]
req_dict = bottle.request.json
(_, obj_dict) = self._db_conn.dbe_read('virtual-network', {'uuid': id})
try:
(ok, result) = self._db_conn.dbe_read('virtual-network', {'uuid': id})
except NoIdError as e:
bottle.abort(404, str(e))
except Exception as e:
ok = False
result = cfgm_common.utils.detailed_traceback()
if not ok:
bottle.abort(500, result)

obj_dict = result
subnet_list = req_dict[
'subnet_list'] if 'subnet_list' in req_dict else []
result = vnc_cfg_types.VirtualNetworkServer.subnet_ip_count(
Expand Down
30 changes: 23 additions & 7 deletions src/config/api-server/vnc_cfg_ifmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from netaddr import IPNetwork

from cfgm_common.uve.vnc_api.ttypes import *
from cfgm_common import ignore_exceptions, utils
from cfgm_common import ignore_exceptions
from cfgm_common.ifmap.client import client, namespaces
from cfgm_common.ifmap.request import NewSessionRequest, PublishRequest
from cfgm_common.ifmap.id import Identity
Expand Down Expand Up @@ -217,7 +217,7 @@ def _ifmap_dequeue_task(self):
self._publish_to_ifmap_dequeue()
except Exception as e:
tb = utils.detailed_traceback()
self.config_log(tb, level=SandeshLevel.SYS_ERROR)
self.config_log(tb, level=SandeshLevel.SYS_ERR)

def _publish_to_ifmap_dequeue(self):
def _publish(requests, traces, publish_discovery=False):
Expand Down Expand Up @@ -878,9 +878,11 @@ def dbe_update_publish(self, obj_type, obj_ids):
# end dbe_update_publish

def _dbe_update_notification(self, obj_info):
(ok, result) = self._db_client_mgr.dbe_read(obj_info['type'], obj_info)
if not ok:
raise Exception(result)
try:
(ok, result) = self._db_client_mgr.dbe_read(obj_info['type'], obj_info)
except NoIdError as e:
# No error, we will hear a delete shortly
return

new_obj_dict = result

Expand Down Expand Up @@ -1131,9 +1133,17 @@ def _update_default_quota(self):

proj_id = self.fq_name_to_uuid('project',
['default-domain', 'default-project'])
(ok, proj_dict) = self.dbe_read('project', {'uuid':proj_id})
try:
(ok, result) = self.dbe_read('project', {'uuid':proj_id})
except NoIdError as e:
ok = False
result = 'Project Not Found: %s' %(proj_id)
if not ok:
self.config_log("Updating default quota failed: %s." %(result),
level=SandeshLevel.SYS_ERR)
return

proj_dict = result
quota = QuotaType()

proj_dict['quota'] = default_quota
Expand Down Expand Up @@ -1446,6 +1456,12 @@ def dbe_read(self, obj_type, obj_ids, obj_fields=None):
[obj_ids['uuid']],
obj_fields)
except NoIdError as e:
# if NoIdError is for obj itself (as opposed to say for parent
# or ref), let caller decide if this can be handled gracefully
# by re-raising
if e._unknown_id == obj_ids['uuid']:
raise

return (False, str(e))

return (ok, cassandra_result[0])
Expand All @@ -1460,7 +1476,7 @@ def dbe_count_children(self, obj_type, obj_id, child_type):
return (False, str(e))

return (ok, cassandra_result)
# end dbe_read
# end dbe_count_children

def dbe_read_multi(self, obj_type, obj_ids_list, obj_fields=None):
if not obj_ids_list:
Expand Down

0 comments on commit 0781f26

Please sign in to comment.