Forum Discussion
nickzxcv
2 years agoLevel 2
Pagination for fabric connections search?
I'm writing a script to see how oversubscribed my fabric ports are, by totaling the bandwidth of connections on each port. I'm having trouble with the pagination, no matter what I set for offset and ...
nickzxcv
Level 2
Here's how I got the pagination to work:
import requests
import logging
def get_bearer_token():
auth_url = 'https://api.equinix.com/oauth2/v1/token'
credentials = {
'grant_type': 'client_credentials',
'client_id': 'XXXXXXXXXXXXXXXXXXXXX',
'client_secret': 'YYYYYYYYYYYYYYYYYYYYYY'
}
credentials_response = requests.post(auth_url, json=credentials).json()
return credentials_response['access_token']
token=get_bearer_token()
headers = {'Authorization': 'Bearer {}'.format(token)}
api_v4_url_base = 'https://api.equinix.com/fabric/v4'
ports = dict()
ports_url = api_v4_url_base + '/ports'
ports_response = requests.get(ports_url, headers=headers).json()
for port in ports_response['data']:
name = port['name']
bandwidth = port['bandwidth']
ports[name] = dict()
ports[name]['bandwidth'] = bandwidth
ports[name]['connected_bandwidth']=0
ports[name]['connections'] = dict()
connections = dict()
connections_url = api_v4_url_base + '/connections/search'
post_body = {'filter': { 'and':
[
{
'property': '/aSide/accessPoint/port/name',
'operator': '=',
'values': list(ports.keys())
},
{
'property': '/operation/equinixStatus',
'operator': '=',
'values': [ 'PROVISIONED' ]
}
]
}, 'pagination': {'offset': 0} }
more = True
while more == True:
connections_response = requests.post(
connections_url, json=post_body, headers=headers).json()
total = connections_response['pagination']['total']
limit = connections_response['pagination']['limit']
if post_body['pagination']['offset'] + limit > total:
more = False
else:
post_body['pagination']['offset'] += limit
for connection in connections_response['data']:
port = connection['aSide']['accessPoint']['port']['name']
name = connection['name']
bandwidth = connection['bandwidth']
ports[port]['connected_bandwidth'] += bandwidth
ports[port]['connections'][name] = connection
print("{}: {} - {}".format(name, bandwidth, port))
print()
Jantzen
2 years agoEquinix Employee
Thanks for sharing your working solution! This will help others that come across this thread.
Related Content
- 3 years ago