Login Create Geo

Here’s a Python example using the DynECT API Python Wrapper:

  • Create a Session Login [POST/Session/], with error checking
  • Create a Geo Service with one Geo Region Group and linked to one node [POST /Geo/<service_name>/]
  • Logout a Session [DELETE /Session/]
#!/usr/bin/env python

"""
Dynect API REST Examples

Logs into the API, creates a brand new Geo Service 
that is linked to one node, deletes the Geo
Service, and then logs out.
"""

from DynectDNS import DynectRest
from pprint import PrettyPrinter
import sys

api = DynectRest()

# We need to login
print "Logging in to API"

args = {
	'customer_name': '<customer_name>',
	'user_name': '<user_name>',
	'password': '<password>',
}

response = api.execute('/REST/Session/', 'POST', args)

if response['status'] != 'success':
	pretty = PrettyPrinter()
	msg = "Login to API failed: %s" % pretty.pformat(response)
	sys.exit(msg)


"""
To create a Geo serivce, one must specify a unique
name and define at least one Geo Region Group. At
this time, the user can choose whether or not to
link this service to a node.

A Geo Region Group consists of a unique name, a
list of countries that the group represents, and 
rdata that will be served.
"""

svc_name = 'example geo'

groups = [{
    'name' : 'example group',
    'countries' : ['US'],
    'rdata' : {
        'a_rdata':[{
            'address':'9.9.9.9'
        }]
    }
}]

nodes = [ 
    {'zone': 'example.com', 'fqdn': 'example.com'},
]

args = { 
    'groups': groups,
    'nodes': nodes
}

print "Creating the Geo Service"
url = "/REST/Geo/%s/" % svc_name
response = api.execute(url, 'POST', args);

if response['status'] != 'success':
    pretty = PrettyPrinter()
    msg = "Creating the Geo Service failed: %s" % pretty.pformat(response)
    sys.exit(msg)

print "Deleting the Geo Service"
url = "/REST/Geo/%s/" % svc_name
response = api.execute(url, 'DELETE');

print "Logging out"
api.execute('/REST/Session/', 'DELETE')

Update Geo

Here’s a Python example using the DynECT API Python Wrapper:

  • Create a Session Login [POST/Session/], with error checking
  • Create a Geo Service with one Geo Region Group and linked to one node [POST /Geo/<service_name>/]
  • Update a Geo Service linking it to one node [PUT /Geo/{service_name}/]
  • Logout a Session [DELETE /Session/]
#!/usr/bin/env python

"""
Dynect API REST Examples

Logs into the API, creates a brand new Geo Service 
that is NOT linked any nodes, udpates the Geo Service,
deletes the Geo Service, and then logs out.
"""

from DynectDNS import DynectRest
from optparse import OptionParser
from pprint import PrettyPrinter
import sys

api = DynectRest()

# We need to login
print "Logging in to API"

args = {
    'customer_name': '<customer_name>',
    'user_name': '<user_name>',
    'password': '<password>',
}

response = api.execute('/REST/Session/', 'POST', args)

if response['status'] != 'success':
    pretty = PrettyPrinter()
    msg = "Login to API failed: %s" % pretty.pformat(response)
    sys.exit(msg)


"""
Create Geo

To create a Geo serivce, one must specify a unique
name and define at least one Geo Region Group. At
this time, the user can choose whether or not to
link this service to a node.

A Geo Region Group consists of a unique name, a
list of countries that the group represents, and 
rdata that will be served.
"""

svc_name = 'example geo'

groups = [{
    'name' : 'example group',
    'countries' : ['US'],
    'rdata' : {
        'a_rdata':[{
            'address':'9.9.9.9'
        }]
    }
}]

create_args = { 
    'groups': groups,
}

print "Creating the Geo Service"
url = "/REST/Geo/%s/" % svc_name
response = api.execute(url, 'POST', create_args);

# If the command failed, tell us why
if response['status'] != 'success':
    pretty = PrettyPrinter()
    msg = "Creating the Geo Service failed: %s" % pretty.pformat(response)
    sys.exit(msg)

nodes = [ 
    {'zone': 'example.com', 'fqdn': 'example.com'},
]

update_args = { 
    'nodes': nodes,
}

"""
UpdateGeo

To update a Geo serivce, one must identify it using
the name specified at the time of creation.

Only the fields specifed are updated. For example, if
a node is to be linked to the Geo Service, `Groups` 
do not need to be specified.
"""

print "Updating the Geo Service"
url = "/REST/Geo/%s/" % svc_name
response = api.execute(url, 'PUT', update_args);

# If the command failed, tell us why
if response['status'] != 'success':
    pretty = PrettyPrinter()
    msg = "Updating the Geo Service failed: %s" % pretty.pformat(response)
    sys.exit(msg)

print "Logging out"
api.execute('/REST/Session/', 'DELETE')