Conditional routing

This policy routes requests based on defined conditions.

If you have a native API that is hosted at two or more endpoints, you can use the condition-based protocol to route specific types of messages to specific endpoints.

In IBM API Studio, the kind Route identifies this policy.

Example of an Conditional routing policy:

kind: Route
apiVersion: api.ibm.com/v1
metadata:
  name: conditionalRouting
  version: 1.0
  namespace: dev
spec:
  default-endpoint:
    $ref: dev:default_endpoint:1.0
    
  conditional-endpoints:
    - condition: "(${request.httpMethod} equals POST) or (${request.headers.name} notEquals ${request.payload.jsonPath[$.customer.name]})"
      endpoint:
        $ref: dev:conditional_endpoint:1.0
---
kind: HTTPEndpoint
apiVersion: api.ibm.com/v1
metadata:
  name: default_endpoint
  version: 1.0
  namespace: dev
spec:
  url: http://localhost:3000/
  method: GET
  connectTimeout: 300
  readTimeout: 200
  sslConfig:
    keyStoreAlias: ""
    keyAlias: ""
    trustStoreAlias: ""
  serviceRegistryParameters:
    - name: ""
      value: ""

---
kind: HTTPEndpoint
apiVersion: api.ibm.com/v1
metadata:
  name: conditional_endpoint
  version: 1.0
  namespace: dev
spec:
  url: http://endpoint1/
  method: GET
  connectTimeout: 101
  readTimeout: 102
  sslConfig:
    keyStoreAlias: ""
    keyAlias: ""
    trustStoreAlias: ""
  serviceRegistryParameters:
    - name: ""
      value: ""

To invoke the Conditional routing policy, you can specify:

Code view Form view Description
spec Endpoint reference In the form view, you can use the Add endpoint button to specify the endpoint details.

You can configure the following policy specifications:

  • default-endpoint. Specifies the URI of the native API endpoint to route the request to. This endpoint is provided as $ref value in the format $ref: namespace :endpoint name: version
  • Use conditional-endpoints to specify the following metadata:
    • condition. Conditions are used to specify when the policy is run. You can add multiple conditions with logical operators. Provide the condition in the format: {set} operator {value} condition {set} operator {value}

      You can use the following conditions:

      • Transform the requests that comply with all the configured conditions in the code view by using and.
      • Transform the requests that comply with any one configured condition in the code view by using or.

      You can use the following operators:

      • Equals
      • Equals ignore case
      • Not equals
      • Not equals ignore case
      • Contains
      • Not Contains
      • Exists
      • Not Exists
      • IP Range
      • Greater Than
      • Lesser Than
    • end-point. Specify the URI of the conditional endpoint to route the request to.

      This endpoint is provided as $ref value in the format $ref: namespace :conditional endpoint name: version.

To configure the default-endpoint and conditional-endpoints, use the kind: HTTPEndpoint to specify the endpoint details:

Code view Form view Description
spec Endpoint reference This denotes various asset properties that you can configure for the endpoint.

These are the endpoint specifications that can be configured:

  • url. Specifies the URL for the default endpoint.
  • method. Specifies the available HTTP routing methods.
  • connectTimeout. Specifies the number of seconds after which connection timeout is initiated.
  • readTimeout. Specifies the number of seconds after which read timeout is initiated.
  • sslConfig. Specifies the SSL configurations.
    You can configure the following SSL details:
    • keyStoreAlias. Specify the keystore alias to identify the API call. This value (along with the value of Client Certificate Alias) is used for conducting an SSL client authentication.
    • keyAlias. Specify the alias of the private key, which should reside in the keystore specified by the keystore alias.
    • trustStoreAlias. Specify the alias for the truststore containing CA certificates used by IBM API Studio to validate the native API’s SSL certificate.
  • serviceRegistryParameters. Specify values that are used for constructing the discovery service URI. Configure the name and value.