Beispiel #1
0
    def _check_response(self, response):
        """Checks the response from GCE for error messages.

        :param response: GCE response
        :return: nothing
        :raises: CloudProviderError with error message from GCE
        """
        if "error" in response:
            error = response['error']['errors'][0]['message']
            raise CloudProviderError("The following error occurred while "
                                     "interacting with the cloud provider "
                                     "`%s`" % error)
Beispiel #2
0
    def _connect(self):
        """
        Connect to the EC2 cloud provider.

        :return: :py:class:`boto.ec2.connection.EC2Connection`
        :raises: Generic exception on error
        """
        # check for existing connection
        if self._ec2_connection:
            return self._ec2_connection

        try:
            log.debug("Connecting to EC2 endpoint %s", self._ec2host)

            # connect to webservice
            ec2_connection = boto.ec2.connect_to_region(
                self._region_name,
                aws_access_key_id=self._access_key,
                aws_secret_access_key=self._secret_key,
                is_secure=self._secure,
                host=self._ec2host,
                port=self._ec2port,
                path=self._ec2path,
            )
            # With the loose setting `BOTO_USE_ENDPOINT_HEURISTICS`
            # which is necessary to work around issue #592, Boto will
            # now accept *any* string as an AWS region name;
            # furthermore, it *always* returns a connection object --
            # so the only way to check that we are not going to run
            # into trouble is to check that there *is* a valid host
            # name on the other end of the connection.
            if ec2_connection.host:
                log.debug("EC2 connection has been successful.")
            else:
                raise CloudProviderError(
                    "Cannot establish connection to EC2 region {0}".format(
                        self._region_name))

            if not self._vpc:
                vpc_connection = None
                self._vpc_id = None
            else:
                vpc_connection, self._vpc_id = self._find_vpc_by_name(
                    self._vpc)

        except Exception as err:
            log.error("Error connecting to EC2: %s", err)
            raise

        self._ec2_connection, self._vpc_connection = (ec2_connection,
                                                      vpc_connection)
        return self._ec2_connection