Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Adding teams support #23

Merged
merged 7 commits into from
May 5, 2019
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 .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
sudo pip install pipenv
pipenv install
pipenv install --dev
- run: pipenv run coverage run --source grafana_api -m xmlrunner test.test_grafana -o test-reports
- run: pipenv run coverage run --source grafana_api -m xmlrunner test.test_grafana test.api.test_team -o test-reports
- run: pipenv run codecov
- save_cache:
key: deps9-{{ .Branch }}-{{ checksum "Pipfile.lock" }}
Expand Down
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ install:
- pip install python-coveralls
- pip install coverage
- pip install nose
- pip install requests-mock

# command to run tests
script:
- nosetests -v --with-xunit --with-coverage --cover-xml --cover-package=grafana_api,api test/test_grafana.py
- nosetests -v --with-xunit --with-coverage --cover-xml --cover-package=grafana_api,api test/

after_success:
- coveralls
Expand Down
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ requests = "~=2.21"
codecov = "~=2.0"
coverage = "~=4.5"
unittest-xml-reporting = "~=2.5"
requests-mock = "~=1.5"
22 changes: 15 additions & 7 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ grafana_api = GrafanaFace(auth='abcde....', host='api.my-grafana-host.com')
grafana_api.search.search_dashboards(tag='applications')

# Find a user by email
grafana_api.users.find_user('[email protected]')
user = grafana_api.users.find_user('[email protected]')

# Add user to team 2
grafana_api.teams.add_team_member(2, user["id"])

# Create or update a dashboard
grafana_api.dashboard.update_dashboard(dashboard={'dashboard': {...}, 'folderId': 0, 'overwrite': True})
Expand Down Expand Up @@ -59,7 +62,7 @@ Work on API implementation still in progress.
| Other | + |
| Preferences | + |
| Snapshot | - |
| Teams | - |
| Teams | + |
| User | + |

## Issue tracker
Expand Down
1 change: 1 addition & 0 deletions grafana_api/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
from .organization import Organization, Organizations
from .search import Search
from .user import User, Users
from .team import Teams
157 changes: 157 additions & 0 deletions grafana_api/api/team.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
from .base import Base


class Teams(Base):
def __init__(self, api):
super().__init__(api)
self.api = api

def search_teams(self, query=None, page=None, perpage=None):
"""

:return:
"""
list_of_teams = []
teams_on_page = None
search_teams_path = '/teams/search'
params = []

if query:
params.append('query=%s' % query)

if page:
iterate = False
params.append('page=%s' % page)
else:
iterate = True
params.append('page=%s')
page = 1

if perpage:
params.append('perpage=%s' % perpage)

search_teams_path += '?'
search_teams_path += '&'.join(params)

if iterate:
while True:
teams_on_page = self.api.GET(search_teams_path % page)
list_of_teams += teams_on_page["teams"]
if len(list_of_teams) == teams_on_page["totalCount"]:
break
page += 1
else:
teams_on_page = self.api.GET(search_teams_path)
list_of_teams += teams_on_page["teams"]

return list_of_teams

def get_team_by_name(self, team_name):
"""

:param team_name:
:return:
"""
search_teams_path = '/teams/search'

search_teams_path += '?name=%s' % team_name

teams_on_page = self.api.GET(search_teams_path)
return teams_on_page["teams"]

def get_team(self, team_id):
"""

:param team_id:
:return:
"""
get_team_path = '/teams/%s' % team_id
r = self.api.GET(get_team_path)
return r

def add_team(self, team):
"""

:param team:
:return:
"""
add_team_path = '/teams'
r = self.api.POST(add_team_path, json=team)
return r

def update_team(self, team_id, team):
"""

:param team_id:
:param team:
:return:
"""
update_team_path = '/teams/%s' % team_id
r = self.api.PUT(update_team_path, json=team)
return r

def delete_team(self, team_id):
"""

:param team_id:
:return:
"""
delete_team_path = '/teams/%s' % team_id
r = self.api.DELETE(delete_team_path)
return True

def get_team_members(self, team_id):
"""

:param team_id:
:return:
"""
get_team_members_path = '/teams/%s/members' % team_id
r = self.api.GET(get_team_members_path)
return r

def add_team_member(self, team_id, user_id):
"""

:param team_id:
:param user_id:
:return:
"""
add_team_member_path = '/teams/%s/members' % team_id
payload = {
"userId": user_id
}
r = self.api.POST(add_team_member_path, json=payload)
return r

def remove_team_member(self, team_id, user_id):
"""

:param team_id:
:param user_id:
:return:
"""
remove_team_member_path = '/teams/%s/members/%s' % (team_id, user_id)
r = self.api.DELETE(remove_team_member_path)
return r

def get_team_preferences(self, team_id):
"""

:param team_id:
:return:
"""
get_team_preferences_path = '/teams/%s/preferences' % team_id
r = self.api.GET(get_team_preferences_path)
return r

def update_team_preferences(self, team_id, preferences):
"""

:param team_id:
:param preferences:
:return:
"""
update_team_preferences_path = '/teams/%s/preferences' % team_id
r = self.api.PUT(update_team_preferences_path, json=preferences)
return r
3 changes: 2 additions & 1 deletion grafana_api/grafana_face.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .grafana_api import GrafanaAPI
from .api import (Admin, Dashboard, Datasource, Folder, Organization, Organizations, Search, User, Users)
from .api import (Admin, Dashboard, Datasource, Folder, Organization, Organizations, Search, User, Users, Teams)


class GrafanaFace:
Expand All @@ -14,3 +14,4 @@ def __init__(self, auth, host="localhost", port=None, url_path_prefix="", protoc
self.search = Search(self.api)
self.user = User(self.api)
self.users = Users(self.api)
self.teams = Teams(self.api)
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def get_version():
'requests',
'pyyaml',
],
tests_require=[
'requests-mock',
],
classifiers=[
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
Expand Down
Empty file added test/api/__init__.py
Empty file.
Loading