Skip to content

Instantly share code, notes, and snippets.

@sulrich
Created November 20, 2023 23:39
Show Gist options
  • Save sulrich/9beab80aa1071fd6294c8fdc42fcc155 to your computer and use it in GitHub Desktop.
Save sulrich/9beab80aa1071fd6294c8fdc42fcc155 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import requests
import base64
import os
import pprint
API_HOST = "www.arista.com"
SESSION_CODE_API_URL = "https://" + API_HOST + "/api/sessionCode/"
search_tests = [
{
"name": "valid sku search",
"mainSku": "DCS-7150S-24",
"url": "https://" + API_HOST + "/api/eox/hwLifecycle/",
},
{
"name": "invalid sku search",
"mainSku": "DCS-7800R3AK-25",
"url": "https://" + API_HOST + "/api/eox/hwLifecycle/",
},
{
"name": "release search",
"releaseTrain": "4.20",
"url": "https://" + API_HOST + "/api/eox/swLifecycle/",
},
{
"name": "invalid release search",
"releaseTrain": "4.40",
"url": "https://" + API_HOST + "/api/eox/swLifecycle/",
},
]
def getSessionCode(api_token):
"""
args:
- api_token(str): the arista API token for the user as a base64 encoded string
returns: the session_code from the API call which can be used for subsequent
interactions. (str)
"""
data = {"accessToken": api_token}
session_info = {}
session_req = requests.post(SESSION_CODE_API_URL, json=data, timeout=5)
if session_req.status_code != requests.codes.ok:
print("session code request error:", session_req)
print("-" * 40)
pprint.pprint(session_req.content)
exit(1)
else:
session_info = session_req.json()
if 200 <= int(session_info["status"]["code"]) < 300:
api_session_key = session_info["data"]["session_code"]
else:
print("error: invalid session info")
print("-" * 40)
pprint.pprint(session_info)
exit(1)
return api_session_key
def getLifecycleData(session_code, search_params):
"""getLifecycleData - builds the search query based on the provided search_params,
args:
- session_code(str): encrypted session_code for the session
- search_params(dict): search parameters
returns:
- dict/list with the search results
"""
search_fields = ["releaseTrain", "mainSku", "altModelNumber"]
search_results = {}
data = {"sessionCode": session_code}
for field in search_fields:
if field in search_params and search_params[field] != "":
data[field] = search_params[field]
info_req = requests.post(search_params["url"], json=data, timeout=5)
try:
search_results = info_req.json()
except requests.exceptions.JSONDecodeError:
print("error: unable to decode info json")
print("-" * 40)
pprint.pprint(info_req.content)
return search_results
def main():
"""where the action is jackson"""
token = os.environ.get("ANET_API_TOKEN")
creds = (base64.b64encode(token.encode())).decode("utf-8")
session_key = getSessionCode(creds)
for idx, search in enumerate(search_tests):
print(f'{idx}: {search["name"]}')
results = getLifecycleData(session_key, search)
pprint.pprint(results)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment