Delegate Subdomain via PyRax on Rackspace Cloud DNS

Sat, 11 March 2017

requirements.txt
git+ssh://git@github.com/rackspace/pyrax.git#egg=pyrax
install_pip.sh
pip install -r requirements.txt --target=./ --upgrade
perform.py
username = "YOUR_RACKSPACE_USERNAME"
api_key = "YOUR_RACKSPACE_API_KEY"
domain_name = "example.com"

from __future__ import print_function
import os
import sys
import os
import sys
import six

import pyrax
import pyrax.exceptions as exc
from pyrax.clouddns import CloudDNSDomain


pyrax.set_setting("identity_type", "rackspace")
pyrax.set_credentials(username=username, api_key=api_key)
dns = pyrax.cloud_dns


try:
    dom = dns.find(name=domain_name)  # type: CloudDNSDomain
    """
    Below can support the following fields:
    - type (required)
    - name (required)
    - data (required)
    - ttl (optional)
    - comment (optional)
    """
    print (dom.add_record({
        "type": "NS",
        "name": "subdomain.example.com",
        "data": "ns1.somedomain.com",
        "ttl": 300
    }))
    print (dom.add_record({
        "type": "NS",
        "name": "subdomain.example.com",
        "data": "ns2.somedomain.com",
        "ttl": 300
    }))
    print (dom.add_record({
        "type": "NS",
        "name": "subdomain.example.com",
        "data": "ns3.somedomain.com",
        "ttl": 300
    }))
    print (dom.add_record({
        "type": "NS",
        "name": "subdomain.example.com",
        "data": "ns4.somedomain.com",
        "ttl": 300
    }))
except exc.NotFound as e:
    print(e)
output.txt
$ python perform.py

[<CloudDNSRecord data=ns1.somedomain.com, domain_id=1234567, id=NS-963960376, name=subdomain.example.com, ttl=300, type=NS>]
[<CloudDNSRecord data=ns2.somedomain.com, domain_id=1234567, id=NS-724558473, name=subdomain.example.com, ttl=300, type=NS>]
[<CloudDNSRecord data=ns3.somedomain.com, domain_id=1234567, id=NS-392957295, name=subdomain.example.com, ttl=300, type=NS>]
[<CloudDNSRecord data=ns4.somedomain.com, domain_id=1234567, id=NS-967296759, name=subdomain.example.com, ttl=300, type=NS>]