Skip to content

Commit

Permalink
Fix parse_body method for zonomi, vultr and liquidweb dns providers
Browse files Browse the repository at this point in the history
Closes apache#589

Signed-off-by: Tomaz Muraus <[email protected]>
  • Loading branch information
aleGpereira authored and Kami committed Oct 3, 2015
1 parent 0855665 commit 637ff03
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 81 deletions.
27 changes: 6 additions & 21 deletions libcloud/common/liquidweb.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from libcloud.common.base import JsonResponse
from libcloud.common.base import ConnectionUserAndKey
from libcloud.utils.py3 import b
from libcloud.utils.py3 import PY3
from libcloud.common.types import ProviderError


Expand Down Expand Up @@ -165,29 +164,15 @@ class LiquidWebResponse(JsonResponse):
error_dict = {}

def __init__(self, response, connection):
self.connection = connection

self.headers = dict(response.getheaders())
self.error = response.reason
self.status = response.status

# This attribute is used when usng LoggingConnection
original_data = getattr(response, '_original_data', None)

if original_data:
self.body = response._original_data
else:
self.body = self._decompress_response(body=response.read(),
headers=self.headers)

if PY3:
self.body = b(self.body).decode('utf-8')
self.objects, self.errors = self.parse_body()
if not self.success():
self.errors = []
super(LiquidWebResponse, self).__init__(response=response,
connection=connection)
self.objects, self.errors = self.parse_body_and_errors()
if self.errors:
error = self.errors.pop()
raise self._make_excp(error, self.status)

def parse_body(self):
def parse_body_and_errors(self):
data = []
errors = []
js = super(LiquidWebResponse, self).parse_body()
Expand Down
26 changes: 5 additions & 21 deletions libcloud/common/vultr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from libcloud.common.base import ConnectionKey, JsonResponse
from libcloud.utils.misc import lowercase_keys
from libcloud.utils.py3 import PY3, b


__all__ = [
Expand Down Expand Up @@ -36,28 +34,14 @@ class VultrResponse(JsonResponse):

def __init__(self, response, connection):

self.connection = connection
self.error = response.reason
self.status = response.status
self.headers = lowercase_keys(dict(response.getheaders()))

original_data = getattr(response, '_original_data', None)

if original_data:
self.body = response._original_data

else:
self.body = self._decompress_response(body=response.read(),
headers=self.headers)

if PY3:
self.body = b(self.body).decode('utf-8')

self.objects, self.errors = self.parse_body()
self.errors = []
super(VultrResponse, self).__init__(response=response,
connection=connection)
self.objects, self.errors = self.parse_body_and_errors()
if not self.success():
raise self._make_excp(self.errors[0])

def parse_body(self):
def parse_body_and_errors(self):
"""
Returns JSON data in a python list.
"""
Expand Down
26 changes: 7 additions & 19 deletions libcloud/common/zonomi.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

from libcloud.common.base import XmlResponse
from libcloud.common.base import ConnectionKey
from libcloud.utils.py3 import b, PY3


__all__ = [
Expand Down Expand Up @@ -51,25 +50,14 @@ class ZonomiResponse(XmlResponse):
objects = None

def __init__(self, response, connection):
self.connection = connection
self.headers = dict(response.getheaders())
self.error = response.reason
self.status = response.status

# This attribute is used when using LoggingConnection
original_data = getattr(response, '_original_data', None)
if original_data:
self.body = response._original_data
else:
self.body = self._decompress_response(body=response.read(),
headers=self.headers)
if PY3:
self.body = b(self.body).decode('utf-8')
self.objects, self.errors = self.parse_body()
self.errors = []
super(ZonomiResponse, self).__init__(response=response,
connection=connection)
self.objects, self.errors = self.parse_body_and_errors()
if self.errors:
raise self._make_excp(self.errors[0])

def parse_body(self):
def parse_body_and_errors(self):
error_dict = {}
actions = None
result_counts = None
Expand Down Expand Up @@ -116,8 +104,8 @@ def parse_body(self):

return (data, errors)

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

def _make_excp(self, error):
"""
Expand Down
24 changes: 11 additions & 13 deletions libcloud/dns/drivers/vultr.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ def list_zones(self):
"""
action = '/v1/dns/list'
params = {'api_key': self.key}
response, errors = self.connection.request(action=action,
params=params).parse_body()
zones = self._to_zones(response[0])
response = self.connection.request(action=action,
params=params)
zones = self._to_zones(response.objects[0])

return zones

Expand All @@ -99,9 +99,9 @@ def list_records(self, zone):

action = '/v1/dns/records'
params = {'domain': zone.domain}
response, errors = self.connection.request(action=action,
params=params).parse_body()
records = self._to_records(response[0], zone=zone)
response = self.connection.request(action=action,
params=params)
records = self._to_records(response.objects[0], zone=zone)

return records

Expand All @@ -118,9 +118,9 @@ def get_zone(self, zone_id):

action = '/v1/dns/list'
params = {'api_key': self.key}
response, errors = self.connection.request(action=action,
params=params).parse_body()
zones = self._to_zones(response[0])
response = self.connection.request(action=action,
params=params)
zones = self._to_zones(response.objects[0])

if not self.ex_zone_exists(zone_id, zones):
raise ZoneDoesNotExistError(value=None, zone_id=zone_id,
Expand Down Expand Up @@ -247,10 +247,8 @@ def create_record(self, name, zone, type, data, extra=None):
params = {'api_key': self.key}
action = '/v1/dns/create_record'

response, errors = self.connection.request(action=action,
params=params,
data=encoded_data,
method='POST').parse_body()
self.connection.request(action=action, params=params,
data=encoded_data, method='POST')
updated_zone_records = zone.list_records()

for record in updated_zone_records:
Expand Down
15 changes: 8 additions & 7 deletions libcloud/dns/drivers/worldwidedns.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(self, key, secret=None, reseller_id=None, secure=True,
:type reseller_id: ``str``
:param secure: Weither to use HTTPS or HTTP. Note: Some providers
only support HTTPS, and it is on by default.
only support HTTPS, and it is on by default.
:type secure: ``bool``
:param host: Override hostname used for connections.
Expand Down Expand Up @@ -180,10 +180,11 @@ def update_zone(self, zone, domain, type='master', ttl=None, extra=None,
:type ttl: ``int``
:param extra: Extra attributes (driver specific) (optional). Values not
specified such as *SECURE*, *IP*, *FOLDER*, *HOSTMASTER*, *REFRESH*,
*RETRY* and *EXPIRE* will be kept as already is. The same will be for
*S(1 to 40)*, *T(1 to 40)* and *D(1 to 40)* if not in raw mode and
for *ZONENS* and *ZONEDATA* if it is.
specified such as *SECURE*, *IP*, *FOLDER*, *HOSTMASTER*,
*REFRESH*, *RETRY* and *EXPIRE* will be kept as already
is. The same will be for *S(1 to 40)*, *T(1 to 40)* and
*D(1 to 40)* if not in raw mode and for *ZONENS* and
*ZONEDATA* if it is.
:type extra: ``dict``
:param ex_raw: Mode we use to do the update using zone file or not.
Expand Down Expand Up @@ -283,8 +284,8 @@ def create_zone(self, domain, type='master', ttl=None, extra=None):
:type ttl: ``int``
:param extra: Extra attributes (driver specific). (optional). Possible
parameter in here should be *DYN* which values should be 1 for standart
and 2 for dynamic. Default is 1.
parameter in here should be *DYN* which values should be
1 for standart and 2 for dynamic. Default is 1.
:type extra: ``dict``
:rtype: :class:`Zone`
Expand Down

0 comments on commit 637ff03

Please sign in to comment.