Skip to content

Commit

Permalink
Merge branch 'master' into error_reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
waprin committed Apr 21, 2016
2 parents 786065d + 664f74a commit c900601
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 3 deletions.
9 changes: 6 additions & 3 deletions appengine/urlfetch/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
"""

import logging
# [START url-imports]
import urllib

# [START urllib2-imports]
import urllib2
# [END urllib2-imports]

# [START urlfetch-imports]
from google.appengine.api import urlfetch
# [END urlfetch-imports]
import webapp2
# [END url-imports]


class UrlLibFetchHandler(webapp2.RequestHandler):
Expand All @@ -46,7 +49,7 @@ class UrlFetchHandler(webapp2.RequestHandler):

def get(self):
# [START urlfetch-get]
url = "http://www.googleadsfasdf.com/"
url = "http://www.google.com/"
try:
result = urlfetch.fetch(url)
if result.status_code == 200:
Expand Down
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:
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

last_etag = r.headers['etag']
# [END hanging_get]

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

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

0 comments on commit c900601

Please sign in to comment.