Creating webhook workflows with the API
To implement a webhook, you create a workflow that specifies parameters such as the HTTP URL, HTTP method, and payload template. You can then use this workflow in your automation policy to orchestrate how actions execute.
For more information, see Workflows Endpoint.
To create a workflow, you can use the API to POST a Workflow object to Turbonomic instance. For example, the following curl commands get
authorization to access a Turbonomic server, and then add a simple webhook
workflow to that server:
Authenticate on the server
This command requests authentication credentials and stores them in a variable you can set to a cookie in subsequent curl headers, where:
<T8c_IP_ADDRESS>is the address of the Turbonomic server<ADMIN_ACCOUNT_NAME>is the name of an account with admin privileges<ADMIN_PWD>is the admin account password
JSESSIONID=$(curl \ --silent \ --cookie-jar - \ --insecure \ https://<T8c_IP_ADDRESS>/vmturbo/rest/login \ --data "username=<ADMIN_ACCOUNT_NAME>&password=<ADMIN_PWD>" \ | awk '/JSESSIONID/{print $7}')Create the workflow
This command creates the workflow on the server, where:
<T8c_IP_ADDRESS>is the address of the Turbonomic server<WEBHOOK_ADDRESS>is the address of the webhook server
curl \ "https://<T8c_IP_ADDRESS>/api/v3/workflows" \ --insecure \ --compressed \ --header 'Accept: application/json' \ --header 'Content-Type: application/json' \ --header "cookie: JSESSIONID=$JSESSIONID" \ --request POST \ --data ' { "displayName": "My_WebHook", "className": "Workflow", "description": "First webhook attempt.", "discoveredBy": { "readonly": false }, "type": "WEBHOOK", "typeSpecificDetails": { "url": "http://<WEBHOOK_ADDRESS>", "method": "POST", "template": "{ \"text\":\"My Webhook Template -- DATA: Action Details: $action.details\" }", "type": "WebhookApiDTO" } } '
This is a simple webhook that sends its template to the indicated URL.
The template payload is the string My Webhook Template -- DATA: Action
Details:, plus the action details that are included in the action's data object. The
variable $action.details is a reference to a field in the
ActionApiDTO object that represents the current action. Your template can reference
any of the fields in this DTO, starting with action as the object name. For
example, $action.createTime gives you the time the action was created. For a full
listing of the ActionApiDTO object, see ActionApiDTO or the API Swagger UI.
For pre-defined request payload examples, see the Turbonomic Orchestration GitHub repository.
Sample webhook application
A webhook workflow sends a message to an application via HTTP. You express the message as a template that can include values from the action data in its payload. This template can express text, JSON, or any other payload that your application can accept.
You can use webhooks to send messages to a number of existing applications, including Slack, Amazon Web Services, and others.
To deploy a simple example, and to test your webhook templates, you can implement a node.js server that receives the webhook message and prints out the template data. If you install this server on a machine in your network, then you can give its URL in the webhook workflow, and test your response to specific actions.
Following is a listing for a node.js web server that you can use.
let port = 9090;
const http = require("http");
console.log(`Starting server on port ${port}`);
http.createServer((request, response) => {
request.setEncoding('utf8');
console.log('REQUEST METHOD: ', request.method);
let datStr = '';
request.on('data', chunk => {datStr = datStr + chunk});
request.on('end', () => {console.log('End of DATA: ', datStr)})
}).listen(port);
When you run this program, it prints a message to the console to say that it is running, and to identify the port it listens on.
When the server receives a message, it prints out the request method, and then
prints out the message payload, as specified in the workflow's template field.
If you have configured an Automation Policy to use this workflow, then this server will log a message for each action that Turbonomic executes on an entity within the policy's scope.