def _connect(self, config): server = config.sat_server or self.options.sat_server self.username = config.sat_username or self.options.sat_username self.password = config.sat_password or self.options.sat_password if not server.startswith("http://") and not server.startswith( "https://"): server = "https://%s" % server if not server.endswith("XMLRPC"): server = "%s/XMLRPC" % server try: self.force_register = self.options.force_register except AttributeError: self.force_register = False self.logger.debug("Initializing satellite connection to %s", server) try: # We need two API endpoints: /XMLRPC and /rpc/api self.server_xmlrpc = xmlrpclib.ServerProxy( server, verbose=0, transport=RequestsXmlrpcTransport(server)) server_api = server.replace('/XMLRPC', '/rpc/api') self.server_rpcapi = xmlrpclib.ServerProxy( server_api, verbose=0, transport=RequestsXmlrpcTransport(server_api)) except Exception as e: self.logger.exception("Unable to connect to the Satellite server") raise SatelliteError( "Unable to connect to the Satellite server: " % str(e)) self.logger.debug("Initialized satellite connection")
def test_use_response_content(self, resp): parser_return_value = [FakeParser(), FakeParser()] transport = RequestsXmlrpcTransport('http://localhost') transport.getparser = MagicMock(return_value=parser_return_value) p = PropertyMock() type(resp).content = p transport.parse_response(resp) assert p.called, 'Response.content should be used instead'
def login(self, url=None): """ Login to server using given credentials. """ url = url or self.url try: # Don't log message containing password self.session = XenAPI.Session( url, transport=RequestsXmlrpcTransport(url)) self.session.xenapi.login_with_password(self.username, self.password) self.logger.debug("XEN pool login successful with user %s" % self.username) except NewMaster as nm: url = nm.new_master() if "://" not in url: url = '%s://%s' % (self.url.partition(":")[0], url) self.logger.debug("Switching to new master: %s", url) return self.login(url) except requests.ConnectionError as e: raise virt.VirtError(str(e)) except Exception as e: self.logger.exception("Unable to login to XENserver %s" % self.url) raise virt.VirtError(str(e))