Skip to main content
All CollectionsOnboarding API
Example of How to Use the Onboarding API – Export Inventory to CSV
Example of How to Use the Onboarding API – Export Inventory to CSV

An example of how to export your UXI resources to CSV files using the onboarding API.

Updated over a week ago

The User Experience Insight (UXI) Onboarding API allows you to simplify most of the tasks associated with assigning your sensors and agents to the proper groups and configuring them to test your networks and services. To use the API, your dashboard must be on the Greenlake Cloud Platform. Additionally, you must be using the group-based assignment feature.


You can find documentation for the Onboarding API here:

Prerequisites

You will need to create a personal API client for the User Experience Insight service manager on the Greenlake Cloud Platform. See the detailed instructions on creating your personal API client in the Greenlake developer guide.

Example Scenario

In this example, suppose we want to get a complete inventory of all our resources and write the data to a collection of CSV files. This can be useful for inventory management, reporting and obtaining resource ID's for when we need to make additional calls to the onboarding API.

Endpoints

Here are the Endpoints you will need:

  1. /networking-uxi/v1alpha1/groups

  2. /networking-uxi/v1alpha1/sensors

  3. /networking-uxi/v1alpha1/agents

  4. /networking-uxi/v1alpha1/wireless-networks

  5. /networking-uxi/v1alpha1/wired-networks

  6. /networking-uxi/v1alpha1/service-tests

Note: The API uses cursor-based pagination.

Putting it together

Here is a Python 3 example of this in action. This example uses Python requests to fetch the data and write it to a CSV file.


This example does the following:

  1. Generates a temporary access token using the credentials of the personal API client.

  2. Retrieves a list of all groups, sensors, agents, wired and wireless networks as well as service-tests.

  3. Writes the details of each resource to a CSV file specific to that resource type.

WARNING: This example is for demonstration purposes only. This example lacks robust error handling and optimizations necessary for a production environment. Do not use the example code in a production environment.

import csv
from typing import Any

import requests
from oauthlib.oauth2 import BackendApplicationClient
from requests.auth import HTTPBasicAuth
from requests_oauthlib import OAuth2Session

# Global constants
AUTH_TOKEN_URL = "https://sso.common.cloud.hpe.com/as/token.oauth2"
UXI_API_BASE_URL = "https://api.capenetworks.com/networking-uxi/v1alpha1"

# Customer specific constants
CLIENT_ID = "<CLIENT_ID>"
CLIENT_SECRET = "<CLIENT_SECRET>"

def main() -> None:
api_token = get_api_token()

groups = get_groups(api_token=api_token)
sensors = get_sensors(api_token=api_token)
agents = get_agents(api_token=api_token)
wireless_networks = get_wireless_networks(api_token=api_token)
wired_networks = get_wired_networks(api_token=api_token)
service_tests = get_service_tests(api_token=api_token)

write_group_inventory_to_csv(groups)
write_sensor_inventory_to_csv(sensors)
write_agent_inventory_to_csv(agents)
write_wireless_network_inventory_to_csv(wireless_networks)
write_wired_network_inventory_to_csv(wired_networks)
write_service_test_inventory_to_csv(service_tests)


def get_api_token() -> str:
client = BackendApplicationClient(CLIENT_ID)
oauth = OAuth2Session(client=client)
auth = HTTPBasicAuth(CLIENT_ID, CLIENT_SECRET)
token = oauth.fetch_token(token_url=AUTH_TOKEN_URL, auth=auth)

return token["access_token"]

def get_groups(api_token: str) -> list[dict[str, Any]]:
return _get_items(path="groups", api_token=api_token)


def get_sensors(api_token: str) -> list[dict[str, Any]]:
return _get_items(path="sensors", api_token=api_token)


def get_agents(api_token: str) -> list[dict[str, Any]]:
return _get_items(path="agents", api_token=api_token)


def get_wireless_networks(api_token: str) -> list[dict[str, Any]]:
return _get_items(path="wireless-networks", api_token=api_token)


def get_wired_networks(api_token: str) -> list[dict[str, Any]]:
return _get_items(path="wired-networks", api_token=api_token)


def get_service_tests(api_token: str) -> list[dict[str, Any]]:
return _get_items(path="service-tests", api_token=api_token)


def _get_items(path: str, api_token: str) -> list[dict[str, Any]]:
HEADERS = {
"Authorization": f"Bearer {api_token}",
}
items: list[dict[str, Any]] = []
next_cursor = None

# This while loop exists to keep making requests until all data for the specified resource is fetched.
while True:
try:
params = {"next": next_cursor} if next_cursor else {}
response = requests.get(
f"{UXI_API_BASE_URL}/{path}", headers=HEADERS, params=params
)
response.raise_for_status()
response_body: dict[str, Any] = response.json()
response_items: list[dict[str, Any]] = response_body.get("items", [])
next_cursor = response_body.get("next")

items.extend(response_items)

if not next_cursor or not response_items:
break
except requests.exceptions.HTTPError as e:
print(f"Error fetching items ({path}): {e}")
break

return items

def write_group_inventory_to_csv(groups: list[dict[str, Any]]) -> None:
if not groups:
return

for group in groups:
del group["type"]

headers = list(groups[0].keys())
with open("group_inventory.csv", "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(headers)
for group in groups:
if group["parent"]:
group["parent"] = group["parent"]["id"]
writer.writerow(list(group.values()))

def write_sensor_inventory_to_csv(sensors: list[dict[str, Any]]) -> None:
if not sensors:
return

for sensor in sensors:
del sensor["type"]

headers = list(sensors[0].keys())
with open("sensor_inventory.csv", "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(headers)

for sensor in sensors:
writer.writerow(list(sensor.values()))

def write_agent_inventory_to_csv(agents: list[dict[str, Any]]) -> None:
if not agents:
return

for agent in agents:
del agent["type"]

headers = list(agents[0].keys())
with open("agent_inventory.csv", "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(headers)

for agent in agents:
writer.writerow(list(agent.values()))

def write_wireless_network_inventory_to_csv(wireless_networks: list[dict[str, Any]]) -> None:
if not wireless_networks:
return

for wireless_network in wireless_networks:
del wireless_network["type"]

headers = list(wireless_networks[0].keys())
with open("wireless_network_inventory.csv", "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(headers)

for wireless_network in wireless_networks:
writer.writerow(list(wireless_network.values()))

def write_wired_network_inventory_to_csv(wired_networks: list[dict[str, Any]]) -> None:
if not wired_networks:
return

for wired_network in wired_networks:
del wired_network["type"]

headers = list(wired_networks[0].keys())
with open("wired_network_inventory.csv", "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(headers)

for wired_network in wired_networks:
writer.writerow(list(wired_network.values()))

def write_service_test_inventory_to_csv(service_tests: list[dict[str, Any]]) -> None:
if not service_tests:
return

for service_test in service_tests:
del service_test["type"]

headers = list(service_tests[0].keys())
with open("service_test_inventory.csv", "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(headers)

for service_test in service_tests:
writer.writerow(list(service_test.values()))

if __name__ == "__main__":
main()
Did this answer your question?