Esempio n. 1
0
 def _setUpResources(self):
     from google.cloud.monitoring.resource import Resource
     info1 = {
         'type': 'gce_instance',
         'labels': {
             'project_id': 'my-project',
             'instance_id': '1234567890123456788',
             'zone': 'us-central1-a',
         }
     }
     info2 = {
         'type': 'gce_instance',
         'labels': {
             'project_id': 'my-project',
             'instance_id': '1234567890123456789',
             'zone': 'us-central1-a',
         }
     }
     self.RESOURCE1 = Resource._from_dict(info1)
     self.RESOURCE2 = Resource._from_dict(info2)
     self.MEMBERS = [info1, info2]
Esempio n. 2
0
 def _setUpResources(self):
     from google.cloud.monitoring.resource import Resource
     info1 = {
         'type': 'gce_instance',
         'labels': {
             'project_id': 'my-project',
             'instance_id': '1234567890123456788',
             'zone': 'us-central1-a',
         }
     }
     info2 = {
         'type': 'gce_instance',
         'labels': {
             'project_id': 'my-project',
             'instance_id': '1234567890123456789',
             'zone': 'us-central1-a',
         }
     }
     self.RESOURCE1 = Resource._from_dict(info1)
     self.RESOURCE2 = Resource._from_dict(info2)
     self.MEMBERS = [info1, info2]
Esempio n. 3
0
    def _from_dict(cls, info):
        """Construct a time series from the parsed JSON representation.

        :type info: dict
        :param info:
            A ``dict`` parsed from the JSON wire-format representation.

        :rtype: :class:`TimeSeries`
        :returns: A time series object.
        """
        metric = Metric._from_dict(info['metric'])
        resource = Resource._from_dict(info['resource'])
        metric_kind = info['metricKind']
        value_type = info['valueType']
        points = [Point._from_dict(p) for p in info.get('points', ())]
        return cls(metric, resource, metric_kind, value_type, points)
Esempio n. 4
0
    def list_members(self, filter_string=None, end_time=None, start_time=None):
        """Lists all members of this group via a ``GET`` request.

        If no ``end_time`` is provided then the group membership over the last
        minute is returned.

        Example::

            >>> for member in group.list_members():
            ...     print(member)

        List members that are Compute Engine VM instances::

            >>> filter_string = 'resource.type = "gce_instance"'
            >>> for member in group.list_members(filter_string=filter_string):
            ...     print(member)

        List historical members that existed between 4 and 5 hours ago::

            >>> import datetime
            >>> t1 = datetime.datetime.utcnow() - datetime.timedelta(hours=4)
            >>> t0 = t1 - datetime.timedelta(hours=1)
            >>> for member in group.list_members(end_time=t1, start_time=t0):
            ...     print(member)


        :type filter_string: string or None
        :param filter_string:
            An optional list filter describing the members to be returned. The
            filter may reference the type, labels, and metadata of monitored
            resources that comprise the group. See the `filter documentation`_.

        :type end_time: :class:`datetime.datetime` or None
        :param end_time:
            The end time (inclusive) of the time interval for which results
            should be returned, as a datetime object. If ``start_time`` is
            specified, then this must also be specified.

        :type start_time: :class:`datetime.datetime` or None
        :param start_time:
            The start time (exclusive) of the time interval for which results
            should be returned, as a datetime object.

        :rtype: list of :class:`~google.cloud.monitoring.resource.Resource`
        :returns: A list of resource instances.

        :raises:
            :exc:`ValueError` if the ``start_time`` is specified, but the
            ``end_time`` is missing.

        .. _filter documentation:
            https://cloud.google.com/monitoring/api/v3/filters#group-filter
        """
        if start_time is not None and end_time is None:
            raise ValueError('If "start_time" is specified, "end_time" must '
                             'also be specified')

        path = '%s/members' % (self.path, )
        resources = []
        page_token = None
        params = {}

        if filter_string is not None:
            params['filter'] = filter_string

        if end_time is not None:
            params['interval.endTime'] = _datetime_to_rfc3339(
                end_time, ignore_zone=False)

        if start_time is not None:
            params['interval.startTime'] = _datetime_to_rfc3339(
                start_time, ignore_zone=False)

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

            response = self.client.connection.api_request(
                method='GET', path=path, query_params=params.copy())
            for info in response.get('members', ()):
                resources.append(Resource._from_dict(info))

            page_token = response.get('nextPageToken')
            if not page_token:
                break

        return resources
Esempio n. 5
0
    def list_members(self, filter_string=None, end_time=None, start_time=None):
        """Lists all members of this group via a ``GET`` request.

        If no ``end_time`` is provided then the group membership over the last
        minute is returned.

        Example::

            >>> for member in group.list_members():
            ...     print(member)

        List members that are Compute Engine VM instances::

            >>> filter_string = 'resource.type = "gce_instance"'
            >>> for member in group.list_members(filter_string=filter_string):
            ...     print(member)

        List historical members that existed between 4 and 5 hours ago::

            >>> import datetime
            >>> t1 = datetime.datetime.utcnow() - datetime.timedelta(hours=4)
            >>> t0 = t1 - datetime.timedelta(hours=1)
            >>> for member in group.list_members(end_time=t1, start_time=t0):
            ...     print(member)


        :type filter_string: str
        :param filter_string:
            (Optional) An optional list filter describing the members to be
            returned. The filter may reference the type, labels, and metadata
            of monitored resources that comprise the group. See the `filter
            documentation`_.

        :type end_time: :class:`datetime.datetime`
        :param end_time:
            (Optional) The end time (inclusive) of the time interval for which
            results should be returned, as a datetime object. If ``start_time``
            is specified, then this must also be specified.

        :type start_time: :class:`datetime.datetime`
        :param start_time:
            (Optional) The start time (exclusive) of the time interval for
            which results should be returned, as a datetime object.

        :rtype: list of :class:`~google.cloud.monitoring.resource.Resource`
        :returns: A list of resource instances.

        :raises:
            :exc:`ValueError` if the ``start_time`` is specified, but the
            ``end_time`` is missing.

        .. _filter documentation:
            https://cloud.google.com/monitoring/api/v3/filters#group-filter
        """
        if start_time is not None and end_time is None:
            raise ValueError('If "start_time" is specified, "end_time" must '
                             'also be specified')

        path = '%s/members' % (self.path,)
        resources = []
        page_token = None
        params = {}

        if filter_string is not None:
            params['filter'] = filter_string

        if end_time is not None:
            params['interval.endTime'] = _datetime_to_rfc3339(
                end_time, ignore_zone=False)

        if start_time is not None:
            params['interval.startTime'] = _datetime_to_rfc3339(
                start_time, ignore_zone=False)

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

            response = self.client._connection.api_request(
                method='GET', path=path, query_params=params.copy())
            for info in response.get('members', ()):
                resources.append(Resource._from_dict(info))

            page_token = response.get('nextPageToken')
            if not page_token:
                break

        return resources