Skip to content

Commit 6c38257

Browse files
Galabar001Jon Wayne Parrott
authored and
Jon Wayne Parrott
committed
Update compute metadata hanging get example to use "wait_for_change". (#333)
We need to include "wait_for_change" in order to perform a hanging get. In addition, we should deal with possible errors (like too many hanging gets). Finally, this change allows for any of the maintenance event supported by the metadata server.
1 parent 2aa4464 commit 6c38257

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

compute/metadata/main.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,44 +26,48 @@
2626
import requests
2727

2828

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

3232

3333
def wait_for_maintenance(callback):
3434
url = METADATA_URL + 'instance/maintenance-event'
35-
last_in_maintenance = False
35+
last_maintenance_event = None
3636
# [START hanging_get]
37-
last_etag = 0
37+
last_etag = '0'
3838

3939
while True:
4040
r = requests.get(
4141
url,
42-
params={'last_etag': last_etag},
42+
params={'last_etag': last_etag, 'wait_for_change': True},
4343
headers=METADATA_HEADERS)
4444

4545
# During maintenance the service can return a 503, so these should
4646
# be retried.
4747
if r.status_code == 503:
4848
time.sleep(1)
4949
continue
50+
r.raise_for_status()
5051

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

54-
if r.text == 'MIGRATE_ON_HOST_MAINTENANCE':
55-
in_maintenance = True
55+
if r.text == 'NONE':
56+
maintenance_event = None
5657
else:
57-
in_maintenance = False
58+
# Possible events:
59+
# MIGRATE_ON_HOST_MAINTENANCE: instance will be migrated
60+
# SHUTDOWN_ON_HOST_MAINTENANCE: instance will be shut down
61+
maintenance_event = r.text
5862

59-
if in_maintenance != last_in_maintenance:
60-
last_in_maintenance = in_maintenance
61-
callback(in_maintenance)
63+
if maintenance_event != last_maintenance_event:
64+
last_maintenance_event = maintenance_event
65+
callback(maintenance_event)
6266

6367

64-
def maintenance_callback(status):
65-
if status:
66-
print('Undergoing host maintenance')
68+
def maintenance_callback(event):
69+
if event:
70+
print('Undergoing host maintenance: {}'.format(event))
6771
else:
6872
print('Finished host maintenance')
6973

compute/metadata/main_test.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import main
1515
import mock
16+
import requests
1617

1718

1819
@mock.patch('main.requests')
@@ -31,6 +32,7 @@ def test_wait_for_maintenance(requests_mock):
3132
response3_mock = mock.Mock()
3233
response3_mock.status_code = 503
3334

35+
requests_mock.codes.ok = requests.codes.ok
3436
requests_mock.get.side_effect = [
3537
response1_mock, response2_mock, response3_mock, response2_mock,
3638
StopIteration()]
@@ -43,5 +45,6 @@ def test_wait_for_maintenance(requests_mock):
4345
pass
4446

4547
assert callback_mock.call_count == 2
46-
assert callback_mock.call_args_list[0][0] == (True,)
47-
assert callback_mock.call_args_list[1][0] == (False,)
48+
assert callback_mock.call_args_list[0][0] == (
49+
'MIGRATE_ON_HOST_MAINTENANCE',)
50+
assert callback_mock.call_args_list[1][0] == (None,)

0 commit comments

Comments
 (0)