如何在 python api 软层 create_instance 调用中指定 private_vlan

How do I specify a private_vlan in a python api softlayer create_instance call

我正在尝试使用 python api 指定私有和 public 我已经在使用但调用失败的 vlan 来订购 vsi。

>>> client = SoftLayer.Client(username=SL_USERNAME, api_key=SL_API_KEY,endpoint_url=SoftLayer.API_PUBLIC_ENDPOINT)
>>> mgr = SoftLayer.VSManager(client)
>>> vsi
{'dedicated': False, 'disks': ['100', '25'], 'hourly': True, 'domain':'vmonic.local', 'private': False, 'cpus': 1, 'private_vlan': 123, 'public_vlan':1234, 'datacenter': 'sjc03', 'ssh_keys': ['12345', '23456'], 'hostname':'ansible-server', 'os_code': 'CENTOS_LATEST', 'local_disk': True, 'memory': 1024}
>>> mgr.create_instance(**vsi)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site- packages/SoftLayer/managers/vs.py",line 534, in
create_instance
inst = self.guest.createObject(self._generate_create_dict(**kwargs))
File "/usr/local/lib/python2.7/site-packages/SoftLayer/API.py", line 373, in
call_handler
return self(name, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/SoftLayer/API.py", line 341, in call
return self.client.call(self.name, name, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/SoftLayer/API.py", line 237, in call
return self.transport(request)
File "/usr/local/lib/python2.7/site-packages/SoftLayer/transports.py",  line 187, in
__call__
raise _ex(ex.faultCode, ex.faultString)
SoftLayer.exceptions.SoftLayerAPIError:
SoftLayerAPIError(SoftLayer_Exception_Public): Could not obtain network VLAN with id
#123.

vlan 和 ssh 密钥 ID 已从上面粘贴的输出中的真实值中清除。

我可以使用 SL 门户创建一个 vsi,指定在 api 调用中失败的相同 vlan。我需要在 python api 中做什么才能完成这项工作?

我验证了你的模板工作正常,public和私有vlan设置成功,根据你的异常,vlan“123”不存在,需要指定public 和私有 vlan 中的网络 vlan 标识符。

请仔细检查“123”和“1234”是否为网络 vlan 标识符

以下脚本将有助于获取特定数据中心(例如 sjc03)的 vlan

"""
List Vlans for specific datacenter
"""
import SoftLayer

# Your SoftLayer API username and key.
USERNAME = 'set me'
API_KEY = 'set me'

# Declare the API client
client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)
mgr = SoftLayer.NetworkManager(client)
# Define Datacenter name
dc = 'sjc03'

try:
    result = mgr.list_vlans(datacenter=dc)
    print(result)
except SoftLayer.SoftLayerAPIError as e:
    print("Error. "
          % (e.faultCode, e.faultString))

Updated

"""
Get Vlan from vlanNumber
"""
import SoftLayer

# Your SoftLayer API username and key.
USERNAME = 'set me'
API_KEY = 'set me'

# Declare the API client
client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)
mgr = SoftLayer.NetworkManager(client)

# Define Vlan number
vlanNumber = 966

try:
    result = mgr.list_vlans(vlan_number=vlanNumber)
    print(result)
except SoftLayer.SoftLayerAPIError as e:
    print("Error. "
          % (e.faultCode, e.faultString))