You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于通过GCP Catalog API识别Compute Engine实例机型的技术咨询

Answer

Great question—this is a common pain point when working with the GCP Billing Catalog API, since instance size details (like vCPU count or exact machine type names) aren’t exposed as standalone filterable fields. While parsing the description field is one way, there are more reliable workarounds that reduce the need for full-text parsing:

1. Fetching SKUs for a specified core count

The best approach combines two GCP APIs to narrow down results before any description parsing:

  • Step 1: Use the Compute Engine Machine Types API to get all machine types in your target region that match your desired core count. For example, to find all N1 instances with 8 vCPUs, query the API for machine types where guestCpus = 8 and name starts with n1-. This gives you exact machine type names (like n1-highcpu-8) and their corresponding families.
  • Step 2: Filter the Catalog API with targeted parameters
    Use the category.resourceGroup (which maps to the instance family, e.g., N1Highcpu for n1-highcpu- machines) plus region, usage type (OnDemand/Spot), and a partial description match for the core count. This drastically reduces the number of SKUs you need to process.

Example filter for the Catalog API:

serviceDisplayName:"Compute Engine" 
AND category.resourceGroup:"N1Highcpu" 
AND serviceRegions:"europe-west4" 
AND category.usageType:"OnDemand" 
AND description:contains("8 VCPU")

2. Fetching SKUs for a specific instance name (e.g., N1Standard 16)

Again, there’s no direct field for the exact instance name, but you can map the instance name to its family and core count to build a precise filter:

  • Map the instance name to its family: N1Standard 16 corresponds to the N1Standard resourceGroup, with 16 vCPUs.
  • Use the same Catalog API filter pattern as above, replacing the resourceGroup and core count in the description match.

For example, to get SKUs for n1-standard-16 in europe-west4 (OnDemand):

serviceDisplayName:"Compute Engine" 
AND category.resourceGroup:"N1Standard" 
AND serviceRegions:"europe-west4" 
AND category.usageType:"OnDemand" 
AND description:contains("16 VCPU")

Bonus: Sample Python code

Here’s how you’d implement this with the GCP Python client library:

from google.cloud import billing_v1

def get_instance_skus(instance_family, core_count, region, usage_type="OnDemand"):
    client = billing_v1.CloudCatalogClient()
    # Compute Engine service ID is fixed as 6F81-5844-456A
    parent = "services/6F81-5844-456A"
    
    filter_str = (
        f'serviceDisplayName:"Compute Engine" '
        f'AND category.resourceGroup:"{instance_family}" '
        f'AND serviceRegions:"{region}" '
        f'AND category.usageType:"{usage_type}" '
        f'AND description:contains("{core_count} VCPU")'
    )
    
    request = billing_v1.ListSkusRequest(parent=parent, filter=filter_str)
    skus = list(client.list_skus(request=request))
    
    return [(sku.sku_id, sku.description) for sku in skus]

# Usage example: Get OnDemand SKUs for N1Highcpu 8 in europe-west4
skus = get_instance_skus("N1Highcpu", 8, "europe-west4")
for sku_id, desc in skus:
    print(f"SKU ID: {sku_id}, Description: {desc}")

Key Notes

  • The category.resourceGroup value is consistent for each instance family (e.g., N1Highmem for memory-optimized N1 instances, N2Standard for N2 standard instances).
  • Using partial description matches with the Catalog API’s filter is more efficient than parsing all SKUs, as filtering happens server-side.
  • If you need to map many instance types, consider caching results from the Machine Types API and Catalog API queries to avoid repeated calls.

内容的提问来源于stack exchange,提问作者Harsha Biyani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:07:16