Assign Sensors to an Existing Group
An example of how to assign UXI sensors to an existing group.
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:
Download the OpenApi specification.
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 assign 2 sensors to an existing group. This will allow the sensors to run tests against the services and networks assigned to the group or any parent of the group.
Endpoints
Here are the endpoints you will need:
/networking-uxi/v1alpha1/sensor-group-assignments
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 assign two sensors to an existing group.
This example does the following:
Generate a temporary access token using the credentials of the personal API client.
Assign two sensors to the same group.
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 http
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>"
# Example specific constants
# The IDs of the sensors to assign to the group.
# This can be retrieved from the API: /networking-uxi/v1alpha1/sensors.
SENSORS_TO_ONBOARD = [
"<SENSOR_ID_1>", # eg: 9108f997-32a0-45ca-ab00-c8c93273c332
"<SENSOR_ID_2>", # eg: 72e4a909-32a1-45cb-ab90-d8c93273c332
]
# The ID of the group to assign the sensors to.
# This can be retrieved from the API: /networking-uxi/v1alpha1/groups.
GROUP_ID = "<GROUP_ID>" # eg: 9bfded8bd7cb
def main():
api_token = get_api_token()
onboard_sensors(api_token=api_token)
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 onboard_sensors(api_token: str) -> None:
HEADERS = {
"Authorization": f"Bearer {api_token}",
"Content-Type": "application/json",
}
for sensor_id in SENSORS_TO_ONBOARD:
payload = {"sensorId": sensor_id, "groupId": GROUP_ID}
try:
response = requests.post(
f"{UXI_API_BASE_URL}/sensor-group-assignments",
headers=HEADERS,
json=payload,
)
response.raise_for_status()
print(f"Successfully assigned sensor '{sensor_id}' to group '{GROUP_ID}'")
except requests.exceptions.HTTPError as e:
print(f"Failed to assign sensor '{sensor_id}' to group '{GROUP_ID}': {e.response.text}")
if __name__ == "__main__":
main()
Updated 10 days ago