Ask Your Question
0

start a vm using python api

asked 2014-05-21 00:05:39 -0600

anonymous user

Anonymous

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']}])
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2014-09-20 18:05:55 -0600

igorm gravatar image

You may want to checkout SDK User Guide on the complete code samples for createing networks and routers using python-neutronclient SDK library.

edit flag offensive delete link more
0

answered 2014-09-20 20:34:53 -0600

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

Looking at the code in python-novaclient/nova/v1_1/servers.py, I think you may have your arguments wrong. It looks like the API is looking for a port-id key, much like you would pass on the command line. The actual code is:

            if nic_info.get('port-id'):
                net_data['port'] = nic_info['port-id']

So, your call might look like:

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

I've just tested that out locally and it seems to work.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Get to know Ask OpenStack

Resources for moderators

Question Tools

1 follower

Stats

Asked: 2014-05-21 00:05:39 -0600

Seen: 582 times

Last updated: Sep 20 '14