Examples

The following are examples that can be followed to get up and running with the SDK.

Note

These examples exist in the project source under the examples directory.

Prerequisites

  • Python 3.x, for installation see python download docs.

  • Python virtual environment, for details see python venv docs.

  • Optional: Specify the log level verbosity to see additional log messages during SDK usage. See the Troubleshooting section for more details.

  • Optional: Ignore untrusted TLS certificate warnings during HTTPS requests to BIG-IP. See the Troubleshooting section for more details.

Configure AS3

This script uses the SDK to update BIG-IP L4-L7 configuration using AS3, provided via a local file.

python example.py
""" Update BIG-IP L4-L7 configuration using AS3

Notes
-----
Set local environment variables first
"""

# export F5_SDK_HOST='192.0.2.10'
# export F5_SDK_USERNAME='admin'
# export F5_SDK_PWD='admin'
# export F5_SDK_AS3_DECL='./my_declaration.json'
# export F5_SDK_LOG_LEVEL='DEBUG'

import os

from f5sdk.bigip import ManagementClient
from f5sdk.bigip.extension import ExtensionClient
from f5sdk.logger import Logger

LOGGER = Logger(__name__).get_logger()


def update_as3_config():
    """ Update AS3 configuration

    Notes
    -----
    Includes package installation, service check while
    maintaining idempotency
    """
    # create management client
    mgmt_client = ManagementClient(
        os.environ['F5_SDK_HOST'],
        user=os.environ['F5_SDK_USERNAME'],
        password=os.environ['F5_SDK_PWD'])

    # create extension client
    as3_client = ExtensionClient(mgmt_client, 'as3')

    # Get installed package version info
    version_info = as3_client.package.is_installed()
    LOGGER.info(version_info['installed'])
    LOGGER.info(version_info['installed_version'])
    LOGGER.info(version_info['latest_version'])

    # install package
    if not version_info['installed']:
        as3_client.package.install()

    # ensure service is available
    as3_client.service.is_available()

    # configure AS3
    return as3_client.service.create(config_file=os.environ['F5_SDK_AS3_DECL'])


if __name__ == '__main__':
    LOGGER.info(update_as3_config())

Get F5 Cloud Services Configuration

This script uses the SDK to get F5 Cloud Services configuration.

python example.py
""" Get the F5 Cloud Services configuration

Notes
-----
Set local environment variables first
"""

# export F5_SDK_USERNAME='admin'
# export F5_SDK_PWD='admin'
# export F5_SDK_CS_SUBSCRIPTION_ID=''
# export F5_SDK_LOG_LEVEL='DEBUG'

import os

from f5sdk.cloud_services import ManagementClient
from f5sdk.cloud_services.subscriptions import SubscriptionClient
from f5sdk.logger import Logger

LOGGER = Logger(__name__).get_logger()


def get_cs_config():
    """ Get Cloud Services configuration """
    # create management client
    cs_client = ManagementClient(
        user=os.environ['F5_SDK_USERNAME'], password=os.environ['F5_SDK_PWD'])

    # create subscription client
    subscription_client = SubscriptionClient(cs_client)

    # get subscription details
    return subscription_client.show(name=os.environ['F5_SDK_CS_SUBSCRIPTION_ID'])


if __name__ == '__main__':
    LOGGER.info(get_cs_config())

Revoke License from BIG-IQ

This script uses the SDK to revoke a licensed BIG-IP (unreachable) from a BIG-IQ license pool.

python example.py
""" Revoke licensed BIG-IP (unreachable) from BIG-IQ license pool

Notes
-----
Set local environment variables first
"""

# export F5_SDK_HOST='192.0.2.10'
# export F5_SDK_USERNAME='admin'
# export F5_SDK_PWD='admin'
# export F5_SDK_ADDRESS_TO_REVOKE='192.0.2.100'
# export F5_SDK_LICENSE_POOL='my_pool'
# export F5_SDK_LOG_LEVEL='DEBUG'

import os

from f5sdk.bigiq import ManagementClient
from f5sdk.bigiq.licensing import AssignmentClient
from f5sdk.bigiq.licensing.pools import MemberManagementClient
from f5sdk.logger import Logger

LOGGER = Logger(__name__).get_logger()


def revoke_license(address, pool):
    """ Revoke license"""

    # create management client
    mgmt_client = ManagementClient(
        os.environ['F5_SDK_HOST'],
        user=os.environ['F5_SDK_USERNAME'],
        password=os.environ['F5_SDK_PWD'])

    # create assignment client, member management client
    assignment_client = AssignmentClient(mgmt_client)
    member_mgmt_client = MemberManagementClient(mgmt_client)

    # list assignments
    assignments = assignment_client.list()

    # get address assignment - there should only be one
    assignments = assignments['items']
    assignment = [i for i in assignments if i['deviceAddress'] == address][0]

    if not assignment:
        raise Exception('Unable to locate assignment from BIG-IQ assignments')

    # perform revoke - unreachable device
    return member_mgmt_client.create(
        config={
            'licensePoolName': pool,
            'command': 'revoke',
            'address': assignment['deviceAddress'],
            'assignmentType': 'UNREACHABLE',
            'macAddress': assignment['macAddress']
        }
    )


if __name__ == '__main__':
    RESPONSE = revoke_license(
        os.environ['F5_SDK_ADDRESS_TO_REVOKE'],
        os.environ['F5_SDK_LICENSE_POOL']
    )
    LOGGER.info('Response: %s', RESPONSE)

Note

The F5 SDK-Python is currently in early development and we want to hear from you! To provide feedback on F5 SDK-Python or this documentation, you can file a GitHub Issue.