Don't write to cache if queries fail

pull/8089/head
Steve Salevan 11 years ago committed by Steve Salevan
parent 71c46efe80
commit 96e815d2b9

@ -93,10 +93,18 @@ class CollinsDefaults(object):
LOG_FORMAT = '%(asctime)-15s %(message)s' LOG_FORMAT = '%(asctime)-15s %(message)s'
class Error(Exception):
pass
class MaxRetriesError(Error):
pass
class CollinsInventory(object): class CollinsInventory(object):
def __init__(self): def __init__(self):
""" Main execution path """ """ Constructs CollinsInventory object and reads all configuration. """
self.inventory = dict() # A list of groups and the hosts in that group self.inventory = dict() # A list of groups and the hosts in that group
self.cache = dict() # Details about hosts in the inventory self.cache = dict() # Details about hosts in the inventory
@ -109,7 +117,28 @@ class CollinsInventory(object):
filename=self.log_location) filename=self.log_location)
self.log = logging.getLogger('CollinsInventory') self.log = logging.getLogger('CollinsInventory')
def _asset_get_attribute(self, asset, attrib):
""" Returns a user-defined attribute from an asset if it exists; otherwise,
returns None. """
if 'ATTRIBS' in asset:
for attrib_block in asset['ATTRIBS'].keys():
if attrib in asset['ATTRIBS'][attrib_block]:
return asset['ATTRIBS'][attrib_block][attrib]
return None
def _asset_has_attribute(self, asset, attrib):
""" Returns whether a user-defined attribute is present on an asset. """
if 'ATTRIBS' in asset:
for attrib_block in asset['ATTRIBS'].keys():
if attrib in asset['ATTRIBS'][attrib_block]:
return True
return False
def run(self): def run(self):
""" Main execution path """
# Updates cache if cache is not present or has expired. # Updates cache if cache is not present or has expired.
successful = True successful = True
if self.args.refresh_cache: if self.args.refresh_cache:
@ -158,9 +187,8 @@ class CollinsInventory(object):
# Locates all assets matching the provided query, exhausting pagination. # Locates all assets matching the provided query, exhausting pagination.
while True: while True:
if num_retries == self.collins_max_retries: if num_retries == self.collins_max_retries:
self.log.error("Maximum of %s retries reached; giving up", raise MaxRetriesError("Maximum of %s retries reached; giving up" % \
self.collins_max_retries) self.collins_max_retries)
break
query_parameters['page'] = cur_page query_parameters['page'] = cur_page
query_url = "%s?%s" % ( query_url = "%s?%s" % (
(CollinsDefaults.ASSETS_API_ENDPOINT % self.collins_host), (CollinsDefaults.ASSETS_API_ENDPOINT % self.collins_host),
@ -249,20 +277,6 @@ class CollinsInventory(object):
action='store_true', default=False, help='Pretty print all JSON output') action='store_true', default=False, help='Pretty print all JSON output')
self.args = parser.parse_args() self.args = parser.parse_args()
def _asset_has_attribute(self, asset, attrib):
if 'ATTRIBS' in asset:
for attrib_block in asset['ATTRIBS'].keys():
if attrib in asset['ATTRIBS'][attrib_block]:
return True
return False
def _asset_get_attribute(self, asset, attrib):
if 'ATTRIBS' in asset:
for attrib_block in asset['ATTRIBS'].keys():
if attrib in asset['ATTRIBS'][attrib_block]:
return asset['ATTRIBS'][attrib_block][attrib]
return None
def update_cache(self): def update_cache(self):
""" Make calls to Collins and saves the output in a cache """ """ Make calls to Collins and saves the output in a cache """
@ -351,6 +365,9 @@ class CollinsInventory(object):
return True return True
def push(self, dictionary, key, value): def push(self, dictionary, key, value):
""" Adds a value to a list at a dictionary key, creating the list if it doesn't
exist. """
if key not in dictionary: if key not in dictionary:
dictionary[key] = [] dictionary[key] = []
dictionary[key].append(value) dictionary[key].append(value)

Loading…
Cancel
Save