Add an Action to an Environment Template - Rafay Product Documentation

Actions

In this section, you will update the "environment template" to use an action. The action will be configured to control the logic defined within the Python code of a function workflow handler.


Step 1: Update Workflow Handler

In this step, you will update the existing Workflow Handler used in the Introductory Exercise. You will update the WorkflowHandler function code logic to handle different action types.

   from typing import Dict, Any
   from logging import Logger

def handle(logger: Logger, request: Dict[str, Any]) -> Dict[str, Any]:
       logger.info(f"Inside function handler, request: {request}", extra={"request": request})

action = request.get("action")

if action == "deploy":
           logger.info("hello world")
           return {
               "output": "hello world",
               "action": action
           }

elif action == "retry":
           logger.info("hello again")
           return {
               "output": "hello again",
               "action": action
           }

else:
           logger.info("unknown action")
           return {
               "output": "unknown action",
               "action": action
           }

Step 2: Update Environment Template

In this step, you will update the existing Environment Template to include an Action. The Action will use the same workflow handler, however, now the logic will be controlled by the Action the user selects.

Next, we will create an input variable that will pass the action value to the workflow handler.

   ## script begin ##
   def eval(**ctx):
     action_name = ctx["trigger"]["payload"].get("action_name")
     if action_name:
       return action_name
     return ctx["trigger"]["payload"].get("action")
   ## script end ##

Note: In the case you want to use a selector to refine where this variable is passed to, you can use environment.action.. to pass the action name to the action


Step 3: Run Action

In this step, we will create a new environment using the recently updated environment template. Once the environment is created, we will run the retry action on the environment to see the output that should be displayed from this action.

After the environment is created, you will see the results sections shows the "deploy" action and the output "hello world".

Next, you will run the previously created retry action on the environment to produce a different output.

Once the action has finished running:

You will see the action is not "retry" and the output is "hello again". The output is different as the logic in the workflow handler was set to return this output value if the action was equal to "retry".


Recap

In this part, you created an action on your environment template. The action was configured to trigger different logic within the workflow handler.