Esempio n. 1
0
    def parse_error(self):
        status = int(self.status)
        error = {"driver": self, "value": ""}

        if status == httplib.UNAUTHORIZED:
            error["value"] = "Authentication failed"
            raise InvalidCredsError(**error)
        elif status == httplib.FORBIDDEN:
            error["value"] = "Authorization failed"
            error["http_code"] = status
            raise ProviderError(**error)
        elif status == httplib.NOT_FOUND:
            context = self.connection.context
            if context["resource"] == "zone":
                error["zone_id"] = context["id"]
                raise ZoneDoesNotExistError(**error)
            elif context["resource"] == "record":
                error["record_id"] = context["id"]
                raise RecordDoesNotExistError(**error)
            elif context["resource"] == "healthcheck":
                error["health_check_id"] = context["id"]
                raise HealthCheckDoesNotExistError(**error)
        elif status == httplib.CONFLICT:
            context = self.connection.context
            if context["resource"] == "zone":
                error["zone_id"] = context["id"]
                raise ZoneAlreadyExistsError(**error)
        elif status == httplib.BAD_REQUEST:
            context = self.connection.context
            body = self.parse_body()
            raise ProviderError(value=body["errormsg"],
                                http_code=status,
                                driver=self)
Esempio n. 2
0
    def parse_error(self):
        status = int(self.status)
        error = {'driver': self, 'value': ''}

        if status == httplib.UNAUTHORIZED:
            error['value'] = 'Authentication failed'
            raise InvalidCredsError(**error)
        elif status == httplib.FORBIDDEN:
            error['value'] = 'Authorization failed'
            error['http_status'] = status
            raise ProviderError(**error)
        elif status == httplib.NOT_FOUND:
            context = self.connection.context
            if context['resource'] == 'zone':
                error['zone_id'] = context['id']
                raise ZoneDoesNotExistError(**error)
            elif context['resource'] == 'record':
                error['record_id'] = context['id']
                raise RecordDoesNotExistError(**error)
            elif context['resource'] == 'healthcheck':
                error['health_check_id'] = context['id']
                raise HealthCheckDoesNotExistError(**error)
        elif status == httplib.CONFLICT:
            context = self.connection.context
            if context['resource'] == 'zone':
                error['zone_id'] = context['id']
                raise ZoneAlreadyExistsError(**error)
        elif status == httplib.BAD_REQUEST:
            context = self.connection.context
            body = self.parse_body()
            raise ProviderError(value=body['errormsg'],
                                http_code=status,
                                driver=self)
Esempio n. 3
0
    def parse_error(self):
        status = int(self.status)

        if status == httplib.UNAUTHORIZED:
            raise InvalidCredsError(value='Authentication failed', driver=self)
        elif status == httplib.FORBIDDEN:
            raise ProviderError(value='Authorization failed',
                                http_code=status,
                                driver=self)
        elif status == httplib.NOT_FOUND:
            context = self.connection.context
            if context['resource'] == 'zone':
                raise ZoneDoesNotExistError(value='',
                                            driver=self,
                                            zone_id=context['id'])
            elif context['resource'] == 'record':
                raise RecordDoesNotExistError(value='',
                                              driver=self,
                                              record_id=context['id'])
        elif status == httplib.CONFLICT:
            context = self.connection.context
            if context['resource'] == 'zone':
                raise ZoneAlreadyExistsError(value='',
                                             driver=self,
                                             zone_id=context['id'])
Esempio n. 4
0
    def parse_error(self) -> str:
        http_code = int(self.status)
        if http_code == httplib.FORBIDDEN:
            raise InvalidCredsError("Invalid credentials")

        body = super().parse_error()

        if http_code == httplib.INTERNAL_SERVER_ERROR:
            return body["error"]["message"]

        if http_code in (httplib.CONFLICT, httplib.NOT_FOUND):
            raise ProviderError(value=body["error"], http_code=http_code)

        return body["error"]
Esempio n. 5
0
    def parse_error(self):
        if self.status == httplib.UNAUTHORIZED:
            raise InvalidCredsError('Invalid provider credentials')

        body = self.parse_body()
        values = list(body.values())[0]

        if 'errortext' in values:
            value = values['errortext']
        else:
            value = self.body

        error = ProviderError(value=value, http_code=self.status,
                              driver=self.connection.driver)
        raise error
Esempio n. 6
0
    def parse_error(self):
        if self.status == httplib.UNAUTHORIZED:
            raise InvalidCredsError('Invalid provider credentials')

        value = None
        body = self.parse_body()
        if hasattr(body, 'values'):
            values = list(body.values())[0]
            if 'errortext' in values:
                value = values['errortext']
        if value is None:
            value = self.body

        if not value:
            value = 'WARNING: error message text sent by provider was empty.'

        error = ProviderError(value=value, http_code=self.status,
                              driver=self.connection.driver)
        raise error
Esempio n. 7
0
    def create_node(self,
                    name,
                    size,
                    image,
                    ex_volumes=None,
                    ex_tags=None,
                    region=None,
                    **kwargs):
        """
        Create a new node.

        :param name: The name to give the node
        :type name: ``str``

        :param size: The size of node to create
        :type size: :class:`.NodeSize`

        :param image: The image to create the node with
        :type image: :class:`.NodeImage`

        :param ex_volumes: Additional volumes to create the node with
        :type ex_volumes: ``dict`` of :class:`.StorageVolume`s

        :param ex_tags: Tags to assign to the node
        :type ex_tags: ``list`` of ``str``

        :param region: The region in which to create the node
        (if None, use default region specified in __init__)
        :type region: :class:`.NodeLocation`

        :return: the newly created node object
        :rtype: :class:`.Node`
        """
        if region is None:
            region = self.region
        organization = self.list_organizations()[0]
        data = {
            'name': name,
            'organization': organization,
            'image': image.id,
            'volumes': ex_volumes or {},
            'commercial_type': size.id,
            'tags': ex_tags or []
        }

        allocate_space = image.extra.get('size', 50)
        for volume in data['volumes']:
            allocate_space += _to_lib_size(volume['size'])

        while allocate_space < size.disk:
            if size.disk - allocate_space > 150:
                bump = 150
            else:
                bump = size.disk - allocate_space

            vol_num = len(data['volumes']) + 1
            data['volumes'][str(vol_num)] = {
                "name": "%s-%d" % (name, vol_num),
                "organization": organization,
                "size": _to_api_size(bump),
                "volume_type": "l_ssd"
            }
            allocate_space += bump

        if allocate_space > size.extra.get('max_disk', size.disk):
            range = ("of %dGB" %
                     size.disk if size.extra.get('max_disk', size.disk)
                     == size.disk else "between %dGB and %dGB" %
                     (size.extra.get('max_disk', size.disk), size.disk))
            raise ProviderError(
                value=("%s only supports a total volume size %s; tried %dGB" %
                       (size.id, range, allocate_space)),
                http_code=400,
                driver=self)

        # no volumes in API
        data.pop('volumes')

        response = self.connection.request('/servers',
                                           data=json.dumps(data),
                                           region=region,
                                           method='POST')
        server = response.object['server']
        node = self._to_node(server)
        node.extra['region'] = (region.id if isinstance(region, NodeLocation)
                                else region) or 'par1'

        # Scaleway doesn't start servers by default, let's do it
        self._action(node.id, 'poweron')

        return node
Esempio n. 8
0
 def parse_error(self):
     if self.body == 'Error: The file already exists\n':
         raise FileAlreadyExistsError()
     raise ProviderError(self.body + " " + self.error, self.error)