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

adds dns support for BuddyDNS #742

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/dns/drivers/buddyns.rst
@@ -0,0 +1,23 @@
BuddyNS Driver Documentation
============================

BuddyNS.com is part of FrontDam GmbH, a small swiss company making
systems for uptime of Internet services, and run by a tribe of passionate
people who work great together.

Read more at: https://www.buddyns.com/about/

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

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

API Docs
--------

.. autoclass:: libcloud.dns.drivers.buddyns.BuddyNSDNSDriver
:members:
:inherited-members:

.. https://www.buddyns.com/support/api/v2/
5 changes: 5 additions & 0 deletions docs/examples/dns/buddyns/instantiate_driver.py
@@ -0,0 +1,5 @@
from libcloud.dns.types import Provider
from libcloud.dns.providers import get_driver

cls = get_driver(Provider.BUDDYNS)
driver = cls('<api key>')
77 changes: 77 additions & 0 deletions libcloud/common/buddyns.py
@@ -0,0 +1,77 @@
# 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.

from libcloud.common.base import ConnectionKey, JsonResponse
Copy link
Contributor

Choose a reason for hiding this comment

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

And the header needs to go here



__all__ = [
'API_HOST',
'BuddyNSException',
'BuddyNSResponse',
'BuddyNSConnection'
]

# Endpoint for buddyns api
API_HOST = 'www.buddyns.com'


class BuddyNSResponse(JsonResponse):
errors = []
objects = []

def __init__(self, response, connection):
super(BuddyNSResponse, self).__init__(response=response,
connection=connection)
self.errors, self.objects = self.parse_body_and_errors()
if not self.success():
raise BuddyNSException(code=self.status,
message=self.errors.pop()['detail'])

def parse_body_and_errors(self):
js = super(BuddyNSResponse, self).parse_body()
if 'detail' in js:
self.errors.append(js)
else:
self.objects.append(js)

return self.errors, self.objects

def success(self):
return len(self.errors) == 0


class BuddyNSConnection(ConnectionKey):
host = API_HOST
responseCls = BuddyNSResponse

def add_default_headers(self, headers):
headers['content-type'] = 'application/json'
headers['Authorization'] = 'Token' + ' ' + self.key

return headers


class BuddyNSException(Exception):

def __init__(self, code, message):
self.code = code
self.message = message
self.args = (code, message)

def __str__(self):
return "%s %s" % (self.code, self.message)

def __repr__(self):
return "BuddyNSException %s %s" % (self.code, self.message)
153 changes: 153 additions & 0 deletions libcloud/dns/drivers/buddyns.py
@@ -0,0 +1,153 @@
# 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.
"""
BuddyNS DNS Driver
"""

import sys
Copy link
Contributor

Choose a reason for hiding this comment

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

You definitely need the Apache license header in this file


try:
import simplejson as json
except ImportError:
import json

from libcloud.dns.types import Provider, ZoneDoesNotExistError,\
ZoneAlreadyExistsError
from libcloud.dns.base import DNSDriver, Zone
from libcloud.common.buddyns import BuddyNSConnection, BuddyNSResponse,\
BuddyNSException

__all__ = [
'BuddyNSDNSDriver'
]


class BuddyNSDNSResponse(BuddyNSResponse):
pass


class BuddyNSDNSConnection(BuddyNSConnection):
responseCls = BuddyNSDNSResponse


class BuddyNSDNSDriver(DNSDriver):
name = 'BuddyNS DNS'
website = 'https://www.buddyns.com'
type = Provider.BUDDYNS
connectionCls = BuddyNSDNSConnection

def list_zones(self):
action = '/api/v2/zone/'
response = self.connection.request(action=action, method='GET')
zones = self._to_zones(items=response.parse_body())

return zones

def get_zone(self, zone_id):
"""
:param zone_id: Zone domain name (e.g. example.com)
:return: :class:`Zone`
"""
action = '/api/v2/zone/%s' % zone_id
try:
response = self.connection.request(action=action, method='GET')
except BuddyNSException:
e = sys.exc_info()[1]
if e.message == 'Not found':
raise ZoneDoesNotExistError(value=e.message, driver=self,
zone_id=zone_id)
else:
raise e
zone = self._to_zone(response.parse_body())

return zone

def create_zone(self, domain, type='master', ttl=None, extra=None):
"""
:param domain: Zone domain name (e.g. example.com)
:type domain: ``str``

:param type: Zone type (This is not really used. See API docs for extra
parameters)
:type type: ``str``

:param ttl: TTL for new records (This is used through the extra param)
:type ttl: ``int``

:param extra: Extra attributes that are specific to the driver
such as ttl.
:type extra: ``dict``

:rtype: :class:`Zone`
Do not forget to pass the master in extra,
Copy link
Member

Choose a reason for hiding this comment

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

I assume master key is only required for slave zones? or?

If so, you could probably throw if type is slave and 'master' not in extra or similar...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to the docs the key master is always required when doing a POST request for creating a new zone.
https://www.buddyns.com/support/api/v2/

extra = {'master':'65.55.37.62'} for example.
"""
action = '/api/v2/zone/'
data = {'name': domain}
if extra is not None:
data.update(extra)
post_data = json.dumps(data)
try:
response = self.connection.request(action=action, method='POST',
data=post_data)
except BuddyNSException:
e = sys.exc_info()[1]
if e.message == 'Invalid zone submitted for addition.':
raise ZoneAlreadyExistsError(value=e.message, driver=self,
zone_id=domain)
else:
raise e

zone = self._to_zone(response.parse_body())

return zone

def delete_zone(self, zone):
"""
:param zone: Zone to be deleted.
:type zone: :class:`Zone`

:return: Boolean
"""
action = '/api/v2/zone/%s' % zone.domain
try:
self.connection.request(action=action, method='DELETE')
except BuddyNSException:
e = sys.exc_info()[1]
if e.message == 'Not found':
raise ZoneDoesNotExistError(value=e.message, driver=self,
zone_id=zone.id)
else:
raise e

return True

def _to_zone(self, item):
common_keys = ['name', ]
extra = {}
for key in item:
if key not in common_keys:
extra[key] = item.get(key)
zone = Zone(domain=item['name'], id=item['name'], type=None,
extra=extra, ttl=None, driver=self)

return zone

def _to_zones(self, items):
zones = []
for item in items:
zones.append(self._to_zone(item))

return zones
1 change: 1 addition & 0 deletions libcloud/dns/types.py
Expand Up @@ -53,6 +53,7 @@ class Provider(object):
NSONE = 'nsone'
LUADNS = 'luadns'
NFSN = 'nfsn'
BUDDYNS = 'buddyns'
# Deprecated
RACKSPACE_US = 'rackspace_us'
RACKSPACE_UK = 'rackspace_uk'
Expand Down
8 changes: 8 additions & 0 deletions libcloud/test/dns/fixtures/buddyns/create_zone_success.json
@@ -0,0 +1,8 @@
{ "name": "microsoft.com",
"name_idn": "microsoft.com",
"serial": null,
"master": "65.55.37.62",
"creation_ts": null,
"status": "https://www.buddyns.com/api/v2/zone/anexample.com/status/",
"delegation": "https://www.buddyns.com/api/v2/zone/anexample.com/delegation/"
}
Empty file.
1 change: 1 addition & 0 deletions libcloud/test/dns/fixtures/buddyns/empty_zones_list.json
@@ -0,0 +1 @@
[]
7 changes: 7 additions & 0 deletions libcloud/test/dns/fixtures/buddyns/get_zone_success.json
@@ -0,0 +1,7 @@
{ "name": "myexample.com",
"name_idn": "myexample.com",
"serial": null,
"master": "65.55.37.62",
"creation_ts": "2016-04-09T06:20:05.140",
"status": "https://www.buddyns.com/api/v2/zone/myexample.com/status/",
"delegation": "https://www.buddyns.com/api/v2/zone/myexample.com/delegation/"}
18 changes: 18 additions & 0 deletions libcloud/test/dns/fixtures/buddyns/list_zones.json
@@ -0,0 +1,18 @@
[ {
"name" : "microsoft.com",
"name_idn" : "microsoft.com",
"creation_ts" : "2013-11-06T19:39:38.205",
"master" : "65.55.37.62",
"serial" : 2013110601,
"status": "/api/v2/zone/microsoft.com/status/",
"delegation": "/api/v2/zone/microsoft.com/delegation/"
},
{
"name" : "google.de",
"name_idn" : "google.de",
"creation_ts" : "2012-06-06T19:53:07.269",
"master" : "154.15.200.6",
"serial" : 1383743519,
"status": "google.de/status/",
"delegation": "/api/v2/zone/bücher.de/delegation/"
} ]
3 changes: 3 additions & 0 deletions libcloud/test/dns/fixtures/buddyns/zone_already_exists.json
@@ -0,0 +1,3 @@
{"errors":
{"name": ["Zone with this Domain already exists."]},
"detail": "Invalid zone submitted for addition."}
@@ -0,0 +1 @@
{"detail": "Not found"}