-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpfmigrate.py
More file actions
145 lines (121 loc) · 4.54 KB
/
Copy pathpfmigrate.py
File metadata and controls
145 lines (121 loc) · 4.54 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/python3
import requests
from PyInquirer import style_from_dict, prompt, Token
from rich.console import Console
from rich.traceback import install
# Register rich traceback hook
install()
console = Console()
LEGACY_API = "https://legacy.packetframe.com/api"
V4_API = "https://packetframe.com/api"
PYINQUIRER_STYLE = style_from_dict({
Token.Separator: "#6C6C6C",
Token.QuestionMark: "#FFF",
Token.Selected: "#dd00ff",
Token.Pointer: "#FFF",
Token.Instruction: "",
Token.Answer: "#dd00ff",
Token.Question: "",
})
API_KEY = ""
V4_API_KEY = ""
def legacy_request(message, route, method, body=None):
with console.status(f"[bold green]{message}..."):
r = requests.request(method, LEGACY_API + route, json=body, headers={"X-API-Key": API_KEY})
if r.status_code != 200:
console.log(f"[bold red]ERROR (request)[reset] code {r.status_code} body {r.text}")
exit(1)
elif not r.json()["success"]:
console.log(f"[bold red]ERROR (api)[reset] {r.json()['message']}")
exit(1)
return r.json()["message"]
def legacy_login():
console.print("[underline]Packetframe (Legacy) Login")
account = prompt([
{
"type": "input",
"name": "username",
"message": "Email:",
},
{
"type": "password",
"message": "Password:",
"name": "password"
}
], style=PYINQUIRER_STYLE)
if account:
r = legacy_request(f"Logging in as {account['username']}", "/auth/login", "POST", account)
console.print("[bold green]Login successful")
global API_KEY
API_KEY = r
def v4_login():
console.print("[underline]Packetframe (v4) Login")
account = prompt([
{
"type": "input",
"name": "username",
"message": "Email:",
},
{
"type": "password",
"message": "Password:",
"name": "password"
}
], style=PYINQUIRER_STYLE)
if account:
r = requests.request("POST", V4_API+"/user/login", json={
"email": account["username"],
"password": account["password"]
})
if r.status_code != 200:
print("Invalid username or password")
exit(1)
global V4_API_KEY
V4_API_KEY = r.json()["data"]["token"]
def select_zone():
zones = legacy_request("Getting zones", "/zones/list", "GET")
answer = prompt([
{
"type": "list",
"name": "Zone",
"message": "Which zone do you want to migrate?",
"choices": map(lambda z: z["zone"], zones),
},
], style=PYINQUIRER_STYLE)["Zone"]
for zone in zones:
if zone["zone"] == answer:
confirm = prompt(questions=[
{
"type": "confirm",
"message": f"Are you sure you want to migrate {zone['zone']}?",
"name": "continue",
"default": False,
},
], style=PYINQUIRER_STYLE)["continue"]
if not confirm:
print("Migration cancelled")
exit()
with console.status(f"[bold green]Adding zone..."):
r = requests.request("POST", V4_API + "/dns/zones", json={"zone": zone["zone"]}, headers={"Authorization": "Token " + V4_API_KEY})
if r.status_code != 200:
print(f"Error adding zone: {r.text}")
zone_id = ""
r = requests.request("GET", V4_API + "/dns/zones", json={}, headers={"Authorization": "Token " + V4_API_KEY})
for z in r.json()["data"]["zones"]:
if z["zone"] == zone["zone"] + ".":
zone_id = z["id"]
with console.status(f"[bold green]Adding records..."):
for record in zone["records"]:
r = requests.request("POST", V4_API + "/dns/records", json={
"zone": zone_id,
"label": record["label"],
"ttl": record["ttl"],
"type": record["type"],
"value": record["value"]
}, headers={"Authorization": "Token " + V4_API_KEY})
if r.status_code != 200:
print(f"Unable to add record ({record['label']} {record['type']} {record['ttl']} {record['value']}): {r.text}")
console.print(f"[bold green]Zone {zone['zone']} migrated successfully!")
legacy_login()
v4_login()
select_zone()