Skip to content

Commit

Permalink
Merge pull request #89 from DataDog/iddl/user-crud
Browse files Browse the repository at this point in the history
[api] Add user CRUD
  • Loading branch information
yannmh committed Oct 19, 2015
2 parents f41c264 + 5760508 commit 5960876
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
2 changes: 1 addition & 1 deletion datadog/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 14 additions & 2 deletions datadog/api/users.py
Original file line number Diff line number Diff line change
@@ -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.
"""
Expand All @@ -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]

Expand Down
1 change: 1 addition & 0 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 39 additions & 1 deletion tests/integration/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time
import unittest
import requests
import simplejson as json

# 3p
from nose.plugins.attrib import attr
Expand All @@ -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
)
Expand Down Expand Up @@ -897,6 +897,44 @@ def test_revoke_embed(self):
with self.assertRaises(ApiError):
dog.Embed.get(embed_id)

def test_user_crud(self):
handle = '[email protected]'
name = 'Test User'
alternate_name = 'Test User Alt'
alternate_email = '[email protected]'

# 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()

0 comments on commit 5960876

Please sign in to comment.