Using OpenStack DNS

Before working with the DNS service, you’ll need to create a connection to your OpenStack cloud by following the Connect user guide. This will provide you with the conn variable used in the examples below.

The primary resource of the DNS service is the server.

List Zones

Zone is a logical grouping of DNS records for a domain, allowing for the centralized management of DNS resources, including domain names, nameservers, and DNS queries.

def list_zones(conn):
    print("List Zones:")

    for zone in conn.dns.zones():
        print(zone)

Full example: dns resource list

List Recordsets

Recordsets allow for the centralized management of various DNS records within a Zone, helping to define how a domain responds to different types of DNS queries.

def list_recordsets(conn, name_or_id):
    print("List Recordsets for Zone")

    zone = conn.dns.find_zone(name_or_id)

    if zone:
        zone_id = zone.id
        recordsets = conn.dns.recordsets(zone_id)

        for recordset in recordsets:
            print(recordset)
    else:
        print("Zone not found.")

Full example: dns resource list

Create Zone

Create a zone. It allows users to define and manage the DNS namespace for a particular domain.

def create_zone(
    conn,
    name,
    email,
    ttl=3600,
    description="Default description",
    zone_type="PRIMARY",
):
    print("Create Zone: ")

    zone = {
        "name": name,
        "email": email,
        "ttl": ttl,
        "description": description,
        "type": zone_type,
    }

    print(conn.dns.create_zone(**zone))

Full example: dns resource list

Create Recordset

Create a recordset. It accepts several parameters that define the DNS record’s properties and sends an API request to OpenStack to create the recordset within a specified DNS zone.

def create_recordset(
    conn,
    name_or_id,
    recordset_name,
    recordset_type="A",
    records=["192.168.1.1"],
    ttl=3600,
    description="Default description",
):
    print("Create Recordset: ")

    zone = conn.dns.find_zone(name_or_id)

    if not zone:
        print("Zone not found.")
        return None

    zone_id = zone.id

    recordset_data = {
        "name": recordset_name,
        "type": recordset_type,
        "records": records,
        "ttl": ttl,
        "description": description,
    }

    print(conn.dns.create_recordset(zone_id, **recordset_data))

Full example: dns resource list

Delete Zone

Delete a zone. It allows users to completely delete the DNS management for a specified domain.

def delete_zone(conn, name_or_id):
    print(f"Delete Zone: {name_or_id}")

    zone = conn.dns.find_zone(name_or_id)

    if zone:
        conn.dns.delete_zone(zone.id)
    else:
        return None

Full example: dns resource list

Delete Recordset

Delete a recordset.

def delete_recordset(conn, name_or_id, recordset_name):
    print(f"Deleting Recordset: {recordset_name} in Zone: {name_or_id}")

    zone = conn.dns.find_zone(name_or_id)

    if zone:
        try:
            recordset = conn.dns.find_recordset(zone.id, recordset_name)
            if recordset:
                conn.dns.delete_recordset(recordset, zone.id)
            else:
                print("Recordset not found")
        except Exception as e:
            print(f"{e}")
    else:
        return None

Full example: dns resource list

Find Zone

The find_zone function searches for and returns a DNS zone by its name using a given connection object.

def find_zone(conn, name_or_id):
    print(f"Find Zone: {name_or_id}")

    zone = conn.dns.find_zone(name_or_id)

    if zone:
        print(zone)
        return zone
    else:
        print("Zone not found.")
        return None

Full example: dns resource list

Find Recordset

The find_recordset function searches for a DNS recordset with a specific name and type within a given zone. If multiple recordsets with the same name exist, the record type can be specified to find the exact match.

def find_recordset(conn, name_or_id, recordset_name, recordset_type=None):
    print(f"Find Recordset: {recordset_name} in Zone: {name_or_id}")

    zone = conn.dns.find_zone(name_or_id)

    if not zone:
        print("Zone not found.")
        return None

    zone_id = zone.id

    try:
        if recordset_type:
            recordset = conn.dns.find_recordset(
                zone_id, recordset_name, type=recordset_type
            )
        else:
            recordset = conn.dns.find_recordset(zone_id, recordset_name)

        if recordset:
            print(recordset)
            return recordset
        else:
            print("Recordset not found in Zone.")
            return None

    except Exception as e:
        print(f"{e}")
        return None

Full example: dns resource list