I have this script and fails when i wanna add ips via API. I copied and modified the script from the docs
import requests
import time
import uuid
import hmac
import hashlib
import base64
import json
import sys
BASE_URL = 'https://xxx.xxxx.com/'
API_TOKEN = 'xxxx'
API_SECRET = 'xxx'
SERVER_ID = 'xxxx'
terraform_response = requests.get('https://app.terraform.io/api/meta/ip-ranges')
terraform_ranges = terraform_response.json()
def auth_request(method, path, headers=None, data=None):
auth_timestamp = str(int(time.time()))
auth_nonce = uuid.uuid4().hex
auth_string = '&'.join([API_TOKEN, auth_timestamp, auth_nonce,
method.upper(), path])
if sys.version_info[0] < 3:
auth_signature = base64.b64encode(hmac.new(
API_SECRET, auth_string, hashlib.sha256).digest())
else:
auth_signature = base64.b64encode(hmac.new(
API_SECRET.encode('utf-8'), auth_string.encode('utf-8'), hashlib.sha256).digest())
auth_headers = {
'Auth-Token': API_TOKEN,
'Auth-Timestamp': auth_timestamp,
'Auth-Nonce': auth_nonce,
'Auth-Signature': auth_signature,
}
if headers:
auth_headers.update(headers)
return getattr(requests, method.lower())(
BASE_URL + path,
headers=auth_headers,
data=data,
)
response = requests.get('https://app.terraform.io/api/meta/ip-ranges')
ranges = response.json()
routes = []
ranges_data = response.json()
for service, ips in ranges_data.items():
for ip in ips:
routes.append({
'network': ip,
'nat': True,
})
#print(json.dumps(routes))
#exit(1)
response = auth_request(
'POST',
'/server/%s/routes' % SERVER_ID,
headers={
'Content-Type': 'application/json',
},
data=json.dumps(routes),
)
assert(response.status_code == 200)
the result of the json data is
[{“network”: “75.2.98.97/32”, “nat”: true}, {“network”: “99.83.150.238/32”, “nat”: true}, {“network”: “52.86.200.106/32”, “nat”: true}, {“network”: “52.86.201.227/32”, “nat”: true}, {“network”: “52.70.186.109/32”, “nat”: true}, {“network”: “44.236.246.186/32”, “nat”: true}, {“network”: “54.185.161.84/32”, “nat”: true}, {“network”: “44.238.78.236/32”, “nat”: true}, {“network”: “52.86.200.106/32”, “nat”: true}, {“network”: “52.86.201.227/32”, “nat”: true}, {“network”: “52.70.186.109/32”, “nat”: true}, {“network”: “44.236.246.186/32”, “nat”: true}, {“network”: “54.185.161.84/32”, “nat”: true}, {“network”: “44.238.78.236/32”, “nat”: true}, {“network”: “52.86.200.106/32”, “nat”: true}, {“network”: “52.86.201.227/32”, “nat”: true}, {“network”: “52.70.186.109/32”, “nat”: true}, {“network”: “44.236.246.186/32”, “nat”: true}, {“network”: “54.185.161.84/32”, “nat”: true}, {“network”: “44.238.78.236/32”, “nat”: true}]