From 576050804055b87fecf4b698e29ffa980e364446 Mon Sep 17 00:00:00 2001 From: Ivan DiLernia Date: Wed, 14 Oct 2015 12:30:30 -0400 Subject: [PATCH] [api] Add user CRUD Additional changes: - Add a deprecation message to `invite` method of `api/users` - 409 HTTP error codes are now recognized as ApiError exceptions --- datadog/api/base.py | 2 +- datadog/api/users.py | 16 +++++++++++-- doc/source/index.rst | 1 + tests/integration/api/test_api.py | 40 ++++++++++++++++++++++++++++++- 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/datadog/api/base.py b/datadog/api/base.py index 48a04475b..a0cdd2bea 100644 --- a/datadog/api/base.py +++ b/datadog/api/base.py @@ -126,7 +126,7 @@ def request(cls, method, path, body=None, attach_host_name=False, response_forma cls._timeout_counter += 1 raise HttpTimeout('%s %s timed out after %d seconds.' % (method, url, _timeout)) except requests.exceptions.HTTPError as e: - if e.response.status_code in (400, 403, 404): + if e.response.status_code in (400, 403, 404, 409): # This gets caught afterwards and raises an ApiError exception pass else: diff --git a/datadog/api/users.py b/datadog/api/users.py index d22f2d52a..cd6ac9554 100644 --- a/datadog/api/users.py +++ b/datadog/api/users.py @@ -1,7 +1,17 @@ -from datadog.api.base import ActionAPIResource +from datadog.api.base import ActionAPIResource, GetableAPIResource, \ + CreateableAPIResource, UpdatableAPIResource, ListableAPIResource, \ + DeletableAPIResource -class User(ActionAPIResource): +class User(ActionAPIResource, GetableAPIResource, CreateableAPIResource, + UpdatableAPIResource, ListableAPIResource, + DeletableAPIResource): + + _class_name = 'user' + _class_url = '/user' + _plural_class_name = 'users' + _json_name = 'user' + """ A wrapper around User HTTP API. """ @@ -17,6 +27,8 @@ def invite(cls, emails): :returns: JSON response from HTTP request """ + print("[DEPRECATION] User.invite() is deprecated. Use `create` instead.") + if not isinstance(emails, list): emails = [emails] diff --git a/doc/source/index.rst b/doc/source/index.rst index a4a02376d..fea38f7f5 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -75,6 +75,7 @@ Datadog.api client requires to run :mod:`datadog` `initialize` method first. .. autoclass:: datadog.api.User :members: :inherited-members: + :exclude-members: invite Datadog.threadstats module diff --git a/tests/integration/api/test_api.py b/tests/integration/api/test_api.py index da7b1078b..5cbabaf87 100644 --- a/tests/integration/api/test_api.py +++ b/tests/integration/api/test_api.py @@ -4,6 +4,7 @@ import time import unittest import requests +import simplejson as json # 3p from nose.plugins.attrib import attr @@ -15,7 +16,6 @@ from datadog import initialize from datadog import api as dog from datadog.api.exceptions import ApiError -from datadog.util.compat import json from tests.util.snapshot_test_utils import ( assert_snap_not_blank, assert_snap_has_no_events ) @@ -897,6 +897,44 @@ def test_revoke_embed(self): with self.assertRaises(ApiError): dog.Embed.get(embed_id) + def test_user_crud(self): + handle = 'user@test.com' + name = 'Test User' + alternate_name = 'Test User Alt' + alternate_email = 'user+1@test.com' + + # test create user + # the user might already exist + try: + u = dog.User.create(handle=handle, name=name) + except ApiError as e: + pass + + # reset user to original status + u = dog.User.update(handle, email=handle, name=name, disabled=False) + assert u['user']['handle'] == handle + assert u['user']['name'] == name + assert u['user']['disabled'] == False + + # test get + u = dog.User.get(handle) + assert u['user']['handle'] == handle + assert u['user']['name'] == name + + # test update user + u = dog.User.update(handle, email=alternate_email, name=alternate_name) + assert u['user']['handle'] == handle + assert u['user']['name'] == alternate_name + assert u['user']['email'] == alternate_email + + # test disable user + dog.User.delete(handle) + u = dog.User.get(handle) + assert u['user']['disabled'] == True + + # test get all users + u = dog.User.get_all() + assert len(u['users']) >= 1 if __name__ == '__main__': unittest.main()