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='INFO'

import os

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

LOGGER = Logger(__name__).get_logger()


def run_example():
    """ 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 = AS3Client(mgmt_client)

    # 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(run_example())

Show F5 Cloud Services Subscription Configuration

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

python example.py
""" Show the F5 Cloud Services subscription declaration

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

# export F5_CS_USER='user@example.com'
# export F5_CS_PWD='example_password'
# export F5_SDK_LOG_LEVEL='INFO'

import os
import json

from f5sdk.cs import ManagementClient
from f5sdk.cs.accounts import AccountClient
from f5sdk.cs.subscriptions import SubscriptionClient
from f5sdk.logger import Logger

LOGGER = Logger(__name__).get_logger()


def run_example():
    """ Get Cloud Services configuration """
    # create management client
    mgmt_client = ManagementClient(
        user=os.environ['F5_CS_USER'],
        password=os.environ['F5_CS_PWD']
    )

    # create account/subscription client
    account_client = AccountClient(mgmt_client)
    subscription_client = SubscriptionClient(mgmt_client)

    # discover account/subscription ID
    account_id = account_client.show_user()['primary_account_id']
    subscription_id = subscription_client.list(
        query_parameters={
            'account_id': account_id
        }
    )['subscriptions'][0]['subscription_id']

    # get subscription details
    return subscription_client.show(name=subscription_id)


if __name__ == '__main__':
    LOGGER.info(json.dumps(run_example(), indent=4))

Show F5 Cloud Services Beacon Declaration

This script uses the SDK to show the F5 Cloud Services Beacon declaration.

python example.py
""" Show the F5 Cloud Services Beacon declaration

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

# export F5_CS_USER='user@example.com'
# export F5_CS_PWD='example_password'
# export F5_SDK_LOG_LEVEL='INFO'

import os
import json

from f5sdk.cs import ManagementClient
from f5sdk.cs.beacon.declare import DeclareClient
from f5sdk.logger import Logger

LOGGER = Logger(__name__).get_logger()


def run_example():
    """ Show F5 Cloud Services Beacon Declaration (Applications, Monitors, etc.) """
    # create management client
    mgmt_client = ManagementClient(
        user=os.environ['F5_CS_USER'],
        password=os.environ['F5_CS_PWD']
    )

    # create declare client
    declare_client = DeclareClient(mgmt_client)

    # get subscription details
    return declare_client.create(config={'action': 'get'})


if __name__ == '__main__':
    LOGGER.info(json.dumps(run_example(), indent=4))

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='INFO'

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 run_example(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 = run_example(
        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.