Start of change

REST steps

You can design a step to invoke a Representational State Transfer (REST) service. A step that calls a REST service is referred to as a REST step. This topic describes the use of REST steps and the Workflows schema elements that you can use to create them.

Start of changeA typical use for a REST step would be to obtain values for your workflow and assign the values to variables defined in the workflow. Suppose, for example, that your workflow requires an IP address and a port number from a REST endpoint. The REST step can be used to invoke this REST endpoint and map the values from the REST endpoint response body to your IP address and port number variables.End of change

An example of a REST step that obtains an IP address and port number is shown in Figure 1.

Notes:
  1. The REST service to be called must support the use of JSON as the content-type for the request body and the response body.
  2. Start of changeIt is assumed that you understand the layout of the returned data and know where to obtain specific values from the JSON string for mapping to variables.End of change
  3. The returned values can be mapped to instance variables only. You cannot modify the values of global variables.

How the user interacts with a REST step

From the user’s perspective, when a step definition includes the <rest> element, the Workflows task enables the Perform button on the step instructions page. When the user presses Finish, the perform wizard issues the REST request. The behavior of the wizard is further controlled through the sub-elements that you define within the <rest> element.

Elements of a REST step

In a workflow definition, a REST step is defined with the <rest> element and a number of sub-elements and attributes that provide details about the REST request. The details include the HTTP method, URI path, query parameters, request body, expected status code, and actual status code. The <rest> element also defines any mapping properties that you use to save the JSON properties (name and value pairs) in the response body to workflow variables.

Table 1 provides a summary of the REST step elements.
Table 1. Summary of REST step elements

Element Name

Description

Required or optional

Supports substitution

Default value

httpMethod Indicates the HTTP method that is used for issuing the REST request. The following values are valid:
  • GET
  • PUT
  • POST
  • DELETE.
Required No None
schemeName Scheme name that is associated with the REST request. If specified, this element must be set to "http." Optional No Scheme name that is defined for z/OSMF, which is "http" by default.
hostname Host name or IP address of the system to which the REST request is directed. For example: www.ibm.com. Optional Yes Host name that is defined for z/OSMF, which is "*" by default.
port Port number to use for the REST request. Optional Yes Port that is defined for z/OSMF, which is "443" by default.
uriPath URI path to use for the REST request. Required Yes None
queryParameters For a GET or POST request, this element contains the query parameters. Optional Yes None
requestBody For a PUT or POST request, this element contains the request body. Optional Yes None
expectedStatusCode The expected HTTP status code from the REST API request. If this value does not match the actualStatusCode value, the workflow step fails. This behavior is similar to what happens when a job template step returns a return code that is greater than the allowed maximum return code. Required No None
actualStatusCode The actual HTTP status code that is received from the REST request. To obtain this value, map it to a workflow variable. Optional No None
propertyMapping The property from the REST response body that is mapped to a workflow variable. You can specify multiple propertyMapping elements in a REST step. Optional No None
Table notes:
  1. The elements <schemeName>, <hostname>, and <port> are used together. You must specify all of these elements or none of them. Otherwise, the workflow fails validation when the user attempts to import the workflow definition into the Workflows task. If you specify none of these elements, the REST request uses the z/OSMF defaults instead.
  2. You must include the "mapTo" attribute on the <actualStatusCode> element. Set it to the name of the workflow variable that is to receive the actual status code value from the REST request. For example:
    <actualStatusCode mapTo="status_code" />

Mapping the returned data to variables

You can assign the values that are returned from a GET request to variables that are defined in your workflow. To do so, include the <propertyMapping> element on the <rest> tag. This element identifies the required values in the JSON response body and saves the values to workflow variables. On the <propertyMapping> element, specify the value to be assigned on the "mapTo" attribute. You can specify multiple <propertyMapping> elements in a REST step.

Start of changeIn the following example, the workflow owner is defined in a property mapping.
<propertyMapping mapTo="workflow_owner">["name"]</propertyMapping>
If the response body is: 
{ "name": "John" }
the workflow variable "workflow_owner" is assigned the value John.End of change

Specifying substitution variables for REST step elements

Start of changeYou can specify substitution variables as values for the elements <hostname>, <port>, <uriPath>, <queryParameters>, and <requestBody>. Doing so allows you to set the values for these elements dynamically from the workflow variables. End of change

For an element that contains variable substitution, do the following:
  • Include the optional attribute "substitution" and set it to true. If not specified, the default is false. A value of true must be specified for the Workflows task to allow the variable substitution.
  • Set the element value to ${instance-WORKFLOW_VARIABLE_NAME}, where WORKFLOW_VARIABLE_NAME is the workflow variable that is defined in the workflow.
A sample REST step is shown in Figure 1. In this example, variable substitution is used in the <requestBody> element to allow dynamic substitution for the stackId and stackname properties.
Figure 1. Sample REST step definition with substitution variables and property mapping variables
<step name="get_ip_and_port_data">
        <title>Get values for workflow variables ip_addr and port from REST API call.</title>
        <description>Invoke RESTful API to get IP address and port number.</description>
	<prereqStep name="get_stack_data_for_rest_request"/>
        <instructions substitution="false">	Execute step to retrieve the IP address and port number 
        from z/OS Communications Server.</instructions>
        <weight>10</weight>
        <skills>REST</skills>
		<rest>
		<httpMethod>POST</httpMethod>
		<uriPath>/zosmf/workflow/WorkflowManager/cloud/ipaddr/</uriPath>
		<requestBody substitution="true">
{
	"name" : "GGAIPA",
	"description" : "IP address created by GGA", 
	"ipaddr" : "1.1.1.2",
	"usageType" : "Internal",
	"workloadDeploymentGroup" : "/wdg/12345",
	"deploymentId" : "deployerA",
	"recoveryMethod" : "MANUAL_DISRUPTIVE", 
	"hostName" : "mvstst1",
	"stack" :  
		{
			"stackId" : "${instance-stack_id}",	
			"stackname" : "${instance-stack_name}"
		},
	"boundTcpPorts"	: [
		"4",
		"81"
	]
}			
		</requestBody>
		<expectedStatusCode>201</expectedStatusCode>
		<actualStatusCode mapTo="status_code" />
		<propertyMapping mapTo="ip_address">["ipaddr"]</propertyMapping>
		<propertyMapping mapTo="port0">["boundPorts"][0]</propertyMapping>
		<propertyMapping mapTo="port1">["boundPorts"][1]</propertyMapping>
		</rest>
	</step> 
In this example, several returned values are mapped to workflow variables, as follows:
  • actualStatusCode is mapped to the "status_code" variable.
  • Start of change"ip_address" property is mapped to the "ipaddr" variable from the JSON object in the response body.End of change
  • Start of change"port0" property is mapped to the first element in an array of ports in the JSON object in the response body.End of change
  • Start of change"port1" property is mapped to the second element in an array of ports in the JSON object in the response body.End of change
End of change