def _http_request(self, api, method='GET', params='', headers=None, timeout=120): if not headers: headers = self._create_headers() end_time = time.time() + timeout while True: try: response, content = httplib2.Http(timeout=timeout).request(api, method, params, headers) if response['status'] in ['200', '201', '202']: return True, content, response else: try: json_parsed = json.loads(content) except ValueError as e: json_parsed = {} json_parsed["error"] = "status: {0}, content: {1}".format(response['status'], content) reason = "unknown" if "error" in json_parsed: reason = json_parsed["error"] message = '{0} {1} body: {2} headers: {3} error: {4} reason: {5} {6} {7}'.\ format(method, api, params, headers, response['status'], reason, content.rstrip('\n'), self._get_auth(headers)) log.error(message) log.debug(''.join(traceback.format_stack())) return False, content, response except socket.error as e: log.error("socket error while connecting to {0} error {1} ".format(api, e)) if time.time() > end_time: raise ServerUnavailableException(ip=self.ip) except httplib2.ServerNotFoundError as e: log.error("ServerNotFoundError error while connecting to {0} error {1} ".format(api, e)) if time.time() > end_time: raise ServerUnavailableException(ip=self.ip) time.sleep(3)
def _rest_upload_file(self, URL, file_path_name, username=None, password=None, curl=False, data_json=None): data = open(file_path_name, 'rb').read() http = httplib2.Http() status = None content = None if curl: cmd = "curl -v -d '%s' %s -u %s:%s" % (data_json, URL, username, password) log.info("Running command : {0}".format(cmd)) content = subprocess.check_output(cmd, shell=True) return status, content status, content = http.request(URL, 'POST', headers=self._create_rest_headers( username, password), body=data) log.info(" Status from rest file upload command is {0}".format(status)) log.info( " Content from rest file upload command is {0}".format(content)) return status, content
def _http_request(self, api, method='GET', params='', headers=None, timeout=120): if not headers: headers = self._create_headers() end_time = time.time() + timeout while True: try: response, content = httplib2.Http(timeout=timeout).request( api, method, params, headers) if response['status'] in ['200', '201', '202']: return True, content, response else: try: json_parsed = json.loads(content) except ValueError: json_parsed = dict() json_parsed["error"] = "status: {0}, content: {1}" \ .format(response['status'], content) reason = "unknown" if "error" in json_parsed: reason = json_parsed["error"] if ("accesskey" in params.lower()) or ( "secretaccesskey" in params.lower() ) or ("password" in params.lower()) or ("secretkey" in params.lower()): message = '{0} {1} body: {2} headers: {3} ' \ 'error: {4} reason: {5} {6} {7}'.\ format(method, api, "Body is being redacted because it contains sensitive info", headers, response['status'], reason, content.rstrip('\n'), RestConnection.get_auth(headers)) else: message = '{0} {1} body: {2} headers: {3} ' \ 'error: {4} reason: {5} {6} {7}'.\ format(method, api, params, headers, response['status'], reason, content.rstrip('\n'), RestConnection.get_auth(headers)) self.log.error(message) self.log.debug(''.join(traceback.format_stack())) return False, content, response except socket.error as e: self.log.error("Socket error while connecting to {0}. " "Error {1}".format(api, e)) if time.time() > end_time: raise ServerUnavailableException(ip=self.ip) except httplib2.ServerNotFoundError as e: self.log.error("ServerNotFoundError while connecting to {0}. " "Error {1}".format(api, e)) if time.time() > end_time: raise ServerUnavailableException(ip=self.ip) sleep(3, log_type="infra")
def _reload_node_certificate(self, host): rest = RestConnection(host) api = rest.baseUrl + "node/controller/reloadCertificate" http = httplib2.Http() status, content = http.request(api, 'POST', headers=self._create_rest_headers( 'Administrator', 'password')) #status, content, header = rest._http_request(api, 'POST') return status, content
def analytics_tool(self, query, port=8095, timeout=650, query_params={}, is_prepared=False, named_prepare=None, verbose = True, encoded_plan=None, servers=None): key = 'prepared' if is_prepared else 'statement' headers = None content="" prepared = json.dumps(query) if is_prepared: if named_prepare and encoded_plan: http = httplib2.Http() if len(servers)>1: url = "http://%s:%s/query/service" % (servers[1].ip, port) else: url = "http://%s:%s/query/service" % (self.ip, port) headers = {'Content-type': 'application/json'} body = {'prepared': named_prepare, 'encoded_plan':encoded_plan} response, content = http.request( url, 'POST', headers=headers, body=json.dumps(body)) return eval(content) elif named_prepare and not encoded_plan: params = 'prepared=' + urllib.quote(prepared, '~()') params = 'prepared="%s"'% named_prepare else: prepared = json.dumps(query) prepared = str(prepared.encode('utf-8')) params = 'prepared=' + urllib.quote(prepared, '~()') if 'creds' in query_params and query_params['creds']: headers = self._create_headers_with_auth( query_params['creds'][0]['user'].encode('utf-8'), query_params['creds'][0]['pass'].encode('utf-8')) api = "%s/analytics/service?%s" % (self.cbas_base_url, params) self.log.info("%s" % api) else: params = {key : query} if 'creds' in query_params and query_params['creds']: headers = self._create_headers_with_auth( query_params['creds'][0]['user'].encode('utf-8'), query_params['creds'][0]['pass'].encode('utf-8')) del query_params['creds'] params.update(query_params) params = urllib.urlencode(params) if verbose: self.log.info('Query params: {0}'.format(params)) api = "%s/analytics/service?%s" % (self.cbas_base_url, params) status, content, header = self._http_request( api, 'POST', timeout=timeout, headers=headers) try: return json.loads(content) except ValueError: return content
def post_range_api_metrics(self, bucket_name): params = self._get_ui_params(bucket_name) authorization = base64.encodestring('%s:%s' % (self.username, self.password)) api = self.base_url + '/pools/default/stats/range/' headers = { 'Content-Type': 'text/plain', 'Authorization': 'Basic %s' % authorization } # We are using it because there are lot of code in _http_request which works only when output is json # In this case response in list, apart from that there is few more checks like we do for redaction # that doesnt work when the body is list # TODO : fix _http_request to work with lists(both body and repsonse) response, content = httplib2.Http(timeout=120).request( api, 'POST', params, headers) if not response['status']: raise Exception(content) return json.loads(content)
def get_region(): """Try to identify public hostname and return corresponding EC2 region. In case of socket exception (it may happen when client is local VM) use VM hostname. Reference: http://bit.ly/instancedata """ try: uri = 'http://169.254.169.254/latest/meta-data/public-hostname' http = httplib2.Http(timeout=5) response, hostname = http.request(uri) except (socket.timeout, socket.error): hostname = socket.gethostname() if 'west' in hostname: return 'west' else: return 'east'
def _http_request(self, api, method='GET', params='', headers=None, timeout=120): if not headers: headers = self._create_headers() while True: try: response, content = httplib2.Http(timeout=timeout).request(api, method, params, headers) if response['status'] in ['200', '201', '202']: return True, content, response else: try: json_parsed = json.loads(content) except ValueError as e: json_parsed = {} json_parsed["error"] = "status: {0}, content: {1}".format(response['status'], content) reason = "unknown" if "error" in json_parsed: reason = json_parsed["error"] message = '{0} {1} body: {2} headers: {3} error: {4} reason: {5} {6} {7}'. \ format(method, api, params, headers, response['status'], reason, content.rstrip('\n'), self._get_auth(headers)) print(message) return False, content, response except: print('ERROR..............')