Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Libcloud-732] Add implementation for World Wide DNS provider #566

Merged
merged 1 commit into from
Sep 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ Loadbalancer
DNS
~~~

- Add new driver for WorldWideDNS service.
(GITHUB-566, LIBCLOUD-732)
[Alejandro Pereira]

- Add new driver for AuroraDNS service.
(GITHUB-562, LIBCLOUD-735)
[Wido den Hollander]
Expand Down
22 changes: 22 additions & 0 deletions docs/dns/drivers/worldwidedns.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
World Wide DNS Driver Documentation
=====================================

`WorldWideDNS`_ provides Primary DNS services, Secondary DNS services, and
Dynamic DNS hosting services for thousands of businesses, website hosting,
website developers, and consumers. Resellers can completely private
label / white label DNS services with custom name servers.

Instantiating the driver
-------------------------------------

.. literalinclude:: /examples/dns/worldwidedns/instantiate_driver.py
:language: python

API Docs
--------

.. autoclass:: libcloud.dns.drivers.worldwidedns.WorldWideDNSDriver
:members:
:inherited-members:

.. _`WorldWideDNS`: https://www.worldwidedns.net/
10 changes: 10 additions & 0 deletions docs/examples/dns/worldwidedns/instantiate_driver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from libcloud.dns.types import Provider
from libcloud.dns.providers import get_driver

cls = get_driver(Provider.WORLDWIDEDNS)

# Normal account
driver = cls('username', 'apikey')

# Reseller account
driver = cls('username', 'apikey', reseller_id='reseller_id')
195 changes: 195 additions & 0 deletions libcloud/common/worldwidedns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re

from libcloud.common.base import ConnectionUserAndKey
from libcloud.common.base import Response
from libcloud.common.types import ProviderError


OK_CODES = ['200', '211', '212', '213']
ERROR_CODES = ['401', '403', '405', '406', '407', '408', '409', '410', '411',
'412', '413', '414', '450', '451']


class WorldWideDNSException(ProviderError):
def __init__(self, value, http_code, code, driver=None):
self.code = code
super(WorldWideDNSException, self).__init__(value, http_code, driver)


class SuspendedAccount(WorldWideDNSException):
def __init__(self, http_code, driver=None):
value = "Login ID you supplied is SUSPENDED, you need to renew" + \
" your account"
super(SuspendedAccount, self).__init__(value, http_code, 401,
driver)


class LoginOrPasswordNotMatch(WorldWideDNSException):
def __init__(self, http_code, driver=None):
value = "Login ID and/or Password you supplied is not on file or" + \
" does not match"
super(LoginOrPasswordNotMatch, self).__init__(value, http_code, 403,
driver)


class NonExistentDomain(WorldWideDNSException):
def __init__(self, http_code, driver=None):
value = "Domain name supplied is not in your account"
super(NonExistentDomain, self).__init__(value, http_code, 405,
driver)


class CouldntRemoveDomain(WorldWideDNSException):
def __init__(self, http_code, driver=None):
value = "Error occured removing domain from name server, try again"
super(CouldntRemoveDomain, self).__init__(value, http_code, 406,
driver)


class LimitExceeded(WorldWideDNSException):
def __init__(self, http_code, driver=None):
value = "Your limit was exceeded, you need to upgrade your account"
super(LimitExceeded, self).__init__(value, http_code, 407,
driver)


class ExistentDomain(WorldWideDNSException):
def __init__(self, http_code, driver=None):
value = "Domain already exists on our servers"
super(ExistentDomain, self).__init__(value, http_code, 408,
driver)


class DomainBanned(WorldWideDNSException):
def __init__(self, http_code, driver=None):
value = "Domain is listed in DNSBL and is banned from our servers"
super(DomainBanned, self).__init__(value, http_code, 409,
driver)


class InvalidDomainName(WorldWideDNSException):
def __init__(self, http_code, driver=None):
value = "Invalid domain name"
super(InvalidDomainName, self).__init__(value, http_code, 410,
driver)


class ErrorOnReloadInNameServer(WorldWideDNSException):
def __init__(self, server, http_code, driver=None):
if server == 1:
value = "Name server #1 kicked an error on reload, contact support"
code = 411
elif server == 2:
value = "Name server #2 kicked an error on reload, contact support"
code = 412
elif server == 3:
value = "Name server #3 kicked an error on reload, contact support"
code = 413
super(ErrorOnReloadInNameServer, self).__init__(value, http_code, code,
driver)


class NewUserNotValid(WorldWideDNSException):
def __init__(self, http_code, driver=None):
value = "New userid is not valid"
super(NewUserNotValid, self).__init__(value, http_code, 414,
driver)


class CouldntReachNameServer(WorldWideDNSException):
def __init__(self, http_code, driver=None):
value = "Couldn't reach the name server, try again later"
super(CouldntReachNameServer, self).__init__(value, http_code, 450,
driver)


class NoZoneFile(WorldWideDNSException):
def __init__(self, http_code, driver=None):
value = "No zone file in the name server queried"
super(NoZoneFile, self).__init__(value, http_code, 451,
driver)


ERROR_CODE_TO_EXCEPTION_CLS = {
'401': SuspendedAccount,
'403': LoginOrPasswordNotMatch,
'405': NonExistentDomain,
'406': CouldntRemoveDomain,
'407': LimitExceeded,
'408': ExistentDomain,
'409': DomainBanned,
'410': InvalidDomainName,
'411': ErrorOnReloadInNameServer,
'412': ErrorOnReloadInNameServer,
'413': ErrorOnReloadInNameServer,
'414': NewUserNotValid,
'450': CouldntReachNameServer,
'451': NoZoneFile,
}


class WorldWideDNSResponse(Response):

def parse_body(self):
"""
Parse response body.

:return: Parsed body.
:rtype: ``str``
"""
if self._code_response(self.body):
codes = re.split('\r?\n', self.body)
for code in codes:
if code in OK_CODES:
continue
elif code in ERROR_CODES:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative which allows this method to be shorted would be to create a module level dict constant which maps code to the exception class (e.g. ERROR_CODE_TO_EXCEPTION_CLS) and using it here.

exception = ERROR_CODE_TO_EXCEPTION_CLS.get(code)
if code in ['411', '412', '413']:
server = int(code[2])
raise exception(server, self.status)
raise exception(self.status)
return self.body

def _code_response(self, body):
"""
Checks if the response body contains code status.

:rtype: ``bool``
"""
available_response_codes = OK_CODES + ERROR_CODES
codes = re.split('\r?\n', body)
if codes[0] in available_response_codes:
return True
return False


class WorldWideDNSConnection(ConnectionUserAndKey):
host = 'www.worldwidedns.net'
responseCls = WorldWideDNSResponse

def add_default_params(self, params):
"""
Add parameters that are necessary for every request

This method adds ``NAME`` and ``PASSWORD`` to
the request.
"""
params["NAME"] = self.user_id
params["PASSWORD"] = self.key
if hasattr(self, 'reseller_id'):
params["ID"] = self.reseller_id
return params
Loading