Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

start a vm using python api

I'm having some trouble to create a VM using python-novaclient api. I created two new ports on two nets using qc.create_port().

When i create the vm, I want to use the port ids I just created. The problem is that I don't know how to reference port id from the function nc.servers.create(). Could you shed some light? code below:

qc.format = 'json'

nc = nclient.Client(username=USER, project_id=TENANT, api_key=PASS, auth_url=AUTH_URL, service_type='compute')

nets = qc.list_networks()


int_net_id_1 = [net['id'] for net in nets['networks'] if net['name'] == 'test-net']
subnet_id_1 = [net['subnets'][0] for net in nets['networks'] if net['name'] == 'test-net']
int_net_id_2 = [net['id'] for net in nets['networks'] if net['name'] == 'test-net-2']
subnet_id_2 = [net['subnets'][0] for net in nets['networks'] if net['name'] == 'test-net-2']
ext_net_id = [net['id'] for net in nets['networks'] if net['name'] == 'ext-net']


port1 = qc.create_port({'port': {'network_id': int_net_id_1[0], 'name': 'web-net1', 'admin_state_up': True}})

port2 = qc.create_port({'port': {'network_id': int_net_id_2[0], 'name': 'web-net2', 'admin_state_up': True}})

img = [img for img in nc.images.list() if img.name == 'ubuntu-template'][0]

my_flavor = [flavor for flavor in nc.flavors.list() if flavor.name == 'm1.small'][0] 

nc.servers.create('test-server-1', img, my_flavor, nics = [{'port': port1['port']['id']}, {'port': port2['port']['id']}])