Support: 1-800-961-4454
Sales Chat
1-800-961-2888

Using Rackspace DNSaaS with Curl (Part 1)

1

This post was originally posted on Robert Collazo’s blog here.

With the release of Rackspace DNSaaS (blog post here), I thought I would put together some examples of using curl with the service. With this release, you now have programmatic access to DNS via the API where you can set TTLs, create and delete various record types, and create and delete domains.

Authentication

First, you’ll have to authenticate. To do this, you will need your API key and username.

$ curl -D – -H "X-Auth-Key: yourAPIKey" -H "X-Auth-User: yourUsername" https://auth.api.rackspacecloud.com/v1.0

You should receive the following response:

HTTP/1.1 204 No Content
Server: Apache/2.2.3 (Mosso Engineering)
X-Storage-Url: https://storage101.dfw1.clouddrive.com/v1/yourAccountHash
Content-Type: application/octet-stream
Date: Date and Time
X-Auth-Token: yourAuthToken
X-Storage-Token: yourStorageToken
X-Server-Management-Url: https://servers.api.rackspacecloud.com/v1.0/yourAccountNumber
Connection: Keep-Alive
X-CDN-Management-Url: https://cdn1.clouddrive.com/v1/yourAccountHash
Content-Length: 0

 

Listing Domain Information

To see a list of your current domains, run the following command. Don’t forget to replace your auth token and account number.

$ curl -X GET -H "X-Auth-Token: yourAuthToken" -H "Accept: application/xml" https://dns.api.rackspacecloud.com/v1.0/yourAccountNumber/domains

Your output will be XML formatted and look like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<domains xmlns="http://docs.rackspacecloud.com/dns/api/v1.0" xmlns:ns2="http://www.w3.org/2005/Atom" xmlns:ns3="http://docs.rackspacecloud.com/dns/api/management/v1.0" totalEntries="2">
    <domain id="1697941" accountId="yourAccountNumber" name="collazo.ws" updated="2011-06-28T04:18:17Z" created="2009-12-11T01:01:26Z" />
    <domain id="4062964" accountId="yourAccountNumber" name="deployitron.com" updated="2011-03-19T08:10:31Z" created="2011-01-21T23:24:44Z" />
</domains>

To see the same thing formatted as JSON output, run the following:

$ curl -X GET -H "X-Auth-Token: yourAuthToken" -H "Accept: application/json" https://dns.api.rackspacecloud.com/v1.0/yourAccountNumber/domains

{
   "domains":[
      {
         "name":"collazo.ws",
         "id":1697941,
         "accountId":yourAccountNumber,
         "updated":"2011-06-28T04:18:17.000+0000",
         "created":"2009-12-11T01:01:26.000+0000"
      },
      {
         "name":"deployitron.com",
         "id":4062964,
         "accountId":yourAccountNumber,
         "updated":"2011-03-19T08:10:31.000+0000",
         "created":"2011-01-21T23:24:44.000+0000"
      }
   ],
   "totalEntries":2
}

To get details about a particular domain, you’ll enter the following:

$ curl -X GET -H "X-Auth-Token: yourAuthToken" -H "Accept: application/xml" https://dns.api.rackspacecloud.com/v1.0/yourAccountNumber/domains/4062964 # <- make sure you replace this with your domain ID

This will output the following in XML format:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<domain xmlns="http://docs.rackspacecloud.com/dns/api/v1.0" xmlns:ns2="http://www.w3.org/2005/Atom" xmlns:ns3="http://docs.rackspacecloud.com/dns/api/management/v1.0" id="4062964" accountId="yourAccountNumber" name="deployitron.com" ttl="3600" emailAddress="ipadmin@stabletransit.com" updated="2011-03-19T08:10:31Z" created="2011-01-21T23:24:44Z">
    <nameservers>
        <nameserver name="ns.rackspace.com" />
        <nameserver name="ns1.rackspace.com" />
    </nameservers>
    <recordsList totalEntries="5">
        <record id="A-1234567" type="A" name="deployitron.com" data="174.143.174.233" ttl="3600" updated="2011-01-22T11:28:10Z" created="2011-01-22T11:28:10Z" />
        <record id="NS-2345678" type="NS" name="deployitron.com" data="dns1.stabletransit.com" ttl="3600" updated="2011-01-21T23:24:44Z" created="2011-01-21T23:24:44Z" />
        <record id="NS-2345680" type="NS" name="deployitron.com" data="dns2.stabletransit.com" ttl="3600" updated="2011-01-21T23:24:44Z" created="2011-01-21T23:24:44Z" />
        <record id="CNAME-4567890" type="CNAME" name="www.deployitron.com" data="deployitron.com" ttl="3600" updated="2011-01-22T11:30:35Z" created="2011-01-22T11:30:35Z" />
        <record id="CNAME-5678901" type="CNAME" name="www2.deployitron.com" data="deployitron.com" ttl="600" updated="2011-03-19T08:10:31Z" created="2011-03-19T08:10:31Z" />
    </recordsList>
</domain>

To get the same thing JSON formatted:

$ curl -X GET -H "X-Auth-Token: yourAuthToken" -H "Accept: application/json" https://dns.api.rackspacecloud.com/v1.0/yourAccountNumber/domains/4062964

{
   "name":"deployitron.com",
   "id":4062964,
   "accountId":yourAccountNumber,
   "nameservers":[
      {
         "name":"ns.rackspace.com"
      },
      {
         "name":"ns1.rackspace.com"
      }
   ],
   "updated":"2011-03-19T08:10:31.000+0000",
   "ttl":3600,
   "recordsList":{
      "records":[
         {
            "name":"deployitron.com",
            "id":"A-1234567",
            "type":"A",
            "data":"174.143.174.233",
            "updated":"2011-01-22T11:28:10.000+0000",
            "ttl":3600,
            "created":"2011-01-22T11:28:10.000+0000"
         },
         {
            "name":"deployitron.com",
            "id":"NS-2345678",
            "type":"NS",
            "data":"dns1.stabletransit.com",
            "updated":"2011-01-21T23:24:44.000+0000",
            "ttl":3600,
            "created":"2011-01-21T23:24:44.000+0000"
         },
         {
            "name":"deployitron.com",
            "id":"NS-2345680",
            "type":"NS",
            "data":"dns2.stabletransit.com",
            "updated":"2011-01-21T23:24:44.000+0000",
            "ttl":3600,
            "created":"2011-01-21T23:24:44.000+0000"
         },
         {
            "name":"www.deployitron.com",
            "id":"CNAME-4567890",
            "type":"CNAME",
            "data":"deployitron.com",
            "updated":"2011-01-22T11:30:35.000+0000",
            "ttl":3600,
            "created":"2011-01-22T11:30:35.000+0000"
         },
         {
            "name":"www2.deployitron.com",
            "id":"CNAME-5678901",
            "type":"CNAME",
            "data":"deployitron.com",
            "updated":"2011-03-19T08:10:31.000+0000",
            "ttl":600,
            "created":"2011-03-19T08:10:31.000+0000"
         }
      ],
      "totalEntries":5
   },
   "emailAddress":"ipadmin@stabletransit.com",
   "created":"2011-01-21T23:24:44.000+0000"
}

 

Creating Domains

To create a domain, we issue the following JSON formatted command:

$ curl -D – -X POST -d '{ "domains":[ { "name":"aeVo8AiHeesh.ws", "emailAddress":"robert@aeVo8AiHeesh.ws", "comment":"This is the first domain created via API", "recordsList": { "records":[ { "ttl":600, "data":"127.0.0.1", "name":"aeVo8AiHeesh.ws", "type":"A" },{ "priority": 10, "ttl":86400, "data":"mail.aeVo8AiHeesh.ws", "name":"aeVo8AiHeesh.ws", "type":"MX" },{ "ttl":300, "data":"v=spf1 mx -all", "name":"aeVo8AiHeesh.ws", "type":"TXT" } ] } } ] }' -H "X-Auth-Token: yourAuthToken" -H "Content-Type: application/json" https://dns.api.rackspacecloud.com/v1.0/yourAccountNumber/domains

And the following is returned:

HTTP/1.1 202 Accepted
X-API-VERSION: 1.0.8
Content-Type: application/json
Date: Fri, 08 Jul 2011 15:18:12 GMT
Content-Length: 155
Server: Jetty(7.3.1.v20110307)

{"jobId":"yourJobID","callbackUrl":"https://dns.api.rackspacecloud.com/v1.0/yourAccountNumber/status/yourJobID"}

It’s important to note your job ID so that you can check the status of your job by issuing the following command:

$ curl -X GET -H "X-Auth-Token: yourAuthToken" -H "Accept: application/json" https://dns.api.rackspacecloud.com/v1.0/yourAccountNumber/status/yourJobID

If your job completed successfully, you should see something similar to the following:

{"domains":[{"name":"aevo8aiheesh.ws","id":2841222,"comment":"This is the first domain created via API","accountId":yourAccountNumber,"nameservers":[{"name":"ns.rackspace.com"},{"name":"ns1.rackspace.com"}],"updated":"2011-07-08T15:21:46.000+0000","ttl":3600,"recordsList":{"records":[{"name":"aevo8aiheesh.ws","id":"A-7088731","type":"A","data":"127.0.0.1","updated":"2011-07-08T15:21:47.000+0000","ttl":600,"created":"2011-07-08T15:21:47.000+0000"},{"name":"aevo8aiheesh.ws","id":"MX-3272903","priority":10,"type":"MX","data":"mail.aeVo8AiHeesh.ws","updated":"2011-07-08T15:21:48.000+0000","ttl":86400,"created":"2011-07-08T15:21:48.000+0000"},{"name":"aevo8aiheesh.ws","id":"TXT-243256","type":"TXT","data":"v=spf1 mx -all","updated":"2011-07-08T15:21:49.000+0000","ttl":300,"created":"2011-07-08T15:21:49.000+0000"}]},"emailAddress":"robert@aeVo8AiHeesh.ws","created":"2011-07-08T15:21:45.000+0000"}]}

I see that it was created and I can verify via dig by running:

$ dig aevo8aiheesh.ws @ns.rackspace.com any

Which returns:

; <<>> DiG 9.6.0-APPLE-P2 <<>> aevo8aiheesh.ws @ns.rackspace.com any
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 40475
;; flags: qr aa rd; QUERY: 1, ANSWER: 6, AUTHORITY: 0, ADDITIONAL: 2
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;aevo8aiheesh.ws.               IN      ANY

;; ANSWER SECTION:
aevo8aiheesh.ws.        3600    IN      SOA     ns1.<a title="rackspace" href="http://www.collazo.ws/tag/rackspace-2">rackspace</a>.com. robert.aevo8aiheesh.ws. 1310138509 3600 3600 3600 3600
aevo8aiheesh.ws.        86400   IN      MX      10 mail.aevo8aiheesh.ws.
aevo8aiheesh.ws.        3600    IN      NS      dns1.stabletransit.com.
aevo8aiheesh.ws.        3600    IN      NS      dns2.stabletransit.com.
aevo8aiheesh.ws.        300     IN      TXT     "v=spf1 mx -all"
aevo8aiheesh.ws.        600     IN      A       127.0.0.1

;; ADDITIONAL SECTION:
dns1.stabletransit.com. 86400   IN      A       69.20.95.4
dns2.stabletransit.com. 86400   IN      A       65.61.188.4

;; Query time: 35 msec
;; SERVER: 69.20.95.4#53(69.20.95.4)
;; WHEN: Fri Jul  8 10:45:38 2011
;; MSG SIZE  rcvd: 241

We can accomplish the same thing via XML by running the following:

$ curl -D - -X POST -d '<domains xmlns="http://docs.rackspacecloud.com/dns/api/v1.0"><domain name="paingeez7uec.com" ttl="5600" emailAddress="robert@paingeez7uec.com" comment="This is the second domain created via API"><recordsList><record type="A" data="127.0.0.1" name="paingeez7uec.com"/><record type="MX" data="mail.paingeez7uec.com" priority="10" ttl="5000" name="paingeez7uec.com"/><record type="TXT" data="v=spf1 mx -all" name="paingeez7uec.com" ttl="600"/></recordsList></domain></domains>' -H "X-Auth-Token: yourAuthToken" -H "Content-Type: application/xml" https://dns.api.rackspacecloud.com/v1.0/yourAccountID/domains

Which outputs:

HTTP/1.1 202 Accepted
X-API-VERSION: 1.0.8
Content-Type: application/json
Date: Fri, 08 Jul 2011 19:35:22 GMT
Content-Length: 155
Server: Jetty(7.3.1.v20110307)

{"jobId":"yourJobID","callbackUrl":"https://dns.api.rackspacecloud.com/v1.0/yourAccountNumber/status/yourJobID"}

Checking the status:

$ curl -X GET -H "X-Auth-Token: yourAuthToken" -H "Accept: application/xml" https://dns.api.rackspacecloud.com/v1.0/yourAccountID/status/yourJobID

Which returns:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><domains xmlns="http://docs.rackspacecloud.com/dns/api/v1.0" xmlns:ns2="http://www.w3.org/2005/Atom" xmlns:ns3="http://docs.rackspacecloud.com/dns/api/management/v1.0"><domain id="2841642" accountId="yourAccountID" name="paingeez7uec.com" ttl="5600" emailAddress="robert@paingeez7uec.com" updated="2011-07-08T19:35:23Z" created="2011-07-08T19:35:23Z" comment="This is the second domain created via API"><nameservers><nameserver name="ns.rackspace.com"/><nameserver name="ns1.rackspace.com"/></nameservers><recordsList><record id="A-7089765" type="A" name="paingeez7uec.com" data="127.0.0.1" ttl="5600" updated="2011-07-08T19:35:24Z" created="2011-07-08T19:35:24Z"/><record id="MX-3274128" type="MX" name="paingeez7uec.com" data="mail.paingeez7uec.com" ttl="5000" priority="10" updated="2011-07-08T19:35:25Z" created="2011-07-08T19:35:25Z"/><record id="TXT-243385" type="TXT" name="paingeez7uec.com" data="v=spf1 mx -all" ttl="600" updated="2011-07-08T19:35:26Z" created="2011-07-08T19:35:26Z"/></recordsList></domain></domains>

To verify the domain exists via dig:

$ dig paingeez7uec.com @ns.rackspace.com any

Which outputs:

; <<>> DiG 9.6.0-APPLE-P2 <<>> paingeez7uec.com @ns.rackspace.com any
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 45478
;; flags: qr aa rd; QUERY: 1, ANSWER: 6, AUTHORITY: 0, ADDITIONAL: 2
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;paingeez7uec.com.              IN      ANY

;; ANSWER SECTION:
paingeez7uec.com.       5600    IN      SOA     ns1.rackspace.com. robert.paingeez7uec.com. 1310153726 5600 5600 5600 5600
paingeez7uec.com.       5000    IN      MX      10 mail.paingeez7uec.com.
paingeez7uec.com.       5600    IN      NS      dns1.stabletransit.com.
paingeez7uec.com.       5600    IN      NS      dns2.stabletransit.com.
paingeez7uec.com.       600     IN      TXT     "v=spf1 mx -all"
paingeez7uec.com.       5600    IN      A       127.0.0.1

;; ADDITIONAL SECTION:
dns1.stabletransit.com. 86400   IN      A       69.20.95.4
dns2.stabletransit.com. 86400   IN      A       65.61.188.4

;; Query time: 42 msec
;; SERVER: 69.20.95.4#53(69.20.95.4)
;; WHEN: Fri Jul  8 14:49:55 2011
;; MSG SIZE  rcvd: 239

Now you can create domains in using both JSON and XML formatted responses.

Deleting Domains

To delete a domain using curl follow these simple steps. You will need your account ID (yourAccountID in previous examples) and the domain ID from a domain list.

JSON:

$ curl -X DELETE -H "X-Auth-Token: yourAuthToken" -H "Accept: application/json" https://dns.api.rackspacecloud.com/v1.0/yourAccountID/domains/2841222

{"jobId":"yourJobID","callbackUrl":"https://dns.api.rackspacecloud.com/v1.0/yourAccountID/status/yourJobID"}

XML:

$ curl -X DELETE -H "X-Auth-Token: yourAuthToken" -H "Accept: application/xml" https://dns.api.rackspacecloud.com/v1.0/yourAccountID/domains/2841642

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><asyncResponse xmlns="http://docs.rackspacecloud.com/dns/api/v1.0"><jobId>yourJobID</jobId><callbackUrl>https://dns.api.rackspacecloud.com/v1.0/yourAccountID/status/yourJobID</callbackUrl></asyncResponse>

 

Stay tuned for part 2 where I will show how to modify domains, and add/delete/modify records.

Tags: , ,

About the Author

This is a post written and contributed by Robert Collazo.

Rob works for Rackspace currently as Manager of Social Media and is a Senior Systems Engineer. He started his career with Rackspace in May of 2007 as a Linux Systems Administrator working under Rackspace Cloud co-founder Jonathan Bryce (when we were called Mosso). He quickly worked his way to Manager of Operations for the Cloud Sites product where he managed a team of dedicated Linux and Windows Administrators. Later, Rob took on the challenge of founding and leading the Rackspace Cloud Professional Services Team. In 2008, he was awarded the coveted Straightjacket award for Fanatical Support. Read his personal blog here.


More
1 Comment

[…] the Rackspace blog there’s a good article on Using Rackspace DNSaaS with Curl (Part 1), which gives you an idea of how to get working with the API […]

avatar The Coder's Notebook » Blog Archive » Using Rackspace DNSaaS with Curl (Part 1) on July 25, 2011 | Reply

Leave a New Comment

(Required)


Racker Powered
©2014 Rackspace, US Inc.