Questo contenuto non è disponibile nella lingua selezionata.
Chapter 4. API Requests in Different Languages
This chapter outlines sending API requests to Red Hat Satellite with curl, Ruby, and Python and provides examples.
4.1. API Requests with curl Copia collegamentoCollegamento copiato negli appunti!
This section outlines how to use curl
with the Satellite API to perform various tasks.
Red Hat Satellite requires the use of HTTPS, and by default a certificate for host identification. If you have not added the Satellite Server certificate as described in Section 3.1, “SSL Authentication Overview”, then you can use the --insecure
option to bypass certificate checks.
For user authentication, you can use the --user
option to provide Satellite user credentials in the form --user username:password
or, if you do not include the password, the command prompts you to enter it. To reduce security risks, do not include the password as part of the command, because it then becomes part of your shell history. Examples in this section include the password only for the sake of simplicity.
Be aware that if you use the --silent
option, curl
does not display a progress meter or any error messages.
Examples in this chapter use the Python json.tool
module to format the output.
4.1.1. Passing JSON Data to the API Request Copia collegamentoCollegamento copiato negli appunti!
You can pass data to Satellite Server with the API request. The data must be in JSON format. When specifying JSON data with the --data
option, you must set the following HTTP headers with the --header
option:
--header "Accept:application/json" \ --header "Content-Type:application/json"
--header "Accept:application/json" \
--header "Content-Type:application/json"
Use one of the following options to include data with the --data
option:
The quoted JSON formatted data enclosed in curly braces
{}
. When passing a value for a JSON type parameter, you must escape quotation marks"
with backslashes\
. For example, within curly braces, you must format"Example JSON Variable"
as\"Example JSON Variable\"
:--data {"id":44, "smart_class_parameter":{"override":"true", "parameter_type":"json", "default_value":"{\"GRUB_CMDLINE_LINUX\": {\"audit\":\"1\",\"crashkernel\":\"true\"}}"}}
--data {"id":44, "smart_class_parameter":{"override":"true", "parameter_type":"json", "default_value":"{\"GRUB_CMDLINE_LINUX\": {\"audit\":\"1\",\"crashkernel\":\"true\"}}"}}
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The unquoted JSON formatted data enclosed in a file and specified by the
@
sign and the filename. For example:--data @file.json
--data @file.json
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Using external files for JSON formatted data has the following advantages:
- You can use your favorite text editor.
- You can use syntax checker to find and avoid mistakes.
- You can use tools to check the validity of JSON data or to reformat it.
Validating a JSON file
Use the json_verify
tool to check the validity of a JSON file:
json_verify < test_file.json
$ json_verify < test_file.json
4.1.2. Retrieving a List of Resources Copia collegamentoCollegamento copiato negli appunti!
This section outlines how to use curl
with the Satellite 6 API to request information from your Satellite deployment. These examples include both requests and responses. Expect different results for each deployment.
The example requests below use python3
to format the respone from the Satellite Server. On RHEL 7 and some older systems, you must use python
instead of python3
.
Listing Users
This example is a basic request that returns a list of Satellite resources, Satellite users in this case. Such requests return a list of data wrapped in metadata, while other request types only return the actual object.
Example request:
curl --request GET --insecure --user sat_username:sat_password \ https://satellite.example.com/api/users | python3 -m json.tool
$ curl --request GET --insecure --user sat_username:sat_password \
https://satellite.example.com/api/users | python3 -m json.tool
Example response:
4.1.3. Creating and Modifying Resources Copia collegamentoCollegamento copiato negli appunti!
This section outlines how to use curl
with the Satellite 6 API to manipulate resources on the Satellite Server. These API calls require that you pass data in json
format with the API call. For more information, see Section 4.1.1, “Passing JSON Data to the API Request”.
Creating a User
This example creates a user using --data
option to provide required information.
Example request:
curl --header "Accept:application/json" \ --header "Content-Type:application/json" --request POST \ --user sat_username:sat_password --insecure \ --data "{\"firstname\":\"Test Name\",\"mail\":\"test@example.com\",\"login\":\"test_user\",\"password\":\"password123\",\"auth_source_id\":1}" \ https://satellite.example.com/api/users | python3 -m json.tool
$ curl --header "Accept:application/json" \
--header "Content-Type:application/json" --request POST \
--user sat_username:sat_password --insecure \
--data "{\"firstname\":\"Test Name\",\"mail\":\"test@example.com\",\"login\":\"test_user\",\"password\":\"password123\",\"auth_source_id\":1}" \
https://satellite.example.com/api/users | python3 -m json.tool
Modifying a User
This example modifies first name and login of the test_user
that was created in Creating a User.
Example request:
curl --header "Accept:application/json" \ --header "Content-Type:application/json" --request PUT \ --user sat_username:sat_password --insecure \ --data "{\"firstname\":\"New Test Name\",\"mail\":\"test@example.com\",\"login\":\"new_test_user\",\"password\":\"password123\",\"auth_source_id\":1}" \ https://satellite.example.com/api/users/8 | python3 -m json.tool
$ curl --header "Accept:application/json" \
--header "Content-Type:application/json" --request PUT \
--user sat_username:sat_password --insecure \
--data "{\"firstname\":\"New Test Name\",\"mail\":\"test@example.com\",\"login\":\"new_test_user\",\"password\":\"password123\",\"auth_source_id\":1}" \
https://satellite.example.com/api/users/8 | python3 -m json.tool
4.2. API Requests with Ruby Copia collegamentoCollegamento copiato negli appunti!
This section outlines how to use Ruby with the Satellite API to perform various tasks.
These are example scripts and commands. Ensure you review these scripts carefully before use, and replace any variables, user names, passwords, and other information to suit your own deployment.
4.2.1. Creating Objects Using Ruby Copia collegamentoCollegamento copiato negli appunti!
This script connects to the Red Hat Satellite 6 API and creates an organization, and then creates three environments in the organization. If the organization already exists, the script uses that organization. If any of the environments already exist in the organization, the script raises an error and quits.
4.2.2. Using Apipie Bindings with Ruby Copia collegamentoCollegamento copiato negli appunti!
Apipie bindings are the Ruby bindings for apipie documented API calls. They fetch and cache the API definition from Satellite and then generate API calls on demand. This example creates an organization, and then creates three environments in the organization. If the organization already exists, the script uses that organization. If any of the environments already exist in the organization, the script raises an error and quits.
4.3. API Requests with Python Copia collegamentoCollegamento copiato negli appunti!
This section outlines how to use Python with the Satellite API to perform various tasks.
These are example scripts and commands. Ensure you review these scripts carefully before use, and replace any variables, user names, passwords, and other information to suit your own deployment.
Example scripts in this section do not use SSL verification for interacting with the REST API.
4.3.1. Creating Objects Using Python Copia collegamentoCollegamento copiato negli appunti!
This script connects to the Red Hat Satellite 6 API and creates an organization, and then creates three environments in the organization. If the organization already exists, the script uses that organization. If any of the environments already exist in the organization, the script raises an error and quits.
Python 2 Example
4.3.2. Requesting information from the API using Python Copia collegamentoCollegamento copiato negli appunti!
This is an example script that uses Python for various API requests.
Python 2 Example
Python 3 Example