Refactored code to use module utility fetch_url function.

pull/18777/head
Jeff Gonzalez 10 years ago committed by Matt Clay
parent 04fb952d49
commit 7ea6650d6c

@ -122,7 +122,6 @@ import os.path
import tempfile import tempfile
import re import re
import shlex import shlex
import urllib2
class keydict(dict): class keydict(dict):
@ -337,19 +336,22 @@ def enforce_state(module, params):
manage_dir = params.get("manage_dir", True) manage_dir = params.get("manage_dir", True)
state = params.get("state", "present") state = params.get("state", "present")
key_options = params.get("key_options", None) key_options = params.get("key_options", None)
error_msg = "Error getting key from: %s"
# if the key is a url, request it and use it as key source
if key.startswith("http"): if key.startswith("http"):
try: try:
gh_key = urllib2.urlopen(key).read() resp, info = fetch_url(module, key)
except urllib2.URLError, e: if info['status'] != 200:
module.fail_json(msg="no key found at: %s" % key) module.fail_json(msg=error_msg % key)
else:
key = gh_key key = resp.read()
except Exception:
module.fail_json(msg=error_msg % key)
# extract individual keys into an array, skipping blank lines and comments # extract individual keys into an array, skipping blank lines and comments
key = [s for s in key.splitlines() if s and not s.startswith('#')] key = [s for s in key.splitlines() if s and not s.startswith('#')]
# check current state -- just get the filename, don't create file # check current state -- just get the filename, don't create file
do_write = False do_write = False
params["keyfile"] = keyfile(module, user, do_write, path, manage_dir) params["keyfile"] = keyfile(module, user, do_write, path, manage_dir)
@ -431,4 +433,5 @@ def main():
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
from ansible.module_utils.urls import *
main() main()

Loading…
Cancel
Save