Ask Your Question
1

Help in Rest API to create instance.

asked 2015-10-15 01:44:18 -0500

dthiyagu gravatar image

updated 2015-10-15 16:48:44 -0500

smaffulli gravatar image

Hi, I am new to Openstack and REST API automation. Could someone please let me know how to convert the below command to REST API? Thanks in Advance.

CLI command to create instance. I need equivalent in rest API.

nova boot --flavor m1.tiny --image cirros-0.3.4-x86_64-uec --nic net-id=531a7856-5d85-45c2-845a-4926cb7f1ed3 --security-group default  demo-instance1

I have written rest api for getting the token, flavor-list, image-list, net-list. I want to know, how to create instance using rest api.

Something like below format:

print
print
print "---------------List of NOVA API-v2 details ---------------"
headers = {
          'Content-Type'   :   'application/json',
          'Accept'         :   'application/json',
          'X-Auth-Token'   :    mytoken
           }

#### Build the URL
CMDpath="/v2"
APIport=NOVAport
url="http://"+hostIP+":"+APIport+CMDpath
print "URL:     ",url



#### Send the GET request
# Note that the second parameter which normally carries the body data
# is "None", making the request a "GET" instead of a "POST"
req = urllib2.Request(url,None, headers)
edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
2

answered 2015-10-15 12:18:09 -0500

Hi

Use --debug option to get know the REST API structure behind every CLI commands. Refer below one for nova instance create

nova --debug boot --flavor m1.tiny --image cirros --nic net-id=acf5f405-9b56-46c5-9a63-b5871f36f68a --security-group default,jp  testinstance

curl -g -i -X POST http://qaopenstack223.ccs.com:8774/v2... -H "User-Agent: python-novaclient" -H "Content-Type: application/json" -H "Accept: application/json" -H "X-Auth-Token: {SHA1}0eee5605668ba803e7f4f52a1140a8ae9a9c5a02" -d '{"server": {"name": "testinstance", "imageRef": "1a210c06-5c8a-4590-b741-57ac63af13a5", "flavorRef": "1", "max_count": 1, "min_count": 1, "networks": [{"uuid": "acf5f405-9b56-46c5-9a63-b5871f36f68a"}], "security_groups": [{"name": "default"}, {"name": "jp"}]}}'

Refer http://docs.python-requests.org/en/la... for python Request module

edit flag offensive delete link more

Comments

Thanks a lot Jayaprakash. Your solution helped me. After giving --debug in the command, i am able to get the REST json format and able to create the instances.

$ nova --debug boot --flavor m1.tiny --image cirros-0.3.4-x86_64-uec --nic net-id=02159b65-a4ad-4c9e-a087-1f3426a333ae --security-group d

dthiyagu gravatar imagedthiyagu ( 2015-10-15 20:04:36 -0500 )edit
1

answered 2016-04-20 07:06:18 -0500

dbaxps gravatar image

Attempting to address the next question regarding REST API

 I have noticed that all previous responded questions deal only with CirrOS instance silently ignoring including in body area "key_name" entry. It is absolutely clear that as far as I will try launch Ubuntu of Fedora VM skipping "key_name" will result disability log into VM via floating IP , what seems to be a kind "of best kept secret", which always hurt people been new to particular type of Openstack APIs .

See
Creating Server via REST API on RDO Mitaka

edit flag offensive delete link more
0

answered 2015-10-15 20:07:14 -0500

dthiyagu gravatar image

Thanks a lot Jayaprakash. Your solution helped me. After giving --debug in the command, i am able to get the REST json format and able to create the instances.

$ nova --debug boot --flavor m1.tiny --image cirros-0.3.4-x86_64-uec --nic net-id=02159b65-a4ad-4c9e-a087-1f3426a333ae --security-group d

Rest API Code: ~~~~~~~~~~~~~~~~

headers = { 'Content-Type' : 'application/json', 'Accept' : 'application/json', 'X-OpenStack-Nova-API-Version' : '2.11', 'X-Auth-Token' : mytoken }

APIport=NOVAport CMDpath="/v2/"+tenantid+"/servers" url="http://"+hostIP+":"+APIport+CMDpath print "URL: ",url

body='{"server": {"name": "vm1", "imageRef": "8275248f-1d55-47f7-808b-1208cfd1045d", "flavorRef": "1", "max_count": 1, "min_count": 1, "networks": [{"uuid": "02159b65-a4ad-4c9e-a087-1f3426a333ae"}], "security_groups": [{"name": "default"}]}}'

req = urllib2.Request(url, body, headers)

response = urllib2.urlopen(req).read() decoded = json.loads(response.decode('utf8')) pret = json.dumps(decoded,sort_keys=True,indent=3) print "pret

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

Stats

Asked: 2015-10-15 01:33:19 -0500

Seen: 3,957 times

Last updated: Apr 20 '16