New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add compute engine metadata client #11
Conversation
|
||
def update_query(url, params): | ||
"""Updates a URL's query parameters | ||
Replaces any current values if they are already present in the URL. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
# Parse the query string. | ||
query_params = urllib.parse.parse_qs(parts.query) | ||
# Update the query parameters with the new parameters. | ||
query_params.update(params) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
query_params = { | ||
key: value for key, value | ||
in six.iteritems(query_params) | ||
if value is not None} |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
# Re-encoded the query string. | ||
new_query = urllib.parse.urlencode(query_params, doseq=True) | ||
# Unsplit the url. | ||
new_parts = parts[:4] + (new_query,) + parts[5:] |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
metadata_flavor == _METADATA_FLAVOR_VALUE) | ||
|
||
except exceptions.TransportError: | ||
# logger.info('Timeout attempting to reach GCE metadata service.') |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
if response.status == http_client.OK: | ||
content = _helpers.from_bytes(response.data) | ||
if response.headers['content-type'] == 'application/json': | ||
return json.loads(content) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
method='GET', | ||
url=_metadata._METADATA_ROOT + PATH, | ||
headers=_metadata._METADATA_HEADERS) | ||
assert result['foo'] == 'bar' |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
headers=_metadata._METADATA_HEADERS) | ||
|
||
|
||
@mock.patch('google.auth._helpers.utcnow', return_value=datetime.datetime.min) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
url=_metadata._METADATA_ROOT + PATH + '/token', | ||
headers=_metadata._METADATA_HEADERS) | ||
assert token == 'token' | ||
assert expiry == utcnow() + datetime.timedelta(seconds=500) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
url=_metadata._METADATA_ROOT + PATH + '/?recursive=True', | ||
headers=_metadata._METADATA_HEADERS) | ||
|
||
assert info['foo'] == 'bar' |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
good-names = i, j, k, ex, Run, _, | ||
fh, | ||
fh, foo, bar |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
||
|
||
def test_get_success_json(mock_request): | ||
foo, bar = 'foo', 'bar' |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
http://example.com?a=2 | ||
>>> update_query(url, {'b': '3'}) | ||
http://example.com?a=1&b=3 | ||
>> update_query(url, {'a': None, 'b': '3'}) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
/cc @elibixby (original author)