Cisco Secure Access Roaming Computers sample script

Roaming Computers API Guide

This guide provides Python client samples for the Cisco Secure Access Roaming Computers API.

Note: Your Secure Access API key must have the permissions to read and write on the deployments.roamingcomputers key scope. For more information about the API key scopes, see Secure Access OAuth 2.0 Scopes.

First get your Secure Access API key, set up your environment, and install the Secure Access API client. For more information, see Samples Overview.

Run the Script

  1. Copy the script to a local file called main.py. Locate the script in your environment in a directory above the cisco directory.
  2. Run python3 main.py.

main.py

"""
Copyright (c) 2025 Cisco and/or its affiliates.
This software is licensed to you under the terms of the Cisco Sample
Code License, Version 1.1 (the "License"). You may obtain a copy of the
License at

https://developer.cisco.com/docs/licenses

All use of the material herein must be in accordance with the terms of
the License. All rights not expressly granted by the License are
reserved. Unless required by applicable law or agreed to separately in
writing, software distributed under the License is distributed on an "AS
IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied.
"""

import requests
from requests_toolbelt import MultipartEncoder
import json
import os
from dotenv import load_dotenv

from cisco.secure_access import API
from cisco.secure_access import deployments
from cisco.secure_access import GET
from cisco.secure_access import PUT
from cisco.secure_access import DELETE
from cisco.secure_access import token_url
from cisco.secure_access import client_id
from cisco.secure_access import client_secret

# Roaming Computers API endpoints
roaming_computers_endpoint = "roamingcomputers"
roaming_computers_details_endpoint = "roamingcomputers/{}"
roaming_computers_orginfo_endpoint = "roamingcomputers/orgInfo"

load_dotenv()

def get_roaming_computers_orginfo(api):
    ''' Get Roaming Computers OrgInfo. '''
    try:
        # Get the OrgInfo properties for the Roaming Computers in the organization.
        response = api.Query(deployments, roaming_computers_orginfo_endpoint, GET)

        # Check if the API request was successful
        if response.status_code == 200:
            print(f"Success. GET {roaming_computers_orginfo_endpoint}, {response.json()}")
            return response.json()
        else:
            print(f"Failed to get the Roaming Computers OrgInfo properties. Status code: {response.status_code}, Response: {response.text}.")
            return None
    except requests.exceptions.RequestException as e:
        print(f"An error occurred: {e}.")

def get_roaming_computers(api):
    ''' Get Roaming Computers. '''
    try:
        # Get Roaming Computers in the organization
        response = api.Query(deployments, roaming_computers_endpoint, GET)

        # Check if the API request was successful
        if response.status_code == 200:
            print(f"Success. GET {roaming_computers_endpoint}, {response.json()}")
            return response.json()
        else:
            print(f"Failed to get the Roaming Computers. Status code: {response.status_code}, Response: {response.text}.")
            return None
    except requests.exceptions.RequestException as e:
        print(f"An error occurred: {e}.")

def get_roaming_computer(api, id):
    ''' Get the properties of the Roaming Computer. '''
    try:
        if id is None:
            raise ValueError("id is required to get the Roaming Computer.")
        url = roaming_computers_details_endpoint.format(id)

        # Get the properties for the Roaming Computer
        response = api.Query(deployments, url, GET)

        # Check if the API request was successful
        if response.status_code == 200:
            print(f"Success. GET {url}, {response.json()}.")
            return response.json()
        else:
            print(f"Failed to get the Roaming Computer {id}. Status code: {response.status_code}, Response: {response.text}.")
            return
    except Exception as e:
        print(f"An error occurred: {e}.")

def put_roaming_computer(api, id, name=None):
    ''' Update Roaming Computers. '''
    try:
        if id is None or name is None:
            raise ValueError("id and name are required to update the Roaming Computer.")

        url = roaming_computers_details_endpoint.format(id)

        # Prepare the payload
        payload = {
            "name": name
        }

        # Update a Roaming Computer
        response = api.Query(deployments, url, PUT, payload)

        # Check the response status
        if response.status_code == 200:
            print(f"Success. PUT {roaming_computers_details_endpoint}, {response.json()}")
            return response.json()
        else:
            print(f"Failed to update the Roaming Computer. Status code: {response.status_code}, Response: {response.text}.")
            return None
    except Exception as e:
        print(f"An error occurred: {e}.")

def delete_roaming_computer(api, id):
    ''' Delete the Roaming Computer. '''
    try:
        if id is None:
            raise ValueError("id is required to delete the Roaming Computers.")
        url = roaming_computers_details_endpoint.format(id)

        # Delete the Roaming Computer
        response = api.Query(deployments, url, DELETE)

        # Check if the API request was successful
        if response.status_code == 204:
            print(f"No Content. DELETE {url}.")
        else:
            print(f"Failed to delete the Roaming Computer {id}. Status code: {response.status_code}, Response: {response.text}.")
    except Exception as e:
        print(f"An error occurred: {e}.")

def main():
    # Exit out if the required client_id or client_secret is not set
    for var in ['API_KEY', 'API_SECRET', 'OUTPUT_DIR']:
        if os.environ.get(var) == None:
            print("Required environment variable: {} not set".format(var))
            exit()

    # Get an API token
    api = API(token_url, client_id, client_secret)

    try:
        # get the roaming computers in the organization
        json_data = get_roaming_computers(api)

        # get a roaming computer in the organization using the deviceId
        deviceId = None
        if json_data:
            deviceId = json_data[0]['deviceId']
        json_data = get_roaming_computer(api, deviceId)

        # update a roaming computer in the organization
        name = "new name for roaming computer"
        json_data = put_roaming_computer(api, deviceId, name)

        # get the orgInfo for the roaming computers
        json_data = get_roaming_computers_orginfo(api)

        # delete a roaming computer
        delete_roaming_computer(api, deviceId)

    except Exception as e:
        print(e)

if __name__ == "__main__":
    main()