def _get_gce_metadata(path=''): try: url = 'http://metadata/computeMetadata/v1/' + path.lstrip('/') headers = {'Metadata-Flavor': 'Google'} response = get_response_object(url, headers=headers) return response.status, '', response.body except Exception as e: return -1, str(e), None
def get_timestamp(self): if not self._timedelta: url = 'https://%s/%s/auth/time' % (API_HOST, API_ROOT) response = get_response_object(url=url, method='GET', headers={}) if not response or not response.body: raise Exception('Failed to get current time from RunAbove API') timestamp = int(response.body) self._timedelta = timestamp - int(time.time()) return int(time.time()) + self._timedelta
def get_timestamp(self): if not self._timedelta: url = 'https://%s%s/auth/time' % (API_HOST, API_ROOT) response = get_response_object(url=url, method='GET', headers={}) if not response or not response.body: raise Exception('Failed to get current time from Ovh API') timestamp = int(response.body) self._timedelta = timestamp - int(time.time()) return int(time.time()) + self._timedelta
def get_timestamp(self): if not self._timedelta: url = "https://%s%s/auth/time" % (self.host, API_ROOT) response = get_response_object(url=url, method="GET", headers={}) if not response or not response.body: raise Exception("Failed to get current time from Ovh API") timestamp = int(response.body) self._timedelta = timestamp - int(time.time()) return int(time.time()) + self._timedelta
def _get_gce_metadata(path="", retry_failed: Optional[bool] = None): try: url = "http://metadata/computeMetadata/v1/" + path.lstrip("/") headers = {"Metadata-Flavor": "Google"} response = get_response_object(url, headers=headers, retry_failed=retry_failed) return response.status, "", response.body except Exception as e: return -1, str(e), None
def download_pricing_file(file_url=DEFAULT_FILE_URL_S3_BUCKET, file_path=CUSTOM_PRICING_FILE_PATH): # type: (str, str) -> None """ Download pricing file from the file_url and save it to file_path. :type file_url: ``str`` :param file_url: URL pointing to the pricing file. :type file_path: ``str`` :param file_path: Path where a download pricing file will be saved. """ from libcloud.utils.connection import get_response_object dir_name = os.path.dirname(file_path) if not os.path.exists(dir_name): # Verify a valid path is provided msg = ('Can\'t write to %s, directory %s, doesn\'t exist' % (file_path, dir_name)) raise ValueError(msg) if os.path.exists(file_path) and os.path.isdir(file_path): msg = ('Can\'t write to %s file path because it\'s a' ' directory' % (file_path)) raise ValueError(msg) response = get_response_object(file_url) body = response.body # Verify pricing file is valid try: data = json.loads(body) except JSONDecodeError: msg = 'Provided URL doesn\'t contain valid pricing data' raise Exception(msg) # pylint: disable=maybe-no-member if not data.get('updated', None): msg = 'Provided URL doesn\'t contain valid pricing data' raise Exception(msg) # No need to stream it since file is small with open(file_path, 'w') as file_handle: file_handle.write(body)
def download_pricing_file(file_url=DEFAULT_FILE_URL, file_path=CUSTOM_PRICING_FILE_PATH): """ Download pricing file from the file_url and save it to file_path. :type file_url: ``str`` :param file_url: URL pointing to the pricing file. :type file_path: ``str`` :param file_path: Path where a download pricing file will be saved. """ dir_name = os.path.dirname(file_path) if not os.path.exists(dir_name): # Verify a valid path is provided msg = ('Can\'t write to %s, directory %s, doesn\'t exist' % (file_path, dir_name)) raise ValueError(msg) if os.path.exists(file_path) and os.path.isdir(file_path): msg = ('Can\'t write to %s file path because it\'s a' ' directory' % (file_path)) raise ValueError(msg) response = get_response_object(file_url) body = response.body # Verify pricing file is valid try: data = json.loads(body) except JSONDecodeError: msg = 'Provided URL doesn\'t contain valid pricing data' raise Exception(msg) # pylint: disable=maybe-no-member if not data.get('updated', None): msg = 'Provided URL doesn\'t contain valid pricing data' raise Exception(msg) # No need to stream it since file is small with open(file_path, 'w') as file_handle: file_handle.write(body)
def test_get_response_object(): with requests_mock.mock() as m: m.get('http://test.com/test', text='data') response = get_response_object('http://test.com/test') assert response.body == 'data'