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

SoftLayer API技术咨询:获取OS价格列表及Terraform适配问题

解答:Terraform + SoftLayer 操作系统编码与价格获取问题

我来一步步帮你解决这些SoftLayer和Terraform集成里的常见问题:


1. operatingSystemReferenceCode 是否可直接用于Terraform?

完全可以!你代码里拿到的flavor['template']['operatingSystemReferenceCode']就是Terraform SoftLayer provider中softlayer_virtual_guest资源里os_reference_code参数需要的精准值。比如UBUNTU_LATEST_64、CENTOS_7_64这类编码,直接填到Terraform配置里就能正确匹配对应的操作系统。

你之前在getItemPrices()里没找到这个编码,是因为getItemPrices()返回的是价格项本身,而操作系统的reference code是关联在模板(template)对象里的,所以你当前通过getCreateObjectOptions()的方式是正确的。


2. 如何获取与SoftLayer门户完全一致的OS列表?

你当前拿到的列表和门户不一致,大概率是因为接口返回了所有支持的OS(包括门户隐藏的过时版本、自定义镜像或特定授权OS)。要和门户对齐,需要做以下过滤:

对齐门户的过滤规则:

  • 只保留公开可用的OS:过滤isActive为true的项
  • 排除自定义/私有镜像:确保template的visibility为PUBLIC
  • 筛选纯操作系统镜像:通过item['categoryCode']过滤,操作系统的categoryCode一般是guest_os

修改后的代码示例:

import SoftLayer
client = SoftLayer.create_client_from_env()

# 获取虚拟机创建选项
create_options = client['Virtual_Guest'].getCreateObjectOptions()
os_list = []

for flavor in create_options['operatingSystems']:
    item = flavor['itemPrice']['item']
    template = flavor['template']
    # 应用过滤条件,匹配门户展示逻辑
    if (flavor['itemPrice']['isActive'] 
        and item['categoryCode'] == 'guest_os' 
        and template.get('visibility') == 'PUBLIC'):
        tmp_dict = {
            'description': item['description'],
            'recurringFee': flavor['itemPrice']['recurringFee'],
            'operatingSystemReferenceCode': template['operatingSystemReferenceCode']
        }
        os_list.append(tmp_dict)

# 按名称排序,和门户展示顺序对齐
os_list.sort(key=lambda x: x['description'])

这样处理后,得到的列表就会和SoftLayer门户上展示的操作系统完全一致,且编码可直接用于Terraform部署。


3. 不同数据中心/核心数对应的OS价格获取与计算

不同数据中心的价格差异

SoftLayer的OS价格分数据中心,你需要在获取价格时指定目标数据中心的locationId:

  1. 先获取目标数据中心的ID:
# 获取所有数据中心信息
datacenters = client['Location'].getDatacenters()
# 示例:获取达拉斯09数据中心的ID
dal09_id = [dc['id'] for dc in datacenters if dc['name'] == 'dal09'][0]
  1. 调用getItemPrices()时传入locationId筛选对应数据中心的价格:
# 替换为你要查询的OS对应的itemId
target_item_id = 12345 
datacenter_prices = client['SoftLayer_Product_Item'].getItemPrices(
    id=target_item_id,
    filter={"itemPrices": {"locationId": {"operation": dal09_id}}}
)

与核心数相关的价格计算

SoftLayer的OS价格是按vCPU核心数阶梯定价的(比如1-2核一个档位,3-4核另一个档位),要计算对应配置的价格:

  1. 确定目标虚拟机的核心数(比如4核)
  2. 找到价格项中capacityRestrictionMinimum和capacityRestrictionMaximum覆盖该核心数的条目
  3. 该条目的recurringFee就是对应核心数下的OS月费

示例代码片段:

target_core_count = 4  # 目标虚拟机核心数

for price in datacenter_prices:
    min_core = price.get('capacityRestrictionMinimum', 0)
    max_core = price.get('capacityRestrictionMaximum', float('inf'))
    if min_core <= target_core_count <= max_core:
        os_monthly_cost = price['recurringFee']
        print(f"4核虚拟机对应OS月费:{os_monthly_cost}")
        break

另外,Terraform部署时会自动根据你指定的cpu_count和os_reference_code匹配正确价格,无需手动计算;但如果需要提前做成本估算,上面的方法就适用。


内容的提问来源于stack exchange,提问作者YuZong Jiang

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 03:45:33