-
Notifications
You must be signed in to change notification settings - Fork 0
/
ax_policy_migration.py
83 lines (74 loc) · 3.56 KB
/
ax_policy_migration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
from pprint import pprint
import requests
import os
#===========================================================#
# HEADER_VALUES #
#===========================================================#
# Current org environment variables
AX_API_TOKEN = os.environ.get("AX_API_TOKEN")
AX = "https://console.automox.com/api"
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("AX_API_TOKEN_2")
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": "Current Org ID",
"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()
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": "Current Org ID"
}
for id in policy_ids:
ax_policies = requests.get(f"{AX}/policies/{id}", headers=AX_HEADERS, params=query)
data = ax_policies.json()
query2 = {
"o": "New Org ID"
}
# 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": 'New Org ID',
"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)