Commit ba9acaad authored by Ilias Tsitsimpis's avatar Ilias Tsitsimpis

astakosclient: Implement get_endpoints method

parent 07cb2d04
......@@ -57,6 +57,8 @@ API_SERVICE_QUOTAS = "/astakos/api/service_quotas"
API_COMMISSIONS = "/astakos/api/commissions"
API_COMMISSIONS_ACTION = API_COMMISSIONS + "/action"
API_FEEDBACK = "/astakos/api/feedback"
API_TOKENS = "/astakos/api/tokens"
TOKENS_ENDPOINTS = "endpoints"
# --------------------------------------------------------------------
......@@ -339,10 +341,40 @@ class AstakosClient():
"""
check_input("send_feedback", self.logger, message=message, data=data)
path = copy(API_FEEDBACK)
req_headers = {'content-type': 'application/json'}
req_body = urllib.urlencode(
{'feedback_msg': message, 'feedback_data': data})
self._call_astakos(token, path, req_headers, req_body, "POST")
self._call_astakos(token, path, None, req_body, "POST")
# ----------------------------------
# do a GET to ``API_TOKENS``/<user_token>/``TOKENS_ENDPOINTS``
def get_endpoints(self, token, belongs_to=None, marker=None, limit=None):
"""Request registered endpoints from astakos
keyword arguments:
token -- user's token (string)
belongs_to -- user's uuid (string)
marker -- return endpoints whose ID is higher than marker's (int)
limit -- maximum number of endpoints to return (int)
Return a json formatted dictionary containing information
about registered endpoints.
WARNING: This api call encodes the user's token inside the url.
It's thoughs security unsafe to use it (both astakosclient and
nginx tend to log requested urls).
Avoid the use of get_endpoints method and use *** instead.
"""
params = {}
if belongs_to is not None:
params['belongsTo'] = str(belongs_to)
if marker is not None:
params['marker'] = str(marker)
if limit is not None:
params['limit'] = str(limit)
path = API_TOKENS + "/" + token + "/" + \
TOKENS_ENDPOINTS + "?" + urllib.urlencode(params)
return self._call_astakos(token, path)
# ----------------------------------
# do a GET to ``API_QUOTAS``
......
......@@ -90,6 +90,14 @@ def _request_status_404(conn, method, url, **kwargs):
return (message, data, status)
def _request_status_403(conn, method, url, **kwargs):
"""This request returns 403"""
message = "UNAUTHORIZED"
status = 403
data = "Forbidden"
return (message, data, status)
def _request_status_401(conn, method, url, **kwargs):
"""This request returns 401"""
message = "UNAUTHORIZED"
......@@ -118,6 +126,8 @@ def _request_ok(conn, method, url, **kwargs):
return _req_quotas(conn, method, url, **kwargs)
elif url.startswith(astakosclient.API_COMMISSIONS):
return _req_commission(conn, method, url, **kwargs)
elif url.startswith(astakosclient.API_TOKENS):
return _req_endpoints(conn, method, url, **kwargs)
else:
return _request_status_404(conn, method, url, **kwargs)
......@@ -274,6 +284,28 @@ def _req_commission(conn, method, url, **kwargs):
return _request_status_400(conn, method, url, **kwargs)
def _req_endpoints(conn, method, url, **kwargs):
"""Request endpoints"""
global token_1, endpoints
# Check input
if conn.__class__.__name__ != "HTTPSConnection":
return _request_status_302(conn, method, url, **kwargs)
if method != "GET":
return _request_status_400(conn, method, url, **kwargs)
token_head = kwargs['headers'].get('X-Auth-Token')
url_split = url[len(astakosclient.API_TOKENS):].split('/')
token_url = url_split[1]
if token_head != token_url:
return _request_status_403(conn, method, url, **kwargs)
if token_url != token_1:
return _request_status_401(conn, method, url, **kwargs)
# Return
return ("", simplejson.dumps(endpoints), 200)
# ----------------------------
# Mock the actual _doRequest
def _mock_request(new_requests):
......@@ -367,6 +399,26 @@ resources = {
"description": "Virtual machine memory",
"service": "cyclades"}}
endpoints = {
"endpoints": [
{"name": "cyclades",
"region": "cyclades",
"internalURL": "https://node1.example.com/ui/",
"adminURL": "https://node1.example.com/v1/",
"type": None,
"id": 5,
"publicURL": "https://node1.example.com/ui/"},
{"name": "pithos",
"region": "pithos",
"internalURL": "https://node2.example.com/ui/",
"adminURL": "https://node2.example.com/v1",
"type": None,
"id": 6,
"publicURL": "https://node2.example.com/ui/"}],
"endpoint_links": [
{"href": "/astakos/api/tokens/0000/endpoints?marker=4&limit=10000",
"rel": "next"}]}
quotas = {
"system": {
"cyclades.ram": {
......@@ -1080,6 +1132,37 @@ class TestCommissions(unittest.TestCase):
self.assertEqual(result, resolve_commissions_rep)
class TestEndPoints(unittest.TestCase):
"""Test cases for endpoints requests"""
# ----------------------------------
def test_get_endpoints(self):
"""Test function call of get_endpoints"""
global token_1, endpoints
_mock_request([_request_ok])
try:
client = AstakosClient("https://example.com")
response = client.get_endpoints(token_1)
except Exception as err:
self.fail("Shouldn't raise Exception %s" % err)
self.assertEqual(response, endpoints)
# ----------------------------------
def test_get_endpoints_wrong_token(self):
"""Test function call of get_endpoints with wrong token"""
global token_2, endpoints
_mock_request([_request_ok])
try:
client = AstakosClient("https://example.com")
client.get_endpoints(token_2, marker=2, limit=100)
except Unauthorized:
pass
except Exception as err:
self.fail("Shouldn't raise Exception %s" % err)
else:
self.fail("Should have raised Unauthorized Exception")
# ----------------------------
# Run tests
if __name__ == "__main__":
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment