Merge pull request #629 from bcoca/sensu_check_2.4

made sensu_check 2.4 friendly
This commit is contained in:
Brian Coca 2015-06-19 12:22:59 -04:00
commit 3f9e2fb4e6

View file

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