Skip to content

Commit d706864

Browse files
authored
Merge pull request #126 from protofy/feat/more_useful_dryrun
Make dryrun more useful for debugging
2 parents 19a38c8 + 6be04d3 commit d706864

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

jamf2snipe

+26-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ from urllib3 import Retry
6262
runtimeargs = argparse.ArgumentParser()
6363
runtimeargs.add_argument("-v", "--verbose", help="Sets the logging level to INFO and gives you a better idea of what the script is doing.", action="store_true")
6464
runtimeargs.add_argument("--auto_incrementing", help="You can use this if you have auto-incrementing enabled in your snipe instance to utilize that instead of adding the Jamf ID for the asset tag.", action="store_true")
65-
runtimeargs.add_argument("--dryrun", help="This checks your config and tries to contact both the JAMFPro and Snipe-it instances, but exits before updating or syncing any assets.", action="store_true")
65+
runtimeargs.add_argument("--dryrun", help="This checks your config and tries to contact both the JAMFPro and Snipe-it instances, and will generate the assets for debugging, but not update or sync anything but exits before updating or syncing any assets.", action="store_true")
66+
runtimeargs.add_argument("--connection_test", help="This checks your config and tries to contact both the JAMFPro and Snipe-it instances.", action="store_true")
6667
runtimeargs.add_argument("-d", "--debug", help="Sets logging to include additional DEBUG messages.", action="store_true")
6768
runtimeargs.add_argument("--do_not_update_jamf", help="Does not update Jamf with the asset tags stored in Snipe.", action="store_false")
6869
runtimeargs.add_argument('--do_not_verify_ssl', help="Skips SSL verification for all requests. Helpful when you use self-signed certificate.", action="store_false")
@@ -91,8 +92,20 @@ elif user_args.verbose:
9192
else:
9293
logging.basicConfig(level=logging.WARNING)
9394

95+
# Notify users if we're doing a connection test.
96+
if user_args.connection_test and user_args.dryrun:
97+
logging.error("You can't use --connection_test and --dryrun at the same time. Please choose one or the other.")
98+
raise SystemExit("Error: Invalid runtime arguments - Exiting.")
99+
if user_args.connection_test and user_args.force:
100+
logging.error("You can't use --connection_test and --force at the same time. Please choose one or the other.")
101+
raise SystemExit("Error: Invalid runtime arguments - Exiting.")
102+
if user_args.connection_test:
103+
print("Connection test: Starting jamf2snipe with a connection test where we'll try to contact both the JAMFPro and Snipe-it instances.")
104+
94105
# Notify users if we're doing a dry run.
95-
if user_args.dryrun:
106+
if user_args.dryrun and user_args.force:
107+
print("Running a dry run with force enabled. This will generate assets for debugging, but not update or sync anything.")
108+
elif user_args.dryrun:
96109
print("Dryrun: Starting jamf2snipe with a dry run where no assets will be updated.")
97110

98111
# Find a valid settings.conf file.
@@ -403,6 +416,9 @@ def search_jamf_mobile(jamf_id):
403416

404417
# Function to update the asset tag of computers in JAMF with an number passed from Snipe.
405418
def update_jamf_asset_tag(jamf_id, asset_tag):
419+
if user_args.dryrun:
420+
logging.debug("Would have updated JAMF asset id: {} with asset tag: {}".format(jamf_id, asset_tag))
421+
return True
406422
api_url = "{}/JSSResource/computers/id/{}".format(jamfpro_base, jamf_id)
407423
payload = """<?xml version="1.0" encoding="UTF-8"?><computer><general><id>{}</id><asset_tag>{}</asset_tag></general></computer>""".format(jamf_id, asset_tag)
408424
logging.debug('Making Get request against: {}\nPayload for the PUT request is: {}\nThe username, password, and headers can be found near the beginning of the output.'.format(api_url, payload))
@@ -426,6 +442,9 @@ def update_jamf_asset_tag(jamf_id, asset_tag):
426442

427443
# Function to update the asset tag of mobile devices in JAMF with an number passed from Snipe.
428444
def update_jamf_mobiledevice_asset_tag(jamf_id, asset_tag):
445+
if user_args.dryrun:
446+
logging.debug("Would have updated JAMF asset id: {} with asset tag: {}".format(jamf_id, asset_tag))
447+
return True
429448
api_url = "{}/JSSResource/mobiledevices/id/{}".format(jamfpro_base, jamf_id)
430449
payload = """<?xml version="1.0" encoding="UTF-8"?><mobile_device><general><id>{}</id><asset_tag>{}</asset_tag></general></mobile_device>""".format(jamf_id, asset_tag)
431450
logging.debug('Making Get request against: {}\nPayload for the PUT request is: {}\nThe username, password, and headers can be found near the beginning of the output.'.format(api_url, payload))
@@ -578,6 +597,9 @@ def create_snipe_asset(payload):
578597

579598
# Function that updates a snipe asset with a JSON payload
580599
def update_snipe_asset(snipe_id, payload):
600+
if user_args.dryrun:
601+
logging.debug("Dry run mode is enabled. We would have updated ID: {} with the following payload: {}".format(snipe_id, payload))
602+
return True
581603
api_url = '{}/api/v1/hardware/{}'.format(snipe_base, snipe_id)
582604
logging.debug('The payload for the snipe update is: {}'.format(payload))
583605
response = session.patch(api_url, headers=snipeheaders, json=payload, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
@@ -745,8 +767,8 @@ else:
745767
raise SystemExit("Unable to get JAMF Computers.")
746768

747769
# After this point we start editing data, so quit if this is a dryrun
748-
if user_args.dryrun:
749-
raise SystemExit("Dryrun: Complete.")
770+
if user_args.connection_test:
771+
raise SystemExit("Connection Test: Complete.")
750772

751773
# From this point on, we're editing data.
752774
logging.info('Starting to Update Inventory')

0 commit comments

Comments
 (0)