ungleich-cli/ungleich_ripe.py

83 lines
3 KiB
Python
Raw Normal View History

import argparse
2019-05-12 09:24:31 +00:00
import ipaddress
import json
import urllib.request
import pprint
2019-10-28 10:42:32 +00:00
import requests
2019-05-12 09:24:31 +00:00
# RIPE_URL = "https://rest.db.ripe.net/{source}/{objecttype}/{key}"
2019-10-28 10:42:32 +00:00
# RIPE_URL = "https://rest.db.ripe.net/ripe"
2019-05-12 09:24:31 +00:00
RIPE_URL = "https://rest-test.db.ripe.net/test"
class ungleichRIPE(object):
def __init__(self, parser, parents):
self.parser = parser
2019-05-12 09:24:31 +00:00
self.parser['ripe-add-route6'] = self.parser['sub'].add_parser(
'ripe-add-route6',
parents=[parents],
help="Create route6 object in the ripe database")
2019-05-12 09:24:31 +00:00
self.parser['ripe-add-route6'].add_argument('--network',
required=True)
self.parser['ripe-add-route6'].add_argument('--description',
required=True)
self.parser['ripe-add-route6'].add_argument('--password',
required=True,
help="Password for accessing the RIPE rest API")
self.parser['ripe-add-route6'].set_defaults(func=self.route6_add)
def route6_add(self, args):
try:
net = ipaddress.IPv6Network(args.network)
except Exception as e:
print("Sorry, {} does not look like an IPv6 network: {}".format(args.network, e))
raise
2019-10-28 10:42:32 +00:00
url = "{}/route6?password={}".format(RIPE_URL, args.password)
2019-05-12 09:24:31 +00:00
ripe_object = {}
ripe_object['route6'] = args.network
ripe_object['origin'] = "AS209898"
ripe_object['descr'] = args.description
ripe_object['mnt-by'] = "mnt-ungleich"
2019-10-28 10:42:32 +00:00
ripe_object['source'] = "TEST"
2019-05-12 09:24:31 +00:00
ripe_attributes = [{ "name": key, "value": value } for key, value in ripe_object.items() ]
# Format according to API layout
2019-05-12 09:24:31 +00:00
ripe_element = {}
2019-10-28 10:42:32 +00:00
ripe_element['objects'] = {
"object": [
{
"attributes": {
"attribute": ripe_attributes
}
}
]
}
2019-05-12 09:24:31 +00:00
data = json.dumps(ripe_element).encode('utf-8')
# debug
2019-10-28 10:42:32 +00:00
# pprint.pprint(ripe_element)
2019-05-12 09:24:31 +00:00
2019-10-28 10:42:32 +00:00
# method = 'POST'
# req = urllib.request.Request(url=url,
# data=data,
# method='POST',
# headers={
# "Content-Type": "application/json",
# "Accept": "application/json"
# })
# print("Adding a v6 route object at {} for {} with {} req={}".format(url, args.network, data, str(req)))
2019-05-12 09:24:31 +00:00
2019-10-28 10:42:32 +00:00
# with urllib.request.urlopen(req) as f:
# print(f.read().decode('utf-8'))
2019-05-12 09:24:31 +00:00
2019-10-28 10:42:32 +00:00
r = requests.post(url, data=data, headers={
"Content-Type": "application/json",
"Accept": "application/json"
})
pprint.pprint(json.loads(r.content.decode("utf-8")))
# print(r.content.decode("utf-8"))