Skip to content

Commit ced555a

Browse files
committed
Implements: #1353
1 parent b03709f commit ced555a

File tree

9 files changed

+132
-8
lines changed

9 files changed

+132
-8
lines changed

IM/REST.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,9 @@ def RESTGetStats():
10801080
else:
10811081
init_date = "1970-01-01"
10821082

1083-
return InfrastructureManager.GetStats(init_date, auth)
1083+
stats = InfrastructureManager.GetStats(init_date, auth)
1084+
bottle.response.content_type = "application/json"
1085+
return format_output(stats, default_type="application/json", field_name="stats")
10841086
except Exception as ex:
10851087
logger.exception("Error getting stats")
10861088
return return_error(400, "Error getting stats: %s" % get_ex_error(ex))

IM/ServiceRequests.py

+14
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class IMBaseRequest(AsyncRequest):
5858
GET_CLOUD_IMAGE_LIST = "GetCloudImageList"
5959
GET_CLOUD_QUOTAS = "GetCloudQuotas"
6060
CHANGE_INFRASTRUCTURE_AUTH = "ChangeInfrastructureAuth"
61+
GET_STATS = "GetStats"
6162

6263
@staticmethod
6364
def create_request(function, arguments=()):
@@ -113,6 +114,8 @@ def create_request(function, arguments=()):
113114
return Request_GetCloudQuotas(arguments)
114115
elif function == IMBaseRequest.CHANGE_INFRASTRUCTURE_AUTH:
115116
return Request_ChangeInfrastructureAuth(arguments)
117+
elif function == IMBaseRequest.GET_STATS:
118+
return Request_GetStats(arguments)
116119
else:
117120
raise NotImplementedError("Function not Implemented")
118121

@@ -445,3 +448,14 @@ def _call_function(self):
445448
Authentication(new_auth),
446449
overwrite,
447450
Authentication(auth_data))
451+
452+
453+
class Request_GetStats(IMBaseRequest):
454+
"""
455+
Request class for the GetStats function
456+
"""
457+
458+
def _call_function(self):
459+
self._error_mesage = "Error getting stats"
460+
(init_date, auth_data) = self.arguments
461+
return IM.InfrastructureManager.InfrastructureManager.GetStats(init_date, Authentication(auth_data))

IM/Stats.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ def _get_data(str_data, auth=None):
4141
resp = {'creation_date': None}
4242
if 'creation_date' in dic and dic['creation_date']:
4343
resp['creation_date'] = str(datetime.datetime.fromtimestamp(float(dic['creation_date'])))
44-
resp['icon'] = None
44+
resp['tosca_name'] = None
4545
if 'extra_info' in dic and dic['extra_info'] and "TOSCA" in dic['extra_info']:
4646
try:
4747
tosca = yaml.safe_load(dic['extra_info']['TOSCA'])
4848
icon = tosca.get("metadata", {}).get("icon", "")
49-
resp['icon'] = os.path.basename(icon)[:-4]
49+
resp['tosca_name'] = os.path.basename(icon)[:-4]
5050
except Exception:
5151
Stats.logger.exception("Error loading TOSCA.")
5252

@@ -89,7 +89,7 @@ def get_stats(init_date="1970-01-01", auth=None):
8989
9090
Return: a list of dict with the stats with the following format:
9191
{'creation_date': '2022-03-07 13:16:14',
92-
'icon': 'kubernetes',
92+
'tosca_name': 'kubernetes',
9393
'vm_count': 2,
9494
'cpu_count': 4,
9595
'memory_size': 1024,

doc/source/REST.rst

+29-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Next tables summaries the resources and the HTTP methods available.
5050
+-------------+------------------------------------------------------+------------------------------------------------------+
5151
| **POST** | | | **Modify** the specified property ``property_name``|
5252
| | | | associated to the infrastructure ``infId``. |
53-
| | | | only ``authorization`` property is valid. |
53+
| | | | only ``authorization`` property is valid. |
5454
+-------------+------------------------------------------------------+------------------------------------------------------+
5555

5656

@@ -421,7 +421,7 @@ GET ``http://imserver.com/clouds/<cloudId>/images``
421421
The id ``cloudId`` is relative to the id field in the AUTHORIZATION header.
422422
The result is JSON format has the following format::
423423

424-
{
424+
{
425425
"images":
426426
[
427427
{
@@ -433,7 +433,7 @@ GET ``http://imserver.com/clouds/<cloudId>/images``
433433
"name" : "Image Name2"
434434
}
435435
]
436-
}
436+
}
437437

438438
GET ``http://imserver.com/clouds/<cloudId>/quotas``
439439
:Response Content-type: application/json
@@ -454,3 +454,29 @@ GET ``http://imserver.com/clouds/<cloudId>/quotas``
454454
"security_groups": {"used": 1, "limit": 10}
455455
}
456456
}
457+
458+
GET ``http://imserver.com/stats``
459+
:Response Content-type: application/json
460+
:ok response: 200 OK
461+
:input fields: ``init_date`` (optional)
462+
:fail response: 401, 400
463+
464+
Return the stats of the current user in the IM service.
465+
Return all the infrastructures deployed by the user showing some
466+
aggregated information. In JSON format::
467+
468+
{
469+
"stats": [
470+
{"creation_date": "2022-03-07 13:16:14",
471+
"tosca_name": "kubernetes",
472+
"vm_count": 2,
473+
"cpu_count": 4,
474+
"memory_size": 1024,
475+
"cloud_type": "OSCAR",
476+
"cloud_host": "sharp-elbakyan5.im.grycap.net",
477+
"hybrid": false,
478+
"im_user": "__OPENID__mcaballer",
479+
"inf_id": "1",
480+
"last_date": "2022-03-23"}
481+
]
482+
}

doc/source/xmlrpc.rst

+32
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,35 @@ This is the list of method names:
406406
Change the authentication data of the infrastructure with ID ``infId``. using
407407
the ``newAuth`` provider. If ``overwrite`` is true, the authentication data will
408408
be overwrited otherwise it will be appended.
409+
410+
``GetVersion``
411+
:ok response: [true, string]
412+
:fail response: [false, ``error``: string]
413+
414+
Return the version of the IM service.
415+
416+
.. _GetStat-xmlrpc:
417+
418+
``GetStat``
419+
:parameter 0: ``init_date``: string
420+
:parameter 1: ``auth``: array of structs
421+
:ok response: [true, list of dicts]
422+
:fail response: [false, ``error``: string]
423+
424+
Return the stats of the current user in the IM service.
425+
Return all the infrastructures since ``init_date`` deployed by the user showing some
426+
aggregated information. In JSON format::
427+
428+
[
429+
{"creation_date": "2022-03-07 13:16:14",
430+
"tosca_name": "kubernetes",
431+
"vm_count": 2,
432+
"cpu_count": 4,
433+
"memory_size": 1024,
434+
"cloud_type": "OSCAR",
435+
"cloud_host": "sharp-elbakyan5.im.grycap.net",
436+
"hybrid": false,
437+
"im_user": "__OPENID__mcaballer",
438+
"inf_id": "1",
439+
"last_date": "2022-03-23"}
440+
]

doc/swagger_api.yaml

+36
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ tags:
1515
description: Manages Virtual Infrastructures.
1616
- name: version
1717
description: Get IM server version.
18+
- name: stats
19+
description: Get IM server stats.
1820
- name: clouds
1921
description: Get cloud information.
2022

@@ -38,6 +40,40 @@ paths:
3840
'400':
3941
description: Invalid status value
4042

43+
/stats:
44+
get:
45+
tags:
46+
- stats
47+
summary: Get IM server stats.
48+
description: >-
49+
Return the stats of the current user in the IM service.
50+
Return all the infrastructures since the init_date parameter deployed
51+
by the user showing some aggregated information.
52+
operationId: GetStats
53+
responses:
54+
'200':
55+
description: successful operation
56+
content:
57+
application/json:
58+
examples:
59+
response:
60+
value:
61+
- creation_date: '2022-03-07 13:16:14'
62+
tosca_name: 'kubernetes'
63+
vm_count: 2
64+
cpu_count: 4
65+
memory_size: 1024
66+
cloud_type: 'OSCAR'
67+
cloud_host: 'sharp-elbakyan5.im.grycap.net'
68+
hybrid: false
69+
im_user: '__OPENID__mcaballer'
70+
inf_id: '1'
71+
last_date: '2022-03-23'
72+
'400':
73+
description: Invalid status value
74+
'401':
75+
description: Unauthorized
76+
4177
/infrastructures:
4278
get:
4379
tags:

im_service.py

+7
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@ def ChangeInfrastructureAuth(inf_id, new_auth_data, overwrite, auth_data):
220220
return WaitRequest(request)
221221

222222

223+
def GetStats(init_date, auth_data):
224+
request = IMBaseRequest.create_request(
225+
IMBaseRequest.GET_STATS, (init_date, auth_data))
226+
return WaitRequest(request)
227+
228+
223229
def launch_daemon():
224230
"""
225231
Launch the IM daemon
@@ -266,6 +272,7 @@ def launch_daemon():
266272
server.register_function(GetCloudImageList)
267273
server.register_function(GetCloudQuotas)
268274
server.register_function(ChangeInfrastructureAuth)
275+
server.register_function(GetStats)
269276

270277
InfrastructureManager.logger.info(
271278
'************ Start Infrastructure Manager daemon (v.%s) ************' % version)

test/unit/ServiceRequests.py

+7
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ def test_change_auth(self, inflist):
200200
IM.ServiceRequests.IMBaseRequest.CHANGE_INFRASTRUCTURE_AUTH, ("", "", False, ""))
201201
req._call_function()
202202

203+
@patch('IM.InfrastructureManager.InfrastructureManager')
204+
def test_get_stats(self, inflist):
205+
import IM.ServiceRequests
206+
req = IM.ServiceRequests.IMBaseRequest.create_request(
207+
IM.ServiceRequests.IMBaseRequest.GET_STATS, ("", ""))
208+
req._call_function()
209+
203210

204211
if __name__ == '__main__':
205212
unittest.main()

test/unit/test_im_logic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@ def test_get_stats(self, check_auth_data, DataBase):
14321432

14331433
stats = IM.GetStats('2001-01-01', auth)
14341434
expected_res = [{'creation_date': '2022-03-07 13:16:14',
1435-
'icon': 'kubernetes',
1435+
'tosca_name': 'kubernetes',
14361436
'vm_count': 2,
14371437
'cpu_count': 4,
14381438
'memory_size': 1024,

0 commit comments

Comments
 (0)