Skip to content

Instantly share code, notes, and snippets.

@stackexpress-shivam
Forked from sandromello/pdns.py
Created December 29, 2016 13:13
Show Gist options
  • Save stackexpress-shivam/a947c24ac40ba52b9f6c6fd58e334247 to your computer and use it in GitHub Desktop.
Save stackexpress-shivam/a947c24ac40ba52b9f6c6fd58e334247 to your computer and use it in GitHub Desktop.
Power DNS API implementation
import unittest, sys, requests, json, uuid
from datetime import datetime
#url = 'http://192.168.99.100/servers/localhost/zones/'
#url = 'http://192.168.99.100/servers/localhost/zones/example.local.'
#url = 'http://192.168.99.100/servers'
SERVER = 'pdns.tempo.com.br:8080'
HEADERS = {'X-API-Key' :'changeme'}
def idt(r):
print json.dumps(r, indent=2),
class ClientTestCase(unittest.TestCase):
def create_new_domain(self):
""" Create a new domain with the proper nameservers and SOA records
"""
url = 'http://%s/servers/localhost/zones' % SERVER
serial = datetime.now().strftime('%Y%m%d')
new_domain_data = {
'name' : 'temponetworks.com.br',
'kind' : 'native',
'masters' : [],
'nameservers' : ['ns1.temponetworks.com.br', 'ns2.temponetworks.com.br'],
'records' : [
{
'content' : 'ns1.temponetworks.com.br hostmaster.temponetworks.com.br %s01 10800 7200 604800 86400' % serial,
'name' : 'temponetworks.com.br',
'ttl' : 300,
'type' : 'SOA',
'disabled' : False
}
]
}
del_url = 'http://%s/servers/localhost/zones/temponetworks.com.br.' % SERVER
r = requests.delete(del_url, headers=HEADERS)
#idt(r.json()),
print r.text, r.status_code
r = requests.post(url, data=json.dumps(new_domain_data), headers=HEADERS)
print json.dumps(r.json(), indent=2), r.status_code
def create_new_A_record(self):
""" Create/Update a new A record
"""
url = 'http://%s/servers/localhost/zones/temponetworks.com.br' % SERVER
new_rrsets = {
'rrsets' : [
{
'name' : 'temponetworks.com.br',
'type' : 'A',
'changetype' : 'REPLACE',
'records' : [
{
'content' : '52.20.203.229',
'name' : 'temponetworks.com.br',
'ttl' : 300,
'type' : 'A',
'disabled' : False,
'priority' : 0
}
]
},
]
}
r = requests.patch(url, data=json.dumps(new_rrsets), headers=HEADERS)
print json.dumps(r.json(), indent=2), r.status_code
new_rrsets['rrsets'][0]['name'] = 'ns1.temponetworks.com.br'
new_rrsets['rrsets'][0]['records'][0]['name'] = 'ns1.temponetworks.com.br'
r = requests.patch(url, data=json.dumps(new_rrsets), headers=HEADERS)
print json.dumps(r.json(), indent=2), r.status_code
new_rrsets['rrsets'][0]['name'] = 'ns2.temponetworks.com.br'
new_rrsets['rrsets'][0]['records'][0]['name'] = 'ns2.temponetworks.com.br'
r = requests.patch(url, data=json.dumps(new_rrsets), headers=HEADERS)
print json.dumps(r.json(), indent=2), r.status_code
def create_new_MX_record(self):
""" Create/Update two MX records
"""
url = 'http://%s/servers/localhost/zones/inova.net' % SERVER
new_rrsets = {
'rrsets' : [
{
'name' : 'temponetworks.com.br',
'type' : 'MX',
'changetype' : 'REPLACE',
'records' : [
{
'content' : '10 mxlite.u.inova.com.br',
'name' : 'inova.net',
'ttl' : 3600,
'type' : 'MX',
'disabled' : False,
},
{
'content' : '20 mxcorp.u.inova.com.br',
'name' : 'inova.net',
'ttl' : 3600,
'type' : 'MX',
'disabled' : False,
}
]
},
]
}
r = requests.patch(url, data=json.dumps(new_rrsets), headers=HEADERS)
print r.text, r.status_code
def create_new_NS_records(self):
url = 'http://%s/servers/localhost/zones/temponetworks.com.br' % SERVER
new_rrsets = {
'rrsets' : [
{
'name' : 'temponetworks.com.br',
'type' : 'NS',
'changetype' : 'REPLACE',
'records' : [
{
'content' : 'ns1.temponetworks.com.br',
'name' : 'temponetworks.com.br',
'ttl' : 300,
'type' : 'NS',
'disabled' : False
},
{
'content' : 'ns2.temponetworks.com.br',
'name' : 'temponetworks.com.br',
'ttl' : 300,
'type' : 'NS',
'disabled' : False
}
]
},
]
}
r = requests.patch(url, data=json.dumps(new_rrsets), headers=HEADERS)
print r.text, r.status_code
def create_new_CNAME_record(self):
url = 'http://%s/servers/localhost/zones/inova.net' % SERVER
new_rrsets = {
'rrsets' : [
{
'name' : 'www.temponetworks.com.br',
'type' : 'CNAME',
'changetype' : 'REPLACE',
'records' : [
{
'content' : 'temponetworks.com.br',
'name' : 'www.temponetworks.com.br',
'ttl' : 300,
'type' : 'CNAME',
'disabled' : False
}
]
},
]
}
r = requests.post(url, data=json.dumps(new_rrsets), headers=HEADERS)
print r.text, r.status_code
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(ClientTestCase('create_new_domain'))
suite.addTest(ClientTestCase('create_new_A_record'))
#suite.addTest(ClientTestCase('create_new_MX_record'))
suite.addTest(ClientTestCase('create_new_NS_records'))
suite.addTest(ClientTestCase('create_new_CNAME_record'))
#suite.addTest(ClientTestCase('test_domain_crud_operations'))
#suite.addTest(ClientTestCase('test_zdomain_report_crud_operations'))
#suite.addTest(ClientTestCase('test_zdomain_reports_crud_operations'))
unittest.TextTestRunner().run(suite)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment