Monday, October 9, 2017

Creation of a Nova Instance using the HEAT API Openstack Ocata and use Neutron API to associate a Floating IP

Creation of a Nova Instance using the HEAT API Openstack Ocata and use Neutron API to associate a Floating IP


# get an auth tokem
curl -d '
   {"auth": {
      "tenantName": "admin",
      "passwordCredentials": {
        "username": "admin",
        "password": "secretpassword"
       }
     }
   }' \
   -H "Content-type: application/json" \
   "http://11.11.1.158:5000/v2.0/tokens" | python -mjson.tool

# the above CURL command generates an AUTH token. This Auth token is used in the subsequent CURL request as needed. Please note that the Auth tokens are valid for 1 hour.
# this also generates the endpoint information to which the username (above admin) has access to

this is a string as "gAAAAABZ2dm6mYkFyY_YL2Dko47Ha8H_GPL-bowgPBfCN2Qec3N5ITRf5MwhIzsAxUtH0IQIN3BE9gUNR2fRIppAYSyiZQ09NsDsU6AdBJXTpgte6P-Rc3prhveDBU8DdwGxaBcllJwbjjy1lL3MbciXyWgX4ZIj1quMKwdqZZ57hEx_lt9FRLw"
   
# get all the stacks for the admin tenant, here the tenant ID for admin project is f4b62d0d71f44126bd22c2f04251b3f1
# the header in the CURL GET call to the HEAT API as X-Auth-Token has the AUTH key generated earlier


curl -X GET http://11.11.1.158:8004/v1/f4b62d0d71f44126bd22c2f04251b3f1/stacks -H "X-Auth-Token: gAAAAABZ2dm6mYkFyY_YL2Dko47Ha8H_GPL-bowgPBfCN2Qec3N5ITRf5MwhIzsAxUtH0IQIN3BE9gUNR2fRIppAYSyiZQ09NsDsU6AdBJXTpgte6P-Rc3prhveDBU8DdwGxaBcllJwbjjy1lL3MbciXyWgX4ZIj1quMKwdqZZ57hEx_lt9FRLw"

# CURL POST call to the HEAT API of openstack with the headers as content-type as application/json.
# there is one more header (-H) having the X-Auth-Token which has the authentication token generared previously
# Please note that in the JSON body being sent to the HEAT API you can also appropriately add the Security Group for the instance to be attached to

curl -X POST http://11.11.1.158:8004/v1/f4b62d0d71f44126bd22c2f04251b3f1/stacks -H "Content-type: application/json" -H "X-Auth-Token: gAAAAABZ2hB-tNCYYZ71ESKM_4QCU_PrrlYYb4winxKe6E-3qpUPx8eHaxWhNypwKupKSvHU3_KZpkZswd0jQ3QFIVDMqDpqbOIBwajb3CiP_Q6JVtp6Neu892n23pAYjrntk3VnuWxJX3zqjNTFxECziYtAmFoYIhp5rRFjqmR9escAOFflLFg" -d \
'{
    "stack_name": "teststack",
    "template": {
        "heat_template_version": "2017-02-24",
        "description": "Simple template to test heat commands",
        "resources": {
            "restapiheatstackservers": {
                "type": "OS::Nova::Server",
                "properties": {
                    "key_name": "key",
                    "flavor": "m1.tiny",
                    "image": "d66b73e7-cb0c-42c5-bf73-d5bbdf245da5",
                    "networks": [
                      {
                        "network": "internal0"
                      }
                     ]
                }
            }
        }
    },
    "timeout_mins": 60
}'

# Stack result of the curl command that is shown as the above CURL POST creates the stack

{"stack": {"id": "8202c7a1-3011-4b7c-87d1-93a470c34101", "links": [{"href": "http://11.11.1.158:8004/v1/f4b62d0d71f44126bd22c2f04251b3f1/stacks/teststack/8202c7a1-3011-4b7c-87d1-93a470c34101", "rel": "self"}]}}

# get the nova instance Port IP details
# This makes a CURL GET call to the NOVA API for the tenant ID. 
# Please note that the URL also contains the ID of the Nova Instance created by the previous HEAT API call
# this is to get the port_id of the instance created by the previous heat API Call

curl -X GET http://11.11.1.158:8774/v2.1/f4b62d0d71f44126bd22c2f04251b3f1/servers/94d359d4-5ea2-42ac-9d0c-afe5d58bb956/os-interface -H "X-Auth-Token: gAAAAABZ2hB-tNCYYZ71ESKM_4QCU_PrrlYYb4winxKe6E-3qpUPx8eHaxWhNypwKupKSvHU3_KZpkZswd0jQ3QFIVDMqDpqbOIBwajb3CiP_Q6JVtp6Neu892n23pAYjrntk3VnuWxJX3zqjNTFxECziYtAmFoYIhp5rRFjqmR9escAOFflLFg" | python -mjson.tool


# Associate the floating IP to the Instance port ID
# this is a CURL POST request to the Neutron API on port 9696 that is neutron port
# the header contains the -H for the content-type as application/json 
# the header also contains (the other -H option) to send the X-Auth-Token to the Neutron REST API

curl -X POST http://11.11.1.158:9696/v2.0/floatingips -H "Content-type: application/json" -H "X-Auth-Token: gAAAAABZ2hB-tNCYYZ71ESKM_4QCU_PrrlYYb4winxKe6E-3qpUPx8eHaxWhNypwKupKSvHU3_KZpkZswd0jQ3QFIVDMqDpqbOIBwajb3CiP_Q6JVtp6Neu892n23pAYjrntk3VnuWxJX3zqjNTFxECziYtAmFoYIhp5rRFjqmR9escAOFflLFg" -d \
'{
    "floatingip": {
        "floating_network_id": "27d8c6cd-f905-48db-bcc7-d36f36bfecc7",
        "port_id": "298951f4-238c-4f16-ac5b-15d42b3a2d57",
        "description": "floating ip for testing"
    }
}'


No comments:

Post a Comment