Table of contents
1.
Introduction
2.
Forming node inventory API requests
3.
Command endpoints
3.1.
POST/command/create-connection
3.2.
POST/command/delete-connection
4.
Query endpoints
4.1.
GET/query/connections
4.2.
POST/query/connections
5.
Node inventory API errors
6.
Frequently Asked Questions
6.1.
What is the use of the sensitive and extract parameter in the GET query connection endpoint?
6.2.
What does the error and replace value of the duplicates key do?
6.3.
What is the difference between ssh and winrm type connection with respect to their sensitive parameters?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Node inventory API v1

Author Yashesvinee V
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hi there! 

Would you like to know how to access device and node connection entries from the Puppet Inventory Service? You are at the right place. In this blog, we shall explore how the Node Inventory API can be used to make HTTP(S) requests to the Puppet inventory service API. This API can also be used to create and delete connection entries in the inventory service database.

Node inventory API v1

Forming node inventory API requests

The node inventory service listens for requests on port 8143 with all endpoints relative to the /inventory/v1 path. The URI path of the API requests is of the following format:

https://<DNS>:8143/inventory/v1/<ENDPOINT>

Where DNS is the PE console host's DNS name.
 

Authenticating Node Inventory API requests is a must. This can be done by passing user authentication tokens in an X-Authentication request header. Tokens can be generated using the puppet-access tool. Using the token, we can post a new connection entry to the inventory service, as shown in the example below.

Connection entry example

Source: Puppet Docs

Command endpoints

There are two command endpoints of the Node inventory API to create and delete connections in the inventory service database.

POST/command/create-connection

This command endpoint creates a new connection to the inventory service database. All connection database entries contain connection settings used to connect to nodes identified by their certnames. The connection options connect to the certnames provided in the payload via the connection type.

The request body is in the form of a JSON body with the following keys:

  • Certnames is an array containing a list of certnames to associate with the given connection details.
     
  • The type can be either ssh or winrm. This defines the connection type to be used to access the node when running a task.
     
  • Parameters are stored as key/value pairs for the specified transport type.
     
  • Sensitive_parameters like usernames and passwords are stored in an encrypted format.
     
  • Duplicates value can be either error or replace. This specifies the handling cases when the given certnames conflict with existing certnames stored in the node inventory connections database. 
     

The following table shows the list of parameters for ssh and winrm types.

SSH and WinRM parameters

A successful response for the POST/command/create-connection request is a status code of 201 with a JSON object containing the connection_id for each connection's record. This endpoint also inserts each of the provided certnames into PuppetDB if they are not already present. They can be viewed on the Nodes page.

POST/command/delete-connection

This endpoint removes specified certnames from all associated connection entries in the inventory service database. The request body consists of a JSON object with a certnames key. The key defines a list of certnames to be deleted. If the request of processed successfully, it returns a status code of 204 with an empty body.

Query endpoints

There are two query endpoints of the Node Inventory API to obtain the list of inventory service connections.

GET/query/connections

This endpoint lists all the connection entries in the inventory database. It can also be used to request information about a specific connection. The request uses a GET method with authentication. It can include optional parameters in the URI path - certname, sensitive and extract. 

If the querying is successful, the response is a JSON object containing an items key. This key is an array of objects, each representing a  known connection. All connection objects have the following keys.

  • connection_id is the unique identifier for the connections entry.
     
  • certnames contains an array of certnames of the matching connection entries.
     
  • The type can be ssh or winrm.
     
  • parameters describe connection settings.
     
  • sensitive_parameters provide permission to access sensitive information.
     

POST/query/connections

This endpoint retrieves the connection details of a set of certnames. The POST request body is a  JSON object. It can be an empty object or with the certname, sensitive and extract parameters. An empty request body returns information on all known connections without sensitive parameters. Connection details for a specific certname can be retrieved by stating it in the certname parameter. The response received for a correct and valid request is the same as the GET/query/connections endpoint.

Node inventory API errors

All error responses of the Node Inventory API as recorded as JSON objects. The error responses have three keys.

  • kind defines the kind of error encountered.
  • msg defines the error message.
  • details is a hash that stores additional information about the error.
     

The common error encountered while using this API are:

  • 500: unknown-error
  • 406: not-acceptable
  • 416: unsupported-type
  • 400: json-parse-error
  • 400: schema-validation-error
  • 403: not-permitted
  • 409: duplicate-certnames

Frequently Asked Questions

What is the use of the sensitive and extract parameter in the GET query connection endpoint?

Sensitive stores a boolean value indicating if we want the response to include sensitive connection parameters. The extract parameters contain an array of keys that want the response to include.

What does the error and replace value of the duplicates key do?

Specifying error as the value for duplicates will return a 409 response if there are duplicate certnames. Specifying the replace option overwrites the existing certname with the new connection details if there is a duplicate.

What is the difference between ssh and winrm type connection with respect to their sensitive parameters?

In winRM connection type, the sensitive-parameters object is allowed to contain only the password key. This key specifies the password to authenticate the connection. SSH connection type allows two sensitive parameters - password and sudo-password.

Conclusion

This blog discusses the Node Inventory API v1. It briefly explains the four endpoints and the errors encountered while using the API. Check out our articles on Overview of Puppet ServerOverview of PuppetDB and Configuring PuppetDB. Explore our Library on Coding Ninjas Studio to gain knowledge on Data Structures and Algorithms, Machine Learning, Deep Learning, Cloud Computing and many more! Test your coding skills by solving our test series and participating in the contests hosted on Coding Ninjas Studio! 

Looking for questions from tech giants like Amazon, Microsoft, Uber, etc.? Look at the problems, interview experiences, and interview bundle for placement preparations. Upvote our blogs if you find them insightful and engaging! Happy Coding!

Thank you

Live masterclass