DNS Examples

Create an ‘A’ record for all your compute nodes

This example creates a new mydomain2.com zone at Zerigo and an A record for all your Rackspace nodes. Value for the A record is the Node’s first public IP address.

from pprint import pprint

from libcloud.dns.types import Provider as DNSProvider
from libcloud.dns.types import RecordType
from libcloud.compute.types import Provider as ComputeProvider
from libcloud.dns.providers import get_driver as get_dns_driver
from libcloud.compute.providers import get_driver as get_compute_driver

CREDENTIALS_RACKSPACE = ("username", "api key")
CREDENTIALS_ZERIGO = ("email", "api key")

cls = get_compute_driver(ComputeProvider.RACKSPACE)
compute_driver = cls(*CREDENTIALS_RACKSPACE)

cls = get_dns_driver(DNSProvider.ZERIGO)
dns_driver = cls(*CREDENTIALS_ZERIGO)

# Retrieve all the nodes
nodes = compute_driver.list_nodes()

# Create a new zone
zone = dns_driver.create_zone(domain="mydomain2.com")

created = []

for node in nodes:
    name = node.name

    ip = node.public_ips[0] if node.public_ips else None

    if not ip:
        continue

    print("Creating {} record (data={}) for node {}".format("A", ip, name))
    record = zone.create_record(name=name, type=RecordType.A, data=ip)
    created.append(record)

print("Done, created %d records" % (len(created)))
pprint(created)

Create a record with a custom TTL

This example shows how to create a record with a custom TTL. Keep in mind that not all of the providers support setting a custom, per record TTL.

from libcloud.dns.types import Provider, RecordType
from libcloud.dns.providers import get_driver

CREDENTIALS_ZERIGO = ("email", "api key")

cls = get_driver(Provider.ZERIGO)
driver = cls(*CREDENTIALS_ZERIGO)

zone = [z for z in driver.list_zones() if z.domain == "example.com"][0]

ttl = 900
record = zone.create_record(name="www", type=RecordType.A, data="127.0.0.1", ttl=ttl)

Create a MX record and specify a priority

Some record types such as MX and SRV allow you to specify priority. This example shows how to do that.

from libcloud.dns.types import Provider, RecordType
from libcloud.dns.providers import get_driver

CREDENTIALS_ZERIGO = ("email", "api key")

cls = get_driver(Provider.ZERIGO)
driver = cls(*CREDENTIALS_ZERIGO)

zone = [z for z in driver.list_zones() if z.domain == "example.com"][0]

extra = {"priority": 10}
record = zone.create_record(name=None, type=RecordType.MX, data="aspmx.l.google.com", extra=extra)

Create a CAA record

Note

Support for CAA record type has been introduced in Libcloud v3.1.0. At this point it has only been implemented and tested with CloudFlare and Gandii Live DNS drivers.

When creating a CAA record, data fields needs to be in the following format: <flags> <tag> <domain name / url> as shown below.

from libcloud.dns.types import Provider, RecordType
from libcloud.dns.providers import get_driver

CREDENTIALS = ("email", "api key")

cls = get_driver(Provider.CLOUDFLARE)
driver = cls(*CREDENTIALS)

zone = [z for z in driver.list_zones() if z.domain == "example.com"][0]

# 1. issue tag
print(zone.create_record(name="www", type=RecordType.CAA, data="0 issue caa.domain.com"))

# 2. issuewild tag
print(zone.create_record(name="www", type=RecordType.CAA, data="0 issuewild caa.domain.com"))

# 3. iodef tag
print(zone.create_record(name="www", type=RecordType.CAA, data="0 iodef caa.domain.com/report"))

Export Libcloud Zone to BIND zone format

Note

This functionality is only available in Libcloud 0.14.0 and above.

This example shows how to export Libcloud Zone to bind format.

Keep in mind that generated bind zone file content doesn’t contain SOA and NS records. This should work fine if you just want to import this file using a DNS provider web interface, but if you want to use it with BIND you need to manually add those records.

Printing the output

from libcloud.dns.types import Provider
from libcloud.dns.providers import get_driver

CREDENTIALS_ZERIGO = ("email", "api key")
ZONE_ID = "example.myzone.com"

Cls = get_driver(Provider.ZERIGO)
driver = Cls(*CREDENTIALS_ZERIGO)

zone = driver.get_zone(zone_id=ZONE_ID)
print(zone.export_to_bind_format())

Saving output into a file

from libcloud.dns.types import Provider
from libcloud.dns.providers import get_driver

CREDENTIALS_ZERIGO = ("email", "api key")
ZONE_ID = "example.myzone.com"

Cls = get_driver(Provider.ZERIGO)
driver = Cls(*CREDENTIALS_ZERIGO)

zone = driver.get_zone(zone_id=ZONE_ID)
zone.export_to_bind_zone_file(file_path="/home/user/example.com")