if

Use the if construct when you want to execute a portion of your assembly only when a specific condition is fulfilled.

Gateway support

Table 1. Table showing which gateways support this policy, and the corresponding policy version
Gateway Policy version
DataPower® Gateway (v5 compatible) 1.0.0
DataPower API Gateway, functionality provided by switch  

This topic describes how to configure the policy in your OpenAPI source; for details on how to configure the policy in the assembly user interface, see if.

About

The if policy has the following format:
- if:
  version: version
  title: title
  description: description
  condition: 'condition_1
      execute: 
        policy_assembly_1 ...

In the condition field, use the form apim.getvariable('context.location.variable') to reference your variables, where context is the context that you want to reference, location is the location of the variable within that context, and variable is the name of the variable.

The execute: section can define any policy assembly, including further if policies. For more information, see execute.

Properties

Table 2. if policy properties
Property Required Description Data type
version Yes The policy version number string
title No A title for the policy. string
description No A policy description. string
condition Yes A script that returns true or false. Use GatewayScript for a DataPower Gateway implementation.

For information about the context variables you can use and how to reference them in your script, see API Connect context variables.

string
execute Yes The policy assembly that you want to execute if the condition returns true. For more information, see execute. string

Example

# carry out different redaction actions depending on the operation

- if:
  version: 1.0.0
  title: clear_region_and_set_body
  condition: 'apim.getvariable('request.body.secret') == true'
    execute:
      - redact:
          title: remove secret field
          actions:
            - action: remove
              from: all
              path: /document/user/secret