Skip to content
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

Adding metadata sample #270

Merged
merged 1 commit into from
Apr 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions compute/metadata/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Compute Engine Metadata Samples

These samples demonstrate interacting with the Compute Engine metadata service. These samples must be run on Compute Engine.

<!-- auto-doc-link -->
<!-- end-auto-doc-link -->
77 changes: 77 additions & 0 deletions compute/metadata/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env python

# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Example of using the Compute Engine API to watch for maintenance notices.

For more information, see the README.md under /compute.
"""

# [START all]

import time

import requests


METADATA_URL = "http://metadata.google.internal/computeMetadata/v1/"
METADATA_HEADERS = {'Metadata-Flavor': 'Google'}


def wait_for_maintenance(callback):
url = METADATA_URL + 'instance/maintenance-event'
last_in_maintenance = False
# [START hanging_get]
last_etag = 0

while True:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably missing something...looks like this code should terminate when status is false but it doesn't look like logic exists to do so?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It never terminates. It constantly watches for migration events and calls the callback. The callback can decide to terminate or whatever.

r = requests.get(
url,
params={'last_etag': last_etag},
headers=METADATA_HEADERS)

# During maintenance the service can return a 503, so these should
# be retried.
if r.status_code == 503:
time.sleep(1)
continue
# [END hanging_get]

last_etag = r.headers['etag']

if r.text == 'MIGRATE_ON_HOST_MAINTENANCE':
in_maintenance = True
else:
in_maintenance = False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in_maintenance = (r.text == 'MIGRATE_ON_HOST_MAINTENANCE')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh, too clever. This a bit more clear.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I disagree, it's not nested in any way, it's not clever as much as what I consider to be standard Pythonic syntax. BUT /bikeshed, don't want to block code purple work etc so whatever.


if in_maintenance != last_in_maintenance:
last_in_maintenance = in_maintenance
callback(in_maintenance)


def maintenance_callback(status):
if status:
print('Undergoing host maintenance')
else:
print('Finished host maintenance')


def main():
wait_for_maintenance(maintenance_callback)


if __name__ == '__main__':
main()
# [END all]
47 changes: 47 additions & 0 deletions compute/metadata/main_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2016, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import main
import mock


@mock.patch('main.requests')
def test_wait_for_maintenance(requests_mock):
# Response 1 is a host maintenance event.
response1_mock = mock.Mock()
response1_mock.status_code = 200
response1_mock.text = 'MIGRATE_ON_HOST_MAINTENANCE'
response1_mock.headers = {'etag': 1}
# Response 2 is the end of the event.
response2_mock = mock.Mock()
response2_mock.status_code = 200
response2_mock.text = 'NONE'
response2_mock.headers = {'etag': 2}
# Response 3 is a 503
response3_mock = mock.Mock()
response3_mock.status_code = 503

requests_mock.get.side_effect = [
response1_mock, response2_mock, response3_mock, response2_mock,
StopIteration()]

callback_mock = mock.Mock()

try:
main.wait_for_maintenance(callback_mock)
except StopIteration:
pass

assert callback_mock.call_count == 2
assert callback_mock.call_args_list[0][0] == (True,)
assert callback_mock.call_args_list[1][0] == (False,)
1 change: 1 addition & 0 deletions compute/metadata/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests==2.9.1