Search code examples
ibm-cloud-infrastructure

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


I am trying to order a vsi using the python api specifying private and public vlans that I am already using but the call fails.

>>> 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.

The vlan and ssh key ids are sanitized from the true values in the pasted output above.

I can create a vsi using the SL portal specifying the same vlans that fail in the api call. What do I need to do in the python api to make this work?


Solution

  • I verified that your template is working fine, The public and private vlans were set successfully, According to the exception that you have, the vlan "123" doesn't exist, It is necessary specify the network vlan identifiers in public and private vlans.

    Please, double check that "123" and "1234" are network vlan identifiers.

    The following script will help to get vlans for a specific datacenter (e.g. sjc03)

    """
    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))