-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
68 lines (53 loc) · 1.79 KB
/
Copy pathmain.py
File metadata and controls
68 lines (53 loc) · 1.79 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
from dotenv import load_dotenv
import os
import requests
import yaml
BASE_AUTH_URL = "https://auth.apps.paloaltonetworks.com/auth/v1/oauth2/access_token"
HEADERS = {
"Accept": "application/json",
}
BASE_URL = "https://api.strata.paloaltonetworks.com/config/deployment/v1/"
AUTH_HEADERS = {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
}
load_dotenv()
TSG_ID = os.environ.get("TSG_ID")
CLIENT_ID = os.environ.get("CLIENT_ID")
SECRET_ID = os.environ.get("SECRET_ID")
def create_token():
auth_url = f"{BASE_AUTH_URL}?grant_type=client_credentials&scope:tsg_id:{TSG_ID}"
token = requests.request(
method="POST",
url=auth_url,
headers=AUTH_HEADERS,
auth=(CLIENT_ID, SECRET_ID),
).json()
HEADERS.update({"Authorization": f'Bearer {token["access_token"]}'})
def read_yaml_file():
with open("dns.yaml", "r") as f:
dns_list = yaml.safe_load(f)
return dns_list
def create_dns_server(dns_config):
url = f"{BASE_URL}internal-dns-servers"
payload = {
"name": dns_config.get("name", ""),
"domain_name": dns_config.get("domain_name", ""),
"primary": dns_config.get("primary", ""),
"secondary": dns_config.get("secondary", ""),
}
try:
response = requests.request(
method="POST", url=url, headers=HEADERS, data=payload
)
response.raise_for_status()
except requests.exceptions.HTTPError as e:
print(f"HTTP error: {e.response.status_code} - {e.response.text}")
except Exception as e:
print(e)
if __name__ == "__main__":
create_token()
dns_list = read_yaml_file()
for dns_config in dns_list:
print(f"Creating DNS Server for {dns_config['name']}")
create_dns_server(dns_config=dns_config)