第2章 API Reference

The full API reference is available on your Satellite Server at https://satellite.example.com/apidoc/v2.html. Be aware that even though versions 1 and 2 of the Satellite 6 API are available, Red Hat only supports version 2.

2.1. Understanding the API Syntax

The built-in API reference shows the API route, or path, preceded by an HTTP verb:

HTTP_VERB API_ROUTE

To work with the API, construct a command using the curl command syntax and the API route from the reference document:

$ curl --request HTTP_VERB \                   1
--insecure \                                   2
--user sat_username:sat_password \             3
--data @file.json \                            4
--header "Accept:application/json,version=2" \ 5
--header "Content-Type:application/json" \      6
--output file                                  7
API_ROUTE \                                    8
| python -m json.tool                          9
1
To use curl for the API call, specify an HTTP verb with the --request option. For example, --request POST.
2
Add the --insecure option to skip SSL peer certificate verification check.
3
Provide user credentials with the --user option.
4
For POST and PUT requests, use the --data option to pass JSON formatted data. For more information, see 「Passing JSON Data to the API Request」.
5 6
When passing the JSON data with the --data option, you must specify the following headers with the --header option. For more information, see 「Passing JSON Data to the API Request」.
7
When downloading content from Satellite Server, specify the output file with the --output option.
8
Use the API route in the following format: https://satellite.example.com/katello/api/activation_keys. In Satellite 6, version 2 of the API is the default. Therefore it is not necessary to use v2 in the URL for API calls.
9
Redirect the output to the Python json.tool module to make the output easier to read.

2.1.1. Using the GET HTTP Verb

Use the GET HTTP verb to get data from the API about an existing entry or resource.

Example

This example requests the amount of Satellite hosts:

Example request:

$ curl --request GET --insecure --user sat_username:sat_password \
https://satellite.example.com/api/hosts | python -m json.tool

Example response:

{
  "total": 2,
  "subtotal": 2,
  "page": 1,
  "per_page": 20,
  "search": null,
  "sort": {
    "by": null,
    "order": null
  },
  "results":
output truncated

The response from the API indicates that there are two results in total, this is the first page of the results, and the maximum results per page is set to 20. For more information, see 「Understanding the JSON Response Format」.

2.1.2. Using the POST HTTP Verb

Use the POST HTTP verb to submit data to the API to create an entry or resource. You must submit the data in JSON format. For more information, see 「Passing JSON Data to the API Request」.

Example

This example creates an activation key.

  1. Create a test file, for example, activation-key.json, with the following content:

    {"organization_id":1, "name":"TestKey", "description":"Just for testing"}
  2. Create an activation key by applying the data in the activation-key.json file:

    Example request:

    $ curl --header "Accept:application/json,version=2" \
    --header "Content-Type:application/json" --request POST \
    --user sat_username:sat_password --insecure \
    --data @activation-key.json \
    https://satellite.example.com/katello/api/activation_keys \
    | python -m json.tool

    Example response:

    {
        "id": 2,
        "name": "TestKey",
        "description": "Just for testing",
        "unlimited_hosts": true,
        "auto_attach": true,
        "content_view_id": null,
        "environment_id": null,
        "usage_count": 0,
        "user_id": 3,
        "max_hosts": null,
        "release_version": null,
        "service_level": null,
        "content_overrides": [
    
        ],
        "organization": {
            "name": "Default Organization",
            "label": "Default_Organization",
            "id": 1
        },
        "created_at": "2017-02-16 12:37:47 UTC",
        "updated_at": "2017-02-16 12:37:48 UTC",
        "content_view": null,
        "environment": null,
        "products": null,
        "host_collections": [
    
        ],
        "permissions": {
            "view_activation_keys": true,
            "edit_activation_keys": true,
            "destroy_activation_keys": true
        }
    }
  3. Verify that the new activation key is present. In the Satellite web UI, navigate to Content > Activation keys to view your activation keys.

2.1.3. Using the PUT HTTP Verb

Use the PUT HTTP verb to change an existing value or append to an existing resource. You must submit the data in JSON format. For more information, see 「Passing JSON Data to the API Request」.

Example

This example updates the TestKey activation key created in the previous example.

  1. Edit the activation-key.json file created previously as follows:

    {"organization_id":1, "name":"TestKey", "description":"Just for testing","max_hosts":"10" }
  2. Apply the changes in the JSON file:

    Example request:

    $ curl --header "Accept:application/json,version=2" \
    --header "Content-Type:application/json" --request PUT \
    --user sat_username:sat_password --insecure \
    --data @activation-key.json \
    https://satellite.example.com/katello/api/activation_keys/2 \
    | python -m json.tool

    Example response:

    {
        "id": 2,
        "name": "TestKey",
        "description": "Just for testing",
        "unlimited_hosts": false,
        "auto_attach": true,
        "content_view_id": null,
        "environment_id": null,
        "usage_count": 0,
        "user_id": 3,
        "max_hosts": 10,
        "release_version": null,
        "service_level": null,
        "content_overrides": [
    
        ],
        "organization": {
            "name": "Default Organization",
            "label": "Default_Organization",
            "id": 1
        },
        "created_at": "2017-02-16 12:37:47 UTC",
        "updated_at": "2017-02-16 12:46:17 UTC",
        "content_view": null,
        "environment": null,
        "products": null,
        "host_collections": [
    
        ],
        "permissions": {
            "view_activation_keys": true,
            "edit_activation_keys": true,
            "destroy_activation_keys": true
        }
    }
  3. In the Satellite web UI, verify the changes by navigating to Content > Activation keys.

2.1.4. Using the DELETE HTTP Verb

To delete a resource, use the DELETE verb with an API route that includes the ID of the resource you want to delete.

Example

This example deletes the TestKey activation key which ID is 2:

Example request:

$ curl --header "Accept:application/json,version=2" \
--header "Content-Type:application/json" --request DELETE \
--user sat_username:sat_password --insecure \
https://satellite.example.com/katello/api/activation_keys/2 \
| python -m json.tool

Example response:

output omitted
    "started_at": "2017-02-16 12:58:17 UTC",
    "ended_at": "2017-02-16 12:58:18 UTC",
    "state": "stopped",
    "result": "success",
    "progress": 1.0,
    "input": {
        "activation_key": {
            "id": 2,
            "name": "TestKey"
output truncated

2.1.5. Relating API Error Messages to the API Reference

The API uses a RAILs format to indicate an error:

Nested_Resource.Attribute_Name

This translates to the following format used in the API reference:

Resource[Nested_Resource_attributes][Attribute_Name_id]