Lesson 2 - First Operation¶
Goal¶
In this lesson we’ll write our first operation. We’ll learn the basic structure of a simple operation by writing one that simply prints out a message.
Get Started¶
Let’s open the print.sl file and start writing the print operation. The print operation is as simple as they get. It just takes in a input and prints it out using Python.
Namespace¶
All CloudSlang files start with a namespace which mirrors the folder structure in which the files are found. In our case we’ve put print.sl in the tutorials/base folder so our namespace should reflect that.
namespace: tutorials.base
The namespace can be used by flows that call this operation.
Operation Name¶
Each operation begins with the operation key which will map to the
contents of the operation body. The first part of that body is a
key:value pair defining the name of the operation. The name of the
operation must be the same as the name of the file it is stored in.
operation:
name: print
YAML Note: Indentation is **very** important in YAML. It is used to
indicate scope. In the example above, you can see that
``name: print`` is indented under the ``operation`` key to denote
that it belongs to the operation. **Always** use spaces to indent.
**Never** use tabs.
Inputs¶
After the name, if the operation takes any inputs, they are listed under
the inputs key. In our case we’ll need to take in the text we want
to print. We’ll name our input text.
inputs:
- text
YAML Note: The ``inputs`` key maps to a list of inputs. In YAML, a
list is signified by prepending a hypen and a space (- ) to each
item.
The values for the inputs are either passed via the CloudSlang CLI, as we do below in this lesson, or from a task in a flow, as we will do in the next lesson.
Inputs can also have related parameters, such as required,
default, overridable and system_property. We will discuss
these parameters in lessons 8 - Input Parameters and
13 - System Properties.
Action¶
Finally, we’ve reached the core of the operation, the action. There are two types of actions in CloudSlang, Python-based actions and Java-based actions.
We’ll start off by creating a Python action that simply prints the text
that was input. To do so, we add an action key that maps to the
action contents. Since our action is a python script we add a key:value
pair with python_script as the key and the script itself as the
value.
action:
python_script: print text
Note: CloudSlang uses the Jython implementation of Python 2.7. For information on Jython’s limitations, see the Jython FAQ.
Python scripts that need 3rd party packages may import them using the procedures described in lesson 14 - 3rd Party Python Packages.
The usage of Java-based actions is beyond the scope of this tutorial. For more information, see the CloudSlang DSL reference.
Run It¶
That’s it. Our operation is all ready. Our next step will be to create a flow that uses the operation we just wrote, but we can actually just run the operation as is.
To do so, save the operation file, fire up the CloundSlang CLI and enter the following at the prompt to run your operation:
run --f <folder path>/tutorials/base/print.sl --i text=Hi
You should see the input text printed out to the screen.
Up Next¶
In the next lesson we’ll write a flow that will call the print operation.
New Code - Complete¶
print.sl
namespace: tutorials.base
operation:
name: print
inputs:
- text
action:
python_script: print text