Merge pull request #629 from bcoca/sensu_check_2.4

made sensu_check 2.4 friendly
reviewable/pr18780/r1
Brian Coca 10 years ago
commit 3f9e2fb4e6

@ -183,19 +183,23 @@ 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: try:
config = json.load(stream) stream = open(path, 'r')
except IOError as e: config = json.load(stream.read())
if e.errno is 2: # File not found, non-fatal except IOError, e:
if state == 'absent': if e.errno is 2: # File not found, non-fatal
reasons.append('file did not exist and state is `absent\'') if state == 'absent':
return changed, reasons reasons.append('file did not exist and state is `absent\'')
config = {} return changed, reasons
else: config = {}
module.fail_json(msg=str(e)) else:
except ValueError: module.fail_json(msg=str(e))
msg = '{path} contains invalid JSON'.format(path=path) except ValueError:
module.fail_json(msg=msg) msg = '{path} contains invalid JSON'.format(path=path)
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 +278,14 @@ 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: try:
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, e:
module.fail_json(msg=str(e)) module.fail_json(msg=str(e))
finally:
if stream:
stream.close()
return changed, reasons return changed, reasons

Loading…
Cancel
Save