Beispiel #1
0
def get_all_buckets(project=None, connection=None):
    """Get all buckets in the project.

    This will not populate the list of blobs available in each
    bucket.

      >>> from gcloud import storage
      >>> for bucket in storage.get_all_buckets():
      >>>   print bucket

    This implements "storage.buckets.list".

    :type project: string
    :param project: Optional. The project to use when listing all buckets.
                    If not provided, falls back to default.

    :type connection: :class:`gcloud.storage.connection.Connection` or
                      ``NoneType``
    :param connection: Optional. The connection to use when sending requests.
                       If not provided, falls back to default.

    :rtype: iterable of :class:`gcloud.storage.bucket.Bucket` objects.
    :returns: All buckets belonging to this project.
    """
    connection = _require_connection(connection)
    if project is None:
        project = get_default_project()
    extra_params = {'project': project}
    return iter(_BucketIterator(connection=connection,
                                extra_params=extra_params))
Beispiel #2
0
    def create(self, project=None):
        """Creates current bucket.

        If the bucket already exists, will raise
        :class:`gcloud.exceptions.Conflict`.

        This implements "storage.buckets.insert".

        :type project: string
        :param project: Optional. The project to use when creating bucket.
                        If not provided, falls back to default.

        :rtype: :class:`gcloud.storage.bucket.Bucket`
        :returns: The newly created bucket.
        :raises: :class:`EnvironmentError` if the project is not given and
                 can't be inferred.
        """
        if project is None:
            project = get_default_project()
        if project is None:
            raise EnvironmentError('Project could not be inferred '
                                   'from environment.')

        query_params = {'project': project}
        self._properties = self.connection.api_request(
            method='POST', path='/b', query_params=query_params,
            data={'name': self.name})
Beispiel #3
0
    def create(self, project=None, connection=None):
        """Creates current bucket.

        If the bucket already exists, will raise
        :class:`gcloud.exceptions.Conflict`.

        This implements "storage.buckets.insert".

        :type project: string
        :param project: Optional. The project to use when creating bucket.
                        If not provided, falls back to default.

        :type connection: :class:`gcloud.storage.connection.Connection` or
                          ``NoneType``
        :param connection: Optional. The connection to use when sending
                           requests. If not provided, falls back to default.

        :rtype: :class:`gcloud.storage.bucket.Bucket`
        :returns: The newly created bucket.
        :raises: :class:`EnvironmentError` if the project is not given and
                 can't be inferred.
        """
        connection = _require_connection(connection)
        if project is None:
            project = get_default_project()
        if project is None:
            raise EnvironmentError("Project could not be inferred " "from environment.")

        query_params = {"project": project}
        api_response = connection.api_request(
            method="POST", path="/b", query_params=query_params, data={"name": self.name}, _target_object=self
        )
        self._set_properties(api_response)
Beispiel #4
0
def list_subscriptions(page_size=None, page_token=None, topic_name=None,
                       project=None, connection=None):
    """List subscriptions for a given project.

    See:
    https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/topics/list

    and (where ``topic_name`` is passed):
    https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/topics/subscriptions/list

    :type page_size: int
    :param page_size: maximum number of topics to return, If not passed,
                      defaults to a value set by the API.

    :type page_token: string
    :param page_token: opaque marker for the next "page" of topics. If not
                       passed, the API will return the first page of topics.

    :type topic_name: string
    :param topic_name: limit results to subscriptions bound to the given topic.

    :type project: string
    :param project: project ID to query.  If not passed, defaults to the
                    project ID inferred from the environment.

    :type connection: :class:`gcloud.pubsub.connection.Connection`
    :param connection: connection to use for the query.  If not passed,
                       defaults to the connection inferred from the
                       environment.

    :rtype: tuple, (list, str)
    :returns: list of :class:`gcloud.pubsub.subscription.Subscription`, plus a
              "next page token" string:  if not None, indicates that
              more topics can be retrieved with another call (pass that
              value as ``page_token``).
    """
    if project is None:
        project = get_default_project()

    connection = _require_connection(connection)

    params = {}

    if page_size is not None:
        params['pageSize'] = page_size

    if page_token is not None:
        params['pageToken'] = page_token

    if topic_name is None:
        path = '/projects/%s/subscriptions' % project
    else:
        path = '/projects/%s/topics/%s/subscriptions' % (project, topic_name)

    resp = connection.api_request(method='GET', path=path, query_params=params)
    topics = {}
    subscriptions = [Subscription.from_api_repr(resource, topics=topics)
                     for resource in resp['subscriptions']]
    return subscriptions, resp.get('nextPageToken')
Beispiel #5
0
 def __init__(self, name, project=None, connection=None):
     if project is None:
         project = get_default_project()
     if connection is None:
         connection = get_default_connection()
     self.name = name
     self.project = project
     self.connection = connection
Beispiel #6
0
def list_subscriptions(page_size=None, page_token=None, topic_name=None,
                       project=None, connection=None):
    """List subscriptions for a given project.

    See:
    https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/topics/list

    and (where ``topic_name`` is passed):
    https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/topics/subscriptions/list

    :type page_size: int
    :param page_size: maximum number of topics to return, If not passed,
                      defaults to a value set by the API.

    :type page_token: string
    :param page_token: opaque marker for the next "page" of topics. If not
                       passed, the API will return the first page of topics.

    :type topic_name: string
    :param topic_name: limit results to subscriptions bound to the given topic.

    :type project: string
    :param project: project ID to query.  If not passed, defaults to the
                    project ID inferred from the environment.

    :type connection: :class:`gcloud.pubsub.connection.Connection`
    :param connection: connection to use for the query.  If not passed,
                       defaults to the connection inferred from the
                       environment.

    :rtype: dict
    :returns: keys include ``subscriptions`` (a list of subscription mappings)
              and ``nextPageToken`` (a string:  if non-empty, indicates that
              more topics can be retrieved with another call (pass that
              value as ``page_token``).
    """
    if project is None:
        project = get_default_project()

    if connection is None:
        connection = get_default_connection()

    params = {}

    if page_size is not None:
        params['pageSize'] = page_size

    if page_token is not None:
        params['pageToken'] = page_token

    if topic_name is None:
        path = '/projects/%s/subscriptions' % project
    else:
        path = '/projects/%s/topics/%s/subscriptions' % (project, topic_name)

    return connection.api_request(method='GET', path=path, query_params=params)
Beispiel #7
0
 def __init__(self, name, project=None, connection=None,
              timestamp_messages=False):
     if project is None:
         project = get_default_project()
     if connection is None:
         connection = get_default_connection()
     self.name = name
     self.project = project
     self.connection = connection
     self.timestamp_messages = timestamp_messages
Beispiel #8
0
def list_topics(page_size=None, page_token=None,
                project=None, connection=None):
    """List topics for a given project.

    See:
    https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/topics/list

    :type page_size: int
    :param page_size: maximum number of topics to return, If not passed,
                      defaults to a value set by the API.

    :type page_token: string
    :param page_token: opaque marker for the next "page" of topics. If not
                       passed, the API will return the first page of topics.

    :type project: string
    :param project: project ID to query.  If not passed, defaults to the
                    project ID inferred from the environment.

    :type connection: :class:`gcloud.pubsub.connection.Connection`
    :param connection: connection to use for the query.  If not passed,
                       defaults to the connection inferred from the
                       environment.

    :rtype: tuple, (list, str)
    :returns: list of :class:`gcloud.pubsub.topic.Topic`, plus a
              "next page token" string:  if not None, indicates that
              more topics can be retrieved with another call (pass that
              value as ``page_token``).
    """
    if project is None:
        project = get_default_project()

    if connection is None:
        connection = get_default_connection()

    params = {}

    if page_size is not None:
        params['pageSize'] = page_size

    if page_token is not None:
        params['pageToken'] = page_token

    path = '/projects/%s/topics' % project
    resp = connection.api_request(method='GET', path=path, query_params=params)
    topics = []
    for full_name in [topic['name'] for topic in resp['topics']]:
        _, t_project, _, name = full_name.split('/')
        topics.append(Topic(name, t_project, connection))
    return topics, resp.get('nextPageToken')
Beispiel #9
0
    def create(self, project=None, connection=None):
        """Creates current bucket.

        If the bucket already exists, will raise
        :class:`gcloud.exceptions.Conflict`.

        This implements "storage.buckets.insert".

        :type project: string
        :param project: Optional. The project to use when creating bucket.
                        If not provided, falls back to default.

        :type connection: :class:`gcloud.storage.connection.Connection` or
                          ``NoneType``
        :param connection: Optional. The connection to use when sending
                           requests. If not provided, falls back to default.

        :rtype: :class:`gcloud.storage.bucket.Bucket`
        :returns: The newly created bucket.
        :raises: :class:`EnvironmentError` if the project is not given and
                 can't be inferred.
        """
        connection = _require_connection(connection)
        if project is None:
            project = get_default_project()
        if project is None:
            raise EnvironmentError('Project could not be inferred '
                                   'from environment.')

        query_params = {'project': project}
        api_response = connection.api_request(method='POST',
                                              path='/b',
                                              query_params=query_params,
                                              data={'name': self.name},
                                              _target_object=self)
        self._set_properties(api_response)
Beispiel #10
0
def list_buckets(project=None, max_results=None, page_token=None, prefix=None,
                 projection='noAcl', fields=None, connection=None):
    """Get all buckets in the project.

    This will not populate the list of blobs available in each
    bucket.

      >>> from gcloud import storage
      >>> for bucket in storage.list_buckets():
      >>>   print bucket

    This implements "storage.buckets.list".

    :type project: string or ``NoneType``
    :param project: Optional. The project to use when listing all buckets.
                    If not provided, falls back to default.

    :type max_results: integer or ``NoneType``
    :param max_results: Optional. Maximum number of buckets to return.

    :type page_token: string or ``NoneType``
    :param page_token: Optional. Opaque marker for the next "page" of buckets.
                       If not passed, will return the first page of buckets.

    :type prefix: string or ``NoneType``
    :param prefix: Optional. Filter results to buckets whose names begin with
                   this prefix.

    :type projection: string or ``NoneType``
    :param projection: If used, must be 'full' or 'noAcl'. Defaults to
                       'noAcl'. Specifies the set of properties to return.

    :type fields: string or ``NoneType``
    :param fields: Selector specifying which fields to include in a
                   partial response. Must be a list of fields. For example
                   to get a partial response with just the next page token
                   and the language of each bucket returned:
                   'items/id,nextPageToken'

    :type connection: :class:`gcloud.storage.connection.Connection` or
                      ``NoneType``
    :param connection: Optional. The connection to use when sending requests.
                       If not provided, falls back to default.

    :rtype: iterable of :class:`gcloud.storage.bucket.Bucket` objects.
    :returns: All buckets belonging to this project.
    """
    if project is None:
        project = get_default_project()
    extra_params = {'project': project}

    if max_results is not None:
        extra_params['maxResults'] = max_results

    if prefix is not None:
        extra_params['prefix'] = prefix

    extra_params['projection'] = projection

    if fields is not None:
        extra_params['fields'] = fields

    result = _BucketIterator(connection=connection,
                             extra_params=extra_params)
    # Page token must be handled specially since the base `Iterator`
    # class has it as a reserved property.
    if page_token is not None:
        result.next_page_token = page_token
    return result
Beispiel #11
0
 def __init__(self, name, project=None, timestamp_messages=False):
     if project is None:
         project = get_default_project()
     self.name = name
     self.project = project
     self.timestamp_messages = timestamp_messages
Beispiel #12
0
def list_buckets(project=None,
                 max_results=None,
                 page_token=None,
                 prefix=None,
                 projection='noAcl',
                 fields=None,
                 connection=None):
    """Get all buckets in the project.

    This will not populate the list of blobs available in each
    bucket.

      >>> from gcloud import storage
      >>> for bucket in storage.list_buckets():
      >>>   print bucket

    This implements "storage.buckets.list".

    :type project: string or ``NoneType``
    :param project: Optional. The project to use when listing all buckets.
                    If not provided, falls back to default.

    :type max_results: integer or ``NoneType``
    :param max_results: Optional. Maximum number of buckets to return.

    :type page_token: string or ``NoneType``
    :param page_token: Optional. Opaque marker for the next "page" of buckets.
                       If not passed, will return the first page of buckets.

    :type prefix: string or ``NoneType``
    :param prefix: Optional. Filter results to buckets whose names begin with
                   this prefix.

    :type projection: string or ``NoneType``
    :param projection: If used, must be 'full' or 'noAcl'. Defaults to
                       'noAcl'. Specifies the set of properties to return.

    :type fields: string or ``NoneType``
    :param fields: Selector specifying which fields to include in a
                   partial response. Must be a list of fields. For example
                   to get a partial response with just the next page token
                   and the language of each bucket returned:
                   'items/id,nextPageToken'

    :type connection: :class:`gcloud.storage.connection.Connection` or
                      ``NoneType``
    :param connection: Optional. The connection to use when sending requests.
                       If not provided, falls back to default.

    :rtype: iterable of :class:`gcloud.storage.bucket.Bucket` objects.
    :returns: All buckets belonging to this project.
    """
    connection = _require_connection(connection)
    if project is None:
        project = get_default_project()
    extra_params = {'project': project}

    if max_results is not None:
        extra_params['maxResults'] = max_results

    if prefix is not None:
        extra_params['prefix'] = prefix

    extra_params['projection'] = projection

    if fields is not None:
        extra_params['fields'] = fields

    result = _BucketIterator(connection=connection, extra_params=extra_params)
    # Page token must be handled specially since the base `Iterator`
    # class has it as a reserved property.
    if page_token is not None:
        result.next_page_token = page_token
    return result