Creating Cloud Servers with the API

Using the API to create Rackspace Legacy Cloud Servers

The below API calls can be used to obtain a token, export an image, and check the export's status. You'll just need to replace the following variables:

${REGION} = The region your image exists in. (dfw, hkg, iad, etc).

${USERNAME} = The user you log into your account with.

${ACCOUNTID} = Your account number.

${APIKEY} = Your user's API Key.

${AUTHTOKEN} = Your API Token obtained from authenticating.

${IMAGEID} = The ID of the image you want to export.

${FLAVORID} = The flavor of the VM you want to build.

${VOLUMEID} = The ID of an existing Boot Volume.

${KEYNAME} = If you want to use SSH Key authentication, the name of the key.

Step 1) Authenticate and Obtain Token

Use your user and API key to obtain an auth token. This command will provide a long alpha-numeric string that is the token.

curl -s https://identity.api.rackspacecloud.com/v2.0/tokens \
  -X POST \
  -d '{"auth":{"RAX-KSKEY:apiKeyCredentials":{"username":"$USERNAME
","apiKey":"$APIKEY"}}}' \
  -H "Content-type: application/json" | grep -o '"token":{[^}]*}' | grep -o '"id":"[^"]*' | cut -d'"' -f4

Step 2) List Available Flavors

Using the Token that you obtained from the API call in Step 1, list out the Rackspace OSPC Flavors to identify which type of server you want to build.

curl -s \
  -H "X-Auth-Token: $AUTHTOKEN" \
  -H "Accept: application/json" \
  "https://$REGION.servers.api.rackspacecloud.com/v2/$ACCOUNTID/flavors/detail"

Step 3) List Available Images

You'll need to know which Image you want your server to use. This API call will list the available images with their IDs.

curl -s \
  -H "X-Auth-Token: $AUTHTOKEN" \
  -H "Accept: application/json" \
  "https://$REGION.servers.api.rackspacecloud.com/v2/$ACCOUNTID/images/detail"

Step 4) Issue Server Build Commands

There are different calls required to build servers depending on if you're using a flavor that boots from volume or local disk.

NOTE: On any of the below API calls you can switch to Password based authentication by removing the header that shows "key_name": "'"$KEY_NAME"'"

Create a Server that boots from Local Disk (with SSH Key Auth)

In this example, you'll need to set the following as well:

${SERVERNAME} = The name of the server you want to create.

curl -X POST \
  -H "X-Auth-Token: $AUTHTOKEN" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  "https://$REGION.servers.api.rackspacecloud.com/v2/$ACCOUNTID/servers" \
  -d '{
    "server": {
      "name": "'"$SERVERNAME"'",
      "imageRef": "'"$IMAGEID"'",
      "flavorRef": "'"$FLAVORID"'",
      "key_name": "'"$KEYNAME"'"
    }
  }'
Create a Server that boots from an Existing Block Storage Volume (with SSH Key Auth)

In this example, you'll need to set the following as well:

${SERVERNAME} = The name of the server you want to create.

curl -X POST \
  -H "X-Auth-Token: $AUTHTOKEN" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  "https://$REGION.servers.api.rackspacecloud.com/v2/$ACCOUNTID/servers" \
  -d '{
    "server": {
      "name": "'"$SERVERNAME"'",
      "imageRef": "",
      "flavorRef": "'"$FLAVORID"'",
      "key_name": "'"$KEYNAME"'",
      "block_device_mapping_v2": [
        {
          "boot_index": "0",
          "uuid": "'"$VOLUMEID"'",
          "source_type": "volume",
          "destination_type": "volume",
          "delete_on_termination": false
        }
      ]
    }
  }'
Create a Block Storage Boot Volume and the Server that boots from it (with SSH Key Auth)

In this example, you'll need to set the following as well:

${SERVERNAME} = The name of the server you want to create.

${BOOTVOLUMESIZEGB} = The number of GBs you want your boot volume to be.

${VOLUMETYPE} = Either SSD or SATA depending on your preference.

curl -X POST \
  -H "X-Auth-Token: $AUTHTOKEN" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  "https://$REGION.servers.api.rackspacecloud.com/v2/$ACCOUNTID/servers" \
  -d '{
    "server": {
      "name": "'"$SERVERNAME"'",
      "imageRef": "",
      "flavorRef": "'"$FLAVORID"'",
      "key_name": "'"$KEYNAME"'",
      "block_device_mapping_v2": [
        {
          "boot_index": "0",
          "uuid": "'"$IMAGEID"'",
          "source_type": "image",
          "destination_type": "volume",
          "volume_size": "'"$BOOTVOLUMESIZEGB"'",
          "volume_type": "'"$VOLUMETYPE"'",
          "delete_on_termination": false
        }
      ]
    }
  }'

After each server creation, if you are not using SSH Key auth you'll receive the root password in the response.


Did this page help you?