made sensu_check 2.4 friendly

This commit is contained in:
Brian Coca 2015-06-19 12:05:50 -04:00 committed by Matt Clay
parent 5afc244147
commit 027aaed757

View file

@ -183,8 +183,8 @@ def sensu_check(module, path, name, state='present', backup=False):
import simplejson as json import simplejson as json
try: try:
with open(path) as stream: stream = open(path, 'r')
config = json.load(stream) config = json.load(stream.read())
except IOError as e: except IOError as e:
if e.errno is 2: # File not found, non-fatal if e.errno is 2: # File not found, non-fatal
if state == 'absent': if state == 'absent':
@ -196,6 +196,9 @@ def sensu_check(module, path, name, state='present', backup=False):
except ValueError: except ValueError:
msg = '{path} contains invalid JSON'.format(path=path) msg = '{path} contains invalid JSON'.format(path=path)
module.fail_json(msg=msg) module.fail_json(msg=msg)
finally:
if stream:
stream.close()
if 'checks' not in config: if 'checks' not in config:
if state == 'absent': if state == 'absent':
@ -274,10 +277,13 @@ def sensu_check(module, path, name, state='present', backup=False):
if backup: if backup:
module.backup_local(path) module.backup_local(path)
try: try:
with open(path, 'w') as stream: stream = open(path, 'w')
stream.write(json.dumps(config, indent=2) + '\n') stream.write(json.dumps(config, indent=2) + '\n')
except IOError as e: except IOError as e:
module.fail_json(msg=str(e)) module.fail_json(msg=str(e))
finally:
if stream:
stream.close()
return changed, reasons return changed, reasons