diff --git a/content/resources/custom/rest_resource.md b/content/resources/custom/rest_resource.md index d08dd51..4bbc95d 100644 --- a/content/resources/custom/rest_resource.md +++ b/content/resources/custom/rest_resource.md @@ -10,6 +10,82 @@ title = "Create a RESTful custom resource" weight = 50 +++ + + The REST resource DSL is a base resource class in Chef Infra Client that allows you to create custom resources that interact with RESTful APIs. Instead of writing HTTP request handling code from scratch, you can extend this resource to create custom resources that automatically handle API interactions, JSON mapping, and state management. @@ -33,8 +109,9 @@ The REST custom resource DSL has the following requirements: ## Configure the API endpoint -You can configure an API endpoint using either the `rest_api_endpoint` method or by using Train transport. +You can configure an API endpoint using Train transport. + ### Train transport endpoint @@ -79,7 +157,7 @@ class Chef::Resource::ApiUser < Chef::Resource resource_name :api_user provides :api_user - # No rest_api_endpoint: base URL comes from the Train transport + # Base URL comes from the Train transport rest_api_collection "/api/v1/users" rest_api_document "/api/v1/users/{username}" @@ -90,6 +168,7 @@ class Chef::Resource::ApiUser < Chef::Resource end ``` + + ## Methods and actions @@ -167,6 +249,7 @@ The `rest_resource` has the following methods and actions. The REST resource provides several DSL methods for configuring API interactions. These methods are called within your custom resource class definition. + #### `rest_api_collection` @@ -243,7 +327,7 @@ Parameters: - `path` (String): URL pattern with optional `{template}` placeholders matching property names - `first_element_only` (Boolean): If `true`, extracts the first element from an array response. Default is `false`. -If you set `rest_identity_property` instead, `rest_api_document` is autogenerated and you don't need to set it manually. + For example: @@ -373,7 +457,6 @@ For example: See the following examples for more information: - [Use JMESPath expressions to map data](#use-jmespath-expressions-to-map-data-in-a-json-structure) -- [Create a custom mapping function](#create-a-custom-mapping-function-with-rest_property_map) ### Actions @@ -424,7 +507,6 @@ Override the `rest_headers` method in your action class to add custom headers su class Chef::Resource::ApiResource < Chef::Resource use "core::rest_resource" - rest_api_endpoint "https://api.example.com" rest_api_collection "/api/v1/resources" rest_api_document "/api/v1/resources/{id}" @@ -503,6 +585,7 @@ end ## Examples + ### Create a REST resource using the Train transport endpoint -The following `api_user` resource omits `rest_api_endpoint`. The base URL is provided instead by the Chef target mode Train transport. Note that `rest_api_document` must now be set manually because `rest_identity_property` can't generate it without a base endpoint. +The following `api_user` resource uses the Chef target mode Train transport to provide the base URL. The resource uses relative paths only, and Train prepends its configured endpoint. ```ruby class Chef::Resource::ApiUser < Chef::Resource @@ -566,7 +650,7 @@ class Chef::Resource::ApiUser < Chef::Resource resource_name :api_user provides :api_user - # No rest_api_endpoint---base URL comes from the Train transport + # Base URL comes from the Train transport rest_api_collection "/api/v1/users" rest_api_document "/api/v1/users/{username}" @@ -707,6 +791,7 @@ rest_property_map({ } ``` + + + + ## Troubleshooting @@ -1018,6 +1110,7 @@ rest_property_map({ }) ``` + ## Additional resources