Skip to content

Commit

Permalink
Merge pull request david-caro#64 from pief/pief-apiwarnings
Browse files Browse the repository at this point in the history
Show duplicated definitions only in debug log level
  • Loading branch information
david-caro committed Feb 17, 2016
2 parents a4c1318 + fb3f3ac commit 656f629
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions foreman/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
OLD_REQ = True
else:
OLD_REQ = False
logger = logging.getLogger(__name__)


def try_int(what):
Expand Down Expand Up @@ -333,7 +334,12 @@ def parse_resource_definition(resource_name, resource_dct):
functions = foreign_methods.setdefault(api.resource, {})
if api.name in functions:
old_api = functions.get(api.name).defs
logging.warning(
# show only in debug the repeated but identical definitions
log_method = logger.warning
if api.url == old_api.url:
log_method = logger.debug

log_method(
"There is a conflict trying to redefine a method "
"for a foreign resource (%s): \n"
"\tresource:\n"
Expand All @@ -357,7 +363,12 @@ def parse_resource_definition(resource_name, resource_dct):
# it's an own method, resource and url match
if api.name in new_dict['_own_methods']:
old_api = new_dict.get(api.name).defs
logging.warning(
log_method = logger.warning
# show only in debug the repeated but identical definitions
if api.url == old_api.url:
log_method = logger.debug

log_method(
"There is a conflict trying to redefine method "
"(%s): \n"
"\tapipie_resource: %s\n"
Expand Down Expand Up @@ -446,7 +457,7 @@ def __new__(meta, cls_name, bases, attrs):
['DEFS'],
)
except ImportError:
logging.error('Unable to import plugin module %s', plugin)
logger.error('Unable to import plugin module %s', plugin)
continue
for http_method, funcs in six.iteritems(myplugin.DEFS):
methods = MetaForeman.convert_plugin_def(http_method, funcs)
Expand All @@ -456,7 +467,7 @@ def _init(self, foreman):
super(self.__class__, self).__init__(foreman)
for name, value in six.iteritems(self.__class__.__dict__):
if isinstance(value, types.FunctionType) and name[0] != '_':
logging.debug(
logger.debug(
'Registering plugin method %s',
name,
)
Expand Down Expand Up @@ -490,7 +501,7 @@ def convert_plugin_def(http_method, funcs):
"""
methods = []
if http_method not in ('GET', 'PUT', 'POST', 'DELETE'):
logging.error(
logger.error(
'Plugin load failure, HTTP method %s unsupported.',
http_method,
)
Expand Down Expand Up @@ -554,7 +565,7 @@ def __init__(self, url, auth=None, version=None, api_version=None,
"""
if api_version is None:
api_version = 1
logging.warning(
logger.warning(
"Api v1 will not be the default in the next version, if you "
"still want to use it, change the call to explicitly ask for "
"it. Though we recommend using the new and improved version 2"
Expand Down Expand Up @@ -680,7 +691,7 @@ def _get_local_defs(self, strict=True):
last_major_match = None
for f_name, f_ver in sorted(files_version, key=lambda x: x[1]):
if f_ver == version:
logging.debug('Found local cached version %s' % f_name)
logger.debug('Found local cached version %s' % f_name)
return json.loads(open(f_name).read())
if f_ver[:2] == version[:2]:
last_major_match = f_name
Expand All @@ -695,7 +706,7 @@ def _get_local_defs(self, strict=True):
"without strict flag to use it"
% (self.version, last_major_match))
else:
logging.warn(
logger.warn(
"Not exact version found, got cached %s for Foreman %s",
last_major_match,
self.version,
Expand Down Expand Up @@ -733,7 +744,7 @@ def _get_remote_defs(self):
try:
os.makedirs(defs_path)
except:
logging.debug('Unable to create cache dir %s', defs_path)
logger.debug('Unable to create cache dir %s', defs_path)
return
cache_fn = '%s/%s-v%s.json' % (
defs_path, self.version,
Expand All @@ -742,12 +753,12 @@ def _get_remote_defs(self):
try:
with open(cache_fn, 'w') as cache_fd:
cache_fd.write(json.dumps(data, indent=4, default=str))
logging.debug('Wrote cache file %s', cache_fn)
logger.debug('Wrote cache file %s', cache_fn)
except:
logging.debug('Unable to write cache file %s', cache_fn)
logger.debug('Unable to write cache file %s', cache_fn)
else:
if res.status_code == 404:
logging.warn(
logger.warn(
"Unable to get api definition from live Foreman instance "
"at '%s', you might want to set the strict_cache to False."
"\nNOTE: Make sure that you have set the config.use_cache "
Expand All @@ -765,12 +776,12 @@ def _get_defs(self, use_cache, strict_cache):
data = None
if use_cache:
try:
logging.debug("Trying local cached definitions first")
logger.debug("Trying local cached definitions first")
data = self._get_local_defs(strict=strict_cache)
except ForemanVersionException as exc:
logging.debug(exc)
logger.debug(exc)
if not data:
logging.debug("Checking remote server for definitions")
logger.debug("Checking remote server for definitions")
data = self._get_remote_defs()
return data

Expand Down Expand Up @@ -809,7 +820,7 @@ def _generate_api_defs(self, use_cache=True, strict_cache=True):
if prop_name.startswith('_'):
continue
if prop_name in old_res:
logging.warning(
logger.warning(
"There is conflict trying to redefine method "
"(%s) with foreign method: \n"
"\tapipie_resource: %s\n",
Expand All @@ -831,7 +842,7 @@ def _generate_api_defs(self, use_cache=True, strict_cache=True):

for f_mname, f_method in six.iteritems(f_methods):
if f_mname in methods:
logging.warning(
logger.warning(
"There is conflict trying to redefine method "
"(%s) with foreign method: \n"
"\tapipie_resource: %s\n",
Expand All @@ -852,7 +863,7 @@ def _generate_api_defs(self, use_cache=True, strict_cache=True):
resource_data,
)
if not resource_data['_own_methods']:
logging.debug('Skipping empty resource %s' % resource_name)
logger.debug('Skipping empty resource %s' % resource_name)
continue
instance = new_resource(self)
setattr(self, resource_name, instance)
Expand Down

0 comments on commit 656f629

Please sign in to comment.