def _request(method, url, content_type=None, _data=None): ''' Makes a HTTP request. Returns the JSON parse, or an obj with an error. ''' opener = _build_opener(_HTTPHandler) request = _Request(url, data=_data) if content_type: request.add_header('Content-Type', content_type) request.get_method = lambda: method try: handler = opener.open(request) except HTTPError as exc: return {'error': '{0}'.format(exc)} return json.loads(handler.read())
def _request(method, url, content_type=None, _data=None): """ Makes a HTTP request. Returns the JSON parse, or an obj with an error. """ opener = _build_opener(_HTTPHandler) request = _Request(url, data=_data) if content_type: request.add_header("Content-Type", content_type) request.get_method = lambda: method try: handler = opener.open(request) except HTTPError as exc: return {"error": "{0}".format(exc)} return salt.utils.json.loads(handler.read())
def _request(method, url, content_type=None, _data=None, user=None, passwd=None): ''' Makes a HTTP request. Returns the JSON parse, or an obj with an error. ''' opener = _build_opener(_HTTPHandler) request = _Request(url, data=_data) if content_type: request.add_header('Content-Type', content_type) if user and passwd: auth_encode = '{0}:{1}'.format(user, passwd).encode('base64')[:-1] auth_basic = "Basic {0}".format(auth_encode) request.add_header('Authorization', auth_basic) request.add_header('Accept', 'application/json') request.get_method = lambda: method try: handler = opener.open(request) except HTTPError as exc: return {'error': '{0}'.format(exc)} return salt.utils.json.loads(handler.read())
def query(action=None, command=None, args=None, method="GET", data=None): """ Make a web call to a Parallels provider """ path = config.get_cloud_config_value( "url", get_configured_provider(), __opts__, search_global=False ) auth_handler = _HTTPBasicAuthHandler() auth_handler.add_password( realm="Parallels Instance Manager", uri=path, user=config.get_cloud_config_value( "user", get_configured_provider(), __opts__, search_global=False ), passwd=config.get_cloud_config_value( "password", get_configured_provider(), __opts__, search_global=False ), ) opener = _build_opener(auth_handler) _install_opener(opener) if action: path += action if command: path += "/{0}".format(command) if not type(args, dict): args = {} kwargs = {"data": data} if isinstance(data, six.string_types) and "<?xml" in data: kwargs["headers"] = { "Content-type": "application/xml", } if args: params = _urlencode(args) req = _Request(url="{0}?{1}".format(path, params), **kwargs) else: req = _Request(url=path, **kwargs) req.get_method = lambda: method log.debug("%s %s", method, req.get_full_url()) if data: log.debug(data) try: result = _urlopen(req) log.debug("PARALLELS Response Status Code: %s", result.getcode()) if "content-length" in result.headers: content = result.read() result.close() items = ET.fromstring(content) return items return {} except URLError as exc: log.error("PARALLELS Response Status Code: %s %s", exc.code, exc.msg) root = ET.fromstring(exc.read()) log.error(root) return {"error": root}
def query(action=None, command=None, args=None, method='GET', data=None): ''' Make a web call to a Parallels provider ''' path = config.get_cloud_config_value('url', get_configured_provider(), __opts__, search_global=False) auth_handler = _HTTPBasicAuthHandler() auth_handler.add_password( realm='Parallels Instance Manager', uri=path, user=config.get_cloud_config_value('user', get_configured_provider(), __opts__, search_global=False), passwd=config.get_cloud_config_value('password', get_configured_provider(), __opts__, search_global=False)) opener = _build_opener(auth_handler) _install_opener(opener) if action: path += action if command: path += '/{0}'.format(command) if not type(args, dict): args = {} kwargs = {'data': data} if isinstance(data, six.string_types) and '<?xml' in data: kwargs['headers'] = { 'Content-type': 'application/xml', } if args: params = _urlencode(args) req = _Request(url='{0}?{1}'.format(path, params), **kwargs) else: req = _Request(url=path, **kwargs) req.get_method = lambda: method log.debug('{0} {1}'.format(method, req.get_full_url())) if data: log.debug(data) try: result = _urlopen(req) log.debug('PARALLELS Response Status Code: {0}'.format( result.getcode())) if 'content-length' in result.headers: content = result.read() result.close() items = ET.fromstring(content) return items return {} except URLError as exc: log.error('PARALLELS Response Status Code: {0} {1}'.format( exc.code, exc.msg)) root = ET.fromstring(exc.read()) log.error(root) return {'error': root}
def query(action=None, command=None, args=None, method='GET', data=None): ''' Make a web call to a Parallels provider ''' path = config.get_cloud_config_value( 'url', get_configured_provider(), __opts__, search_global=False ) auth_handler = _HTTPBasicAuthHandler() auth_handler.add_password( realm='Parallels Instance Manager', uri=path, user=config.get_cloud_config_value( 'user', get_configured_provider(), __opts__, search_global=False ), passwd=config.get_cloud_config_value( 'password', get_configured_provider(), __opts__, search_global=False ) ) opener = _build_opener(auth_handler) _install_opener(opener) if action: path += action if command: path += '/{0}'.format(command) if not type(args, dict): args = {} kwargs = {'data': data} if isinstance(data, str) and '<?xml' in data: kwargs['headers'] = { 'Content-type': 'application/xml', } if args: params = _urlencode(args) req = _Request(url='{0}?{1}'.format(path, params), **kwargs) else: req = _Request(url=path, **kwargs) req.get_method = lambda: method log.debug('{0} {1}'.format(method, req.get_full_url())) if data: log.debug(data) try: result = _urlopen(req) log.debug( 'PARALLELS Response Status Code: {0}'.format( result.getcode() ) ) if 'content-length' in result.headers: content = result.read() result.close() items = ET.fromstring(content) return items return {} except URLError as exc: log.error( 'PARALLELS Response Status Code: {0} {1}'.format( exc.code, exc.msg ) ) root = ET.fromstring(exc.read()) log.error(root) return {'error': root}
def _get_options(ret=None): """ Get the couchdb options from salt. ''' attrs = {'url': 'url', 'db': 'db', 'user': '******', 'passwd': 'passwd', 'redact_pws': 'redact_pws', 'minimum_return': 'minimum_return'} _options = salt.returners.get_returner_options(__virtualname__, ret, attrs, __salt__=__salt__, __opts__=__opts__) if 'url' not in _options: log.debug("Using default url.") _options["url"] = "http://salt:5984/" if "db" not in _options: log.debug("Using default database.") _options["db"] = "salt" if 'user' not in _options: log.debug("Not athenticating with a user.") _options['user'] = None if 'passwd' not in _options: log.debug("Not athenticating with a password.") _options['passwd'] = None if 'redact_pws' not in _options: log.debug("Not redacting passwords.") _options['redact_pws'] = None if 'minimum_return' not in _options: log.debug("Not minimizing the return object.") _options['minimum_return'] = None return _options def _redact_passwords(path, key, value): if 'password' in str(key) and value: return key, 'XXX-REDACTED-XXX' return key, value def _minimize_return(path, key, value): if isinstance(key, str) and key.startswith('__pub'): return False return True def _generate_doc(ret): """ Create a object that will be saved into the database based on options. """ # Create a copy of the object that we will return. retc = ret.copy() # Set the ID of the document to be the JID. retc["_id"] = ret["jid"] # Add a timestamp field to the document retc["timestamp"] = time.time() return retc def _request(method, url, content_type=None, _data=None, user=None, passwd=None): ''' Makes a HTTP request. Returns the JSON parse, or an obj with an error. """ opener = _build_opener(_HTTPHandler) request = _Request(url, data=_data) if content_type: request.add_header('Content-Type', content_type) if user and passwd: auth_encode = '{0}:{1}'.format(user, passwd).encode('base64')[:-1] auth_basic = "Basic {0}".format(auth_encode) request.add_header('Authorization', auth_basic) request.add_header('Accept', 'application/json') request.get_method = lambda: method try: handler = opener.open(request) except HTTPError as exc: return {"error": "{0}".format(exc)} return salt.utils.json.loads(handler.read())