import requests
from time import sleep
url = "http://193.10.237.3:8118/api/v1/hardware"
token = "YOUR SNIPE-IT TOKEN HERE"
RACK = "H"
ROOM = "D204A"
locations = {"D204A": 2, "D209": 8, "D216A": 3, "D216B": 4, "D119A": 5, "D119B": 6}
assets = ["CNAP00051", "IT014202", "IT014201", "IT014218", "IT014210", "CNAP00142"]
serials = ["FCZ184960N3", "FCZ2814R0GE", "FCZ2814R0GB", "FOC2812Y1E2", "FOC2812Y1BJ", "FCW1837A0HB"]
aszip = list(zip(assets, serials))
def batch_asset_creation(aszip, room, rack, headers):
# location ids: 8 = d209, 2 = d204a
# category ids: 2 = router, 3 = switch
# model id: 1 = 2911, 2 = C8200, 3 = C1000, 4 = C2960X
template = {
"company_id": 1,
"status_id": 2,
"archived": False,
"warranty_months": None,
"depreciate": False,
"supplier_id": None,
"requestable": False,
"rtd_location_id": 8,
"location_id": locations[room],
"assigned_to_id": locations[room],
}
payload = [
{
"asset_tag": aszip[0][0],
"serial": aszip[0][1],
"name": f"R1-{room}-{rack}",
"notes": f"Router1-rack{rack}",
"category": 2,
"model_id": 1,
},
{
"asset_tag": aszip[1][0],
"serial": aszip[1][1],
"name": f"R2-{room}-{rack}",
"notes": f"Router2-rack{rack}",
"category": 2,
"model_id": 2,
},
{
"asset_tag": aszip[2][0],
"serial": aszip[2][1],
"name": f"R3-{room}-{rack}",
"notes": f"Router3-rack{rack}",
"category": 2,
"model_id": 2,
},
{
"asset_tag": aszip[3][0],
"serial": aszip[3][1],
"name": f"S1-{room}-{rack}",
"notes": f"Switch1-rack{rack}",
"category": 3,
"model_id": 3,
},
{
"asset_tag": aszip[4][0],
"serial": aszip[4][1],
"name": f"S2-{room}-{rack}",
"notes": f"Switch2-rack{rack}",
"category": 3,
"model_id": 3,
},
{
"asset_tag": aszip[5][0],
"serial": aszip[5][1],
"name": f"S3-{room}-{rack}",
"notes": f"Switch3-rack{rack}",
"category": 3,
"model_id": 4,
},
]
try:
for entry in payload:
entry.update(template)
response = requests.post(url, json=entry, headers=headers)
print(f'Created {entry["name"]} - Status: {response.status_code}')
device_id = response.json()["payload"]["id"]
response = checkout_asset_to_location(device_id, room)
print(f'Checking in {entry["name"]} - Status: {response.status_code}')
sleep(0.1)
return True
except:
return False
def batch_create_single_asset_type(asset, category, model_id, room):
payload = {
"company_id": 1,
"status_id": 2,
"archived": False,
"warranty_months": None,
"depreciate": False,
"supplier_id": None,
"requestable": False,
"rtd_location_id": 8,
"location_id": locations[room],
"assigned_to_id": locations[room],
"asset_tag": asset,
"category": category,
"model_id": model_id
}
return requests.post(url, json=payload, headers=headers)
def checkout_asset_to_location(asset, room):
checkout_payload = {
"status_id": 2,
"checkout_to_type": "location",
"assigned_location": locations[room],
}
return requests.post(f'{url}/{asset}/checkout', json=checkout_payload, headers=headers)
headers = {
"accept": "application/json",
"Authorization": f"Bearer {token}",
"content-type": "application/json"
}
NUCS = [
"IT014071","IT014086","IT014072","IT014074","IT014085","IT014073",
"IT014075","IT014076","IT014077","IT014078","IT014080","IT014079",
"IT014081","IT014082","IT014083","IT014084"
]
for asset in NUCS:
response = batch_create_single_asset_type(asset, 5, 6, "D204A")
print(f'Created {asset} - Status: {response.status_code}')
device_id = response.json()["payload"]["id"]
response = checkout_asset_to_location(device_id, "D204A")
print(f'Checking in {asset} - Status: {response.status_code}')
sleep(0.1)
Rudimentary script to populate a snipe-it instance with assets, in the same vein as Netbox but more focused on asset management directly. Still requires some manual fiddling, which is not ideal.