This repository has been archived by the owner on Dec 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnetbox.py
149 lines (126 loc) · 4.39 KB
/
netbox.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
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
146
147
148
import json
import requests
import ipaddress
netbox_url = "http://XXX.XXX.XXX.XXX/api"
twingate_url = "https://XXXXXXXXX.twingate.com/api/graphql/"
twingate_token = "XXXXXXXXXXXXXXX"
netbox_token = "XXXXXXXXXXX"
netbox_headers = {
"Authorization": f"Token {netbox_token}",
"Accept": "application/json",
"Content-Type": "application/json"
}
twingate_headers = {
'X-API-KEY': (twingate_token),
'Content-Type': 'application/json',
'Cookie': 'csrftoken=BfevZbn9nAsnJIekbalPWSfwbg1bSNIg'
}
# dictonary of the twingate api http bodies
body = {
"resource": {
"query": """
query getResources {
resources {
edges {
node {
id
name
}
}
}
}
"""
},
"group": {
"query": """
{
groups(after: null, first:100) {
edges {
node {
id
name
createdAt
updatedAt
isActive
type
users {
edges{
node{
id
email
firstName
lastName
}
}
}
}
}
}
}
"""
},
"network": {
"query": """
{
remoteNetworks(after: null, first:100) {
edges {
node {
id
name
createdAt
updatedAt
}
}
}
}
"""
}
}
# Get device with tag from netbox
netbox_response = requests.get(f"{netbox_url}/dcim/devices?tag=twingate", headers=netbox_headers)
if netbox_response.status_code == 200:
netbox_data = json.loads(netbox_response.text)
else:
print(f"Error getting devices from Netbox: {netbox_response.text}")
# Get Twingate resources
response = requests.post(twingate_url, json=body["resource"], headers=twingate_headers)
twingate_resources = [d['node'] for d in response.json()["data"]["resources"]["edges"]]
# Get Twingate groups
response = requests.post(twingate_url, json=body["group"], headers=twingate_headers)
twingate_groups = [d['node'] for d in response.json()["data"]["groups"]["edges"]]
# Get Twingate remote networks
response = requests.post(twingate_url, json=body["network"], headers=twingate_headers)
twingate_networks = [d['node'] for d in response.json()["data"]["remoteNetworks"]["edges"]]
for device in netbox_data['results']:
device_name = device['name']
device_ip = device['primary_ip']['address']
tenant_name = device['tenant']['name']
device_ip = ipaddress.IPv4Address(device_ip.split("/")[0])
if device_name in [d['name'] for d in twingate_resources]:
print(f"{device_name} is a Twingate resource already, skipping.")
break
group = [d for d in twingate_groups if tenant_name == d['name']]
remote_network = [d for d in twingate_networks if tenant_name == d['name']]
# confirm the group and remote network exist in Twingate
if len(group) == 0:
print(f"Group {tenant_name} not found in Twingate")
if len(remote_network) == 0:
print(f"RemoteNetworks {tenant_name} not found in Twingate")
break
query = f"""
mutation {{
resourceCreate(address:"{device_ip}", groupIds:"{group[0]["id"]}", name:"{device_name}", remoteNetworkId:"{remote_network[0]["id"]}")
{{
error
ok
}}
}}
"""
response = requests.post(twingate_url, json={'query': query}, headers=twingate_headers)
# Check for successful response from Twingate API
response = json.loads(response.text)
if response["data"][list(response["data"].keys())[0]]["ok"]:
print(f"Successfully added device (Twingate resource) {device_name}.")
else:
print(f"Failed to add device (Twingate resource) {device_name}. Error: {response['data'][list(response['data'].keys())[0]]['error']}")
print("Script Completed.")