Skip to content

Commit fc0bc0e

Browse files
authored
Merge pull request #2035 from supriyagarg/monitoring-api
Add Group support to the Monitoring API
2 parents 891e4a8 + 5dfa1be commit fc0bc0e

File tree

9 files changed

+1443
-0
lines changed

9 files changed

+1443
-0
lines changed

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
Client <monitoring-client>
129129
monitoring-metric
130130
monitoring-resource
131+
monitoring-group
131132
monitoring-query
132133
monitoring-timeseries
133134
monitoring-label

docs/monitoring-group.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Groups
2+
======
3+
4+
.. automodule:: gcloud.monitoring.group
5+
:members:
6+
:show-inheritance:
7+

docs/monitoring-usage.rst

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,86 @@ before you call
154154
projects.metricDescriptors
155155

156156

157+
Groups
158+
------
159+
160+
A group is a dynamic collection of *monitored resources* whose membership is
161+
defined by a `filter`_. These groups are usually created via the
162+
`Stackdriver dashboard`_. You can list all the groups in a project with the
163+
:meth:`~gcloud.monitoring.client.Client.list_groups` method::
164+
165+
>>> for group in client.list_groups():
166+
... print(group.id, group.display_name, group.parent_id)
167+
('a001', 'Production', None)
168+
('a002', 'Front-end', 'a001')
169+
('1003', 'Back-end', 'a001')
170+
171+
See :class:`~gcloud.monitoring.group.Group` and the API documentation for
172+
`Groups`_ and `Group members`_ for more information.
173+
174+
You can get a specific group based on it's ID as follows::
175+
176+
>>> group = client.fetch_group('a001')
177+
178+
You can get the current members of this group using the
179+
:meth:`~gcloud.monitoring.group.Group.list_members` method::
180+
181+
>>> for member in group.list_members():
182+
... print(member)
183+
184+
Passing in ``end_time`` and ``start_time`` to the above method will return
185+
historical members based on the current filter of the group. The group
186+
membership changes over time, as *monitored resources* come and go, and as they
187+
change properties.
188+
189+
You can create new groups to define new collections of *monitored resources*.
190+
You do this by creating a :class:`~gcloud.monitoring.group.Group` object using
191+
the client's :meth:`~gcloud.monitoring.client.Client.group` factory and then
192+
calling the object's :meth:`~gcloud.monitoring.group.Group.create` method::
193+
194+
>>> filter_string = 'resource.zone = "us-central1-a"'
195+
>>> group = client.group(
196+
... display_name='My group',
197+
... filter_string=filter_string,
198+
... parent_id='a001',
199+
... is_cluster=True)
200+
>>> group.create()
201+
>>> group.id
202+
'1234'
203+
204+
You can further manipulate an existing group by first initializing a Group
205+
object with it's ID or name, and then calling various methods on it.
206+
207+
Delete a group::
208+
209+
>>> group = client.group('1234')
210+
>>> group.exists()
211+
True
212+
>>> group.delete()
213+
214+
215+
Update a group::
216+
217+
>>> group = client.group('1234')
218+
>>> group.exists()
219+
True
220+
>>> group.reload()
221+
>>> group.display_name = 'New Display Name'
222+
>>> group.update()
223+
224+
.. _Stackdriver dashboard:
225+
https://support.stackdriver.com/customer/portal/articles/\
226+
1535145-creating-groups
227+
.. _filter:
228+
https://cloud.google.com/monitoring/api/v3/filters#group-filter
229+
.. _Groups:
230+
https://cloud.google.com/monitoring/api/ref_v3/rest/v3/\
231+
projects.groups
232+
.. _Group members:
233+
https://cloud.google.com/monitoring/api/ref_v3/rest/v3/\
234+
projects.groups.members
235+
236+
157237
Time Series Queries
158238
-------------------
159239

gcloud/monitoring/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from gcloud.monitoring.client import Client
1818
from gcloud.monitoring.connection import Connection
19+
from gcloud.monitoring.group import Group
1920
from gcloud.monitoring.label import LabelDescriptor
2021
from gcloud.monitoring.label import LabelValueType
2122
from gcloud.monitoring.metric import Metric
@@ -33,6 +34,7 @@
3334
__all__ = (
3435
'Client',
3536
'Connection',
37+
'Group',
3638
'LabelDescriptor', 'LabelValueType',
3739
'Metric', 'MetricDescriptor', 'MetricKind', 'ValueType',
3840
'Aligner', 'Query', 'Reducer',

gcloud/monitoring/client.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
from gcloud.client import JSONClient
3232
from gcloud.monitoring.connection import Connection
33+
from gcloud.monitoring.group import Group
3334
from gcloud.monitoring.metric import MetricDescriptor
3435
from gcloud.monitoring.metric import MetricKind
3536
from gcloud.monitoring.metric import ValueType
@@ -282,3 +283,82 @@ def list_resource_descriptors(self, filter_string=None):
282283
https://cloud.google.com/monitoring/api/v3/filters
283284
"""
284285
return ResourceDescriptor._list(self, filter_string)
286+
287+
def group(self, group_id=None, display_name=None, parent_id=None,
288+
filter_string=None, is_cluster=False):
289+
"""Factory constructor for group object.
290+
291+
.. note::
292+
This will not make an HTTP request; it simply instantiates
293+
a group object owned by this client.
294+
295+
:type group_id: string or None
296+
:param group_id: The ID of the group.
297+
298+
:type display_name: string or None
299+
:param display_name:
300+
A user-assigned name for this group, used only for display
301+
purposes.
302+
303+
:type parent_id: string or None
304+
:param parent_id:
305+
The ID of the group's parent, if it has one.
306+
307+
:type filter_string: string or None
308+
:param filter_string:
309+
The filter string used to determine which monitored resources
310+
belong to this group.
311+
312+
:type is_cluster: boolean
313+
:param is_cluster:
314+
If true, the members of this group are considered to be a cluster.
315+
The system can perform additional analysis on groups that are
316+
clusters.
317+
318+
:rtype: :class:`Group`
319+
:returns: The group created with the passed-in arguments.
320+
321+
:raises:
322+
:exc:`ValueError` if both ``group_id`` and ``name`` are specified.
323+
"""
324+
return Group(
325+
self,
326+
group_id=group_id,
327+
display_name=display_name,
328+
parent_id=parent_id,
329+
filter_string=filter_string,
330+
is_cluster=is_cluster,
331+
)
332+
333+
def fetch_group(self, group_id):
334+
"""Fetch a group from the API based on it's ID.
335+
336+
Example::
337+
338+
>>> try:
339+
>>> group = client.fetch_group('1234')
340+
>>> except gcloud.exceptions.NotFound:
341+
>>> print('That group does not exist!')
342+
343+
:type group_id: string
344+
:param group_id: The ID of the group.
345+
346+
:rtype: :class:`~gcloud.monitoring.group.Group`
347+
:returns: The group instance.
348+
349+
:raises: :class:`gcloud.exceptions.NotFound` if the group is not found.
350+
"""
351+
return Group._fetch(self, group_id)
352+
353+
def list_groups(self):
354+
"""List all groups for the project.
355+
356+
Example::
357+
358+
>>> for group in client.list_groups():
359+
... print((group.display_name, group.name))
360+
361+
:rtype: list of :class:`~gcloud.monitoring.group.Group`
362+
:returns: A list of group instances.
363+
"""
364+
return Group._list(self)

0 commit comments

Comments
 (0)