Code Generation for Connections#

For connections, you can use the ibm_watsonx_data_integration.codegen.generator.PythonGenerator class to automate the transformation.

Prerequisites#

To properly generate a script, you will need a correctly configured authenticator class (for example: IAMAuthenticator).

Masking Authentication Credentials#

The part of the generated script will include the creation of an authenticator object based on the one you have configured. By default, the sensitive values, such as api keys, will be replaced with environment variables. If you would like to include sensitive credentials in your script, you can pass mask_credentials=False into the constructor of ibm_watsonx_data_integration.codegen.generator.PythonGenerator.

If you do choose to leave the credentials masked, you can expect the following environment variables to replace their corresponding credentials:

Example usage#

The example below demonstrates how to regenerate Python SDK code from a Connection object fetched directly from a project.

>>> from ibm_watsonx_data_integration.codegen import PythonGenerator
>>>
>>> connection_name
'db2 connection'
>>>
>>> connection = project.connections.get(name=connection_name)
>>> connection
Connection(name='db2 connection')
>>>
>>> generator = PythonGenerator(
...     source=connection,
...     destination='/tmp/output.py',
...     auth=auth,  # pragma: allowlist secret
...     base_api_url='https://api.ca-tor.dai.cloud.ibm.com'
... )
>>> generator.save()
PosixPath('/tmp/output.py')

Generating Jupyter Notebooks#

You can also generate code as a Jupyter notebook instead of a Python script. The notebook organizes the code into logical cells:

  • Imports: All required imports

  • Authentication: Setting up the authenticator

  • Platform Setup: Creating the Platform instance

  • Project Setup: Getting or creating the project

  • Connection Creation: Creating the connection with all properties

>>> from ibm_watsonx_data_integration.codegen.generator import PythonGenerator, PythonGeneratorFormat
>>>
>>> # Generate as Jupyter notebook
>>> generator = PythonGenerator(
...     source=connection,
...     destination='/tmp/output.ipynb',
...     auth=auth,  # pragma: allowlist secret
...     base_api_url='https://api.ca-tor.dai.cloud.ibm.com',
...     output_format=PythonGeneratorFormat.NOTEBOOK
... )
>>> generator.save()
PosixPath('/tmp/output.ipynb')

The generated notebook can be opened in JupyterLab, Jupyter Notebook, or any compatible notebook environment.

Example Output#

The following is an example of the output saved to /tmp/output.py:

import os
from ibm_watsonx_data_integration import Platform
from ibm_watsonx_data_integration.common.auth import IAMAuthenticator
auth = IAMAuthenticator( # pragma: allowlist secret
    api_key=os.getenv('IBM_CLOUD_API_KEY'),
    base_auth_url='https://cloud.ibm.com',
)
platform = Platform(
    auth=auth, # pragma: allowlist secret
    base_url='https://cloud.ibm.com',
    base_api_url='https://api.ca-tor.dai.cloud.ibm.com',
)
project = platform.projects.get(project_id='29c07359-c05b-4868-aa95-381e03d568e0')
connection = project.create_connection(
    name='db2-conn',
    datasource_type=platform.datasources.get(datasource_id='8c1a4480-1c29-4b33-9086-9cb799d7b157'),
    description='',
    properties={
        'database': 'mydatabase',
        'password': 'mypassword', # pragma: allowlist secret
        'port': '3000',
        'username_password_security': 'default',
        'host': 'myhost',
        'ssl': 'false',
        'username_password_encryption': 'default',
        'username': 'myusername'
    }
)