Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example script to create an organisation, repository and project in KIE Workbench, via the REST API.
#!/bin/bash
# --------------------------------------------------------------------------------
#
# Script to demonstrate using the KIE (Drools) Workbench REST API to:
#
# create an organistion.
# create a repository associated with the organisation.
# create a project in the repository.
#
# Based on the documentation here:
# https://docs.jboss.org/drools/release/6.5.0.Final/drools-docs/html/ch20.html
#
# At time of writing, the official documentation seems to be a little bit behind
# the current state of the API. Therefore, if you use the example entities provided
# in the documentation, the API calls will not work.
#
# Some of the values hardcoded below (URL, username, password), are based
# on those defined in the Drools Workbench Showcase Docker image:
# https://hub.docker.com/r/jboss/drools-workbench-showcase/
# I would recommend turning those into arguments or environment variables if you
# intend to make use of this script. I have done that here, just to keep everything
# for the example in one place. Please, don't keep the hardcoded password!
#
# --------------------------------------------------------------------------------
# --------------------------------------------------------------------------------
# First, we create an organisation
# --------------------------------------------------------------------------------
API_RESPONSE=`curl -X POST -H "Content-Type: application/json" --user admin:admin \
127.0.0.1:8080/drools-wb/rest/organizationalunits \
-d '{ "name": "com.sctrcd.kiewb", \
"description": "Example Workbench Organisation", \
"owner": "Scattercode", \
"defaultGroupId": "com.sctrcd.kiewb" }'`
echo "API_RESPONSE: "
echo "$API_RESPONSE"
echo ""
JOB_STATE=`echo $API_RESPONSE | jq -c '. | {status}'`
JOB_ID=`echo $API_RESPONSE | jq -c '. | {jobId}'`
JOB_ID=${JOB_ID#'{"jobId":"'}
JOB_ID=${JOB_ID%'"}'}
echo "JOB_STATE: $JOB_STATE"
echo "JOB_ID: $JOB_ID"
if [ "$JOB_STATE" != '{"status":"APPROVED"}' ]
then
echo "Request rejected. Request state: $JOB_STATE"
exit 1
fi
# All jobs are async. We need to keep checking the state of the job until it is flagged as SUCCESS or fails.
while [[ $JOB_STATE == '{"status":"APPROVED"}' || $JOB_STATE == '{"status":"ACCEPTED"}' ]]; do
JOB_STATE=`curl 127.0.0.1:8080/drools-wb/rest/jobs/$JOB_ID --user admin:admin | jq -c '. | { status }'`
echo "JOB_STATE: $JOB_STATE"
sleep 1s
done
if [ "$JOB_STATE" != '{"status":"SUCCESS"}' ]
then
echo "Request accepted, but failed. Job state: $JOB_STATE"
if [ "$JOB_STATE" != '{"status":"BAD_REQUEST"}' ]
then
# A BAD_REQUEST state indicates that the resource is already there.
exit 1
fi
fi
echo "Request succeeded. Job state: $JOB_STATE"
# --------------------------------------------------------------------------------
# Now that we have an organisation, we can create a repository.
# --------------------------------------------------------------------------------
API_RESPONSE=`curl -X POST -H "Content-Type: application/json" --user admin:admin \
127.0.0.1:8080/drools-wb/rest/repositories \
-d '{ "name": "rulesrepo", \
"description": "Example rules repo", \
"userName": null, "password": null, "gitURL": null, \
"requestType": "new", \
"organizationalUnitName": "com.sctrcd.kiewb" }'`
echo ""
echo "API_RESPONSE: "
echo "$API_RESPONSE"
echo ""
JOB_STATE=`echo $API_RESPONSE | jq -c '. | {status}'`
JOB_STATE=`echo $API_RESPONSE | jq -c '. | {status}'`
JOB_ID=`echo $API_RESPONSE | jq -c '. | {jobId}'`
JOB_ID=${JOB_ID#'{"jobId":"'}
JOB_ID=${JOB_ID%'"}'}
echo "JOB_STATE: $JOB_STATE"
echo "JOB_ID: $JOB_ID"
if [ "$JOB_STATE" != '{"status":"APPROVED"}' ]
then
echo "Request rejected. Request state: $JOB_STATE"
exit 1
fi
# All jobs are async. We need to keep checking the state of the job until it is flagged as SUCCESS or fails.
while [[ $JOB_STATE == '{"status":"APPROVED"}' || $JOB_STATE == '{"status":"ACCEPTED"}' ]]; do
JOB_STATE=`curl metis:8080/drools-wb/rest/jobs/$JOB_ID --user admin:admin | jq -c '. | { status }'`
echo "JOB_STATE: $JOB_STATE"
sleep 1s
done
if [ "$JOB_STATE" != '{"status":"SUCCESS"}' ]
then
echo "Request accepted, but failed. Job state: $JOB_STATE"
if [ "$JOB_STATE" != '{"status":"BAD_REQUEST"}' ]
then
# A BAD_REQUEST state indicates that the resource is already there.
exit 1
fi
fi
echo "Request succeeded. Job state: $JOB_STATE"
# --------------------------------------------------------------------------------
# Now that we have a repository, lets create a project in it.
# --------------------------------------------------------------------------------
API_RESPONSE=`curl -X POST -H "Content-Type: application/json" --user admin:admin \
127.0.0.1:8080/drools-wb/rest/repositories/rulesrepo/projects/ \
-d '{ "name": "rulesproject", \
"description": "Example rules project" }'`
echo ""
echo "API_RESPONSE: "
echo "$API_RESPONSE"
echo ""
JOB_STATE=`echo $API_RESPONSE | jq -c '. | {status}'`
JOB_STATE=`echo $API_RESPONSE | jq -c '. | {status}'`
JOB_ID=`echo $API_RESPONSE | jq -c '. | {jobId}'`
JOB_ID=${JOB_ID#'{"jobId":"'}
JOB_ID=${JOB_ID%'"}'}
echo "JOB_STATE: $JOB_STATE"
echo "JOB_ID: $JOB_ID"
if [ "$JOB_STATE" != '{"status":"APPROVED"}' ]
then
echo "Request rejected. Request state: $JOB_STATE"
exit 1
fi
# All jobs are async. We need to keep checking the state of the job until it is flagged as SUCCESS or fails.
while [[ $JOB_STATE == '{"status":"APPROVED"}' || $JOB_STATE == '{"status":"ACCEPTED"}' ]]; do
JOB_STATE=`curl metis:8080/drools-wb/rest/jobs/$JOB_ID --user admin:admin | jq -c '. | { status }'`
echo "JOB_STATE: $JOB_STATE"
sleep 1s
done
if [ "$JOB_STATE" != '{"status":"SUCCESS"}' ]
then
echo "Request accepted, but failed. Job state: $JOB_STATE"
if [ "$JOB_STATE" != '{"status":"BAD_REQUEST"}' ]
then
# A BAD_REQUEST state indicates that the resource is already there.
exit 1
fi
fi
echo "Request succeeded. Job state: $JOB_STATE"
@gratiartis

This comment has been minimized.

Copy link
Owner Author

@gratiartis gratiartis commented Mar 20, 2017

A bit verbose. I might refactor to wrap the job state checks into a function call, which ought to trim things down.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.