Skip to content

Commit 2be8675

Browse files
authored
Add new module Cloud Admin (#1033)
* add Cloud Admin module * formatting * add examples * fix comment syntax
1 parent f23320e commit 2be8675

File tree

7 files changed

+96
-7
lines changed

7 files changed

+96
-7
lines changed

atlassian/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
from .bamboo import Bamboo
22
from .bitbucket import Bitbucket
33
from .bitbucket import Bitbucket as Stash
4+
from .cloud_admin import CloudAdminOrgs, CloudAdminUsers
45
from .confluence import Confluence
56
from .crowd import Crowd
7+
from .insight import Insight
68
from .jira import Jira
79
from .marketplace import MarketPlace
810
from .portfolio import Portfolio
911
from .service_desk import ServiceDesk
1012
from .xray import Xray
11-
from .insight import Insight
12-
1313

1414
__all__ = [
1515
"Confluence",
1616
"Jira",
1717
"Bitbucket",
18+
"CloudAdminOrgs",
19+
"CloudAdminUsers",
1820
"Portfolio",
1921
"Bamboo",
2022
"Stash",

atlassian/cloud_admin.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# coding=utf-8
2+
import logging
3+
4+
from .rest_client import AtlassianRestAPI
5+
6+
log = logging.getLogger(__name__)
7+
8+
ADMIN_URL = "https://api.atlassian.com"
9+
10+
11+
class CloudAdminOrgs(AtlassianRestAPI):
12+
def __init__(self, admin_api_key, *args, **kwargs):
13+
kwargs["token"] = admin_api_key
14+
kwargs["api_root"] = "admin"
15+
kwargs["api_version"] = "v1"
16+
super(CloudAdminOrgs, self).__init__(url=ADMIN_URL, *args, **kwargs)
17+
18+
def get_organizations(self):
19+
url = self.resource_url("orgs")
20+
return self.get(url)
21+
22+
23+
class CloudAdminUsers(AtlassianRestAPI):
24+
def __init__(self, admin_api_key, *args, **kwargs):
25+
kwargs["token"] = admin_api_key
26+
kwargs["api_root"] = "users"
27+
kwargs["api_version"] = None
28+
super(CloudAdminUsers, self).__init__(ADMIN_URL, *args, **kwargs)
29+
30+
def get_profile(self, account_id):
31+
url = self.resource_url("{}/manage/profile".format(account_id))
32+
return self.get(url)

atlassian/rest_client.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
import requests
66
from oauthlib.oauth1 import SIGNATURE_RSA
7+
from requests import HTTPError
78
from requests_oauthlib import OAuth1, OAuth2
89
from six.moves.urllib.parse import urlencode
9-
from requests import HTTPError
10+
1011
from atlassian.request_utils import get_default_logger
1112

1213
log = get_default_logger(__name__)
@@ -94,7 +95,7 @@ def _create_token_session(self, token):
9495
self._update_header("Authorization", "Bearer {token}".format(token=token))
9596

9697
def _create_kerberos_session(self, _):
97-
from requests_kerberos import HTTPKerberosAuth, OPTIONAL
98+
from requests_kerberos import OPTIONAL, HTTPKerberosAuth
9899

99100
self._session.auth = HTTPKerberosAuth(mutual_authentication=OPTIONAL)
100101

@@ -392,9 +393,12 @@ def raise_for_status(self, response):
392393
if 400 <= response.status_code < 600:
393394
try:
394395
j = response.json()
395-
error_msg = "\n".join(
396-
j.get("errorMessages", list()) + [k + ": " + v for k, v in j.get("errors", dict()).items()]
397-
)
396+
if self.url == "https://api.atlassian.com":
397+
error_msg = "\n".join([k + ": " + v for k, v in j.items()])
398+
else:
399+
error_msg = "\n".join(
400+
j.get("errorMessages", list()) + [k + ": " + v for k, v in j.get("errors", dict()).items()]
401+
)
398402
except Exception:
399403
response.raise_for_status()
400404
else:

docs/cloud_admin.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Cloud Admin module
2+
==================
3+
4+
CloudAdminOrgs
5+
--------------
6+
7+
.. code-block:: python
8+
9+
# Returns a list of your organizations
10+
cloud_admin_orgs.get_organizations()
11+
12+
13+
CloudAdminUsers
14+
---------------
15+
16+
.. code-block:: python
17+
18+
# Returns information about a single Atlassian account by ID
19+
cloud_admin_users.get_profile(account_id)

docs/index.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,21 @@ And to Bitbucket Cloud:
219219
password=bitbucket_app_password,
220220
cloud=True)
221221
222+
Getting started with Cloud Admin module
223+
---------------------------------------
224+
225+
Add a connection:
226+
227+
.. code-block:: python
228+
229+
from atlassian import CloudAdminOrgs, CloudAdminUsers
230+
231+
cloud_admin_orgs = CloudAdminOrgs(
232+
admin-api-key=admin-api-key)
233+
234+
cloud_admin_users = CloudAdminUsers(
235+
admin-api-key=admin-api-key)
236+
222237
.. toctree::
223238
:maxdepth: 2
224239

@@ -229,6 +244,7 @@ And to Bitbucket Cloud:
229244
bamboo
230245
service_desk
231246
xray
247+
cloud_admin
232248

233249
.. |Build Status| image:: https://github.com/atlassian-api/atlassian-python-api/workflows/Test/badge.svg?branch=master
234250
:target: https://pypi.python.org/pypi/atlassian-python-api
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# coding=utf-8
2+
from atlassian import CloudAdminOrgs
3+
4+
# How to get organizations
5+
6+
cloud_admin_orgs = CloudAdminOrgs(admin_api_key="admin_api_key")
7+
8+
cloud_admin_orgs.get_organizations()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# coding=utf-8
2+
from atlassian import CloudAdminUsers
3+
4+
# How to get user profile
5+
6+
cloud_admin_users = CloudAdminUsers(admin_api_key="admin_api_key")
7+
8+
cloud_admin_users.get_organizations(account_id="account_id")

0 commit comments

Comments
 (0)