第 4 章 不同语言的 API 请求
本章概述了使用 curl、Ruby 和 Python 向 Red Hat Satellite 发送 API 请求,并提供示例。
4.1. 使用 curl 的 API 请求
本节概述了如何在 Satellite API 中使用 curl 执行各种任务。
Red Hat Satellite 需要使用 HTTPS,默认情况下,证书用于主机识别。如果您尚未添加 Satellite 服务器证书,如 第 3.1 节 “SSL 身份验证概述” 所述,您可以使用 --insecure 选项来绕过证书检查。
对于用户身份验证,您可以使用 --user 选项以 --user username:password格式提供 Satellite 用户凭证,或者如果不包含密码,命令会提示您输入它。要降低安全风险,请不要将密码作为命令的一部分,因为它将成为 shell 历史记录的一部分。本节中的示例仅包含仅包含简单性的密码。
请注意,如果您使用 --silent 选项,curl 不会显示进度计量或任何错误消息。
本章中的示例使用 Python json.tool 模块来格式化输出。
4.1.1. 将 JSON 数据传递给 API 请求
您可以使用 API 请求将数据传递到卫星服务器。数据必须采用 JSON 格式。当使用 --data 选项指定 JSON 数据时,您必须使用 --header 选项设置以下 HTTP 标头:
--header "Accept:application/json" \ --header "Content-Type:application/json"
使用以下选项之一通过 --data 选项包括数据:
用大括号
{}括起的 JSON 格式的数据。为 JSON 类型参数传递值时,您必须转义引号",带有反斜杠\。例如,在大括号中,您必须将"Example JSON 变量"格式化为\"Example JSON Variable\":--data {"id":44, "smart_class_parameter":{"override":"true", "parameter_type":"json", "default_value":"{\"GRUB_CMDLINE_LINUX\": {\"audit\":\"1\",\"crashkernel\":\"true\"}}"}}未括起的 JSON 格式数据包含在文件中,并由
@符号和文件名指定。例如:--data @file.json将外部文件用于 JSON 格式数据具有以下优点:
- 您可以使用您首选的文本编辑器。
- 您可以使用语法检查程序来查找并避免错误。
- 您可以使用工具来检查 JSON 数据的有效性或重新格式化它。
验证 JSON 文件
使用 json_verify 工具检查 JSON 文件的有效性:
$ json_verify < test_file.json4.1.2. 检索资源列表
本节概述了如何将 curl 与 Satellite 6 API 搭配使用,从 Satellite 部署请求信息。这些示例包括请求和响应。每个部署的预期结果各有不同。
列出用户
此示例是返回 Satellite 资源列表的基本请求,本例中为 Satellite 用户。此类请求返回元数据嵌套的数据列表,其他请求类型仅返回实际对象。
请求示例:
$ curl --request GET --insecure --user sat_username:sat_password \ https://satellite.example.com/api/users | python -m json.tool
响应示例:
{
"page": 1,
"per_page": 20,
"results": [
{
"admin": false,
"auth_source_id": 1,
"auth_source_name": "Internal",
"created_at": "2018-09-21 08:59:22 UTC",
"default_location": null,
"default_organization": null,
"description": "",
"effective_admin": false,
"firstname": "",
"id": 5,
"last_login_on": "2018-09-21 09:03:25 UTC",
"lastname": "",
"locale": null,
"locations": [],
"login": "test",
"mail": "example@domain.com",
"organizations": [
{
"id": 1,
"name": "Default Organization"
}
],
"ssh_keys": [],
"timezone": null,
"updated_at": "2018-09-21 09:04:45 UTC"
},
{
"admin": true,
"auth_source_id": 1,
"auth_source_name": "Internal",
"created_at": "2018-09-20 07:09:41 UTC",
"default_location": null,
"default_organization": {
"description": null,
"id": 1,
"name": "Default Organization",
"title": "Default Organization"
},
"description": "",
"effective_admin": true,
"firstname": "Admin",
"id": 4,
"last_login_on": "2018-12-07 07:31:09 UTC",
"lastname": "User",
"locale": null,
"locations": [
{
"id": 2,
"name": "Default Location"
}
],
"login": "admin",
"mail": "root@example.com",
"organizations": [
{
"id": 1,
"name": "Default Organization"
}
],
"ssh_keys": [],
"timezone": null,
"updated_at": "2018-11-14 08:19:46 UTC"
}
],
"search": null,
"sort": {
"by": null,
"order": null
},
"subtotal": 2,
"total": 2
}4.1.3. 创建和修改资源
本节概述了如何将 curl 与 Satellite 6 API 搭配使用,以操作卫星服务器上的资源。这些 API 调用要求您使用 API 调用以 json 格式传递数据。更多信息请参阅 第 4.1.1 节 “将 JSON 数据传递给 API 请求”。
创建用户
这个示例使用 --data 选项创建用户以提供所需的信息。
请求示例:
$ 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 | python -m json.tool
修改用户
这个示例修改在 创建用户 中创建的 test_user 的名字和登录。
请求示例:
$ 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 | python -m json.tool