Skip to content

Commit 6e6ecf0

Browse files
committed
Implements: #1353
1 parent 9e96f26 commit 6e6ecf0

File tree

3 files changed

+49
-65
lines changed

3 files changed

+49
-65
lines changed

IM/InfrastructureManager.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1746,8 +1746,7 @@ def GetStats(date, auth):
17461746
"""
17471747
# First check the auth data
17481748
auth = InfrastructureManager.check_auth_data(auth)
1749-
im_auth = auth.getAuthInfo("InfrastructureManager")[0]
1750-
stats = Stats.get_stats(date, im_auth)
1749+
stats = Stats.get_stats(date, auth)
17511750
if not stats:
17521751
raise Exception("ERROR connecting with the database!.")
17531752
else:

IM/Stats.py

+12-14
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
from IM.db import DataBase
2424
from IM.auth import Authentication
2525
from IM.config import Config
26-
from IM.VirtualMachine import VirtualMachine
27-
26+
from radl.radl_parse import parse_radl
2827

2928
class Stats():
3029

@@ -34,12 +33,14 @@ class Stats():
3433
@staticmethod
3534
def _get_data(str_data, auth=None):
3635
dic = json.loads(str_data)
36+
inf_auth = Authentication(dic['auth'])
37+
if auth is not None and not inf_auth.compare(auth, 'InfrastructureManager'):
38+
return None
39+
3740
resp = {'creation_date': None}
3841
if 'creation_date' in dic and dic['creation_date']:
3942
resp['creation_date'] = str(datetime.datetime.fromtimestamp(float(dic['creation_date'])))
40-
auth = Authentication.deserialize(dic['auth'])
4143
resp['icon'] = None
42-
im_auth = auth.getAuthInfo("InfrastructureManager")[0]
4344
if 'extra_info' in dic and dic['extra_info'] and "TOSCA" in dic['extra_info']:
4445
try:
4546
tosca = yaml.safe_load(dic['extra_info']['TOSCA'])
@@ -55,28 +56,25 @@ def _get_data(str_data, auth=None):
5556
resp['cloud_host'] = None
5657
resp['hybrid'] = False
5758
for vm_data in dic['vm_list']:
58-
vm = VirtualMachine.deserialize(vm_data)
5959

6060
# only get the cloud of the first VM
6161
if not resp['cloud_type']:
62-
resp['cloud_type'] = vm.cloud.type
62+
resp['cloud_type'] = vm_data["cloud"]["type"]
6363
if not resp['cloud_host']:
64-
resp['cloud_host'] = vm.cloud.get_url()
65-
elif resp['cloud_host'] != vm.cloud.get_url():
64+
resp['cloud_host'] = vm_data["cloud"]["server"]
65+
elif resp['cloud_host'] != vm_data["cloud"]["server"]:
6666
resp['hybrid'] = True
6767

68-
vm_sys = vm.info.systems[0]
68+
vm_sys = parse_radl(vm_data['info']).systems[0]
6969
if vm_sys.getValue('cpu.count'):
7070
resp['cpu_count'] += vm_sys.getValue('cpu.count')
7171
if vm_sys.getValue('memory.size'):
7272
resp['memory_size'] += vm_sys.getFeature('memory.size').getValue('M')
7373
resp['vm_count'] += 1
7474

75-
if auth is None or im_auth.compare(auth):
76-
resp['im_user'] = im_auth.get('username', "")
77-
return resp
78-
else:
79-
return None
75+
im_auth = inf_auth.getAuthInfo("InfrastructureManager")[0]
76+
resp['im_user'] = im_auth.get('username', "")
77+
return resp
8078

8179
@staticmethod
8280
def get_stats(init_date="1970-01-01", auth=None):

test/unit/test_im_logic.py

+36-49
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import sys
2424
import json
2525
import base64
26+
import yaml
2627

2728
from mock import Mock, patch, MagicMock
2829

@@ -1390,72 +1391,58 @@ def test_change_inf_auth(self):
13901391
" provided for the InfrastructureManager."))
13911392

13921393
@patch('IM.Stats.DataBase')
1393-
def test_get_stats(self, DataBase):
1394+
@patch('IM.InfrastructureManager.InfrastructureManager.check_auth_data')
1395+
def test_get_stats(self, check_auth_data, DataBase):
13941396

13951397
radl = """
1396-
network public ( outbound = 'yes' )
1397-
system plants (
1398-
name = 'plants' and
1399-
memory.size = 488M and
1400-
script = '#!/bin/bash
1401-
echo "Hola"
1402-
' and
1403-
cpu.count = 0.5 and
1404-
disk.0.image.url = 'oscar://sharp-elbakyan5.im.grycap.net/grycap/image' and
1405-
input.0.provider = 'minio.default' and
1406-
input.0.path = 'input' and
1407-
output.0.provider = 'minio.default' and
1408-
output.0.path = 'output' and
1409-
alpine = 0 and
1410-
net_interface.0.connection = 'public' and
1411-
state = 'configured' and
1412-
provider.type = 'OSCAR' and
1413-
instance_id = 'plants' and
1414-
net_interface.0.dns_name = 'vnode-0' and
1415-
token = 'ebfb145ab57a35520669fc89bc115edab1b231a5c8f92e895ecaee256f841bac'
1416-
)
1417-
1418-
configure plants ()
1419-
contextualize (
1420-
system plants configure plants step 1
1421-
)
1422-
deploy plants 1
1398+
system node (
1399+
memory.size = 512M and
1400+
cpu.count = 2
1401+
)"""
14231402

1424-
"""
1403+
auth = Authentication([{'type': 'InfrastructureManager', 'token': 'atoken',
1404+
'username': '__OPENID__mcaballer', 'pass': 'pass'}])
1405+
check_auth_data.return_value = auth
14251406

14261407
db = MagicMock()
14271408
inf_data = {
14281409
"id": "1",
1429-
"auth": [
1430-
{"password": "https://aai.egi.eu/oidc/a67fff9c06e62401975aeb29ad35b8faf7c4fc74b4526a5504e5132b01496831@egi.eu",
1431-
"type": "InfrastructureManager",
1432-
"username": "__OPENID__mcaballer"
1433-
}
1434-
],
1410+
"auth": auth.auth_list,
1411+
"creation_date": 1646655374,
1412+
"extra_info": {"TOSCA": yaml.dump({"metadata": {"icon": "kubernetes.png"}})},
14351413
"vm_list": [
1436-
{"last_update": 1646656730,
1437-
"destroy": True,
1438-
"state": "deleting",
1439-
"id": "plants",
1440-
"im_id": 0,
1414+
{
14411415
"cloud": {
1442-
"id": "oscar",
14431416
"type": "OSCAR",
14441417
"server": "sharp-elbakyan5.im.grycap.net",
1445-
"protocol": "https"
14461418
},
1447-
"info": radl,
1448-
"cont_out": "",
1449-
"configured": True,
1450-
"creation_date": 1646655378
1419+
"info": radl
14511420
},
1452-
"extra_info": {"TOSCA": {"metadata": {"icon": "kubernetes.png"}}}
1421+
{
1422+
"cloud": {
1423+
"type": "OSCAR",
1424+
"server": "sharp-elbakyan5.im.grycap.net",
1425+
},
1426+
"info": radl
1427+
}
14531428
]
14541429
}
1455-
db.select.return_value = [(inf_data, '2022-03-23', '1')]
1430+
db.select.return_value = [(json.dumps(inf_data).encode(), '2022-03-23', '1')]
14561431
DataBase.return_value = db
14571432

1458-
IM.GetStats()
1433+
stats = IM.GetStats('2001-01-01', auth)
1434+
expected_res = [{'creation_date': '2022-03-07 13:16:14',
1435+
'icon': 'kubernetes',
1436+
'vm_count': 2,
1437+
'cpu_count': 4,
1438+
'memory_size': 1024,
1439+
'cloud_type': 'OSCAR',
1440+
'cloud_host': 'sharp-elbakyan5.im.grycap.net',
1441+
'hybrid': False,
1442+
'im_user': '__OPENID__mcaballer',
1443+
'inf_id': '1',
1444+
'last_date': '2022-03-23'}]
1445+
self.assertEqual(stats, expected_res)
14591446

14601447

14611448
if __name__ == "__main__":

0 commit comments

Comments
 (0)