3. CAWBS Batch library

Overview

The cawbsbatch.py library provides a streamlined interface for batch job automation in CoreAuto. It facilitates secure authentication, retrieval of keystore credentials, and integration with CoreAuto APIs for executing batch workflows. Designed with simplicity and robustness, cawbsbatch.py is used for managing automation tasks that do not require real-time interaction.

Installation

Ensure that Python 3.6+ is installed on your system. To use the library, copy the provided Python code into your project and configure the required environment variables.

Environment Variables

Before using the library, the following environment variables must be defined:

ENV			Specifies the environment (e.g., "production").
CA_ACCESS_CODE		API access code for authentication.
CA_WBS_URL		Base URL of the CoreAuto API.

Initialization

Init()

Initializes the library by authenticating with the API and setting up necessary headers.

  • Returns:
    • status_code: HTTP response code.
    • error: Error message (if any).
  • Possible Error Codes:
    • 601: Missing environment variables.
    • 602: Initialization already completed.
    • 401: Invalid access credentials.
import cawbsbatch

response = cawbsbatch.Init()
if response['status_code'] == 200:
    print("Initialization successful!")
else:
    print(f"Initialization failed: {response['error']}")


GetKeystore(keylist)

Fetches values from the keystore based on a comma-separated list of keys.

  • Parameters:
    • keylist: Comma-separated string of key names.
  • Returns:
    • status_code: HTTP response code.
    • answer: Dictionary containing key-value pairs.
  • Possible Error Codes:
    • 603: Initialization required.
    • 605: Specific key not found.
response = cawbsbatch.GetKeystore("db_host,db_user,db_password")
if response['status_code'] == 200:
    credentials = response['answer']
    print("Keystore retrieved successfully:", credentials)
else:
    print(f"Keystore fetch error: {response['error']}")

Error Handling

The library provides consistent error handling with structured responses. Use the status_code and error fields in the return values to diagnose and handle issues effectively.

Common Error Scenarios

  • Uninitialized Library: Always call Init() before using other functions.
  • Missing Environment Variables: Ensure all required environment variables are defined.
  • Invalid Keys: Verify the requested keys exist in the CoreAuto keystore.

Best Practices

  1. Secure Environment Variables:
    • Use environment management tools like .env files or cloud secret managers to define ENV, CA_ACCESS_CODE, and CA_WBS_URL.
    • Rotate access codes periodically.
  2. Error Logging:
    • Log errors locally or in a monitoring system to facilitate debugging.
  3. Key Validation:
    • Before using GetKeystore(), confirm the required keys exist in the CoreAuto keystore.
  4. Batch Workflow:
    • Use cawbsbatch.py for non-real-time workflows to simplify interactions with CoreAuto APIs.

Examples


Example: Fetching Database Credentials

import cawbsbatch

# Step 1: Initialize the library
response = cawbsbatch.Init()
if response['status_code'] != 200:
    print(f"Initialization failed: {response['error']}")
    exit(1)

# Step 2: Retrieve database credentials
response = cawbsbatch.GetKeystore("db_host,db_name,db_user,db_password")
if response['status_code'] == 200:
    credentials = response['answer']
    db_host = credentials['db_host']
    db_name = credentials['db_name']
    db_user = credentials['db_user']
    db_password = credentials['db_password']
    print("Database credentials retrieved successfully!")
else:
    print(f"Failed to fetch keystore: {response['error']}")
    exit(1)


Example: Handling Missing Keys

response = cawbsbatch.GetKeystore("invalid_key")
if response['status_code'] == 605:
    print(f"Key not found: {response['error']}")
else:
    print("Keys retrieved:", response['answer'])