From 882f438683482caf34163f3409a1cfeb733cc088 Mon Sep 17 00:00:00 2001 From: collin-koss Date: Wed, 8 Apr 2026 16:15:08 -0700 Subject: [PATCH] Update profile operation to be compatible with Pycentral latest 2.0a17. Update account credential handling to use yaml and match the standard of other workflows in the repo. Update the respectative documentation changes in files. Update version of PyCentrla to latest in requirements. --- profile-operations/README.md | 19 +++++----- profile-operations/account_credentials.yaml | 4 +++ profile-operations/central_token.json | 7 ---- profile-operations/profile_operations.py | 39 +++++++-------------- profile-operations/requirements.txt | 2 +- 5 files changed, 26 insertions(+), 45 deletions(-) create mode 100644 profile-operations/account_credentials.yaml delete mode 100644 profile-operations/central_token.json diff --git a/profile-operations/README.md b/profile-operations/README.md index ad18ac4..3648bff 100644 --- a/profile-operations/README.md +++ b/profile-operations/README.md @@ -28,25 +28,22 @@ source venv/bin/activate # On Windows use: venv\Scripts\activate pip install -r requirements.txt ``` -_This workflow is tested on the `pycentral` SDK (version: `2.0a7`). Please check compatibility before executing on older/newer versions as there may be changes_ +_This workflow is tested on the `pycentral` SDK (version: `2.0a17`). Please check compatibility before executing on older/newer versions as there may be changes_ ## Configuration -### Credentials Configuration (central_token.json) +### Credentials Configuration (account_credentials.yaml) For API operations in new HPE Aruba Networking Central: -```json -{ - "new_central": { - "base_url": "", - "client_id": "", - "client_secret": "", - } -} +```yaml +new_central: + cluster_name: #e.g. EU-1 + client_id: + client_secret: ``` -**Sample Input:** See [`central_token.json`](./central_token.json) in this repository for an example credential file. +**Sample Input:** See [`account_credentials.yaml`](./account_credentials.yaml) in this repository for an example credential file. > [!TIP] > **Where to find these:** diff --git a/profile-operations/account_credentials.yaml b/profile-operations/account_credentials.yaml new file mode 100644 index 0000000..84bc543 --- /dev/null +++ b/profile-operations/account_credentials.yaml @@ -0,0 +1,4 @@ +new_central: + cluster_name: # or base_url: + client_id: + client_secret: \ No newline at end of file diff --git a/profile-operations/central_token.json b/profile-operations/central_token.json deleted file mode 100644 index ec5f663..0000000 --- a/profile-operations/central_token.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "new_central": { - "base_url": "", - "client_id": "", - "client_secret": "" - } -} \ No newline at end of file diff --git a/profile-operations/profile_operations.py b/profile-operations/profile_operations.py index c020b39..144bfe1 100644 --- a/profile-operations/profile_operations.py +++ b/profile-operations/profile_operations.py @@ -1,8 +1,6 @@ -import json - from pycentral import NewCentralBase from pycentral.profiles import Profiles -from pycentral.utils.url_utils import NewCentralURLs +from pycentral.utils.url_utils import generate_url def main(): @@ -11,30 +9,19 @@ def main(): # to do to use either of these is initialize a Central connection. Let's see how # to do that. - # Generating a central connection with pycentral requires passing a dictionary - # containing information to the NewCentralBase class. This dictionary should - # include the base URL, client ID, and client secret for the Central API. Best - # practice is to load this information from another file. For this example we will - # load the credentials from a JSON file named 'central_token.json'. Please - # reference the 'central_token.json' file in this directory for an example of how - # the credential information should be structured. - - # Load the credentials from the JSON file - try: - with open("central_token.json", "r") as file: - credentials = json.load(file) - except FileNotFoundError: - print("Error: Credentials file central_token.json not found.") - return 1 - except json.JSONDecodeError as e: - print(f"Error parsing JSON file 'central_token.json': {e}") - return 1 + # Generating a central connection with pycentral requires passing credential + # information to the NewCentralBase class. Required credential information includes + # the Central base URL/cluster, client ID, and client secret for the Central API. + # Best practice is to load this information from another file. For this example we + # will load the credentials from a YAML file named 'account_credentials.yaml'. + # Please reference the 'account_credentials.yaml' file in this directory for an + # example of how the credential information should be structured. - # Now that we have the credentials, we can create a connection to Central. + # Create a connection to Central using the account credentials file. try: print("Connecting to Central...") central_conn = NewCentralBase( - token_info=credentials, enable_scope=True, log_level="ERROR" + token_info="account_credentials.yaml", enable_scope=True, log_level="ERROR" ) print("Connected to Central successfully!") except Exception as e: @@ -66,7 +53,7 @@ def main(): } # Finally, we use the generate_url function to create the full URL for the API # endpoint that will be consumed by the Pycentral functions. - dns_url = NewCentralURLs.generate_url(api_endpoint=dns_endpoint) + dns_url = generate_url(api_endpoint=dns_endpoint) # Let's try creating a DNS profile using the create_profile function. We simply # need to pass the data we have prepared to the function along with the Central @@ -155,7 +142,7 @@ def main(): "is-l3-vlan": None, } # Generate the URL for the VLAN endpoint. - vlan_url = NewCentralURLs.generate_url(api_endpoint=vlan_endpoint) + vlan_url = generate_url(api_endpoint=vlan_endpoint) # Let's try creating several VLAN profiles using the bulk create_profiles # function. We can create a list of dictionaries containing the profile @@ -216,7 +203,7 @@ def main(): print("\nDeleting VLAN profiles...") # Create a list of URLs with identifiers to delete the profiles. path_list = [ - NewCentralURLs.generate_url(api_endpoint=vlan_endpoint) + f"/{profile['vlan']}" + generate_url(api_endpoint=vlan_endpoint) + f"/{profile['vlan']}" for profile in vlan_profiles ] bulk_delete = Profiles.delete_profiles( diff --git a/profile-operations/requirements.txt b/profile-operations/requirements.txt index 540fc9d..51484f7 100644 --- a/profile-operations/requirements.txt +++ b/profile-operations/requirements.txt @@ -1 +1 @@ -pycentral==2.0a7 \ No newline at end of file +pycentral==2.0a17 \ No newline at end of file