File tree Expand file tree Collapse file tree 3 files changed +38
-5
lines changed Expand file tree Collapse file tree 3 files changed +38
-5
lines changed Original file line number Diff line number Diff line change 32
32
33
33
_LOGGER = logging .getLogger (__name__ )
34
34
35
- _METADATA_ROOT = "http://{}/computeMetadata/v1/" .format (
36
- os .getenv (environment_vars .GCE_METADATA_ROOT , "metadata.google.internal" )
37
- )
35
+ # Environment variable GCE_METADATA_HOST is originally named
36
+ # GCE_METADATA_ROOT. For compatiblity reasons, here it checks
37
+ # the new variable first; if not set, the system falls back
38
+ # to the old variable.
39
+ _GCE_METADATA_HOST = os .getenv (environment_vars .GCE_METADATA_HOST , None )
40
+ if not _GCE_METADATA_HOST :
41
+ _GCE_METADATA_HOST = os .getenv (
42
+ environment_vars .GCE_METADATA_ROOT , "metadata.google.internal"
43
+ )
44
+ _METADATA_ROOT = "http://{}/computeMetadata/v1/" .format (_GCE_METADATA_HOST )
38
45
39
46
# This is used to ping the metadata server, it avoids the cost of a DNS
40
47
# lookup.
Original file line number Diff line number Diff line change 40
40
41
41
# These two variables allow for customization of the addresses used when
42
42
# contacting the GCE metadata service.
43
+ GCE_METADATA_HOST = "GCE_METADATA_HOST"
43
44
GCE_METADATA_ROOT = "GCE_METADATA_ROOT"
44
45
"""Environment variable providing an alternate hostname or host:port to be
45
- used for GCE metadata requests."""
46
+ used for GCE metadata requests.
47
+
48
+ This environment variable is originally named GCE_METADATA_ROOT. System will
49
+ check the new variable first; should there be no value present,
50
+ the system falls back to the old variable.
51
+ """
46
52
47
53
GCE_METADATA_IP = "GCE_METADATA_IP"
48
54
"""Environment variable providing an alternate ip:port to be used for ip-only
Original file line number Diff line number Diff line change @@ -155,7 +155,27 @@ def test_get_success_text():
155
155
assert result == data
156
156
157
157
158
- def test_get_success_custom_root ():
158
+ def test_get_success_custom_root_new_variable ():
159
+ request = make_request ("{}" , headers = {"content-type" : "application/json" })
160
+
161
+ fake_root = "another.metadata.service"
162
+ os .environ [environment_vars .GCE_METADATA_HOST ] = fake_root
163
+ reload_module (_metadata )
164
+
165
+ try :
166
+ _metadata .get (request , PATH )
167
+ finally :
168
+ del os .environ [environment_vars .GCE_METADATA_HOST ]
169
+ reload_module (_metadata )
170
+
171
+ request .assert_called_once_with (
172
+ method = "GET" ,
173
+ url = "http://{}/computeMetadata/v1/{}" .format (fake_root , PATH ),
174
+ headers = _metadata ._METADATA_HEADERS ,
175
+ )
176
+
177
+
178
+ def test_get_success_custom_root_old_variable ():
159
179
request = make_request ("{}" , headers = {"content-type" : "application/json" })
160
180
161
181
fake_root = "another.metadata.service"
You can’t perform that action at this time.
0 commit comments