Skip to content

Commit b03709f

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

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

IM/REST.py

+30-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import json
2020
import base64
2121
import bottle
22+
import datetime
2223

2324
from IM.InfrastructureInfo import IncorrectVMException, DeletedVMException, IncorrectStateException
2425
from IM.InfrastructureManager import (InfrastructureManager, DeletedInfrastructureException,
@@ -1049,14 +1050,42 @@ def ReturnOptions(**kwargs):
10491050

10501051

10511052
@app.route('/version', method='GET')
1052-
def RESTGeVersion():
1053+
def RESTGetVersion():
10531054
try:
10541055
from IM import __version__ as version
10551056
return format_output(version, field_name="version")
10561057
except Exception as ex:
10571058
return return_error(400, "Error getting IM version: %s" % get_ex_error(ex))
10581059

10591060

1061+
@app.route('/stats', method='GET')
1062+
def RESTGetStats():
1063+
try:
1064+
auth = get_auth_header()
1065+
except Exception:
1066+
return return_error(401, "No authentication data provided")
1067+
1068+
try:
1069+
if "init_date" in bottle.request.params.keys():
1070+
init_date = bottle.request.params.get("init_date").lower()
1071+
init_date = init_date.replace("/", "-")
1072+
parts = init_date.split("-")
1073+
try:
1074+
year = int(parts[0])
1075+
month = int(parts[1])
1076+
day = int(parts[2])
1077+
datetime.date(year, month, day)
1078+
except Exception:
1079+
return return_error(400, "Incorrect format in init_date parameter: YYYY/MM/dd")
1080+
else:
1081+
init_date = "1970-01-01"
1082+
1083+
return InfrastructureManager.GetStats(init_date, auth)
1084+
except Exception as ex:
1085+
logger.exception("Error getting stats")
1086+
return return_error(400, "Error getting stats: %s" % get_ex_error(ex))
1087+
1088+
10601089
@app.route('/infrastructures/:infid/vms/:vmid/disks/:disknum/snapshot', method='PUT')
10611090
def RESTCreateDiskSnapshot(infid=None, vmid=None, disknum=None):
10621091
try:

IM/Stats.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,18 @@ def get_stats(init_date="1970-01-01", auth=None):
8787
- init_date(str): Only will be returned infrastructure created afther this date.
8888
- auth(Authentication): parsed authentication tokens.
8989
90-
Return: a list of dict with the stats.
90+
Return: a list of dict with the stats with the following format:
91+
{'creation_date': '2022-03-07 13:16:14',
92+
'icon': 'kubernetes',
93+
'vm_count': 2,
94+
'cpu_count': 4,
95+
'memory_size': 1024,
96+
'cloud_type': 'OSCAR',
97+
'cloud_host': 'sharp-elbakyan5.im.grycap.net',
98+
'hybrid': False,
99+
'im_user': '__OPENID__mcaballer',
100+
'inf_id': '1',
101+
'last_date': '2022-03-23'}
91102
"""
92103
stats = []
93104
db = DataBase(Config.DATA_DB)

test/unit/REST.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,12 @@
5353
RESTStartVM,
5454
RESTStopVM,
5555
RESTRebootVM,
56-
RESTGeVersion,
56+
RESTGetVersion,
5757
RESTCreateDiskSnapshot,
5858
RESTImportInfrastructure,
5959
RESTGetCloudInfo,
6060
RESTChangeInfrastructureAuth,
61+
RESTGetStats,
6162
return_error,
6263
format_output)
6364

@@ -1074,6 +1075,21 @@ def test_ChangeInfrastructureAuth(self, ChangeInfrastructureAuth, bottle_request
10741075
"password": "new_pass"}])
10751076
self.assertEqual(ChangeInfrastructureAuth.call_args_list[0][0][2], True)
10761077

1078+
@patch("bottle.request")
1079+
@patch("IM.InfrastructureManager.InfrastructureManager.GetStats")
1080+
def test_GetStats(self, GetStats, bottle_request):
1081+
"""Test REST GetStats."""
1082+
bottle_request.return_value = MagicMock()
1083+
bottle_request.headers = {"AUTHORIZATION": "type = InfrastructureManager; username = user; password = pass"}
1084+
GetStats.return_value = [{"stats"}]
1085+
bottle_request.params = {'init_date': '2010-01-01'}
1086+
res = RESTGetStats()
1087+
self.assertEqual(res, [{"stats"}])
1088+
self.assertEqual(GetStats.call_args_list[0][0][0], '2010-01-01')
1089+
self.assertEqual(GetStats.call_args_list[0][0][1].auth_list, [{"type": "InfrastructureManager",
1090+
"username": "user",
1091+
"password": "pass"}])
1092+
10771093

10781094
if __name__ == "__main__":
10791095
unittest.main()

0 commit comments

Comments
 (0)