/
Python Script: sample_script_session_vars 1.py

Python Script: sample_script_session_vars 1.py

Description

The sample_script_session_vars 1.py script defines a MockEventHandler class that implements the IEventHandler interface using the zope.interface library. The primary purpose of this class is to process events by performing various operations on a session object provided in the context. The script includes logging statements to track the session's state before and after each operation.

This script is used to store user login credentials, environment variables as set by the user in the Cloud Services Portal, and session variables (key and var). This session is persistent. The script is uploaded to the Cloud Services Portal through the Application tab (Cloud Services Portal > Configuration> Integrations > Data Connector > Automations).

This script is utilized when customers upload their own script instead of subscribing to an AppDirect marketplace script. If customers are using automation scripts from the marketplace, there is no need to upload this script. For information on uploading your own scripts for use with Data Connector, see Uploading Python Automation Scripts for Use with the Cloud Services Portal.

What the Script Does

  1. Logging Environment Variables and Event:

    • The process method logs the environment variables and the event being processed.

  2. Session Operations:

    • Add Operations: Adds key-value pairs ('key-1', 'value-1'), ('key-2', 'value-2'), and ('key-3', 'value-3') to the session.

    • Get Operations: Retrieves and logs the values associated with 'key-1', 'key-2', and 'key-3'.

    • Delete Operation: Deletes 'key-3' from the session and logs the session state.

    • Clear Operation: Clears all entries from the session and logs the session state.

import interface import zope import os import logging log = logging.getLogger('infoblox_driver') @zope.interface.implementer(interface.IEventHandler) class MockEventHandler:     def __init__(self, attributes=None):         pass     def process(self, context, event):         log.info("Read environments...")         env_items = os.environ.items()         log.info(f"Processing the event: {event}\nenvironment items: {env_items}")         sess = context.session         # session add operations         log.info("Session before add operation: {}".format(sess.items()))         sess.add('key-1', 'value-1')         log.info("Session after add operation: {}".format(sess.items()))         key=sess.get('key-1')         log.info("Get key operation")         log.info(key)         log.info("Session before add operation: {}".format(sess.items()))         sess.add('key-2', 'value-2')         log.info("Session after add operation: {}".format(sess.items()))         key1=sess.get('key-2')         log.info("Get key operation-1")         log.info(key1)         log.info("Session before add operation: {}".format(sess.items()))         sess.add('key-3', 'value-3')         log.info("Session after add operation: {}".format(sess.items()))         key2=sess.get('key-3')         log.info("Get key operation-2")         log.info(key2)         #session delete operation         sess.delete('key-3')         log.info("Deleted success-key-3")         log.info("Session after delete operation: {}".format(sess.items()))         # session clear operation         log.info("Session before clear operation: {}".format(sess.items()))         sess.clear()         log.info("Session after clear operation: {}".format(sess.items()))