Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve cache code #603

Merged
merged 9 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import logging
import copy
import requests
import datetime
from requests.exceptions import Timeout
from werkzeug.middleware.proxy_fix import ProxyFix
from flask_dance.consumer import OAuth2ConsumerBlueprint
Expand Down Expand Up @@ -251,6 +252,10 @@ def home():
flash("Error getting User info: \n" + account_info.text, 'error')
return render_template('home.html', oidc_name=settings.oidcName)

# Force to get the user credentials to cache them
scheduler.add_job(func=utils.get_cache_creds, trigger='date', run_date=datetime.datetime.now(),
misfire_grace_time=20, args=[cred, get_cred_id()], id='get_cache_creds')

# if there are any next url, redirect to it
if "next" in session and session["next"]:
next_url = session.pop("next")
Expand Down Expand Up @@ -497,7 +502,7 @@ def showinfrastructures():
app.logger.exception("Error getting vm info: %s" % ex)
radl_json = []
try:
creds = utils.get_cache_creds(session, cred, get_cred_id())
creds = utils.get_cache_creds(cred, get_cred_id())
except Exception as ex:
app.logger.exception("Error getting user credentials: %s" % ex)
creds = []
Expand Down Expand Up @@ -767,7 +772,7 @@ def configure():
app.logger.debug("Template: " + json.dumps(toscaInfo[selected_tosca]))

try:
creds = utils.get_cache_creds(session, cred, get_cred_id(), 1)
creds = utils.get_cache_creds(cred, get_cred_id(), 1)
except Exception as ex:
flash("Error getting user credentials: %s" % ex, "error")
creds = []
Expand Down Expand Up @@ -1121,7 +1126,7 @@ def manage_creds():
creds = {}

try:
creds = utils.get_cache_creds(session, cred, get_cred_id())
creds = utils.get_cache_creds(cred, get_cred_id())
# Get the project_id in case it has changed
utils.get_project_ids(creds)
except Exception as e:
Expand Down Expand Up @@ -1176,8 +1181,7 @@ def write_creds():
# Get project_id to save it to de DB
utils.get_project_ids([creds])
# delete cached credentials
if 'creds' in session:
del session['creds']
utils.clear_cache_creds(get_cred_id())
cred.write_creds(creds["id"], get_cred_id(), creds, cred_id in [None, ''])
if val_res == 0:
flash("Credentials successfully written!", 'success')
Expand All @@ -1195,8 +1199,7 @@ def delete_creds():
cred_id = request.args.get('cred_id', "")
try:
# delete cached credentials
if 'creds' in session:
del session['creds']
utils.clear_cache_creds(get_cred_id())
cred.delete_cred(cred_id, get_cred_id())
flash("Credentials successfully deleted!", 'success')
except Exception as ex:
Expand All @@ -1215,8 +1218,7 @@ def enable_creds():
if val_res == 2:
flash(val_msg, 'warning')
# delete cached credentials
if 'creds' in session:
del session['creds']
utils.clear_cache_creds(get_cred_id())
cred.enable_cred(cred_id, get_cred_id(), enable)
except Exception as ex:
flash("Error updating credentials %s!" % ex, 'error')
Expand Down
5 changes: 4 additions & 1 deletion app/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import unittest
import json
import defusedxml.ElementTree as etree
from app import create_app
from app import create_app, utils
from urllib.parse import urlparse
from mock import patch, MagicMock

Expand Down Expand Up @@ -234,6 +234,7 @@ def test_settings(self, avatar):
def test_infrastructures(self, avatar, get, user_data, get_creds):
user_data.return_value = "type = InfrastructureManager; token = access_token"
get_creds.return_value = []
utils.CREDS_CACHE = {}
get.side_effect = self.get_response
self.login(avatar)
res = self.client.get('/infrastructures')
Expand Down Expand Up @@ -431,6 +432,7 @@ def test_configure(self, get, get_creds, avatar):
self.assertEqual(200, res.status_code)
self.assertIn(b"Select Optional Features:", res.data)

utils.CREDS_CACHE = {}
get_creds.return_value = [{"id": "credid", "type": "fedcloud", "host": "site_url",
"vo": "voname", "enabled": True},
{"id": "credid1", "type": "OpenStack", "host": "site_url1",
Expand Down Expand Up @@ -569,6 +571,7 @@ def test_manage_creds(self, get_project_ids, get_sites, get_creds, avatar):
get_sites.return_value = {"SITE_NAME": {"url": "URL", "state": "", "id": ""},
"SITE2": {"url": "URL2", "state": "CRITICAL", "id": ""}}
get_creds.return_value = [{"id": "credid", "type": "fedcloud", "host": "site_url", "project_id": "project"}]
utils.CREDS_CACHE = {}
res = self.client.get('/manage_creds')
self.assertEqual(200, res.status_code)
self.assertIn(b'credid', res.data)
Expand Down
16 changes: 12 additions & 4 deletions app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
SITE_LIST = {}
LAST_UPDATE = 0
PORT_SPECT_TYPES = ["PortSpec", "tosca.datatypes.network.PortSpec", "tosca.datatypes.indigo.network.PortSpec"]
CREDS_CACHE = {}


def _getStaticSitesInfo(force=False):
Expand Down Expand Up @@ -984,13 +985,20 @@ def merge_templates(template, new_template):
return template


def get_cache_creds(session, cred, userid, enabled=None):
if "creds" not in session:
session["creds"] = cred.get_creds(userid)
def get_cache_creds(cred, userid, enabled=None):
global CREDS_CACHE
if userid not in CREDS_CACHE:
CREDS_CACHE[userid] = cred.get_creds(userid)

res = []
for cred in session["creds"]:
for cred in CREDS_CACHE[userid]:
if enabled is None or enabled == cred['enabled']:
res.append(cred)

return res


def clear_cache_creds(userid):
global CREDS_CACHE
if userid in CREDS_CACHE:
del CREDS_CACHE[userid]
Loading