You are here

Slicehost DNS API Python code

I'm setting up quite a few domains these days. The interface of the company is far from great and it doesn't allow bulk updates for DNS records. Fortunately I still have a Slicehost account. At the moment I don't have any VPS running there but they still allow their customers to use their DNS services. I think that's a smart way of retaining clients - and getting new ones.

I've been using their DNS services at times when I, a friend or a client was in need for it. And like Slicehost slices, the services have been very reliable. So I decided to check out their API and I was happy to see there was some Python code. I dove into it and wrote a nice script (I love Python) that allows me to create and update DNS zones and records for the many new domains I recently got.

I thought it could be useful for someone else as well, so here it is.
If you don't have a Slicehost account yet, and you want to create one (because of this script? ;) please use sign up through this link to give me some Slicehost credit :)

If you're on Debian or Ubuntu you can fetch the required pyactiveresource library like this:

sudo apt-get install python-setuptools
sudo easy_install pyactiveresource

The code

"""
Slicehost DNS API Python code

written by Guaka in 2011

This is free and unencumbered software released into the public domain.  See http://unlicense.org/
"""

import urllib, re, sys, os
from pyactiveresource.activeresource import ActiveResource

api_key = 'YOURAPIKEY - GET THIS IN YOUR SLICEHOST CONTROL PANEL'
api_url = 'https://%[email protected]/' % api_key

class Record(ActiveResource):
    _site = api_url

class Zone(ActiveResource):
    _site = api_url


def new_record(zone_id, rectype, name, data, ttl = 3600):
    myrecord = Record({'record_type': rectype,
                       'zone_id': zone_id,
                       'name': name,
                       'data': data,
                       'ttl': ttl,
                       })
    myrecord.save()

def new_zone(name):
    myzone = Zone({'origin': name, 'ttl': 3600})
    myzone.save()
    return myzone



def set_default_records(zone_id, ip, domain):
    new_record(zone_id, 'A', '*', ip)
    new_record(zone_id, 'A', '@', ip)
    new_record(zone_id, 'A', domain + '.', ip)
    new_record(zone_id, 'NS', domain + '.', 'ns1.slicehost.net.')
    new_record(zone_id, 'NS', domain + '.', 'ns2.slicehost.net.')
    new_record(zone_id, 'NS', domain + '.', 'ns3.slicehost.net.')
    records = Record.find(zone_id = zone_id)
    return records


def update_zones(domainlist, ip):
    zone_dict = dict([(x.attributes['origin'][0:-1], x.attributes['id'])
                      for x in Zone.find()])
    for domain in domainlist:
        print domain
        if zone_dict.has_key(domain):
            zone_id = zone_dict[domain]
            print "existing", zone_id
        else:
            zone_id = new_zone(domain).id
            print "new", zone_id
        print [x.attributes for x in set_default_records(zone_id, ip, domain)]

domaintext = """                                                                                                                                               
yourdomains.net to-update.to can-be-put-here.com
separated-by-whitespace.org
"""

domains = domaintext.split()
print domains
update_zones(domains, '123.123.123.123')