Python httplib2.HttpLib2Error() Examples

The following are 30 code examples of httplib2.HttpLib2Error(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module httplib2 , or try the search function .
Example #1
Source File: monitors.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def send(self, metric_pb):
    body = self.encode_to_json(metric_pb)

    try:
      resp, content = self._http.request(self._endpoint,
          method='POST',
          body=body,
          headers={'Content-Type': 'application/json'})
      if resp.status == 200:
        self._failed = False
      else:
        logging.warning('HttpsMonitor.send received status %d: %s', resp.status,
                        content)
        self._failed = True
    except (ValueError, errors.Error,
            socket.timeout, socket.error, socket.herror, socket.gaierror,
            httplib2.HttpLib2Error):
      logging.exception('HttpsMonitor.send failed')
      self._failed = True 
Example #2
Source File: __init__.py    From starthinker with Apache License 2.0 6 votes vote down vote up
def object_put(auth, path, data, mimetype='application/octet-stream'):
  bucket, filename = path.split(':', 1)
  service = get_service('storage', 'v1', auth)

  media = MediaIoBaseUpload(data, mimetype=mimetype, chunksize=CHUNKSIZE, resumable=True)
  request = service.objects().insert(bucket=bucket, name=filename, media_body=media)

  response = None
  errors = 0
  while response is None:
    error = None
    try:
      status, response = request.next_chunk()
      if project.verbose and status: print("Uploaded %d%%." % int(status.progress() * 100))
    except HttpError as e:
      if e.resp.status < 500: raise
      error = e
    except (httplib2.HttpLib2Error, IOError) as e:
      error = e

    errors = (errors + 1) if error else 0
    if errors > RETRIES: raise error

  if project.verbose: print("Uploaded 100%.") 
Example #3
Source File: monitors.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def send(self, metric_pb):
    body = self.encode_to_json(metric_pb)

    try:
      resp, content = self._http.request(self._endpoint,
          method='POST',
          body=body,
          headers={'Content-Type': 'application/json'})
      if resp.status == 200:
        self._failed = False
      else:
        logging.warning('HttpsMonitor.send received status %d: %s', resp.status,
                        content)
        self._failed = True
    except (ValueError, errors.Error,
            socket.timeout, socket.error, socket.herror, socket.gaierror,
            httplib2.HttpLib2Error):
      logging.exception('HttpsMonitor.send failed')
      self._failed = True 
Example #4
Source File: stackdriver_logging.py    From forseti-security with Apache License 2.0 6 votes vote down vote up
def get_project_sinks(self, project_id):
        """Get information about project sinks.
        Args:
            project_id (str): The id of the project.
        Returns:
            list: The response of retrieving the project sinks.
        Raises:
            ApiExecutionError: ApiExecutionError is raised if the call to the
                GCP API fails.
        """
        name = self.repository.projects_sinks.get_name(project_id)

        try:
            paged_results = self.repository.projects_sinks.list(name)
            flattened_results = api_helpers.flatten_list_results(paged_results,
                                                                 'sinks')
            LOGGER.debug('Getting information about project sinks,'
                         ' project_id = %s, flattened_results = %s',
                         project_id, flattened_results)
            return flattened_results
        except (errors.HttpError, HttpLib2Error) as e:
            api_exception = api_errors.ApiExecutionError(
                'projects_sinks', e, 'name', name)
            LOGGER.exception(api_exception)
            raise api_exception 
Example #5
Source File: stackdriver_logging.py    From forseti-security with Apache License 2.0 6 votes vote down vote up
def get_billing_account_sinks(self, account_id):
        """Get information about billing_account sinks.
        Args:
            account_id (str): The id of the billing account.
        Returns:
            list: The response of retrieving the billing_account sinks.
        Raises:
            ApiExecutionError: ApiExecutionError is raised if the call to the
                GCP API fails.
        """
        name = self.repository.billing_accounts_sinks.get_name(account_id)

        try:
            paged_results = self.repository.billing_accounts_sinks.list(name)
            flattened_results = api_helpers.flatten_list_results(paged_results,
                                                                 'sinks')
            LOGGER.debug('Getting information about billing_account sinks,'
                         ' billing_account_id = %s, flattened_results = %s',
                         account_id, flattened_results)
            return flattened_results
        except (errors.HttpError, HttpLib2Error) as e:
            api_exception = api_errors.ApiExecutionError(
                'billing_accounts_sinks', e, 'name', name)
            LOGGER.exception(api_exception)
            raise api_exception 
Example #6
Source File: appengine.py    From forseti-security with Apache License 2.0 6 votes vote down vote up
def get_service(self, project_id, service_id):
        """Gets information about a specific service.

        Args:
            project_id (str): The id of the project.
            service_id (str): The id of the service to query.

        Returns:
            dict: A Service resource dict for a given project_id and
            service_id.
        """
        try:
            results = self.repository.app_services.get(
                project_id, target=service_id)
            LOGGER.debug('Getting information about a specific service,'
                         ' project_id = %s, service_id = %s, results = %s',
                         project_id, service_id, results)
            return results
        except (errors.HttpError, HttpLib2Error) as e:
            if _is_status_not_found(e):
                return {}
            raise api_errors.ApiExecutionError(project_id, e) 
Example #7
Source File: appengine.py    From forseti-security with Apache License 2.0 6 votes vote down vote up
def get_app(self, project_id):
        """Gets information about an application.

        Args:
            project_id (str): The id of the project.

        Returns:
            dict: The response of retrieving the AppEngine app.
        """
        try:
            results = self.repository.apps.get(project_id)
            LOGGER.debug('Getting information about an application,'
                         ' project_id = %s, result = %s', project_id, results)
            return results
        except (errors.HttpError, HttpLib2Error) as e:
            if _is_status_not_found(e):
                return {}
            raise api_errors.ApiExecutionError(project_id, e) 
Example #8
Source File: httplib2_utils.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def request(self, uri, method='GET', body=None, *args, **kwargs):
    for i in range(1, self._max_tries + 1):
      try:
        response, content = self._http.request(uri, method, body, *args,
                                               **kwargs)

        if self._retrying_statuses_fn(response.status):
          logging.info('RetriableHttp: attempt %d receiving status %d, %s',
                       i, response.status,
                       'final attempt' if i == self._max_tries else \
                       'will retry')
        else:
          break
      except (ValueError, errors.Error,
              socket.timeout, socket.error, socket.herror, socket.gaierror,
              httplib2.HttpLib2Error) as error:
        logging.info('RetriableHttp: attempt %d received exception: %s, %s',
                     i, error, 'final attempt' if i == self._max_tries else \
                     'will retry')
        if i == self._max_tries:
          raise
      time.sleep(self._backoff_time)

    return response, content 
Example #9
Source File: httplib2_utils.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def request(self, uri, method='GET', body=None, *args, **kwargs):
    for i in range(1, self._max_tries + 1):
      try:
        response, content = self._http.request(uri, method, body, *args,
                                               **kwargs)

        if self._retrying_statuses_fn(response.status):
          logging.info('RetriableHttp: attempt %d receiving status %d, %s',
                       i, response.status,
                       'final attempt' if i == self._max_tries else \
                       'will retry')
        else:
          break
      except (ValueError, errors.Error,
              socket.timeout, socket.error, socket.herror, socket.gaierror,
              httplib2.HttpLib2Error) as error:
        logging.info('RetriableHttp: attempt %d received exception: %s, %s',
                     i, error, 'final attempt' if i == self._max_tries else \
                     'will retry')
        if i == self._max_tries:
          raise
      time.sleep(self._backoff_time)

    return response, content 
Example #10
Source File: monitors.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def send(self, metric_pb):
    body = self.encode_to_json(metric_pb)

    try:
      resp, content = self._http.request(self._endpoint,
          method='POST',
          body=body,
          headers={'Content-Type': 'application/json'})
      if resp.status == 200:
        self._failed = False
      else:
        logging.warning('HttpsMonitor.send received status %d: %s', resp.status,
                        content)
        self._failed = True
    except (ValueError, errors.Error,
            socket.timeout, socket.error, socket.herror, socket.gaierror,
            httplib2.HttpLib2Error):
      logging.exception('HttpsMonitor.send failed')
      self._failed = True 
Example #11
Source File: monitors.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def send(self, metric_pb):
    body = self.encode_to_json(metric_pb)

    try:
      resp, content = self._http.request(self._endpoint,
          method='POST',
          body=body,
          headers={'Content-Type': 'application/json'})
      if resp.status == 200:
        self._failed = False
      else:
        logging.warning('HttpsMonitor.send received status %d: %s', resp.status,
                        content)
        self._failed = True
    except (ValueError, errors.Error,
            socket.timeout, socket.error, socket.herror, socket.gaierror,
            httplib2.HttpLib2Error):
      logging.exception('HttpsMonitor.send failed')
      self._failed = True 
Example #12
Source File: stackdriver_logging.py    From forseti-security with Apache License 2.0 6 votes vote down vote up
def get_folder_sinks(self, folder_id):
        """Get information about folder sinks.
        Args:
            folder_id (str): The id of the folder.
        Returns:
            list: The response of retrieving the folder sinks.
        Raises:
            ApiExecutionError: ApiExecutionError is raised if the call to the
                GCP API fails.
        """
        name = self.repository.folders_sinks.get_name(folder_id)

        try:
            paged_results = self.repository.folders_sinks.list(name)
            flattened_results = api_helpers.flatten_list_results(paged_results,
                                                                 'sinks')
            LOGGER.debug('Getting information about folder sinks,'
                         ' folder_id = %s, flattened_results = %s',
                         folder_id, flattened_results)
            return flattened_results
        except (errors.HttpError, HttpLib2Error) as e:
            api_exception = api_errors.ApiExecutionError(
                'folders_sinks', e, 'name', name)
            LOGGER.exception(api_exception)
            raise api_exception 
Example #13
Source File: stackdriver_logging.py    From forseti-security with Apache License 2.0 6 votes vote down vote up
def get_organization_sinks(self, org_id):
        """Get information about organization sinks.
        Args:
            org_id (str): The id of the organization.
        Returns:
            list: The response of retrieving the organization sinks.
        Raises:
            ApiExecutionError: ApiExecutionError is raised if the call to the
                GCP API fails.
        """
        name = self.repository.organizations_sinks.get_name(org_id)

        try:
            paged_results = self.repository.organizations_sinks.list(name)
            flattened_results = api_helpers.flatten_list_results(paged_results,
                                                                 'sinks')
            LOGGER.debug('Getting information about organization sinks,'
                         ' org_id = %s, flattened_results = %s',
                         org_id, flattened_results)
            return flattened_results
        except (errors.HttpError, HttpLib2Error) as e:
            api_exception = api_errors.ApiExecutionError(
                'organizations_sinks', e, 'name', name)
            LOGGER.exception(api_exception)
            raise api_exception 
Example #14
Source File: compute.py    From forseti-security with Apache License 2.0 6 votes vote down vote up
def get_image(self, project_id, image_name):
        """Get an image from a project.

        Args:
            project_id (str): The project id.
            image_name (str): The image name to get.

        Returns:
            dict: A Compute Image resource dict.
            https://cloud.google.com/compute/docs/reference/latest/images
        """
        try:
            results = self.repository.images.get(project_id, target=image_name)
            LOGGER.debug('Getting an image from a project, project_id = %s, '
                         'image_name = %s, results = %s',
                         project_id, image_name, results)
            return results
        except (errors.HttpError, HttpLib2Error) as e:
            api_not_enabled, details = _api_not_enabled(e)
            if api_not_enabled:
                raise api_errors.ApiNotEnabledError(details, e)
            raise api_errors.ApiExecutionError(project_id, e) 
Example #15
Source File: gce.py    From alfred-gmail with MIT License 6 votes vote down vote up
def _refresh(self, http_request):
        """Refreshes the access_token.

        Skip all the storage hoops and just refresh using the API.

        Args:
            http_request: callable, a callable that matches the method
                          signature of httplib2.Http.request, used to make
                          the refresh request.

        Raises:
            HttpAccessTokenRefreshError: When the refresh fails.
        """
        try:
            self._retrieve_info(http_request)
            self.access_token, self.token_expiry = _metadata.get_token(
                http_request, service_account=self.service_account_email)
        except httplib2.HttpLib2Error as e:
            raise client.HttpAccessTokenRefreshError(str(e)) 
Example #16
Source File: monitors.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def send(self, metric_pb):
    body = self.encode_to_json(metric_pb)

    try:
      resp, content = self._http.request(self._endpoint,
          method='POST',
          body=body,
          headers={'Content-Type': 'application/json'})
      if resp.status == 200:
        self._failed = False
      else:
        logging.warning('HttpsMonitor.send received status %d: %s', resp.status,
                        content)
        self._failed = True
    except (ValueError, errors.Error,
            socket.timeout, socket.error, socket.herror, socket.gaierror,
            httplib2.HttpLib2Error):
      logging.exception('HttpsMonitor.send failed')
      self._failed = True 
Example #17
Source File: appengine.py    From forseti-security with Apache License 2.0 6 votes vote down vote up
def get_version(self, project_id, service_id, version_id):
        """Gets information about a specific version of a service.

        Args:
            project_id (str): The id of the project.
            service_id (str): The id of the service to query.
            version_id (str): The id of the version to query.

        Returns:
            dict: A Version resource dict for a given project_id and
            service_id.
        """
        try:
            results = self.repository.service_versions.get(
                project_id, target=version_id, services_id=service_id)
            LOGGER.debug('Getting information about a specific version'
                         ' of a service, project_id = %s, service_id = %s,'
                         ' version_id = %s, results = %s',
                         project_id, service_id, version_id, results)
            return results
        except (errors.HttpError, HttpLib2Error) as e:
            if _is_status_not_found(e):
                return {}
            raise api_errors.ApiExecutionError(project_id, e) 
Example #18
Source File: httplib2_utils.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def request(self, uri, method='GET', body=None, *args, **kwargs):
    for i in range(1, self._max_tries + 1):
      try:
        response, content = self._http.request(uri, method, body, *args,
                                               **kwargs)

        if self._retrying_statuses_fn(response.status):
          logging.info('RetriableHttp: attempt %d receiving status %d, %s',
                       i, response.status,
                       'final attempt' if i == self._max_tries else \
                       'will retry')
        else:
          break
      except (ValueError, errors.Error,
              socket.timeout, socket.error, socket.herror, socket.gaierror,
              httplib2.HttpLib2Error) as error:
        logging.info('RetriableHttp: attempt %d received exception: %s, %s',
                     i, error, 'final attempt' if i == self._max_tries else \
                     'will retry')
        if i == self._max_tries:
          raise
      time.sleep(self._backoff_time)

    return response, content 
Example #19
Source File: appengine.py    From forseti-security with Apache License 2.0 6 votes vote down vote up
def list_services(self, project_id):
        """Lists services of a project.

        Args:
            project_id (str): The id of the project.

        Returns:
            list: A list of Service resource dicts for a project_id.
        """
        try:
            paged_results = self.repository.app_services.list(project_id)
            flattened_results = api_helpers.flatten_list_results(
                paged_results, 'services')
            LOGGER.debug('Listing services of a project, project_id = %s,'
                         ' flattened_results = %s',
                         project_id, flattened_results)
            return flattened_results
        except (errors.HttpError, HttpLib2Error) as e:
            if _is_status_not_found(e):
                return []
            raise api_errors.ApiExecutionError(project_id, e) 
Example #20
Source File: compute.py    From forseti-security with Apache License 2.0 6 votes vote down vote up
def _flatten_list_results(project_id, paged_results, item_key):
    """Flatten results and handle exceptions.

    Args:
        project_id (str): The project id the results are for.
        paged_results (list): A list of paged API response objects.
            [{page 1 results}, {page 2 results}, {page 3 results}, ...]
        item_key (str): The name of the key within the inner "items" lists
            containing the objects of interest.

    Returns:
        list: A list of items.

    Raises:
        ApiNotEnabledError: Raised if the API is not enabled for the project.
        ApiExecutionError: Raised if there is another error while calling the
            API method.
    """
    try:
        return api_helpers.flatten_list_results(paged_results, item_key)
    except (errors.HttpError, HttpLib2Error) as e:
        api_not_enabled, details = _api_not_enabled(e)
        if api_not_enabled:
            raise api_errors.ApiNotEnabledError(details, e)
        raise api_errors.ApiExecutionError(project_id, e) 
Example #21
Source File: compute.py    From forseti-security with Apache License 2.0 6 votes vote down vote up
def get_snapshots(self, project_id):
        """Return the list of all snapshots in the project.

        Args:
            project_id (str): The project id.

        Returns:
            list: A list of snapshot resources for this project.
        """

        try:
            LOGGER.debug('Getting the list of all snapshots in project: %s',
                         project_id)
            repository = self.repository.snapshots
            results = repository.list(project_id)
            return api_helpers.flatten_list_results(results, 'items')
        except (errors.HttpError, HttpLib2Error) as e:
            api_not_enabled, details = _api_not_enabled(e)
            if api_not_enabled:
                err = api_errors.ApiNotEnabledError(details, e)
            else:
                err = api_errors.ApiExecutionError(project_id, e)

            LOGGER.warning(err)
            raise err 
Example #22
Source File: httplib2_utils.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def request(self, uri, method='GET', body=None, *args, **kwargs):
    for i in range(1, self._max_tries + 1):
      try:
        response, content = self._http.request(uri, method, body, *args,
                                               **kwargs)

        if self._retrying_statuses_fn(response.status):
          logging.info('RetriableHttp: attempt %d receiving status %d, %s',
                       i, response.status,
                       'final attempt' if i == self._max_tries else \
                       'will retry')
        else:
          break
      except (ValueError, errors.Error,
              socket.timeout, socket.error, socket.herror, socket.gaierror,
              httplib2.HttpLib2Error) as error:
        logging.info('RetriableHttp: attempt %d received exception: %s, %s',
                     i, error, 'final attempt' if i == self._max_tries else \
                     'will retry')
        if i == self._max_tries:
          raise
      time.sleep(self._backoff_time)

    return response, content 
Example #23
Source File: monitors.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def send(self, metric_pb):
    body = self.encode_to_json(metric_pb)

    try:
      resp, content = self._http.request(self._endpoint,
          method='POST',
          body=body,
          headers={'Content-Type': 'application/json'})
      if resp.status == 200:
        self._failed = False
      else:
        logging.warning('HttpsMonitor.send received status %d: %s', resp.status,
                        content)
        self._failed = True
    except (ValueError, errors.Error,
            socket.timeout, socket.error, socket.herror, socket.gaierror,
            httplib2.HttpLib2Error):
      logging.exception('HttpsMonitor.send failed')
      self._failed = True 
Example #24
Source File: httplib2_utils.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def request(self, uri, method='GET', body=None, *args, **kwargs):
    for i in range(1, self._max_tries + 1):
      try:
        response, content = self._http.request(uri, method, body, *args,
                                               **kwargs)

        if self._retrying_statuses_fn(response.status):
          logging.info('RetriableHttp: attempt %d receiving status %d, %s',
                       i, response.status,
                       'final attempt' if i == self._max_tries else \
                       'will retry')
        else:
          break
      except (ValueError, errors.Error,
              socket.timeout, socket.error, socket.herror, socket.gaierror,
              httplib2.HttpLib2Error) as error:
        logging.info('RetriableHttp: attempt %d received exception: %s, %s',
                     i, error, 'final attempt' if i == self._max_tries else \
                     'will retry')
        if i == self._max_tries:
          raise
      time.sleep(self._backoff_time)

    return response, content 
Example #25
Source File: httplib2_utils.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def request(self, uri, method='GET', body=None, *args, **kwargs):
    for i in range(1, self._max_tries + 1):
      try:
        response, content = self._http.request(uri, method, body, *args,
                                               **kwargs)

        if self._retrying_statuses_fn(response.status):
          logging.info('RetriableHttp: attempt %d receiving status %d, %s',
                       i, response.status,
                       'final attempt' if i == self._max_tries else \
                       'will retry')
        else:
          break
      except (ValueError, errors.Error,
              socket.timeout, socket.error, socket.herror, socket.gaierror,
              httplib2.HttpLib2Error) as error:
        logging.info('RetriableHttp: attempt %d received exception: %s, %s',
                     i, error, 'final attempt' if i == self._max_tries else \
                     'will retry')
        if i == self._max_tries:
          raise
      time.sleep(self._backoff_time)

    return response, content 
Example #26
Source File: compute.py    From forseti-security with Apache License 2.0 6 votes vote down vote up
def is_api_enabled(self, project_id):
        """Checks if the Compute API is enabled for the specified project.

        Args:
            project_id (str): The project id.

        Returns:
            bool: True if the API is enabled, else False.
        """
        try:
            result = self.repository.projects.get(project_id, fields='name')
            LOGGER.debug('Checking if Compute API is enabled, project_id = '
                         '%s, result = %s', project_id, result)
            return bool('name' in result)  # True if name, otherwise False.
        except (errors.HttpError, HttpLib2Error) as e:
            api_not_enabled, _ = _api_not_enabled(e)
            if api_not_enabled:
                return False
            raise api_errors.ApiExecutionError(project_id, e) 
Example #27
Source File: httplib2_utils.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def request(self, uri, method="GET", body=None, *args, **kwargs):
    request_bytes = 0
    if body is not None:
      request_bytes = len(body)
    http_metrics.request_bytes.add(request_bytes, fields=self.fields)

    start_time = self.time_fn()
    try:
      response, content = super(InstrumentedHttp, self).request(
          uri, method, body, *args, **kwargs)
    except socket.timeout:
      self._update_metrics(http_metrics.STATUS_TIMEOUT, start_time)
      raise
    except (socket.error, socket.herror, socket.gaierror):
      self._update_metrics(http_metrics.STATUS_ERROR, start_time)
      raise
    except (httplib.HTTPException, httplib2.HttpLib2Error) as ex:
      status = http_metrics.STATUS_EXCEPTION
      if 'Deadline exceeded while waiting for HTTP response' in str(ex):
        # Raised on Appengine (gae_override/httplib.py).
        status = http_metrics.STATUS_TIMEOUT
      self._update_metrics(status, start_time)
      raise
    http_metrics.response_bytes.add(len(content), fields=self.fields)

    self._update_metrics(response.status, start_time)

    return response, content 
Example #28
Source File: httplib2_utils.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def request(self, uri, method="GET", body=None, *args, **kwargs):
    request_bytes = 0
    if body is not None:
      request_bytes = len(body)
    http_metrics.request_bytes.add(request_bytes, fields=self.fields)

    start_time = self.time_fn()
    try:
      response, content = super(InstrumentedHttp, self).request(
          uri, method, body, *args, **kwargs)
    except socket.timeout:
      self._update_metrics(http_metrics.STATUS_TIMEOUT, start_time)
      raise
    except (socket.error, socket.herror, socket.gaierror):
      self._update_metrics(http_metrics.STATUS_ERROR, start_time)
      raise
    except (httplib.HTTPException, httplib2.HttpLib2Error) as ex:
      status = http_metrics.STATUS_EXCEPTION
      if 'Deadline exceeded while waiting for HTTP response' in str(ex):
        # Raised on Appengine (gae_override/httplib.py).
        status = http_metrics.STATUS_TIMEOUT
      self._update_metrics(status, start_time)
      raise
    http_metrics.response_bytes.add(len(content), fields=self.fields)

    self._update_metrics(response.status, start_time)

    return response, content 
Example #29
Source File: credentials_lib.py    From apitools with Apache License 2.0 5 votes vote down vote up
def CredentialsFromFile(path, client_info, oauth2client_args=None):
    """Read credentials from a file."""
    user_agent = client_info['user_agent']
    scope_key = client_info['scope']
    if not isinstance(scope_key, six.string_types):
        scope_key = ':'.join(scope_key)
    storage_key = client_info['client_id'] + user_agent + scope_key

    if _NEW_FILESTORE:
        credential_store = multiprocess_file_storage.MultiprocessFileStorage(
            path, storage_key)
    else:
        credential_store = multistore_file.get_credential_storage_custom_string_key(  # noqa
            path, storage_key)
    if hasattr(FLAGS, 'auth_local_webserver'):
        FLAGS.auth_local_webserver = False
    credentials = credential_store.get()
    if credentials is None or credentials.invalid:
        print('Generating new OAuth credentials ...')
        for _ in range(20):
            # If authorization fails, we want to retry, rather than let this
            # cascade up and get caught elsewhere. If users want out of the
            # retry loop, they can ^C.
            try:
                flow = oauth2client.client.OAuth2WebServerFlow(**client_info)
                flags = _GetRunFlowFlags(args=oauth2client_args)
                credentials = tools.run_flow(flow, credential_store, flags)
                break
            except (oauth2client.client.FlowExchangeError, SystemExit) as e:
                # Here SystemExit is "no credential at all", and the
                # FlowExchangeError is "invalid" -- usually because
                # you reused a token.
                print('Invalid authorization: %s' % (e,))
            except httplib2.HttpLib2Error as e:
                print('Communication error: %s' % (e,))
                raise exceptions.CredentialsError(
                    'Communication error creating credentials: %s' % e)
    return credentials 
Example #30
Source File: cloudsql.py    From forseti-security with Apache License 2.0 5 votes vote down vote up
def get_instances(self, project_id):
        """Gets all CloudSQL instances for a project.

        Args:
            project_id (int): The project id for a GCP project.

        Returns:
            list: A list of database Instance resource dicts for a project_id.
            https://cloud.google.com/sql/docs/mysql/admin-api/v1beta4/instances

            [{"kind": "sql#instance", "name": "sql_instance1", ...}
             {"kind": "sql#instance", "name": "sql_instance2", ...},
             {...}]

        Raises:
            ApiExecutionError: ApiExecutionError is raised if the call to the
                GCP ClodSQL API fails
        """

        try:
            paged_results = self.repository.instances.list(project_id)
            flattened_results = api_helpers.flatten_list_results(
                paged_results, 'items')
            LOGGER.debug('Getting all the cloudsql instances of a project,'
                         ' project_id = %s, flattened_results = %s',
                         project_id, flattened_results)
            return flattened_results
        except (errors.HttpError, HttpLib2Error) as e:
            api_exception = api_errors.ApiExecutionError(
                'instances', e, 'project_id', project_id)
            LOGGER.exception(api_exception)
            raise api_exception