Login Resources
Here’s a Ruby example calling REST resources via simple net/https to:
- Create a Session Login [POST /Session/]
- Grab the Session Token from the Login, and Check the Session is still alive [GET /Session/]
- Logout a Session [DELETE /Session/]
Note the http header Auth-Token
for passing a Session Token to subsequent commands
#!/usr/bin/env ruby
require 'net/https'
require 'uri'
require 'json'
if __FILE__ == $0
# Set your customer name, username, and password on the command line
CUSTOMER_NAME = ARGV[0]
USER_NAME = ARGV[1]
PASSWORD = ARGV[2]
# Set up our HTTP object with the required host and path
url = URI.parse('https://api2.dynect.net/REST/Session/')
headers = { "Content-Type" => 'application/json' }
http = Net::HTTP.new(url.host, url.port)
http.set_debug_output $stderr
http.use_ssl = true
# Login and get an authentication token that will be used for all subsequent requests.
session_data = { :customer_name => CUSTOMER_NAME, :user_name => USER_NAME, :password => PASSWORD }
resp, data = http.post(url.path, session_data.to_json, headers)
print 'POST Session Response: ', data, 'n'
result = JSON.parse(data)
auth_token = result['data']['token']
# Is the session still alive?
headers = { "Content-Type" => 'application/json', 'Auth-Token' => auth_token }
resp, data = http.get(url.path, headers)
print 'GET Session Response: ', data, 'n'
# Logout
resp, data = http.delete(url.path, headers)
print 'DELETE Session Response: ', data, 'n'
end
Create a Record
Here’s a Ruby example calling REST resources via simple net/https to:
- Create a Session Login
[POST /Session/]
, with error checking - Create an A Record at a host
[POST /ARecord/#{ZONE}/#{FQDN}/]
- Publish the Zone to DNS
[PUT /Zone/#{ZONE}/
- Logout a Session
[DELETE /Session/]
Note most of the examples here do no error checking (for simplicity of demonstration),
you will want to check the ‘status’ of the Response for ‘success’ or ‘failure’
and proceed accordingly. Failed commands will provide messages with information.
#!/usr/bin/env ruby
require 'net/https'
require 'uri'
require 'json'
if __FILE__ == $0
# Set the desired parameters on the command line
CUSTOMER_NAME = ARGV[0]
USER_NAME = ARGV[1]
PASSWORD = ARGV[2]
ZONE = ARGV[3]
FQDN = ARGV[4]
# Set up our HTTP object with the required host and path
url = URI.parse('https://api2.dynect.net/REST/Session/')
headers = { "Content-Type" => 'application/json' }
http = Net::HTTP.new(url.host, url.port)
http.set_debug_output $stderr
http.use_ssl = true
# Login and get an authentication token that will be used for all subsequent requests.
session_data = { :customer_name => CUSTOMER_NAME, :user_name => USER_NAME, :password => PASSWORD }
resp, data = http.post(url.path, session_data.to_json, headers)
result = JSON.parse(data)
if result['status'] == 'success'
auth_token = result['data']['token']
else
puts "Command Failed:n"
# the messages returned from a failed command are a list
result['msgs'][0].each{|key, value| print key, " : ", value, "n"}
end
# New headers to use from here on with the auth-token set
headers = { "Content-Type" => 'application/json', 'Auth-Token' => auth_token }
# Create the A record
url = URI.parse("https://api2.dynect.net/REST/ARecord/#{ZONE}/#{FQDN}/")
record_data = { :rdata => { :address => "10.10.10.10" }, :ttl => "30" }
resp, data = http.post(url.path, record_data.to_json, headers)
print 'POST ARecord Response: ', data, 'n';
# Publish the changes
url = URI.parse("https://api2.dynect.net/REST/Zone/#{ZONE}/")
publish_data = { "publish" => "true" }
resp, data = http.put(url.path, publish_data.to_json, headers)
print 'PUT Zone Response: ', data, 'n';
# Get the A record
url = URI.parse("https://api2.dynect.net/REST/ARecord/#{ZONE}/#{FQDN}/")
resp, data = http.get(url.path, headers)
print 'GET ARecord Response: ', data, 'n';
# Logout
url = URI.parse('https://api2.dynect.net/REST/Session/')
resp, data = http.delete(url.path, headers)
print 'DELETE Session Response: ', data, 'n';
end