You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by GitBox <gi...@apache.org> on 2019/07/29 23:57:13 UTC

[GitHub] [libcloud] zepheiryan commented on a change in pull request #1323: Add Gandi LiveDNS driver

zepheiryan commented on a change in pull request #1323: Add Gandi LiveDNS driver
URL: https://github.com/apache/libcloud/pull/1323#discussion_r308484457
 
 

 ##########
 File path: libcloud/common/gandi_live.py
 ##########
 @@ -0,0 +1,250 @@
+# 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.
+"""
+Gandi Live driver base classes
+"""
+
+import json
+
+from libcloud.common.base import ConnectionKey, JsonResponse
+from libcloud.common.types import ProviderError
+
+from libcloud.utils.py3 import httplib
+
+__all__ = [
+    'API_HOST',
+    'GandiLiveBaseError',
+    'JsonParseError',
+    'ResourceNotFoundError',
+    'InvalidRequestError',
+    'ResourceConflictError',
+    'GandiLiveResponse',
+    'GandiLiveConnection',
+    'BaseGandiLiveDriver',
+]
+
+API_HOST = 'dns.api.gandi.net'
+
+
+class GandiLiveBaseError(ProviderError):
+    """
+    Exception class for Gandi Live driver
+    """
+    pass
+
+
+class JsonParseError(GandiLiveBaseError):
+    pass
+
+
+# Example:
+# {
+#   "code": 404,
+#   "message": "Unknown zone",
+#   "object": "LocalizedHTTPNotFound",
+#   "cause": "Not Found"
+# }
+class ResourceNotFoundError(GandiLiveBaseError):
+    pass
+
+
+# Example:
+# {
+#    "code": 400,
+#    "message": "zone or zone_uuid must be set",
+#    "object": "HTTPBadRequest",
+#    "cause": "No zone set.",
+#    "errors": [
+#      {
+#        "location": "body",
+#        "name": "zone_uuid",
+#        "description": "\"FAKEUUID\" is not a UUID"
+#      }
+#   ]
+# }
+class InvalidRequestError(GandiLiveBaseError):
+    pass
+
+
+# Examples:
+# {
+#   "code": 409,
+#   "message": "Zone Testing already exists",
+#   "object": "HTTPConflict",
+#   "cause": "Duplicate Entry"
+# }
+# {
+#   "code": 409,
+#   "message": "The domain example.org already exists",
+#   "object": "HTTPConflict",
+#   "cause": "Duplicate Entry"
+# }
+# {
+#   "code": 409,
+#   "message": "This zone is still used by 1 domains",
+#   "object": "HTTPConflict",
+#   "cause": "In use"
+# }
+class ResourceConflictError(GandiLiveBaseError):
+    pass
+
+
+class GandiLiveResponse(JsonResponse):
+    """
+    A Base Gandi Live Response class to derive from.
+    """
+
+    def success(self):
+        """
+        Determine if our request was successful.
+
+        For the Gandi Live response class, tag all responses as successful and
+        raise appropriate Exceptions from parse_body.
+
+        :return: C{True}
+        """
+
+        return True
+
+    def parse_body(self):
+        """
+        Parse the JSON response body, or raise exceptions as appropriate.
+
+        :return:  JSON dictionary
+        :rtype:   ``dict``
+        """
+        if len(self.body) == 0 and not self.parse_zero_length_body:
+            return self.body
+
+        json_error = False
+        try:
+            body = json.loads(self.body)
+        except Exception:
+            # If there is both a JSON parsing error and an unsuccessful http
+            # response (like a 404), we want to raise the http error and not
+            # the JSON one, so don't raise JsonParseError here.
+            body = self.body
+            json_error = True
+
+        # Service does not appear to return HTTP 202 Accepted for anything.
+        valid_http_codes = [
+            httplib.OK,
+            httplib.CREATED,
+            httplib.NO_CONTENT
+        ]
+        if self.status in valid_http_codes:
+            if json_error:
+                raise JsonParseError(body, self.status)
+            else:
+                return body
+        elif self.status == httplib.NOT_FOUND:
+            if (not json_error) and ('cause' in body):
+                message = self._get_error(body)
+            else:
+                message = body
+            raise ResourceNotFoundError(message, self.status)
+        elif self.status == httplib.BAD_REQUEST:
+            if (not json_error) and ('cause' in body):
+                message = self._get_error(body)
+            else:
+                message = body
+            raise InvalidRequestError(message, self.status)
+        elif self.status == httplib.CONFLICT:
+            if (not json_error) and ('cause' in body):
+                message = self._get_error(body)
+            else:
+                message = body
+            raise ResourceConflictError(message, self.status)
+        else:
+            if (not json_error) and ('cause' in body):
 
 Review comment:
   I originally grabbed most of this from the Google common lib.  Fixed this and the previous one - does it matter who clicks the 'resolve conversation' button (if it's used)?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services