The BlueCat Python API is a set of wrapper classes abstracting the functionality of the BlueCat Address Manager API Client along with certain other BAM and BDDS functions (for example, dynamic updates to host records).
The following example prints the names and addresses of
servers under a given BlueCat Address Manager (BAM)
configuration.
# Print the names and addresses of servers under a given BlueCat Address Manager (BAM) configuration.
from bluecat_libraries.address_manager.api import Client
from bluecat_libraries.address_manager.constants import ObjectType
from bluecat_libraries.http_client.exceptions import ErrorResponse
with Client(<bam_url>) as client:
client.login(<username>, <password>)
try:
config = client.get_entity_by_name(0, <config_name>, ObjectType.CONFIGURATION)
if config is None:
print("No configuration found with the given name.")
else:
servers = client.get_entities(config["id"], ObjectType.SERVER)
for server in servers:
print(server["name"], server["properties"]["servicesIPv4Address"])
except ErrorResponse as e:
# `client.get_entity_by_name()` and `client.get_entities()` call BAM endpoints so either may
# result in an error response.
print(e.message)
client.logout()
The standard usage pattern is to get an instance of the
Python API client through
flask.g.user
. Below is an example 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
]
)
An alternative object-oriented approach may be available
for certain operations, as shown below.
Note: BlueCat encourages using the Python API
client in the style of the first example since it more comprehensively covers the
BAM and BDDS functionality.
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 OO 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 OO 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
]
)
In case a BAM endpoint does not yet have a corresponding
wrapper in the Python API client, the client can simulate calls to that endpoint
directly. 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)
Note: For complete code samples for API refer to the BlueCat Gateway
WebHelp and Documentation.