Getting startes with the OpenStack API

This guide help you to manage your instances using the Python OpenStack client or Calling Request API.

Requirements

  • Register NIPA Cloud Space portal

  • Access project or create new one and Topup wallet first.

  • Download Openrc file from your project

1. Using OpenStack Client Tools

1.1 Requirements

Please refer to the following guides:

Preparing an environment for using the Openstack API

On Ubuntu:

  • update packet cache

sudo apt update && sudo apt upgrade -y
  • Install Python3 before installing the OpenStack client

sudo apt install python3-dev python3-pip
  • install the OpenStack client

sudo apt install python3-openstackclient
  • verify by accessing the help tools

openstack --help

Setting environment for using the Openstack

  • Download Openrc file from our portal

  • On Ubuntu

    • Use source command and enter your password to login Horizon source <openrc.sh file>

1.2 Instructions

You can obtain the list of possible commands by reading the embedded documentation:

openstack command list

You can filter the commands displayed by indicating the group.

openstack command list --group compute

It is also possible to get information about a command by adding "help" in front of it:

nc-user@instance-1234:~$ openstack help flavor list 
usage: openstack flavor list [-h] [-f {csv,json,table,value,yaml}] [-c COLUMN][--quote {all,minimal,none,nonnumeric}] [--noindent][--max-width <integer>] [--fit-width] [--print-empty][--sort-column SORT_COLUMN][--sort-ascending | --sort-descending] [--public | --private | --all][--min-disk <min-disk>] [--min-ram <min-ram>] [--long][--marker <flavor-id>] [--limit <num-flavors>]

1.3 Example Create Instance With OpenStack Commandline

  • spec

    • Instance name: Instance-2001

    • Flavor: csa.large.v2

    • Volume: standard-ssd 40 GB

    • VPC : default

  • list flavor

openstack flavor list
  • list network/subnet

openstack network list
openstack subnet list
  • list security group

openstack security group list
  • list keypair

openstack keypair list
  • create instance

openstack server create ..........

Ex.

openstack server create \
  --flavor 3d7f13f6-11df-45a0-a78b-fb20a64c2849 \
  --image 6a0e2c93-682d-4d3b-a473-5251faefb8fc \
  --boot-from-volume 20 \
  --nic net-id=2aa7a98d-ae38-4844-82d6-9ab5c2460b69 \
  --key-name KeyPairProd \
  Instance-2001
  • Verify that the instance is created using this command.

openstack server list 

Note : you can add option --debug to get information about request body and response body of you api request

2. Using API Request

Request an authentication token from the Identity endpoint that your cloud administrator gave you. Send a payload of credentials in the request as shown in Authenticate. If the request succeeds, the server returns an authentication token.

Send API requests and include the token in the X-Auth-Token header. Continue to send API requests with that token until the service completes the request or the Unauthorized (401) error occurs.

If the Unauthorized (401) error occurs, request another token.

Example body

{
    "auth": {
        "identity": {
            "methods": [
                "password"
            ],
            "password": {
                "user": {
                    "id": "ee4dfb6e5540447cb3741905149d9b6e",
                    "password": "devstacker"
                }
            }
        },
        "scope": {
            "project": {
                "domain": {
                    "id": "default"
                },
                "name": "admin"
            }
        }
    }
}

example command

curl -i -X POST \
  http://<identity-service-url>/v3/auth/tokens \
  -H "Content-Type: application/json" \
  -d '
{
  "auth": {
    "identity": {
      "methods": ["password"],
      "password": {
        "user": {
          "name": "test@domain.com",
          "password": "<password>",
          "domain": { "id": "<domain-id>" }
        }
      }
    },
    "scope": {
      "project": {
        "name": "<project_name>",
          "domain": { "id": "<domain-id>" }
      }
    }
  }
}'

If the request succeeds, it returns the Created (201) response code along with the token as a value in the X-Subject-Token response header. The header is followed by a response body that has an object of type token which has the token expiration date and time in the form "expires_at":"datetime" along with other attributes.

2.1 Example list instance via Send API requests

This section shows how to make some basic Compute API calls. For a complete list of Compute API calls, see Compute API.

Export the token ID to the OS_TOKEN environment variable. For example:

export OS_TOKEN=xxxxxxxxxxx

You need to know the Url of service compute, this is include in response of keystone login body.

export OS_COMPUTE_API=xxxxxxxxxx

Then, use the Compute API to list flavors, substituting the Compute API endpoint with one containing your project ID below:

curl -s -H "X-Auth-Token: $OS_TOKEN" \  $OS_COMPUTE_API/servers \  | python -m json.tool

sent stop instance api

curl -L -X POST -H "X-Auth-Token: $OS_TOKEN" \
     -H "Content-Type: application/json" \
     -d '{"os-stop": null}' \
     $OS_COMPUTE_API/servers/{server_id}/action

Ex.

curl -L -X POST -H "X-Auth-Token: $OS_TOKEN" \
     -H "Content-Type: application/json" \
     -d '{"os-stop": null}' \
     https://instance-api.nipa.cloud/v2.1/servers/{server_id}/action

2.2 referrent

Last updated