Skip to content

Commit

Permalink
Modularize Foreman API guide
Browse files Browse the repository at this point in the history
* Modularize content in "guides/common/modules" related to the Foreman API guide
* Unifying anchors
  Is a original effort by Lena to bring the Foreman API guide upstream,
  we briefly discussed to way to set anchors.
  If I remember correctly, we though about using the api- prefix similar
  to the cli- prefix for CLI procedures. I consider this to have value
  on its own; and allow for a smoother transition in case we ever decide
  that we want to also show the API procedure as part of "normal aka. WebUI-based" workflows.
* Using long options for commands (mostly curl) to provide better readability
* Using one argument per line to also simplify readability
  • Loading branch information
maximiliankolb committed Oct 18, 2024
1 parent 27cc32d commit df5282e
Show file tree
Hide file tree
Showing 35 changed files with 956 additions and 790 deletions.
14 changes: 14 additions & 0 deletions guides/common/assembly_api-requests-in-various-languages.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ include::modules/con_api-requests-in-various-languages.adoc[]

include::modules/proc_calling-the-api-in-curl.adoc[leveloffset=+1]

include::modules/proc_passing-json-data-to-the-api-request.adoc[leveloffset=+2]

include::modules/proc_retrieving-a-list-of-resources.adoc[leveloffset=+2]

include::modules/proc_creating-and-modifying-resources.adoc[leveloffset=+2]

include::modules/proc_calling-the-api-in-ruby.adoc[leveloffset=+1]

include::modules/proc_creating-objects-by-using-ruby.adoc[leveloffset=+2]

include::modules/proc_using-apipie-bindings-with-ruby.adoc[leveloffset=+2]

include::modules/proc_calling-the-api-in-python.adoc[leveloffset=+1]

include::modules/proc_creating-objects-by-using-python.adoc[leveloffset=+2]

include::modules/proc_retrieving-resource-information-by-using-python.adoc[leveloffset=+2]
2 changes: 1 addition & 1 deletion guides/common/modules/con_api-cheat-sheet.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ You can use the API on {ProjectServer} via HTTPS on port 443.

For example, in Ruby, you can specify the {ProjectServer} URL as follows:

[options="nowrap", subs="+quotes,verbatim,attributes"]
[source, none, options="nowrap", subs="+quotes,verbatim,attributes"]
----
url = 'https://_{foreman-example-com}_/api/v2/'
ifdef::katello,orcharhino,satellite[]
Expand Down
20 changes: 11 additions & 9 deletions guides/common/modules/con_api-request-composition.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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

[source, none, options="nowrap", subs="+quotes,attributes"]
----
HTTP_METHOD API_ROUTE
----
Expand All @@ -11,15 +12,16 @@ To work with the API, construct a command by using the `curl` command syntax and

[options="nowrap", subs="+quotes,attributes"]
----
$ curl --request _HTTP_METHOD_ \ #<1>
--insecure \ #<2>
--user _My_User_Name_:__My_Password__ \ #<3>
--data @_input_file_.json \ #<4>
$ curl \
--request _HTTP_METHOD_ \ #<1>
--insecure \ #<2>
--user _My_User_Name_:__My_Password__ \ #<3>
--data @_My_Input_File_.json \ #<4>
--header "Accept:application/json" \ #<5>
--header "Content-Type:application/json" \ #<5>
--output _output_file_ #<6>
_API_ROUTE_ \ #<7>
| python3 -m json.tool #<8>
--output _My_Output_File_ #<6>
_API_ROUTE_ \ #<7>
| python3 -m json.tool #<8>
----

<1> To use `curl` for the API call, specify an HTTP method with the `--request` option.
Expand All @@ -29,9 +31,9 @@ For example, `--request POST`.
For more information, see xref:ssl-authentication-overview[].
<3> Provide {Project} 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 xref:sect-API_Guide-Passing_JSON_Data_with_the_API_Request[].
For more information, see xref:passing-json-data-to-the-api-request[].
<5> When passing the JSON data with the `--data` option, you must specify the following headers with the `--header` option.
For more information, see xref:sect-API_Guide-Passing_JSON_Data_with_the_API_Request[].
For more information, see xref:passing-json-data-to-the-api-request[].
<6> When downloading content from {ProjectServer}, specify the output file with the `--output` option.
<7> Use the API route in the following format: `https://_{foreman-example-com}_/katello/api/activation_keys`.
In {ProjectX}, version 2 of the API is the default.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ You can access the full API reference on your {ProjectServer}.
.Procedure
* In your browser, access the following URL:
+
[options="nowrap", subs="+quotes,attributes"]
[source, none, options="nowrap", subs="+quotes,attributes"]
----
https://_{foreman-example-com}_/apidoc/v2.html
----
Expand Down
26 changes: 17 additions & 9 deletions guides/common/modules/proc_applying-errata-to-hosts.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ The following is the basic syntax of a PUT request:

[options="nowrap", subs="+quotes,attributes"]
----
$ curl --header "Accept:application/json" \
--header "Content-Type:application/json" --request PUT \
$ curl \
--header "Accept:application/json" \
--header "Content-Type:application/json" \
--request PUT \
--user _My_User_Name_:__My_Password__ \
--data _My_JSON_Formatted_Data_ https://_{foreman-example-com}_
--data _My_JSON_Formatted_Data_ \
https://_{foreman-example-com}_
----

You can browse the built-in API doc to find a URL to use for applying errata.
Expand All @@ -18,31 +21,36 @@ Navigate to *Hosts* > *Host Collections* and select a host collection.
Go to *Collection Actions* > *Errata Installation* and notice the search query box contents.
For example, for a Host Collection called _my-collection_, the search box contains `host_collection="my-collection"`.

[id="exam-API_Guide-Applying_Errata_to_a_Host"]
[id="api-applying-errata-to-a-host"]
.Applying errata to a host

This example uses the API URL for bulk actions `/katello/api/hosts/bulk/install_content` to show the format required for a simple search.

Example request:

[options="nowrap", subs="+quotes,attributes"]
----
$ curl --header "Accept:application/json" \
--header "Content-Type:application/json" --request PUT \
$ curl \
--header "Accept:application/json" \
--header "Content-Type:application/json" \
--request PUT \
--user _My_User_Name_:__My_Password__ \
--data "{\"organization_id\":1,\"included\":{\"search\":\"_my-host_\"},\"content_type\":\"errata\",\"content\":[\"_RHBA-2016:1981_\"]}" \
https://_{foreman-example-com}_/api/v2/hosts/bulk/install_content
----

[id="exam-API_Guide-Applying_Errata_to_a_Host_Collection"]
[id="api-applying-errata-to-a-host-collection"]
.Applying errata to a host collection

In this example, notice the level of escaping required to pass the search string `host_collection="my-collection"` as seen in the {ProjectWebUI}.
Example request:

[options="nowrap", subs="+quotes,attributes"]
----
$ curl --header "Accept:application/json" \
--header "Content-Type:application/json" --request PUT \
$ curl \
--header "Accept:application/json" \
--header "Content-Type:application/json" \
--request PUT \
--user _My_User_Name_:__My_Password__ \
--data "{\"organization_id\":1,\"included\":{\"search\":\"host_collection=\\\"_my-collection_\\\"\"},\"content_type\":\"errata\",\"content\":[\"_RHBA-2016:1981_\"]}" \
https://_{foreman-example-com}_/api/v2/hosts/bulk/install_content
Expand Down
183 changes: 0 additions & 183 deletions guides/common/modules/proc_calling-the-api-in-curl.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,186 +13,3 @@ 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.

[id="sect-API_Guide-Passing_JSON_Data_with_the_API_Request"]
== Passing JSON data to the API request

You can pass data to {ProjectServer} 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"
----

Use one of the following options to include data with the `--data` option.

.JSON-formatted string
Enclose the quoted JSON-formatted data 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\"`:

[options="nowrap", subs="+quotes,attributes"]
----
--data {"id":44, "smart_class_parameter":{"override":"true", "parameter_type":"json", "default_value":"{\"GRUB_CMDLINE_LINUX\": {\"audit\":\"1\",\"crashkernel\":\"true\"}}"}}
----

.JSON-formatted file
The unquoted JSON-formatted data enclosed in a file and specified by the `@` sign and the filename.
For example:

[options="nowrap", subs="+quotes,attributes"]
----
--data @_file_.json
----

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.

Use the `json_verify` tool to check the validity of the JSON file:

[options="nowrap", subs="+quotes,attributes"]
----
$ json_verify < _file_.json
----

[id="sect-API_Guide-Performing_Simple_Queries"]
== Retrieving a list of resources

This section outlines how to use `curl` with the {ProjectX} API to request information from {Project}.
These examples include both requests and responses.
Expect different results for each deployment.

.Listing users

This example is a basic request that returns a list of {Project} resources, {Project} 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:
[options="nowrap", subs="+quotes,attributes"]
----
$ curl --request GET --user _My_User_Name_:__My_Password__ \
https://_{foreman-example-com}_/api/users | python3 -m json.tool
----

Example response:
[options="nowrap", subs="+quotes,attributes"]
----
{
"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": "[email protected]",
"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": "[email protected]",
"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
}
----

[id="sect-API_Guide-Creating_and_Modifying_Resources"]
== Creating and modifying resources

You can use `curl` to manipulate resources on your {ProjectServer}.
API calls to {Project} require data in `json` format.
For more information, see xref:sect-API_Guide-Passing_JSON_Data_with_the_API_Request[].

[id="ex-API_Guide-Creating_a_New_User"]
.Creating a user

This example creates a user by providing required information in the `--data` option.

Example request:
[options="nowrap", subs="+quotes,attributes"]
----
$ curl --header "Accept:application/json" \
--header "Content-Type:application/json" --request POST \
--user _My_User_Name_:__My_Password__ \
--data "{\"firstname\":\"_Test Name_\",\"mail\":\"[email protected]_\",\"login\":\"_test_user_\",\"password\":\"_password123_\",\"auth_source_id\":__1__}" \
https://_{foreman-example-com}_/api/users | python3 -m json.tool
----

.Modifying a user

This example modifies given name and login of the `test_user` that was created in xref:ex-API_Guide-Creating_a_New_User[].

Example request:
[options="nowrap", subs="+quotes,attributes"]
----
$ curl --header "Accept:application/json" \
--header "Content-Type:application/json" --request PUT \
--user _My_User_Name_:__My_Password__ \
--data "{\"firstname\":\"_New Test Name_\",\"mail\":\"[email protected]_\",\"login\":\"_new_test_user_\",\"password\":\"_password123_\",\"auth_source_id\":__1__}" \
https://_{foreman-example-com}_/api/users/_8_ | python3 -m json.tool
----
Loading

0 comments on commit df5282e

Please sign in to comment.