The UltraDNS API and Powershell

cloudyAt work we’re currently using Neustar’s UltraDNS service to host 200+ DNS records, and I started a project to automate changing IP addresses to switch to DR sites. There is a well documented API for this, with great examples and solutions built mostly on Python, Perl, and Java. UltraDNS has published examples for all three of those on their Github page, and there is even a Perl module available on CPAN.

Since Powershell is my current lingua franca, I put together this rough writeup with some test code. I’ll eventually shape things into a Powershell module that does a few different functions, then tie it into a web interface to fully automate switching to DR IP addresses. The rest of my team is of course, in a nerdgasmic state over being able to press a few buttons to accomplish this.

First, have a look at the https://restapi.ultradns.com/v1/docs, which gives you a quick overview. UltraDNS customers also have access to more in depth documentation, including a full user guide. Some familiarity with using REST web services, Powershell’s built in commands for them, and reading the API documentation is helpful.

The API is at https://restapi.ultradns.com/v1 and uses tokens to authenticate. To use the service, you’ll need to first build a call to get an access token. This token has to be passed on to subsequent calls. The code below will return the value of the accessToken property. It goes without saying the credentials should be hidden in production.

In this example I’m using the test URI. This call to /token will return some other output. Remove the pipe to Select-Object to see it.

# Get auth token.
function getToken {

# Set a var with the URI.
$url = "https://test-restapi.ultradns.com/v1/authorization/token"

# Set the body with an array of params to pass in.
$body = @{

grant_type='password';
username='user@somedomain.com';
password='someuserspassword';

}
# Call the service, pass in the body as a POST with x-ww-form-urlencoded content type, and get just the token.
Invoke-RestMethod -Uri $url -Body $body -Method POST -Verbose -ContentType "application/x-www-form-urlencoded" | Select-Object accessToken

}

Now I can get my access token any time with;

$auth = getToken
$auth.accessToken

This code gets the A record(s) for a given domain;

# Here we call the zones method, pass in the domain and the type of record (eg; A) as part of the URI.
# $domain must end with period
$domain = "test.com"
$url = "https://test-restapi.ultradns.com/v1/zones/$domain./rrsets/A"

# Set the header with the token.
$headers = @{

authorization="Bearer $token";

}
# Get the response from rrSets.
Invoke-RestMethod -Ur $url -Headers $headers -Method GET -ContentType "application/x-www-form-urlencoded" | Select-Object rrSets

One thought on “The UltraDNS API and Powershell

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *