scsd-configs/code/newcentral_devices.py
2025-03-05 15:22:17 -05:00

89 lines
2.5 KiB
Python

import requests
import json
DEBUG = False
url = "https://sso.common.cloud.hpe.com/as/token.oauth2"
payload = {
"grant_type": "client_credentials",
"client_id": "65098f0f-747f-4c13-a338-c13028184ecd",
"client_secret": "710067957ef344918e90f8c890655f5e"
}
headers = {
"accept": "application/json",
"content-type": "application/x-www-form-urlencoded"
}
all_text="""
all:
vars:
ansible_network_os: arubanetworks.aoscx.aoscx
ansible_connection: arubanetworks.aoscx.aoscx # REST API via pyaoscx connection method
ansible_aoscx_validate_certs: False
ansible_aoscx_use_proxy: False
ansible_acx_no_proxy: True
ansible_ssh_common_args: "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
"""
response = requests.post(url, data=payload, headers=headers)
#print(response.text)
response_json=json.loads(response.text)
access_token=response_json['access_token']
devices={}
page_count=1
page_size=100
url_base='https://us4.api.central.arubanetworks.com'
while True:
url = f"{url_base}/network-monitoring/v1alpha1/devices?limit={page_size}&next={page_count}"
#url = f"{url_base}/network-monitoring/v1alpha1/device-inventory?limit={page_size}&next={page_count}"
headers = {
"accept": "application/json",
"authorization": f"Bearer {access_token}"
}
response = requests.get(url, headers=headers)
items = json.loads(response.text)['items']
for d in items:
#print (d['deviceName'])
if d['status']=="ONLINE":
devices[d['deviceName']]=d
if DEBUG: print(d['deviceName'])
if json.loads(response.text)['next'] is None:
break
page_count+=1
for d in devices:
if devices[d]['deployment']=='Stack':
if DEBUG: print(d,'Stack',devices[d])
headers['site-id']=devices[d]['siteId']
url = f"{url_base}/network-monitoring/v1alpha1/stack/{devices[d]['id']}/members"
response = requests.get(url, headers=headers)
if DEBUG: print(response.text)
current_building=''
#print (devices)
for d in sorted(devices):
building=devices[d]['deviceName'].split("-")[0]
if not building == current_building:
print(f"{building}:")
print( " hosts:")
current_building=building
print(f" {devices[d]['deviceName']}:")
print(f" ansible_host: {devices[d]['ipv4']}")
print(f" building_name: {current_building}")
print(f" serial_num: {devices[d]['serialNumber']}")
print(f"{all_text}")