Skip to content

Commit

Permalink
Merge pull request #46 from signalfx/wip/detectors-by-tag
Browse files Browse the repository at this point in the history
More detector API support; version 1.0.13
  • Loading branch information
mdubbyap authored Dec 6, 2016
2 parents 426cdb0 + a4b7703 commit 7e80a4b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

This file documents important changes to the SignalFx Python client library.

- [[1.0.13]- 2016-12-05: More features from detector APIs](#1013---2016-12-05-more-features-from-detector-apis)
- [[1.0.12]- 2016-11-28: Detector APIs](#1012---2016-11-28-detector-apis)
- [[1.0.11]- 2016-11-23: Long value support](#1011---2016-11-23-long-value-support)
- [[1.0.10]- 2016-11-21: Unicode event properties fix](#1010---2016-11-21-unicode-event-properties-fix)
Expand All @@ -11,6 +12,16 @@ This file documents important changes to the SignalFx Python client library.
- [[1.0.5] - 2016-09-29: Python 3 compatibility](#105---2016-09-29-python-3-compatibility)
- [[1.0.1] - 2016-06-02: Support for SignalFlow API](#101---2016-06-02-support-for-signalflow-api)

#### [1.0.13] - 2016-12-05: More features from detector APIs

Added support for the `/v2/detector/validate` endpoint via
`rest.validate_detector()`, and support for searching detectors by tags
when using `rest.get_detectors()`.

It is also now possible to pass `ignore_not_found=True` to REST delete
operations to ignore failures on attempting to remove a non-existent
resource for which the DELETE call would otherwise return a 404.

#### [1.0.12] - 2016-11-28: Detector APIs

Added support for managing SignalFlow V2 detectors via the REST client.
Expand Down
54 changes: 38 additions & 16 deletions signalfx/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,43 +42,45 @@ def _u(self, *args):
def _get(self, url, params=None, session=None, timeout=None):
session = session or self._session
timeout = timeout or self._timeout
_logger.debug('URL being retrieved: %s (params: %s)',
pprint.pformat(url), params)
_logger.debug('GET %s (params: %s)', url, params)
response = session.get(url, timeout=timeout, params=params)
_logger.debug('Getting from SignalFx %s (%d %s)',
_logger.debug('Getting from SignalFx %s (%d): %s',
'succeeded' if response.ok else 'failed',
response.status_code, response.text)
return response

def _put(self, url, data, session=None, timeout=None):
session = session or self._session
timeout = timeout or self._timeout
_logger.debug('Raw datastream being sent: %s', pprint.pformat(data))
_logger.debug('PUT %s: %s', url, pprint.pformat(data))
response = session.put(url, json=data, timeout=timeout)
_logger.debug('Putting to SignalFx %s (%d %s)',
_logger.debug('Putting to SignalFx %s (%d): %s',
'succeeded' if response.ok else 'failed',
response.status_code, response.text)
return response

def _post(self, url, data, session=None, timeout=None):
session = session or self._session
timeout = timeout or self._timeout
_logger.debug('Raw datastream being sent: %s', pprint.pformat(data))
_logger.debug('POST %s: %s', url, pprint.pformat(data))
response = session.post(url, json=data, timeout=timeout)
_logger.debug('Posting to SignalFx %s (%d %s)',
_logger.debug('Posting to SignalFx %s (%d): %s',
'succeeded' if response.ok else 'failed',
response.status_code, response.text)
return response

def _delete(self, url, session=None, timeout=None):
def _delete(self, url, session=None, timeout=None,
ignore_not_found=False):
session = session or self._session
timeout = timeout or self._timeout
_logger.debug('url associated with delete request: %s',
pprint.pformat(url))
_logger.debug('DELETE %s', url)
response = session.delete(url, timeout=timeout)
_logger.debug('Deleting from SignalFx %s (%d %s)',
_logger.debug('Deleting from SignalFx %s (%d)',
'succeeded' if response.ok else 'failed',
response.status_code, response.text)
response.status_code)
if response.status_code is requests.codes.not_found and \
ignore_not_found:
response.status_code = requests.codes.no_content
return response

def _search_metrics_and_metadata(self, metadata_endpoint, query,
Expand Down Expand Up @@ -337,7 +339,7 @@ def get_organization(self, **kwargs):
return resp.json()

# functionality related to detectors
def get_detectors(self, name=None, batch_size=100, **kwargs):
def get_detectors(self, name=None, tags=None, batch_size=100, **kwargs):
"""Retrieve all (v2) detectors matching the given name; all (v2)
detectors otherwise.
Expand All @@ -349,7 +351,12 @@ def get_detectors(self, name=None, batch_size=100, **kwargs):
while True:
resp = self._get(
self._u(self._DETECTOR_ENDPOINT_SUFFIX),
params={'offset': offset, 'limit': batch_size, 'name': name},
params={
'offset': offset,
'limit': batch_size,
'name': name,
'tags': tags or [],
},
**kwargs)
resp.raise_for_status()
data = resp.json()
Expand All @@ -359,6 +366,20 @@ def get_detectors(self, name=None, batch_size=100, **kwargs):
offset = len(detectors)
return detectors

def validate_detector(self, detector):
"""Validate a detector.
Validates the given detector; throws a 400 Bad Request HTTP error if
the detector is invalid; otherwise doesn't return or throw anything.
Args:
detector (object): the detector model object. Will be serialized as
JSON.
"""
resp = self._post(self._u(self._DETECTOR_ENDPOINT_SUFFIX, 'validate'),
data=detector)
resp.raise_for_status()

def create_detector(self, detector):
"""Creates a new detector.
Expand Down Expand Up @@ -388,14 +409,15 @@ def update_detector(self, detector_id, detector):
resp.raise_for_status()
return resp.json()

def delete_detector(self, detector_id):
def delete_detector(self, detector_id, **kwargs):
"""Remove a detector.
Args:
detector_id (string): the ID of the detector.
"""
resp = self._delete(self._u(self._DETECTOR_ENDPOINT_SUFFIX,
detector_id))
detector_id),
**kwargs)
resp.raise_for_status()
# successful delete returns 204, which has no response json
return resp
2 changes: 1 addition & 1 deletion signalfx/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2015-2016 SignalFx, Inc. All rights reserved.

name = 'signalfx'
version = '1.0.12'
version = '1.0.13'

0 comments on commit 7e80a4b

Please sign in to comment.