Gateway's Address Manager REST v1 API clients provide access to the Address Manager Legacy v1 API. The REST v1 clients are intended for Gateway users who already have existing workflows that use the previous version of the Address Manager API.
There are three REST v1 clients:
Standard API client: Also called the "Specification" client. This client consists of a set of "wrappers" for Address Manager REST v1 API endpoints. Each wrapper is associated with a specific component of the Address Manager API. When you call a wrapper from a Gateway workflow, it passes on the request to the associated component of the Address Manager API.
Object-oriented API client: This client accesses the Address Manager REST v1 API with an object-oriented approach. This approach treats clients, configurations, and other resources as objects, which can be useful in some circumstances.
"Raw" Address Manager API client (raw_api): This client makes calls to the Address Manager REST v1 API directly, using a "raw" client. This can be useful if you need to use a REST v1 API endpoint that does not yet have a corresponding wrapper in the Standard API client.
Where possible, BlueCat recommends using the Standard API client. The Object-oriented API Client does not support the same breadth of Gateway functionality as the Specification API client.
BAMAPI
class provides access to both the Address Manager REST v1 and REST v2 clients. However, only one of the REST
v1 client or the REST v2 client can be active on a single instance of Gateway.- For a reference of wrappers for classes, methods, and other REST v1 API elements, see Address Manager REST v1 API client reference.
Prerequisites
In order to access the Address Manager REST v1 API clients:
The configured REST API version for the instance of Gateway must be REST v1. For more details, see Setting the Address Manager REST API client version.
Note: Only one of the REST v1 and REST v2 clients can be active on a specific instance of Gateway
Using the standard REST v1 API client in scripts
To work with the Standard API client (so that you can access the Address Manager Legacy v1 API), use the BAMAPI
class.
flask.g.user
:
# Get the API client.
api = g.user.bam_api.v1
api = g.user.get_api().spec_api
While this syntax will
still work, we recommend using the updated syntax where possible.# 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)
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.bam_api.v1
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
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 REST v1 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 (see Using the legacy Address Manager REST v1 API client).
If you need to
use an Address Manager REST v1 API endpoint that does not yet have a
corresponding wrapper in the Python API client, you can use the Python API client to
make 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)
-