Merge pull request #734 from ansible/fetch_url-uptimerobot

Port uptimerobot to fetch_url
This commit is contained in:
Toshio Kuratomi 2015-07-28 11:21:08 -07:00
commit aa1b4caa8a

View file

@ -51,118 +51,95 @@ EXAMPLES = '''
import json import json
import urllib import urllib
import urllib2
import time import time
API_BASE = "http://api.uptimerobot.com/" API_BASE = "http://api.uptimerobot.com/"
API_ACTIONS = dict( API_ACTIONS = dict(
status='getMonitors?', status='getMonitors?',
editMonitor='editMonitor?' editMonitor='editMonitor?'
) )
API_FORMAT = 'json' API_FORMAT = 'json'
API_NOJSONCALLBACK = 1 API_NOJSONCALLBACK = 1
CHANGED_STATE = False CHANGED_STATE = False
SUPPORTS_CHECK_MODE = False SUPPORTS_CHECK_MODE = False
def checkID(params):
data = urllib.urlencode(params) def checkID(module, params):
full_uri = API_BASE + API_ACTIONS['status'] + data data = urllib.urlencode(params)
full_uri = API_BASE + API_ACTIONS['status'] + data
req = urllib2.urlopen(full_uri) req, info = fetch_url(module, full_uri)
result = req.read()
result = req.read() jsonresult = json.loads(result)
req.close()
jsonresult = json.loads(result) return jsonresult
req.close()
return jsonresult
def startMonitor(params): def startMonitor(module, params):
params['monitorStatus'] = 1 params['monitorStatus'] = 1
data = urllib.urlencode(params)
data = urllib.urlencode(params) full_uri = API_BASE + API_ACTIONS['editMonitor'] + data
req, info = fetch_url(module, full_uri)
full_uri = API_BASE + API_ACTIONS['editMonitor'] + data result = req.read()
jsonresult = json.loads(result)
req = urllib2.urlopen(full_uri) req.close()
return jsonresult['stat']
result = req.read()
jsonresult = json.loads(result)
req.close()
return jsonresult['stat']
def pauseMonitor(params): def pauseMonitor(module, params):
params['monitorStatus'] = 0 params['monitorStatus'] = 0
data = urllib.urlencode(params)
data = urllib.urlencode(params) full_uri = API_BASE + API_ACTIONS['editMonitor'] + data
req, info = fetch_url(module, full_uri)
full_uri = API_BASE + API_ACTIONS['editMonitor'] + data result = req.read()
jsonresult = json.loads(result)
req = urllib2.urlopen(full_uri) req.close()
return jsonresult['stat']
result = req.read()
jsonresult = json.loads(result)
req.close()
return jsonresult['stat']
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
state = dict(required=True, choices=['started', 'paused']), state = dict(required=True, choices=['started', 'paused']),
apikey = dict(required=True), apikey = dict(required=True),
monitorid = dict(required=True) monitorid = dict(required=True)
), ),
supports_check_mode=SUPPORTS_CHECK_MODE supports_check_mode=SUPPORTS_CHECK_MODE
) )
params = dict( params = dict(
apiKey=module.params['apikey'], apiKey=module.params['apikey'],
monitors=module.params['monitorid'], monitors=module.params['monitorid'],
monitorID=module.params['monitorid'], monitorID=module.params['monitorid'],
format=API_FORMAT, format=API_FORMAT,
noJsonCallback=API_NOJSONCALLBACK noJsonCallback=API_NOJSONCALLBACK
) )
check_result = checkID(params) check_result = checkID(module, params)
if check_result['stat'] != "ok": if check_result['stat'] != "ok":
module.fail_json( module.fail_json(
msg="failed", msg="failed",
result=check_result['message'] result=check_result['message']
) )
if module.params['state'] == 'started': if module.params['state'] == 'started':
monitor_result = startMonitor(params) monitor_result = startMonitor(module, params)
else: else:
monitor_result = pauseMonitor(params) monitor_result = pauseMonitor(module, params)
module.exit_json(
msg="success",
module.exit_json( result=monitor_result
msg="success", )
result=monitor_result
)
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
main() from ansible.module_utils.urls import *
if __name__ == '__main__':
main()