Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_get():
x = "hello world"
def test_get_or_abort():
return x
assert x == utils.get_or_abort(test_get_or_abort)
def test_http_connection_error(mock_log):
err = "ConnectionError"
def connection_error():
x = Response()
x.status_code = 500
x.reason = err
raise ConnectionError(err, response=x)
with pytest.raises(InternalServerError):
utils.get_or_abort(connection_error)
mock_log.error.assert_called_with(err)
if env != '*':
query.add(EqualsOperator("environment", env))
if len(query.operations) == 0:
query = None
# Generator needs to be converted (graph / total)
try:
value = int(value)
except ValueError:
if value is not None and query is not None:
query.add(EqualsOperator('value', unquote_plus(value)))
except TypeError:
pass
facts = [f for f in get_or_abort(
puppetdb.facts,
name=fact,
query=query)]
total = len(facts)
counts = {}
json = {
'draw': draw,
'recordsTotal': total,
'recordsFiltered': total,
'data': []}
for fact_h in facts:
line = []
if not fact:
num_nodes_query = ExtractOperator()
num_nodes_query.add_field(FunctionOperator('count'))
num_nodes_query.add_query(query)
if app.config['OVERVIEW_FILTER'] is not None:
query.add(app.config['OVERVIEW_FILTER'])
num_resources_query = ExtractOperator()
num_resources_query.add_field(FunctionOperator('count'))
num_resources_query.add_query(EqualsOperator("environment", env))
num_nodes = get_or_abort(
puppetdb._query,
'nodes',
query=num_nodes_query)
num_resources = get_or_abort(
puppetdb._query,
'resources',
query=num_resources_query)
metrics['num_nodes'] = num_nodes[0]['count']
metrics['num_resources'] = num_resources[0]['count']
try:
metrics['avg_resources_node'] = "{0:10.0f}".format(
(num_resources[0]['count'] / num_nodes[0]['count']))
except ZeroDivisionError:
metrics['avg_resources_node'] = 0
nodes = get_or_abort(puppetdb.nodes,
query=query,
unreported=app.config['UNRESPONSIVE_HOURS'],
with_status=True,
with_event_numbers=app.config['WITH_EVENT_NUMBERS'])
prefix = 'puppetlabs.puppetdb.population'
query_type = ''
# Puppet DB version changed the query format from 3.2.0
# to 4.0 when querying mbeans
if get_db_version(puppetdb) < (4, 0, 0):
query_type = 'type=default,'
num_nodes = get_or_abort(
puppetdb.metric,
"{0}{1}".format(prefix, ':%sname=num-nodes' % query_type))
num_resources = get_or_abort(
puppetdb.metric,
"{0}{1}".format(prefix, ':%sname=num-resources' % query_type))
avg_resources_node = get_or_abort(
puppetdb.metric,
"{0}{1}".format(prefix,
':%sname=avg-resources-per-node' % query_type))
metrics['num_nodes'] = num_nodes['Value']
metrics['num_resources'] = num_resources['Value']
metrics['avg_resources_node'] = "{0:10.0f}".format(
avg_resources_node['Value'])
else:
query = AndOperator()
query.add(EqualsOperator('catalog_environment', env))
num_nodes_query = ExtractOperator()
num_nodes_query.add_field(FunctionOperator('count'))
num_nodes_query.add_query(query)
if app.config['OVERVIEW_FILTER'] is not None:
def metrics(env):
"""Lists all available metrics that PuppetDB is aware of.
:param env: While this parameter serves no function purpose it is required
for the environments template block
:type env: :obj:`string`
"""
envs = environments()
check_env(env, envs)
metrics = get_or_abort(puppetdb._query, 'mbean')
return render_template('metrics.html',
metrics=sorted(metrics.keys()),
envs=envs,
current_env=env)
def catalog_node(env, node_name):
"""Fetches from PuppetDB the compiled catalog of a given node.
:param env: Find the catalog with this environment value
:type env: :obj:`string`
"""
envs = environments()
check_env(env, envs)
if app.config['ENABLE_CATALOG']:
catalog = get_or_abort(puppetdb.catalog,
node=node_name)
return render_template('catalog.html',
catalog=catalog,
envs=envs,
current_env=env)
else:
log.warn('Access to catalog interface disabled by administrator')
abort(403)
def environments():
puppetdb = get_puppetdb()
envs = get_or_abort(puppetdb.environments)
x = []
for env in envs:
x.append(env['name'])
return x
def radiator(env):
"""This view generates a simplified monitoring page
akin to the radiator view in puppet dashboard
"""
envs = environments()
check_env(env, envs)
if env == '*':
query_type = ''
if get_db_version(puppetdb) < (4, 0, 0):
query_type = 'type=default,'
query = None
metrics = get_or_abort(
puppetdb.metric,
'puppetlabs.puppetdb.population:%sname=num-nodes' % query_type)
num_nodes = metrics['Value']
else:
query = AndOperator()
metric_query = ExtractOperator()
query.add(EqualsOperator("catalog_environment", env))
metric_query.add_field(FunctionOperator('count'))
metric_query.add_query(query)
metrics = get_or_abort(
puppetdb._query,
'nodes',
query=metric_query)
num_nodes = metrics[0]['count']
else:
query = AndOperator()
query.add(EqualsOperator('catalog_environment', env))
num_nodes_query = ExtractOperator()
num_nodes_query.add_field(FunctionOperator('count'))
num_nodes_query.add_query(query)
if app.config['OVERVIEW_FILTER'] is not None:
query.add(app.config['OVERVIEW_FILTER'])
num_resources_query = ExtractOperator()
num_resources_query.add_field(FunctionOperator('count'))
num_resources_query.add_query(EqualsOperator("environment", env))
num_nodes = get_or_abort(
puppetdb._query,
'nodes',
query=num_nodes_query)
num_resources = get_or_abort(
puppetdb._query,
'resources',
query=num_resources_query)
metrics['num_nodes'] = num_nodes[0]['count']
metrics['num_resources'] = num_resources[0]['count']
try:
metrics['avg_resources_node'] = "{0:10.0f}".format(
(num_resources[0]['count'] / num_nodes[0]['count']))
except ZeroDivisionError:
metrics['avg_resources_node'] = 0
nodes = get_or_abort(puppetdb.nodes,