Cannot assign instance to a given network from Python novaclient API

Asked by aditya neelkanth

Hi,
I have 2 networks and when I create an instance, it attaches itself automatically to both the networks by default.
I want to mainly disable this and attach the instance to a network of my choice specified by the net-id
I am writing Python novaclient API calls for this:

import os
import time
import novaclient.v1_1.client as nvclient
from credentials import get_nova_creds
creds=get_nova_creds()
nova=nvclient.Client(**creds)
image=nova.images.find(name="Ubuntu 12.10 cloudimg amd64")
flavor=nova.flavors.find(name="m1.tiny")
#network=nova.networks.find(label="net_left")

instance=nova.servers.create(name="samplehost", image=image, flavor=flavor,nic= '9883ec93-4589-4fa2-825d-7a9a8215fd07') //9883ec93-4589-4fa2-825d-7a9a8215fd07 is the ID of a network named 'net_left'
status=instance.status

print "Status: %s" % status

My problem is in the nova.servers.create command where I am not able to specify the correct arguments for the net-id.

Could someone help me on by specifying the correct way to give the 'net-id' argument

Thanks

Question information

Language:
English Edit question
Status:
Solved
For:
OpenStack Compute (nova) Edit question
Assignee:
No assignee Edit question
Solved by:
ZhiQiang Fan
Solved:
Last query:
Last reply:
Revision history for this message
aditya neelkanth (aditya-neel09) said :
#1
Revision history for this message
Best ZhiQiang Fan (aji-zqfan) said :
#2

i think the common way to solve such problem is:

1. find documents in docs.openstack.org
    for i.e. http://docs.openstack.org/api/openstack-compute/2/content/NetworksSection.html , it may not be right, but can provide a general knowlege, like it said:"The parameter fixed_ip is used only when network uuid is specified; also, when port is specified, network uuid and fixed_ip are properties of the port and are ignored. Omit fixed_ip and (network) uuid to avoid validation errors."
2. use cli help,
    for this case: nova help create, it can give you a direct param help message
3. read the source code, code is power
    for i.e. in novaclient/base.py:

384 if nics is not None:
385 # NOTE(tr3buchet): nics can be an empty list
386 all_net_data = []
387 for nic_info in nics:
388 net_data = {}
389 # if value is empty string, do not send value in body
390 if nic_info.get('net-id'):
391 net_data['uuid'] = nic_info['net-id']
392 if nic_info.get('v4-fixed-ip'):
393 net_data['fixed_ip'] = nic_info['v4-fixed-ip']
394 if nic_info.get('port-id'):
395 net_data['port'] = nic_info['port-id']
396 all_net_data.append(net_data)
397 body['server']['networks'] = all_net_data

so, nics is a list of dict, each may have key of net-id, v4-fixed-ip, port-id, in your case, you can use nova.servers.create(name="samplehost", image=image, flavor=flavor,nics=[{'net-id': '9883ec93-4589-4fa2-825d-7a9a8215fd07'}])

it may not be right, because it depends on which version of novaclient you have installed. you can check and fix it

good luck

Revision history for this message
ZhiQiang Fan (aji-zqfan) said :
#3

oh, sorry, novaclient use boot instead of create, so the nova help command is `nova help boot`

Revision history for this message
aditya neelkanth (aditya-neel09) said :
#4

That was exactly what i was looking for....awesome....thanks a lot

Revision history for this message
aditya neelkanth (aditya-neel09) said :
#5

Thanks ZhiQiang Fan, that solved my question.

Revision history for this message
ZhiQiang Fan (aji-zqfan) said :
#6

you're welcome

ps, you can subscribe the mailing list:
1. http://www.openstack.org/community/
2. http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack
to get more help, because the anwsers.launchpad.net is not so actvie as mailing list

and/or you can join irc channel to get realtime help (if someone interests on the question):
1. https://wiki.openstack.org/wiki/IRC