-
Notifications
You must be signed in to change notification settings - Fork 2
/
create-network.py
61 lines (49 loc) · 1.69 KB
/
create-network.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
# Create a network based on the command line parameters
import sys
import os
import argparse
import json
import time
import requests
# Get the key from the environment
key = os.environ.get('NETTICA_API_KEY')
if key == None:
# If the API key is not set in the environment, check args
if len(sys.argv) > 1:
key = sys.argv[1]
else:
# If the API key is not set in the environment or args, exit
print('You must set NETTICA_API_KEY in the environment or pass it as an argument')
sys.exit(1)
# Parse the command line arguments for the name, subnet, and DNS servers
parser = argparse.ArgumentParser(description='Create a network')
parser.add_argument('--name', help='Name of the network', required=True)
parser.add_argument('--subnet', help='Subnet of the network', required=True)
parser.add_argument('--dns', help='DNS servers for the network', required=False)
args = parser.parse_args()
# Create a new request
# Set the request URL
url = 'https://my.nettica.com/api/v1.0/net'
# Set the request headers
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-API-KEY': key
}
# Set the request payload
payload = {
'netName': args.name,
'subnet': [ args.subnet ],
}
# Add the DNS servers if they were specified
if args.dns:
payload['dns'] = [ args.dns ]
# Send the request to create the network
response = requests.post(url, headers=headers, json=payload)
# Check the response status code
if response.status_code == 200:
print('Network created successfully!')
print (json.dumps(response.json(), indent=2))
else:
print('Failed to create network:', response.status_code)
print(json.dumps(response.json(), indent=2))