mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-10-23 12:33:59 -07:00
Fix urlopen usage to use open_url instead
Add a travis test for urlopen usage
This commit is contained in:
parent
e97d448838
commit
3db8070aa3
9 changed files with 52 additions and 54 deletions
|
@ -45,26 +45,24 @@ import os
|
|||
import sys
|
||||
import time
|
||||
import ConfigParser
|
||||
import urllib2
|
||||
import base64
|
||||
|
||||
try:
|
||||
import json
|
||||
except ImportError:
|
||||
import simplejson as json
|
||||
|
||||
from ansible.module_utils.urls import open_url
|
||||
|
||||
def api_get(link, config):
|
||||
try:
|
||||
if link == None:
|
||||
request = urllib2.Request(config.get('api','uri')+config.get('api','login_path'))
|
||||
request.add_header("Accept",config.get('api','login_type'))
|
||||
url = config.get('api','uri') + config.get('api','login_path')
|
||||
headers = {"Accept": config.get('api','login_type')}
|
||||
else:
|
||||
request = urllib2.Request(link['href']+'?limit=0')
|
||||
request.add_header("Accept",link['type'])
|
||||
# Auth
|
||||
base64string = base64.encodestring('%s:%s' % (config.get('auth','apiuser'),config.get('auth','apipass'))).replace('\n', '')
|
||||
request.add_header("Authorization", "Basic %s" % base64string)
|
||||
result = urllib2.urlopen(request)
|
||||
url = link['href'] + '?limit=0'
|
||||
headers = {"Accept": link['type']}
|
||||
result = open_url(url, headers=headers, url_username=config.get('auth','apiuser').replace('\n', ''),
|
||||
url_password=config.get('auth','apipass').replace('\n', ''))
|
||||
return json.loads(result.read())
|
||||
except:
|
||||
return None
|
||||
|
|
|
@ -67,7 +67,6 @@ Tested against Ansible 1.8.2 and Collins 1.3.0.
|
|||
|
||||
|
||||
import argparse
|
||||
import base64
|
||||
import ConfigParser
|
||||
import logging
|
||||
import os
|
||||
|
@ -76,7 +75,6 @@ import sys
|
|||
from time import time
|
||||
import traceback
|
||||
import urllib
|
||||
import urllib2
|
||||
|
||||
try:
|
||||
import json
|
||||
|
@ -85,6 +83,7 @@ except ImportError:
|
|||
|
||||
from six import iteritems
|
||||
|
||||
from ansible.module_utils.urls import open_url
|
||||
|
||||
class CollinsDefaults(object):
|
||||
ASSETS_API_ENDPOINT = '%s/api/assets'
|
||||
|
@ -198,10 +197,11 @@ class CollinsInventory(object):
|
|||
(CollinsDefaults.ASSETS_API_ENDPOINT % self.collins_host),
|
||||
urllib.urlencode(query_parameters, doseq=True)
|
||||
)
|
||||
request = urllib2.Request(query_url)
|
||||
request.add_header('Authorization', self.basic_auth_header)
|
||||
try:
|
||||
response = urllib2.urlopen(request, timeout=self.collins_timeout_secs)
|
||||
response = open_url(query_url,
|
||||
timeout=self.collins_timeout_secs,
|
||||
url_username=self.collins_username,
|
||||
url_password=self.collins_password)
|
||||
json_response = json.loads(response.read())
|
||||
# Adds any assets found to the array of assets.
|
||||
assets += json_response['data']['Data']
|
||||
|
@ -261,8 +261,6 @@ class CollinsInventory(object):
|
|||
|
||||
log_path = config.get('collins', 'log_path')
|
||||
self.log_location = log_path + '/ansible-collins.log'
|
||||
self.basic_auth_header = "Basic %s" % base64.encodestring(
|
||||
'%s:%s' % (self.collins_username, self.collins_password))[:-1]
|
||||
|
||||
def parse_cli_args(self):
|
||||
""" Command line argument processing """
|
||||
|
|
|
@ -28,7 +28,6 @@ version_added: None
|
|||
author: Michael Scherer
|
||||
'''
|
||||
|
||||
import urllib2
|
||||
try:
|
||||
import json
|
||||
except ImportError:
|
||||
|
@ -39,6 +38,8 @@ import sys
|
|||
import ConfigParser
|
||||
import StringIO
|
||||
|
||||
from ansible.module_utils.urls import open_url
|
||||
|
||||
configparser = None
|
||||
|
||||
|
||||
|
@ -66,34 +67,21 @@ def get_config(env_var, config_var):
|
|||
return result
|
||||
|
||||
|
||||
def get_json_from_api(url):
|
||||
req = urllib2.Request(url, None, {'Accept': 'application/json; version=1.5'})
|
||||
response = urllib2.urlopen(req)
|
||||
def get_json_from_api(url, username, password):
|
||||
headers = {'Accept': 'application/json; version=1.5'}
|
||||
response = open_url(url, headers=headers, url_username=username, url_password=password)
|
||||
return json.loads(response.read())['data']
|
||||
|
||||
|
||||
def passwd_setup(top_level_url, username, password):
|
||||
# create a password manager
|
||||
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
|
||||
password_mgr.add_password(None, top_level_url, username, password)
|
||||
|
||||
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
|
||||
opener = urllib2.build_opener(handler)
|
||||
|
||||
urllib2.install_opener(opener)
|
||||
|
||||
|
||||
username = get_config('ANSIBLE_OPENSHIFT_USERNAME', 'default_rhlogin')
|
||||
password = get_config('ANSIBLE_OPENSHIFT_PASSWORD', 'password')
|
||||
broker_url = 'https://%s/broker/rest/' % get_config('ANSIBLE_OPENSHIFT_BROKER', 'libra_server')
|
||||
|
||||
|
||||
passwd_setup(broker_url, username, password)
|
||||
|
||||
response = get_json_from_api(broker_url + '/domains')
|
||||
response = get_json_from_api(broker_url + '/domains', username, password)
|
||||
|
||||
response = get_json_from_api("%s/domains/%s/applications" %
|
||||
(broker_url, response[0]['id']))
|
||||
(broker_url, response[0]['id']), username, password)
|
||||
|
||||
result = {}
|
||||
for app in response:
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import urllib
|
||||
import urllib2
|
||||
try:
|
||||
import json
|
||||
except ImportError:
|
||||
|
@ -27,6 +26,7 @@ from optparse import OptionParser
|
|||
|
||||
from six import iteritems
|
||||
|
||||
from ansible.module_utils.urls import open_url
|
||||
|
||||
class ProxmoxNodeList(list):
|
||||
def get_names(self):
|
||||
|
@ -86,7 +86,7 @@ class ProxmoxAPI(object):
|
|||
'password': self.options.password,
|
||||
})
|
||||
|
||||
data = json.load(urllib2.urlopen(request_path, request_params))
|
||||
data = json.load(open_url(request_path, data=request_params))
|
||||
|
||||
self.credentials = {
|
||||
'ticket': data['data']['ticket'],
|
||||
|
@ -94,11 +94,10 @@ class ProxmoxAPI(object):
|
|||
}
|
||||
|
||||
def get(self, url, data=None):
|
||||
opener = urllib2.build_opener()
|
||||
opener.addheaders.append(('Cookie', 'PVEAuthCookie={}'.format(self.credentials['ticket'])))
|
||||
|
||||
request_path = '{}{}'.format(self.options.url, url)
|
||||
request = opener.open(request_path, data)
|
||||
|
||||
headers = {'Cookie': 'PVEAuthCookie={}'.format(self.credentials['ticket'])}
|
||||
request = open_url(request_path, data=data, headers=headers)
|
||||
|
||||
response = json.load(request)
|
||||
return response['data']
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue