SoftLayer API:如何查询各数据中心全局负载均衡器带宽价格?
Hey there! Let's figure out why your current code is returning a $0 cost for LOAD_BALANCER_BANDWIDTH and how to get the actual per-GB pricing for your global load balancer across data centers.
Why you're seeing $0
The LOAD_BALANCER_BANDWIDTH item returned by SoftLayer_Product_Package.getItems(id=805) is a placeholder for metered resources, not a fixed-priced line item. Global load balancer bandwidth is typically billed based on actual outbound usage, so the upfront "fee" in the package item is $0—you only pay for what you use, and that usage-based pricing isn't stored in the base package items.
How to get the real per-GB cost
To pull the actual per-GB bandwidth rates for each data center, you need to query the pricing details specifically for that bandwidth item. Here's a modified code snippet that does this:
import SoftLayer # Initialize your client (use environment vars or fill in credentials directly) client = SoftLayer.create_client_from_env() # Step 1: Fetch package items and locate the bandwidth item's ID package_items = client["SoftLayer_Product_Package"].getItems(id=805) try: bandwidth_item = next(item for item in package_items if item['keyName'] == 'LOAD_BALANCER_BANDWIDTH') bandwidth_item_id = bandwidth_item['id'] except StopIteration: print("Could not find LOAD_BALANCER_BANDWIDTH item in package 805") exit(1) # Step 2: Get location-specific pricing for the bandwidth item location_prices = client["SoftLayer_Product_Pricing"].getLocationPrices(id=bandwidth_item_id) # Step 3: Print formatted per-GB costs per data center print("Global Load Balancer Bandwidth Pricing (per GB):") for price in location_prices: location_name = price['location']['name'] # Metered bandwidth uses either monthly recurring fee or hourly rate cost_per_gb = price['recurringFee'] if price['recurringFee'] else price['hourlyRecurringFee'] print(f"- {location_name}: ${cost_per_gb}")
A quick note on overages
If you still see $0 for base bandwidth pricing, it might mean your global load balancer plan includes a set amount of free bandwidth, and only overages are billed. In that case, scan the package items for an entry with a keyName like LOAD_BALANCER_BANDWIDTH_OVERAGE, then repeat the above steps using that item's ID to get overage rates.
Double-checking your existing setup
To confirm how bandwidth is billed for your specific load balancer, use this snippet to inspect your active billing items:
# Replace with your load balancer's unique ID load_balancer_id = YOUR_LOAD_BALANCER_ID billing_details = client["SoftLayer_Network_LoadBalancer_Global"].getBillingItem(id=load_balancer_id) print("Current billing details for your load balancer:") print(billing_details)
This will show you if bandwidth is included in your base plan or billed as a separate metered item.
内容的提问来源于stack exchange,提问作者YuZong Jiang

