File tree Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Original file line number Diff line number Diff line change 1
1
import os
2
- import json
3
2
import logging
4
3
5
4
logger = logging .getLogger (__name__ )
@@ -17,7 +16,17 @@ def _detect_region_of_azure_function():
17
16
18
17
19
18
def _detect_region_of_azure_vm (http_client ):
20
- url = "http://169.254.169.254/metadata/instance?api-version=2021-01-01"
19
+ url = (
20
+ "http://169.254.169.254/metadata/instance"
21
+
22
+ # Utilize the "route parameters" feature to obtain region as a string
23
+ # https://docs.microsoft.com/en-us/azure/virtual-machines/windows/instance-metadata-service?tabs=linux#route-parameters
24
+ "/compute/location?format=text"
25
+
26
+ # Location info is available since API version 2017-04-02
27
+ # https://docs.microsoft.com/en-us/azure/virtual-machines/windows/instance-metadata-service?tabs=linux#response-1
28
+ "&api-version=2021-01-01"
29
+ )
21
30
logger .info (
22
31
"Connecting to IMDS {}. "
23
32
"You may want to use a shorter timeout on your http_client" .format (url ))
@@ -29,5 +38,5 @@ def _detect_region_of_azure_vm(http_client):
29
38
"IMDS {} unavailable. Perhaps not running in Azure VM?" .format (url ))
30
39
return None
31
40
else :
32
- return json . loads ( resp .text )[ "compute" ][ "location" ]
41
+ return resp .text . strip ()
33
42
You can’t perform that action at this time.
0 commit comments