Troubleshooting Python Policy Migration
The purpose of this article is to address some of the issues people might encounter while testing out this worklet (taken from: https://www.automox.com/blog/winning-worklets-python-policy-migration).
Note: At the end of the page is the edited script that has several notes inside, which will be addressed individually:
# May need to install "requests", may need to upgrade pip, and double check API key
- This is just an initial note stating you may need the module "requests" and you may need to upgrade your "pip" installation. Double checking the API key is always recommended!
# <- Take out "os.environ.get()"
- Make sure to remove the "
os.environ.get()
" module (including the parentheses). This module is used when you have an API key stored on the device locally (elsewhere). Also, quotations are needed around the "AX" variable. - This happens twice in the script
# print(data) <- optional for testing (comment out everything south of function list_specific_policy)
- A "print(data)" function was added for testing purposes. It is just after the "
data = ax.policies.json()
" line. The purpose of this test is to determine if the API key is correct. If the API key is incorrect, it will return an error such as:{'errors': ['Access denied']}
. This is critical to test to ensure the API key is correct. If you receive the "Access Denied" error, you will also be presented with a traceback error. - If running the "print(data)" function, it is also critically important to comment out everything south of function "list_specific_policy" when testing the API key. If this section is not commented out, it will preemptively copy all of the policies from the old org to the new one.
#!/usr/bin/env python3
# May need to install "requests", may need to upgrade pip, and double check API key
from pprint import pprint
import requests
import os
#===========================================================#
# HEADER_VALUES #
#===========================================================#
# Current org environment variables
AX_API_TOKEN = os.environ.get("537be933-466d-44be-aec4-d625c7664d82") # <- Take out "os.environ.get()"
AX = https://console.automox.com/api # <- Needs quotations
AX_HEADERS = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": f"Bearer {AX_API_TOKEN}",
}
# New org environment variables
AX_API_TOKEN_2 = os.environ.get("1cf54c94-ae8e-4e37-b9f6-4c00c24bd181") # <- Take out "os.environ.get()"
AX_HEADERS_2 = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": f"Bearer {AX_API_TOKEN_2}",
}
#======================================================================================================================#
# SCRIPT_INFO #
# This script was built to migrate policies from one Automox org to another. It will only move policies that are not #
# currently in the new org. If it detects a policy with the same name it will give a 400 response. Response code 201 #
# means it successfully moved a policy #
# #
# Author: Ryan Braunstein #
# Company: Automox, Inc. #
# Version: 1.0 #
# Version Notes: Be the first to update this section!!! #
#======================================================================================================================#
# Retrieves all policy ID numbers from the current org
def retrieve_all_policy_ids():
policy_ids = []
query = {
"o": "11750",
"page": "0",
"limit": "500"
}
#Cycles through the policy IDs and adds them to a list
ax_policies = requests.get(f"{AX}/policies", headers=AX_HEADERS, params=query)
data = ax_policies.json()
# print(data) <- optional for testing (comment out everything south of function list_specific_policy)
for policy in data:
policy_ids.append(policy['id'])
return policy_ids
# Runs through the list of policies, formats them, and POSTs them to the new Org
def list_specific_policy(policy_ids):
query = {
"o": "11750"
}
for id in policy_ids:
ax_policies = requests.get(f"{AX}/policies/{id}", headers=AX_HEADERS, params=query)
data = ax_policies.json()
query2 = {
"o": "105729"
}
# Formats all relevant info for the current org to be POSTed to the new org
body = {
"name": data['name'],
"policy_type_name": data['policy_type_name'],
"organization_id": '105729',
"schedule_days": data['schedule_days'],
"schedule_weeks_of_month": data['schedule_weeks_of_month'],
"schedule_months": data['schedule_months'],
"schedule_time": data['schedule_time'],
"configuration": data['configuration']
}
ax_policies_post = requests.post(f"{AX}/policies", json=body, headers=AX_HEADERS_2, params=query2)
pprint(ax_policies_post)
if __name__ == "__main__":
policies = retrieve_all_policy_ids()
list_specific_policy(policies)
Comments
0 comments
Article is closed for comments.