4.1. Authentication

The session module is an interface to authentication via the dyn.tm REST API. As noted in the advanced section, DynectSession‘s are implemented as Singleton types, which means that, in most cases, you don’t need to keep track of your DynectSession instances after you create them. However, there are several examples of ways in which you can use these session instances which will be outlined below.

class dyn.tm.session.DynectSession(customer, username, password, host='api.dynect.net', port=443, ssl=True, api_version='current', auto_auth=True, key=None, history=False, proxy_host=None, proxy_port=None, proxy_user=None, proxy_pass=None)[source]

Base object representing a DynectSession Session

__init__(customer, username, password, host='api.dynect.net', port=443, ssl=True, api_version='current', auto_auth=True, key=None, history=False, proxy_host=None, proxy_port=None, proxy_user=None, proxy_pass=None)[source]

Initialize a Dynect Rest Session object and store the provided credentials

Parameters:
  • host – DynECT API server address
  • port – Port to connect to DynECT API server
  • ssl – Enable SSL
  • api_version – version of the api to use
  • customer – DynECT customer name
  • username – DynECT Customer’s username
  • password – User’s password
  • auto_auth – declare whether or not to automatically log in
  • key – A valid AES-256 password encryption key to be used when encrypting your password
  • history – A boolean flag determining whether or not you would like to store a record of all API calls made to review later
  • proxy_host – A proxy host to utilize
  • proxy_port – The port that the proxy is served on
  • proxy_user – A username to connect to the proxy with if required
  • proxy_pass – A password to connect to the proxy with if required
authenticate()[source]

Authenticate to the DynectSession service with the provided credentials

log_out()[source]

Log the current session out from the DynECT API system

permissions

Permissions of the currently logged in user

update_password(new_password)[source]

Update the current users password

Parameters:new_password – The new password to use
uri_root = '/REST'
user_permissions_report(user_name=None)[source]

Returns information regarding the requested user’s permission access

Parameters:user_name – The user whose permissions will be returned. Defaults to the current user

4.1.1. The Basics

For basic usage, you need not do anything more than simply

>>> from dyn.tm.session import DynectSession
>>> DynectSession('customer', 'user', 'password')

4.1.2. Permissions

Using a DynectSession instance, you can also verify the current permissions associated with your session by simply checking the permissions property of your DynectSession instance.

>>> from dyn.tm.session import DynectSession
>>> s = DynectSession('customer', 'user', 'password')
>>> s.permissions
[u'ZoneGet', u'ZoneUpdate', u'ZoneCreate', u'ZoneDelete', ...

4.1.3. Additional Features

The majority of these features exist mainly to provide a cleaner interface to working with sessions as Singleton types.

4.1.3.1. Multiple Sessions

To manage multiple user accounts, use a DynectMultiSession instance and call the new_user_session method

>>> from dyn.tm.session import DynectMultiSession
>>> s = DynectMultiSession('customer', 'user', 'password')
>>> s.new_user_session('customer_two', 'user_two', 'password_two')

This will authenticate a second user. You can then switch between your open user sessions with set_active_session by passing a username. You can also pass the customer name as a keyword argument (in case you have the same username in two different customers).Use the get_open_sessions method to get a dictionary of all open sessions

>>> current_sessions = dynect_session.get_open_sessions()
>>> # loop through all open sessions
>>> for session in current_sessions:
...     dynect_session.set_active_session(session['user_name'], customer=session['customer'])
...     print("Zones for {0}".format(dynect_session.username))
...     print(get_all_zones())

log_out_active_session can be called to only log out of the active session. Calling log_out will log out of all open sessions

4.1.3.1.1. DynectSession as a Context Manager

As of version 1.2.0 you have the ability to use a DynectSession as a context manager, like so

>>> from dyn.tm.session import DynectSession
>>> with DynectSession('customer', 'user', 'password') as s:
...     return s.permissions

This feature is particularly useful if you’re looking to manage multiple user accounts programatically.

4.1.3.1.2. Overriding Sessions

As of version 1.2.0 you have the ability to override an existing DynectSession with the use of the new_session class method like so

>>> from dyn.tm.session import DynectSession
>>> s = DynectSession('customer', 'user', 'password')
>>> s = DynectSession.new_session('customer', 'another_user', 'password')

4.1.3.1.3. Getting Sessions

If you don’t want to track your current DynectSession, but want to be able to access your current one later, you can make use of the get_session class method like so

>>> from dyn.tm.session import DynectSession
>>> DynectSession('customer', 'user', 'password')
>>> DynectSession.get_session().username
'user'

4.1.3.1.4. Session History

As of version 1.3.0 users can now optionally allow DynectSessions to store a history of API calls that are made. This can be particularly useful for debugging, as well as for use when contacting Support.

>>> >>> from dyn.tm.session import DynectSession
>>> s = DynectSession('customer', 'user', 'password', history=True)
>>> s.history
... [('2014-10-14T11:15:17.351740',
...   '/REST/Session/',
...   'POST',
...   {'customer_name': 'customer', 'password': '*****', 'user_name': 'user'},
...   u'success')]

Please note that if you do not specify history as True when you log in, that your history will not be recorded and s.history will return None