@@ -949,6 +949,61 @@ def get_snipe_user_id(username):
949949 return "NotFound"
950950
951951
952+ def get_snipe_locations (previous = []):
953+ locations_url = f"{ snipe_base } /api/v1/locations"
954+ payload = {"limit" : 100 , "offset" : len (previous )}
955+ logging .debug ("The payload for the snipe locations GET is {}" .format (payload ))
956+ response = session .get (
957+ locations_url ,
958+ headers = snipeheaders ,
959+ params = payload ,
960+ verify = user_args .do_not_verify_ssl ,
961+ hooks = {"response" : request_handler },
962+ )
963+ response_json = response .json ()
964+ current = response_json ["rows" ]
965+ if previous :
966+ current = previous + current
967+ if response_json ["total" ] > len (current ):
968+ logging .debug (
969+ "We have more than 100 locations, get the next page - total: {} current: {}" .format (
970+ response_json ["total" ], len (current )
971+ )
972+ )
973+ return get_snipe_locations (current )
974+ else :
975+ return current
976+
977+
978+ def get_snipe_location_id (location_name ):
979+ if location_name == "" :
980+ return "NotFound"
981+ location_name = location_name .lower ()
982+ for location in snipe_locations :
983+ for value in location .values ():
984+ if str (value ).lower () == location_name :
985+ id = location ["id" ]
986+ return id
987+ logging .debug (
988+ "No matches in snipe_locations for {}, querying the API for the next closest match" .format (
989+ location_name
990+ )
991+ )
992+ location_id_url = "{}/api/v1/locations" .format (snipe_base )
993+ payload = {"search" : location_name , "limit" : 1 , "sort" : "name" , "order" : "asc" }
994+ logging .debug ("The payload for the snipe location search is: {}" .format (payload ))
995+ response = session .get (
996+ location_id_url ,
997+ headers = snipeheaders ,
998+ params = payload ,
999+ verify = user_args .do_not_verify_ssl ,
1000+ hooks = {"response" : request_handler },
1001+ )
1002+ try :
1003+ return response .json ()["rows" ][0 ]["id" ]
1004+ except :
1005+ return "NotFound"
1006+
9521007# Function that creates a new Snipe Model - not an asset - with a JSON payload
9531008def create_snipe_model (payload ):
9541009 api_url = "{}/api/v1/models" .format (snipe_base )
@@ -1096,7 +1151,28 @@ def checkin_snipe_asset(asset_id):
10961151
10971152
10981153# Function that checks out an asset in snipe
1099- def checkout_snipe_asset (user , asset_id , checked_out_user = None ):
1154+ def checkout_snipe_asset (location , asset_id , checked_out_user = None ):
1155+ jamfsplit = config ["user-mapping" ]["jamf_api_field" ].split ()
1156+ checked_out = None
1157+
1158+ user = None
1159+ building = None
1160+ for field in jamfsplit :
1161+ if field in location :
1162+ if field == "username" :
1163+ user = location [field ]
1164+ elif field == "building" :
1165+ building = location [field ]
1166+
1167+ if user :
1168+ checked_out = checkout_snipe_asset_to_user (user , asset_id , checked_out_user )
1169+
1170+ if not checked_out and building :
1171+ checked_out = checkout_snipe_asset_to_location (building , asset_id , checked_out_user )
1172+
1173+ return checked_out
1174+
1175+ def checkout_snipe_asset_to_user (user , asset_id , checked_out_user = None ):
11001176 logging .debug ("Asset {} is being checked out to {}" .format (user , asset_id ))
11011177 user_id = get_snipe_user_id (user )
11021178 if user_id == "NotFound" :
@@ -1142,6 +1218,36 @@ def checkout_snipe_asset(user, asset_id, checked_out_user=None):
11421218 return response
11431219
11441220
1221+ def checkout_snipe_asset_to_location (location , asset_id , checked_out_user = None ):
1222+ location_id = get_snipe_location_id (location )
1223+ api_url = "{}/api/v1/hardware/{}/checkout" .format (snipe_base , asset_id )
1224+ logging .info ("Checking out {} to check it out to {}" .format (asset_id , location ))
1225+ payload = {
1226+ "checkout_to_type" : "location" ,
1227+ "assigned_location" : location_id ,
1228+ "note" : "Assignment made automatically, via script from Jamf." ,
1229+ }
1230+ logging .debug ("The payload for the snipe checkin is: {}" .format (payload ))
1231+ response = session .post (
1232+ api_url ,
1233+ headers = snipeheaders ,
1234+ json = payload ,
1235+ verify = user_args .do_not_verify_ssl ,
1236+ hooks = {"response" : request_handler },
1237+ )
1238+ logging .debug ("The response from Snipe IT is: {}" .format (response .json ()))
1239+ if response .status_code == 200 :
1240+ logging .debug ("Got back status code: 200 - {}" .format (response .content ))
1241+ return "CheckedOut"
1242+ else :
1243+ logging .error (
1244+ "Asset checkout failed for asset {} with error {}" .format (
1245+ asset_id , response .text
1246+ )
1247+ )
1248+ return response
1249+
1250+
11451251### Run Testing ###
11461252# Report if we're verifying SSL or not.
11471253logging .info ("SSL Verification is set to: {}" .format (user_args .do_not_verify_ssl ))
@@ -1239,6 +1345,7 @@ jamf_types = {"computers": jamf_computer_list, "mobile_devices": jamf_mobile_lis
12391345
12401346if user_args .users or user_args .users_force or user_args .users_inverse :
12411347 snipe_users = get_snipe_users ()
1348+ snipe_locations = get_snipe_locations ()
12421349
12431350TotalNumber = 0
12441351if user_args .computers :
@@ -1461,7 +1568,7 @@ for jamf_type in jamf_types:
14611568 )
14621569 )
14631570 checkout_snipe_asset (
1464- jamf ["{}" . format ( jamfsplit [0 ])][ "{}" . format ( jamfsplit [ 1 ]) ],
1571+ jamf [jamfsplit [0 ]],
14651572 new_snipe_asset [1 ].json ()["payload" ]["id" ],
14661573 "NewAsset" ,
14671574 )
@@ -1585,7 +1692,7 @@ for jamf_type in jamf_types:
15851692 )
15861693 continue
15871694 checkout_snipe_asset (
1588- jamf ["{}" . format ( jamfsplit [0 ])][ "{}" . format ( jamfsplit [ 1 ]) ],
1695+ jamf [jamfsplit [0 ]],
15891696 snipe_id ,
15901697 snipe ["rows" ][0 ]["assigned_to" ],
15911698 )
0 commit comments