Gateway Python APIs - Platform - BlueCat Gateway - 22.11.1

Gateway Administration Guide

Locale
English
Product name
BlueCat Gateway
Version
22.11.1

Gateway contains a full-featured API for building automated solutions within BlueCat Gateway and BlueCat Address Manager.

When building an automation solution, use these APIs if you need to work with the BlueCat Gateway UI, if you need to use BlueCat Gateway workflows and functionality, or if there's something you can't do with the external BlueCat Python Libraries.

There are several ways to access the Gateway API.

Using the standard API client (spec_api)

The standard Gateway Python API Client contains wrappers for most Address Manager API features. It is designed for use in Python scripts. To use this client in scripts, you must first import the appropriate libraries, then get an instance of the client through flask.g.user:
# Get the API client.
api = g.user.get_api().spec_api
You can then pass API calls and references through that client instance:
# Obtain a configuration with a specific name config_name, using the get_entity_by_name() function
configuration = api.get_entity_by_name(0, config_name, ObjectType.CONFIGURATION)
For example, the script below implements a Gateway endpoint that returns the names and addresses of servers under a given BAM configuration:
from flask import g, jsonify

from main_app import app
from bluecat import route
from bluecat_libraries.address_manager.constants.object_type import ObjectType
from bluecat_libraries.http_client.exceptions import ErrorResponse


@route(app, "/get-servers/<string:config_name>", methods=["GET"])
def get_servers(config_name):

    # Get the API client.
    api = g.user.get_api().spec_api

    try:
        # Obtain the configuration with the given name.
        configuration = api.get_entity_by_name(0, config_name, ObjectType.CONFIGURATION)
        if configuration is None:
            return "Configuration does not exist", 404

        # Obtain servers under the given configuration.
        servers = api.get_entities(configuration["id"], ObjectType.SERVER)

    # If something goes wrong, the API client typically raises `ErrorResponse`. Catch it to handle errors.
    except ErrorResponse as e:
        return e.message, 500

    # Return server names and addresses.
    return jsonify(
        [
            {"name": server["name"], "address": server["properties"]["servicesIPv4Address"]}
            for server in servers
        ]
    )

Using the object-oriented API client

Some GatewayAPI operations can be accessed with an object-oriented approach. This approach treats clients, configurations, and other resources as objects, which can be useful in some circumstances.
Note: Where possible, BlueCat recommends using the standard API client instead. The Object-oriented API Client does not support the breadth of Address Manager and BDDS functionality as the Specification API client.
from flask import g, jsonify

from main_app import app
from bluecat import route
from bluecat.api_exception import APIException


@route(app, "/get-servers/<string:config_name>", methods=["GET"])
def get_servers(config_name):

    # Get the Object-Oriented API client.
    api = g.user.get_api()

    try:
        # Obtain the configuration with the given name.
        configuration = api.get_configuration(config_name)

        # Obtain servers under the given configuration.
        servers = configuration.get_servers()

    # If something goes wrong, the Object-Oriented API client typically raises `APIException`. Catch it to handle errors.
    except APIException as e:
        return e.get_message(), 500

    # Return server names and addresses.
    return jsonify(
        [
            {"name": server.name(), "address": server.get_service_ip4_addresses()[0]}
            for server in servers
        ]
    )

Using the raw API to access the Address Manager API directly (raw_api)

The Standard Gateway API provides wrappers for most Address Manager API methods, classes, and other references. To use the Gateway API with those wrappers, use the standard API above.

If you need to use an Address Manager API endpoint that does not yet have a corresponding wrapper in the Python API client, you can use the Python API client to simulate calls to that endpoint directly, using a "raw" client. Below is the same get-servers example reworked to make such calls:

from flask import g, jsonify

from main_app import app
from bluecat import route
from bluecat_libraries.address_manager.constants.object_type import ObjectType
from bluecat_libraries.http_client.exceptions import ErrorResponse


@route(app, "/get-servers/<string:config_name>", methods=["GET"])
def get_servers(config_name):

    # Get the raw client.
    api = g.user.get_api().raw_api

    try:
        # Obtain the configuration with the given name.
        configuration = api.getEntityByName(parentId=0, name=config_name, type=ObjectType.CONFIGURATION)
        if configuration["id"] == 0:
            return "Configuration does not exist", 404

        # Obtain up to 10 servers under the given configuration.
        servers = api.getEntities(parentId=configuration["id"], type=ObjectType.SERVER, start=0, count=10)

    # If something goes wrong, the raw client typically raises `ErrorResponse`. Catch it to handle errors.
    except ErrorResponse as e:
        return e.message, 500

    # Extract server names and addresses.
    out = []
    for server in servers:
        # The `getEntities` method returns properties as a string of the form `k1=v1|k2=v2|...`
        for kv in server["properties"].split("|"):
            k, v = kv.split("=", maxsplit=1)
            if k == "servicesIPv4Address":
                out.append({"name": server["name"], "address": v})
                break

    return jsonify(out)
-