Feature #31299
openSupport for additional request with xpath
Description
Webhooks plugin could be used to directly connect to some backend services which are simple enough (e.g. HTTP REST) to do CRUD. Creating resources is straightforward, but to update and delete additional request usually needs to be done to find out resource ID.
For that, we can implement a helpers: http_request and json_xpath, user should be able to use them in URL ERB to generate. For example for Ansible Tower (AWX):
JSON.parse(::ForemanWebhooks::WebhookService.request(url: "https://tower.virt/api/v2/hosts/?name=localhost", user: "admin", password: "admin").body)["results"]0["id"]
The helper will only be able to connect to the webhook service itself, so credentials, URL, SSL info all is already known and helper cannot change that, but it can change: HTTP method, path and payload:
http_request(:GET, '/api/v2/hosts/?name=localhost', '')
This can be chained with an "xpath-like" library (https://github.com/joshbuddy/jsonpath) for complete solution:
json_xpath("//results/[0]/id", http_request(:GET, '/api/v2/hosts/?name=localhost', '')) => 42
The request will be done during request in Rails, not in the background job. This is the only drawback, an optional (short) timeout will be present to solve this issue.
Updated by Lukas Zapletal about 4 years ago
- Related to Tracker #30290: Foreman webhooks integration story added