Validate and limit service inputs with decorators
This topic describes how you can use decorators within IAG to limit the inputs passed into a service that you execute using the iagctl run service command.
A decorator defines the inputs a service expects: which parameters it accepts, what type they must be, and which are required. When a user runs a service through a workflow, the decorator validates that the correct inputs were provided before execution begins. A single decorator can be shared across multiple services that accept the same inputs, reducing duplication. Think of a decorator as a form definition that you attach to one or more services.
Decorators in playbooks
To best understand decorators, consider an Ansible Playbook service called simple-ansible that uses the following playbook.
Notice that the example playbook takes in a single variable called caller. When running the service, you can use the --set flag to pass a value for caller to the playbook.
JSON Schema in decorators
Suppose that you want to limit the types of inputs that can pass to the playbook using the --set flag. Decorators allow you to limit the available parameters by using defined JSON Schema standards.
Consider the example JSON Schema shown below that limits the available inputs to a key of caller with values of documentation or learner.
You can explore all available options within JSON Schema by referencing the official spec.
Create a decorator resource
First, save the JSON schema above as a file called simple-deco.json.
Next, create a decorator resource called simple-deco within IAG using the json schema file that you created in the previous step.
Create a gateway service with decorators
Once you create the decorator resource, create a service that uses it. The following example creates an Ansible Playbook service with the simple-deco decorator.
Set the --use flag when using the service run command to get basic, high-level information about the decorator.
Successful decoration
When you pass in a valid value for caller and run the Ansible playbook service, the playbook succeeds.
Decoration error
If you try to pass in a value for caller that is not documentation or learner, you receive an error.
Additionally, if you try to set any value for a key that is not caller, an error returns.
Boolean properties in Python script services
Decorators support boolean as a property type for Python script services. Boolean properties enable flag-style argument passing — instead of --set verbose=true, you pass --set verbose to set the flag, and omit it entirely to leave it unset.
Define a boolean property
To define a boolean property, set "type": "boolean" in the decorator schema:
Pass boolean arguments at runtime
When a property is defined as boolean in the decorator, use bare --set key syntax to pass the flag. Omit the key entirely to leave it unset.
How IAG passes the flag to your script depends on whether the service has a decorator with the property typed as boolean:
With a decorator, IAG passes the flag as --verbose. Parse it in your script using action='store_true':
Without a decorator, IAG passes the flag as --verbose=true. Parse it as a string value:
Learn more
For more information on decorator operations, see the following iagctl CLI commands: