Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api] Add user CRUD #89

Merged
merged 1 commit into from
Oct 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = '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()