Using the Address Manager REST v2 API client - Platform - BlueCat Gateway - 23.1

Gateway Administration Guide

Locale
English
Product name
BlueCat Gateway
Version
23.1

The Gateway Platform provides workflows with an already authenticated client to BlueCat Address Manager RESTful v2 API through the global user field: g.user.bam_api.v2.

The Address Manager REST v2 API client in Gateway provides direct access to the Address Manager RESTful v2 API. Unlike the REST v1 API client, the REST v2 client does not consist of wrappers for Address Manager API functions. Instead, the REST v2 client passes on calls you make directly to the Address Manager RESTful v2 API.

For example, to pass on a GET request to the Address Manager RESTful v2 API, you would use the client's http_get() method.

  • For more details on the Client class's methods, see Client class (REST v2 API Client).

  • For more details on the Address Manager RESTful v2 API (to which the the client provides access), see the Address Manager RESTful v2 API Guide.

  • For a complete reference to Address Manager RESTful v2 API endpoints, see the API's Swagger docs at http://{Address Manager IP address}/api/doc, where <Address Manager IP address> is the IP address of the relevant Address Manager 9.5 or higher instance.

Prerequisites

In order to access the Address Manager RESTful v2 API client:

  • You must be connecting to BlueCat Integrity (Address Manager) 9.5 or later.

    Note: The REST v2 client is not available in earlier versions.
  • The configured REST API version for the instance of Gateway must be REST v2. 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 single instance of Gateway.

Using the REST v2 client in scripts

The Gateway platform provides an instance of a client to the Address Manager RESTful v2 API. It is already authenticated for the current user and ready to be used by workflows. It is accessible as g.user.bam_api.v2.

Note: The 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.
# Get the API client.
api = g.user.bam_api.v2

A full example:

from flask import g, jsonify

from bluecat import route
from main_app import app
from bluecat.gateway.decorators import api_exc_handler


def get_server_address(interfaces):
    for interface in interfaces:
        if interface["type"] == "NetworkInterface":
            return interface["managementAddress"]


@route(app, "/get-servers/<string:config_name>", methods=["GET"])
@api_exc_handler
def get_servers(config_name):
    # Get the REST v2 API client.
    api_clientv2 = g.user.bam_api.v2

    # Unlike the REST v1 client, we can get both configuration data and server data in one request.
    rdata = api_clientv2.http_get(
        "/servers",
        params={
            "fields": "id, name, _embedded.interfaces.type, _embedded.interfaces.managementAddress, embed(interfaces)",
            "filter": f"configuration.name:'{config_name}'",
        },
    )
    servers = rdata["data"]

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