Microsoft Azure Traffic Manager Management Client Library for Python
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Geographic hierarchy management for geographic-based traffic routing including region codes and hierarchical geographic mappings. This enables routing users to the most appropriate endpoint based on their geographic location.
Retrieves the default geographic hierarchy used by Traffic Manager for geographic traffic routing, containing all available region codes and their hierarchical relationships.
def get_default() -> TrafficManagerGeographicHierarchy:
"""
Gets the default geographic hierarchy for Traffic Manager.
Returns:
TrafficManagerGeographicHierarchy: Complete geographic hierarchy structure
"""Usage Example:
# Get the geographic hierarchy
geo_hierarchy = client.geographic_hierarchies.get_default()
# Explore the hierarchy structure
def print_regions(region, indent=0):
prefix = " " * indent
print(f"{prefix}{region.name} ({region.code})")
if region.regions:
for child_region in region.regions:
print_regions(child_region, indent + 1)
# Print the complete hierarchy
root_region = geo_hierarchy.geographic_hierarchy
print("Geographic Hierarchy:")
print_regions(root_region)
# Find specific region codes
def find_region_codes(region, search_name):
codes = []
if search_name.lower() in region.name.lower():
codes.append(region.code)
if region.regions:
for child in region.regions:
codes.extend(find_region_codes(child, search_name))
return codes
# Find all US-related regions
us_codes = find_region_codes(root_region, "United States")
print(f"US region codes: {us_codes}")
# Find European regions
eu_codes = find_region_codes(root_region, "Europe")
print(f"European region codes: {eu_codes}")class TrafficManagerGeographicHierarchy:
"""Traffic Manager geographic hierarchy."""
geographic_hierarchy: Region # Root region containing all geographic mappings
class Region:
"""Geographic region in the hierarchy."""
code: str # Region code (e.g., "US", "FR", "WORLD-EUR")
name: str # Human-readable region name
regions: List[Region] # Child regions in the hierarchy# Major continental regions
WORLD_REGIONS = [
"WORLD-AF", # Africa
"WORLD-AS", # Asia
"WORLD-EU", # Europe
"WORLD-NA", # North America
"WORLD-OC", # Oceania
"WORLD-SA" # South America
]# Common country codes (ISO 3166-1 alpha-2)
COUNTRY_CODES = [
"US", # United States
"CA", # Canada
"MX", # Mexico
"BR", # Brazil
"GB", # United Kingdom
"FR", # France
"DE", # Germany
"IT", # Italy
"ES", # Spain
"NL", # Netherlands
"JP", # Japan
"CN", # China
"IN", # India
"AU", # Australia
"KR", # South Korea
"SG" # Singapore
]# United States regional codes
US_REGIONS = [
"US-AK", # Alaska
"US-AL", # Alabama
"US-AR", # Arkansas
"US-AZ", # Arizona
"US-CA", # California
"US-CO", # Colorado
"US-CT", # Connecticut
"US-DC", # District of Columbia
"US-DE", # Delaware
"US-FL", # Florida
"US-GA", # Georgia
"US-HI", # Hawaii
"US-IA", # Iowa
"US-ID", # Idaho
"US-IL", # Illinois
"US-IN", # Indiana
"US-KS", # Kansas
"US-KY", # Kentucky
"US-LA", # Louisiana
"US-MA", # Massachusetts
"US-MD", # Maryland
"US-ME", # Maine
"US-MI", # Michigan
"US-MN", # Minnesota
"US-MO", # Missouri
"US-MS", # Mississippi
"US-MT", # Montana
"US-NC", # North Carolina
"US-ND", # North Dakota
"US-NE", # Nebraska
"US-NH", # New Hampshire
"US-NJ", # New Jersey
"US-NM", # New Mexico
"US-NV", # Nevada
"US-NY", # New York
"US-OH", # Ohio
"US-OK", # Oklahoma
"US-OR", # Oregon
"US-PA", # Pennsylvania
"US-RI", # Rhode Island
"US-SC", # South Carolina
"US-SD", # South Dakota
"US-TN", # Tennessee
"US-TX", # Texas
"US-UT", # Utah
"US-VA", # Virginia
"US-VT", # Vermont
"US-WA", # Washington
"US-WI", # Wisconsin
"US-WV", # West Virginia
"US-WY" # Wyoming
]from azure.mgmt.trafficmanager.models import Profile, TrafficRoutingMethod
# Create profile with geographic routing
profile = Profile(
location="global",
profile_status="Enabled",
traffic_routing_method=TrafficRoutingMethod.GEOGRAPHIC,
dns_config={
"relative_name": "geo-app",
"ttl": 60
},
monitor_config={
"protocol": "HTTPS",
"port": 443,
"path": "/health"
}
)
geo_profile = client.profiles.create_or_update(
resource_group_name="my-rg",
profile_name="geo-routing-profile",
parameters=profile
)from azure.mgmt.trafficmanager.models import Endpoint
# North American endpoint
na_endpoint = Endpoint(
target="na-service.example.com",
endpoint_status="Enabled",
geo_mapping=[
"US", # United States
"CA", # Canada
"MX" # Mexico
]
)
# European endpoint
eu_endpoint = Endpoint(
target="eu-service.example.com",
endpoint_status="Enabled",
geo_mapping=[
"WORLD-EU" # All of Europe (includes all European countries)
]
)
# Specific European countries endpoint
eu_specific_endpoint = Endpoint(
target="eu-premium.example.com",
endpoint_status="Enabled",
geo_mapping=[
"GB", # United Kingdom
"FR", # France
"DE", # Germany
"NL" # Netherlands
]
)
# Asia-Pacific endpoint
apac_endpoint = Endpoint(
target="apac-service.example.com",
endpoint_status="Enabled",
geo_mapping=[
"JP", # Japan
"AU", # Australia
"SG", # Singapore
"KR", # South Korea
"IN" # India
]
)
# Default/fallback endpoint (no geo mapping = catches all unmapped traffic)
default_endpoint = Endpoint(
target="global-service.example.com",
endpoint_status="Enabled"
# No geo_mapping = default for unmapped regions
)
# Create endpoints
client.endpoints.create_or_update("my-rg", "geo-profile", "ExternalEndpoints", "north-america", na_endpoint)
client.endpoints.create_or_update("my-rg", "geo-profile", "ExternalEndpoints", "europe", eu_endpoint)
client.endpoints.create_or_update("my-rg", "geo-profile", "ExternalEndpoints", "asia-pacific", apac_endpoint)
client.endpoints.create_or_update("my-rg", "geo-profile", "ExternalEndpoints", "default", default_endpoint)WORLD-*) and drill down to specific countries/states as needed# Route by major continents
endpoints = [
{"name": "north-america", "geo_mapping": ["WORLD-NA"]},
{"name": "europe", "geo_mapping": ["WORLD-EU"]},
{"name": "asia", "geo_mapping": ["WORLD-AS"]},
{"name": "default", "geo_mapping": []} # Fallback
]# Separate endpoints for data sovereignty
endpoints = [
{"name": "eu-gdpr", "geo_mapping": ["WORLD-EU"]}, # EU for GDPR compliance
{"name": "us-cloud", "geo_mapping": ["US", "CA"]}, # North America
{"name": "apac", "geo_mapping": ["JP", "AU", "SG", "KR"]}, # Regulated APAC countries
{"name": "rest-of-world", "geo_mapping": []} # Everything else
]# Optimize for specific high-traffic regions
endpoints = [
{"name": "us-east", "geo_mapping": ["US-NY", "US-MA", "US-VA", "US-DC", "US-NJ"]},
{"name": "us-west", "geo_mapping": ["US-CA", "US-WA", "US-OR", "US-NV"]},
{"name": "uk-ireland", "geo_mapping": ["GB", "IE"]},
{"name": "central-eu", "geo_mapping": ["DE", "FR", "NL", "BE"]},
{"name": "nordic", "geo_mapping": ["SE", "NO", "DK", "FI"]},
{"name": "global", "geo_mapping": []} # Default
]# Use nested profiles to combine geographic with other routing methods
# Parent profile: Geographic routing
# Child profiles: Performance or weighted routing within each region
parent_profile = Profile(
traffic_routing_method="Geographic",
# ... other config
)
# Child profile for North America with performance routing
na_child_profile = Profile(
traffic_routing_method="Performance",
# ... other config
)
# Nested endpoint pointing to NA child profile
na_nested_endpoint = Endpoint(
target_resource_id="/subscriptions/.../trafficManagerProfiles/na-performance-profile",
endpoint_status="Enabled",
geo_mapping=["US", "CA", "MX"]
)def discover_geographic_codes():
"""Helper function to explore available geographic codes."""
hierarchy = client.geographic_hierarchies.get_default()
def extract_codes(region, codes_dict, parent_path=""):
path = f"{parent_path}/{region.name}" if parent_path else region.name
codes_dict[region.code] = {
"name": region.name,
"path": path,
"parent": parent_path
}
if region.regions:
for child in region.regions:
extract_codes(child, codes_dict, path)
all_codes = {}
extract_codes(hierarchy.geographic_hierarchy, all_codes)
return all_codes
# Get all available codes
geo_codes = discover_geographic_codes()
# Find codes for a specific region
europe_codes = {k: v for k, v in geo_codes.items() if "Europe" in v["path"]}
us_codes = {k: v for k, v in geo_codes.items() if "United States" in v["path"]}
print(f"European region codes: {list(europe_codes.keys())}")
print(f"US region codes: {list(us_codes.keys())}")Install with Tessl CLI
npx tessl i tessl/pypi-azure-mgmt-trafficmanager