|
| 1 | +# Automating the deployment and execution of rulesets with the REST API |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +This sample demonstrates how to automate the deployment and execution of a ruleset using the REST API. |
| 6 | + |
| 7 | +In this sample, you will: |
| 8 | +- Deploy IBM ODM on either Kubernetes or Docker. |
| 9 | +- Optionally import a Decision Service into Decision Center. |
| 10 | +- Deploy a ruleset from Decision Center to Decision Server. |
| 11 | +- Execute the deployed ruleset. |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | +Ensure you have the following tools installed: |
| 16 | +- Docker 24.0.x or later, or Kubernetes 1.27+. |
| 17 | +- [jq for JSON processing](https://jqlang.github.io/jq/download/) |
| 18 | + |
| 19 | +## Installation of ODM |
| 20 | + |
| 21 | +Select your preferred deployment method and follow the corresponding guide: |
| 22 | + * [Kubernetes](README-KUBERNETES.md) |
| 23 | + * [Docker](README-DOCKER.md) |
| 24 | + |
| 25 | +## Configuration |
| 26 | + |
| 27 | +### 1. Set Environment Variables |
| 28 | + |
| 29 | +Define the following environment variables: |
| 30 | + |
| 31 | +<!-- markdown-link-check-disable --> |
| 32 | + |
| 33 | +| Name | Description | Value on Kubernetes | Value on Docker | |
| 34 | +| - | - | - | - | |
| 35 | +| DC_API_URL | Decision Center API URL | see below | http://localhost:9060/decisioncenter-api | |
| 36 | +| DSR_URL | Decision Server Runtime URL | see below | http://localhost:9060/DecisionService | |
| 37 | + |
| 38 | +<!-- markdown-link-check-enable--> |
| 39 | +If ODM is deployed on Kubernetes, you can check the **Access the ODM services** section for your platform to find out how to get those URLs: |
| 40 | +- [Amazon EKS](https://github.com/DecisionsDev/odm-docker-kubernetes/blob/master/platform/eks/README.md#6-access-the-odm-services) |
| 41 | +- [Azure](https://github.com/DecisionsDev/odm-docker-kubernetes/blob/master/platform/azure/README.md#access-odm-services) |
| 42 | +- [Google GKE](https://github.com/DecisionsDev/odm-docker-kubernetes/blob/master/platform/gcloud/README.md#6-access-odm-services) |
| 43 | +- [Minikube](https://github.com/DecisionsDev/odm-docker-kubernetes/blob/master/platform/minikube/README.md#c-access-the-odm-services) |
| 44 | +- [Openshift](https://github.com/DecisionsDev/odm-docker-kubernetes/blob/master/platform/roks/README.md#4-access-the-odm-services) |
| 45 | + |
| 46 | +### 2. Deploy the `Loan Validation Service` Decision Service |
| 47 | +If the `Loan Validation Service` is not available in Decision Center, import it using the following commands: |
| 48 | + |
| 49 | +```bash |
| 50 | +# configure the Authentication (using Basic Auth) |
| 51 | +export AUTH_CREDENTIALS=(--user "odmAdmin:odmAdmin") |
| 52 | + |
| 53 | +export DECISION_SERVICE_NAME="Loan Validation Service" |
| 54 | +export DS_FILENAME="${DECISION_SERVICE_NAME// /_}.zip" # replace spaces by underscores |
| 55 | +export DS_FILENAME_URLENCODED="${DECISION_SERVICE_NAME// /%20}.zip" # replace spaces by %20 |
| 56 | + |
| 57 | +# download the Decision Service zip file |
| 58 | +curl -sL -o ${DS_FILENAME} "https://github.com/DecisionsDev/odm-for-dev-getting-started/blob/master/${DS_FILENAME_URLENCODED}?raw=1" |
| 59 | + |
| 60 | +# upload the Decision Service in Decision Center |
| 61 | +curl -sk -X POST ${AUTH_CREDENTIALS[@]} -H "accept: application/json" -H "Content-Type: multipart/form-data" \ |
| 62 | + --form "file=@${DS_FILENAME};type=application/zip" \ |
| 63 | + "${DC_API_URL}/v1/decisionservices/import" |
| 64 | +``` |
| 65 | + |
| 66 | +## Running the sample |
| 67 | + |
| 68 | +```bash |
| 69 | +# configure the Authentication (using Basic Auth) |
| 70 | +export AUTH_CREDENTIALS=(--user "odmAdmin:odmAdmin") |
| 71 | + |
| 72 | +# get the ID of the Decision Service in Decision Center |
| 73 | +export DECISION_SERVICE_NAME="Loan Validation Service" |
| 74 | +export DECISION_SERVICE_NAME_URLENCODED="${DECISION_SERVICE_NAME// /%20}" # replace spaces by %20 |
| 75 | +export GET_DECISIONSERVICE_RESULT=$(curl -sk -X GET ${AUTH_CREDENTIALS[@]} -H "accept: application/json" "${DC_API_URL}/v1/decisionservices?q=name%3A${DECISION_SERVICE_NAME_URLENCODED}") |
| 76 | +export DECISIONSERVICEID=$(echo ${GET_DECISIONSERVICE_RESULT} | jq -r '.elements[0].id') |
| 77 | + |
| 78 | +# get the ID of the deployment configuration in Decision Center |
| 79 | +export DEPLOYMENT_NAME="production deployment" |
| 80 | +export DEPLOYMENT_NAME_URLENCODED="${DEPLOYMENT_NAME// /%20}" # replace spaces by %20 |
| 81 | +export GET_DEPLOYMENT_RESULT=$(curl -sk -X GET ${AUTH_CREDENTIALS[@]} -H "accept: application/json" "${DC_API_URL}/v1/decisionservices/${DECISIONSERVICEID}/deployments?q=name%3A${DEPLOYMENT_NAME_URLENCODED}") |
| 82 | +export DEPLOYMENTCONFIGURATIONID=$(echo ${GET_DEPLOYMENT_RESULT} | jq -r '.elements[0].id') |
| 83 | + |
| 84 | +# deploy the ruleapp from Decision Center |
| 85 | +curl -sk -X POST ${AUTH_CREDENTIALS[@]} -H "accept: application/json" "${DC_API_URL}/v1/deployments/${DEPLOYMENTCONFIGURATIONID}/deploy" | jq |
| 86 | + |
| 87 | +# execute the ruleset that was deployed |
| 88 | +export RULESET_PATH="/production_deployment/loan_validation_production" |
| 89 | +curl -sk -X POST ${AUTH_CREDENTIALS[@]} -H "accept: application/json" -H "Content-Type: application/json" -d "@payload.json" ${DSR_URL}/rest${RULESET_PATH} | jq |
| 90 | +``` |
| 91 | + |
| 92 | +> [!NOTE] |
| 93 | +> The commands above rely on the Basic Authentication. |
| 94 | +> |
| 95 | +> - In an environment with OpenID Connect, the authentication can be performed using an access token retrieved with the `client_credentials` grant type by: |
| 96 | +> - setting the environment variables |
| 97 | +> - CLIENT_ID |
| 98 | +> - CLIENT_SECRET |
| 99 | +> - OPENID_TOKEN_URL |
| 100 | +> - and replacing `export auth_credentials=(--user "odmAdmin:odmAdmin")` by the commands below: |
| 101 | +> ```bash |
| 102 | +> export ACCESS_TOKEN=$(curl -sk -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "client_id=${CLIENT_ID}&scope=openid&client_secret=${CLIENT_SECRET}&grant_type=client_credentials" ${OPENID_TOKEN_URL} | jq -r '.access_token') |
| 103 | +> export AUTH_CREDENTIALS=(-H "Authorization: Bearer ${ACCESS_TOKEN}") |
| 104 | +> ``` |
| 105 | +> - In a CP4BA environment, the authentication can be performed using a [Zen API key](https://www.ibm.com/docs/en/cloud-paks/cp-biz-automation/24.0.1?topic=access-using-zen-api-key-authentication). To do so: |
| 106 | +> - set the environment variables |
| 107 | +> - USERNAME |
| 108 | +> - ZEN_API_KEY |
| 109 | +> - replace `export auth_credentials=(--user "odmAdmin:odmAdmin")` by the commands below: |
| 110 | +> ```bash |
| 111 | +> export BASE64ENCODED_USERNAME_AND_APIKEY=$(echo -n "${USERNAME}:${ZEN_API_KEY}" | base64) |
| 112 | +> export AUTH_CREDENTIALS=(-H "Authorization: ZenApiKey ${BASE64ENCODED_USERNAME_AND_APIKEY}") |
| 113 | +> ``` |
| 114 | +
|
| 115 | +## Expected result |
| 116 | +
|
| 117 | +1. After deploying the ruleset, you should see a confirmation message: |
| 118 | +
|
| 119 | + ``` |
| 120 | + { |
| 121 | + "id": "e51db153-bca2-49a8-8998-2b5e6c93f6cb", |
| 122 | + "internalId": "dsm.DSDeploymentReport:35:35", |
| 123 | + "name": "Report 2025-01-27_11-11-37-203", |
| 124 | + "createdBy": "odmAdmin", |
| 125 | + "createdOn": "2025-01-27T10:11:37.000+00:00", |
| 126 | + "lastchangedBy": "odmAdmin", |
| 127 | + "lastChangedOn": "2025-01-27T10:11:51.000+00:00", |
| 128 | + "status": "COMPLETED", |
| 129 | + "ruleAppName": "production_deployment", |
| 130 | + "messages": { |
| 131 | + "elements": [ |
| 132 | + { |
| 133 | + "message": "The version '1.0' of the ruleset 'loan_validation_production' was successfully deployed to Decision Service Execution (http://localhost:9060/res).", |
| 134 | + "ruleArtifact": null, |
| 135 | + "severity": "INFO" |
| 136 | + }, |
| 137 | + { |
| 138 | + "message": "The RuleApp was successfully deployed to Decision Service Execution (http://localhost:9060/res).", |
| 139 | + "ruleArtifact": null, |
| 140 | + "severity": "INFO" |
| 141 | + } |
| 142 | + ], |
| 143 | + "totalCount": 2, |
| 144 | + "number": 0, |
| 145 | + "size": 2 |
| 146 | + }, |
| 147 | + "snapshot": { |
| 148 | + "id": "f5efc08f-23d2-46ef-bed8-515fec6b46cf", |
| 149 | + "internalId": "dsm.DsDeploymentBsln:71:71", |
| 150 | + "name": "production-deployment_2025-01-27_11-11-37-153", |
| 151 | + "createdBy": "odmAdmin", |
| 152 | + "createdOn": "2025-01-27T10:11:37.000+00:00", |
| 153 | + "lastchangedBy": "odmAdmin", |
| 154 | + "lastChangedOn": "2025-01-27T10:11:38.000+00:00", |
| 155 | + "parentId": "1558f25b-daa6-4982-8b0b-48a388c7c202", |
| 156 | + "documentation": null, |
| 157 | + "buildMode": "DecisionEngine", |
| 158 | + "kind": "DeploymentSnapshot" |
| 159 | + }, |
| 160 | + "servers": [ |
| 161 | + "Decision Service Execution" |
| 162 | + ], |
| 163 | + "archive": null |
| 164 | + } |
| 165 | + ``` |
| 166 | + |
| 167 | +2. After executing the ruleset, the response should include the rule execution results: |
| 168 | + ``` |
| 169 | + { |
| 170 | + "report": { |
| 171 | + "borrower": { |
| 172 | + "firstName": "string", |
| 173 | + "lastName": "string", |
| 174 | + "birth": "1988-09-29T01:49:45.000+0000", |
| 175 | + "SSN": { |
| 176 | + "areaNumber": "string", |
| 177 | + "groupCode": "string", |
| 178 | + "serialNumber": "string" |
| 179 | + }, |
| 180 | + "yearlyIncome": 3, |
| 181 | + "zipCode": "string", |
| 182 | + "creditScore": 3, |
| 183 | + "spouse": { |
| 184 | + "birth": "1982-12-08T14:13:09.850+0000", |
| 185 | + "SSN": { |
| 186 | + "areaNumber": "", |
| 187 | + "groupCode": "", |
| 188 | + "serialNumber": "" |
| 189 | + }, |
| 190 | + "yearlyIncome": 0, |
| 191 | + "creditScore": 0, |
| 192 | + "latestBankruptcy": { |
| 193 | + "chapter": 0 |
| 194 | + } |
| 195 | + }, |
| 196 | + "latestBankruptcy": { |
| 197 | + "date": "2014-09-18T23:18:33.000+0000", |
| 198 | + "chapter": 3, |
| 199 | + "reason": "string" |
| 200 | + } |
| 201 | + }, |
| 202 | + "loan": { |
| 203 | + "numberOfMonthlyPayments": 3, |
| 204 | + "startDate": "2025-08-19T17:27:14.000+0000", |
| 205 | + "amount": 3, |
| 206 | + "loanToValue": 1.051732E+7 |
| 207 | + }, |
| 208 | + "validData": true, |
| 209 | + "insuranceRequired": true, |
| 210 | + "insuranceRate": 0.02, |
| 211 | + "approved": true, |
| 212 | + "messages": [], |
| 213 | + "yearlyInterestRate": 0.0, |
| 214 | + "monthlyRepayment": 0.0, |
| 215 | + "insurance": "2%", |
| 216 | + "message": "", |
| 217 | + "yearlyRepayment": 0.0 |
| 218 | + }, |
| 219 | + "__DecisionID__": "aa46362d-fa30-4331-b241-d1ff7e8ac4270" |
| 220 | + } |
| 221 | + ``` |
0 commit comments