Quantcast
Channel: Scripting – JJClements.co.uk
Viewing all articles
Browse latest Browse all 8

PowerShell script for Dyn / Dynect REST API

$
0
0

Dyn or Dynect are an Internet based company, offering products to monitor, control, and optimise online infrastructure, and also domain registration services and email products. The company that I am currently working for, recently had a requirement to automate the creation of DNS A records in their Dyn Managed DNS account as part of some automation work.

After some debugging I ended up with a simple function below which was based on an example I found that used Curl to do the same thing.

#####################################################################################
#This script/function was written using the Curl examples found in the following URL#
#http://dyn.com/blog/dynect-api-example-curl-managed-dns-aws-heroku-devops          #
#                                                                                   #
#This script/function creates a new node in Dynect and sets a DNS A record for it   #
#for the predefined zone using the Dynect REST API                                  #
#usage: dynectcreate <newnodename> <123.123.123.123>                                #
#####################################################################################

####################################
#Start of user changeable variables#
####################################
$customername = "mycompanyname"
$username = "james"
$password = "guessme"
$zone = "foo.bar"
##################################
#End of user changeable variables#
##################################

function dynectcreate
{
     param([string]$node, [string]$ipaddress)

     try
     {

     #Format credentials accordingly for the body content of the initial request below
     $credentials = '{"customer_name":"' + $customername + '","user_name":"' + $username + '","password":"' + $password + '"}'

     #Use credentials to request and store a session token from Dynect for later use
     $result = Invoke-RestMethod -Uri https://api2.dynect.net/REST/Session -Method Post -ContentType "application/json" -Body $credentials
     $token = $result.data.token



     #Create the header with the session token data for subsequent Dynect calls
     $headers = @{"Auth-Token" = "$token"}

     #Format the DNS record data accordingly for the body content of the new record request
     $recorddata = '{"rdata":{"address":"' + $ipaddress + '"}}'

     #Create new DNS A record for the node (note: node is automatically created if it doesn't already exist as per API documentation) and suppress any output
     Invoke-RestMethod -Uri "https://api2.dynect.net/REST/ARecord/$zone/$node.$zone." -Method Post -ContentType "application/json" -Headers $headers -Body $recorddata | out-null

     #Publish the zone (commit the change) and suppress any output
     Invoke-RestMethod -Uri "https://api2.dynect.net/REST/Zone/$zone" -Method Put -ContentType "application/json" -Headers $headers -Body '{"publish":true}' | out-null

     #Remove the session token/logout and suppress any output
     Invoke-RestMethod -Uri "https://api2.dynect.net/REST/Session" -Method Delete -ContentType "application/json" -Headers $headers | out-null

     }

     catch
     {
     }
}

The function above can be called as per the included example: dynectcreate newnodename 123.123.123.123

The script could be easily modified to add different types of other DNS records, or even remove them if required, this functionality was out of scope for me though.

Notes
  • It goes without saying that you will need to provide the correct Dyn credentials in the 'user changeable variables' section of the function
  • With the above point in mind, I recently found that the newer DynID credentials do not seem to work with the REST API (even though they apparently should)
  • If you converted your login that is to be used against the REST API to the newer DynID format, you will need to create a new Dyn user account that is NOT a DynID account for the script to work

Hopefully the working syntax in this function will save someone some time in the future!


Viewing all articles
Browse latest Browse all 8

Trending Articles