Skip to content

Commit

Permalink
Merge pull request #608 from grycap/egi_brand_v2
Browse files Browse the repository at this point in the history
Egi brand v2
  • Loading branch information
micafer authored Dec 17, 2024
2 parents 93586a3 + b06b198 commit b515ae7
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 75 deletions.
37 changes: 14 additions & 23 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
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 @@ -246,10 +245,6 @@ 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, session['userid'], 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 @@ -496,7 +491,7 @@ def showinfrastructures():
app.logger.exception("Error getting vm info: %s" % ex)
radl_json = []
try:
creds = utils.get_cache_creds(cred, session['userid'], get_cred_id())
creds = cred.get_creds(get_cred_id())
except Exception as ex:
app.logger.exception("Error getting user credentials: %s" % ex)
creds = []
Expand Down Expand Up @@ -765,13 +760,6 @@ def configure():
else:
app.logger.debug("Template: " + json.dumps(toscaInfo[selected_tosca]))

try:
creds = utils.get_cache_creds(cred, session['userid'], get_cred_id(), 1)
except Exception as ex:
flash("Error getting user credentials: %s" % ex, "error")
creds = []
utils.get_project_ids(creds)

# Enable to get input values from URL parameters
for input_name, input_value in selected_template["inputs"].items():
value = request.args.get(input_name, None)
Expand All @@ -788,7 +776,7 @@ def configure():
return render_template('createdep.html',
template=selected_template,
selectedTemplate=selected_tosca,
creds=creds, input_values=inputs,
input_values=inputs,
infra_name=infra_name, child_templates=child_templates,
vos=utils.getVOs(session), utils=utils)

Expand Down Expand Up @@ -1120,14 +1108,23 @@ def manage_creds():
creds = {}

try:
creds = utils.get_cache_creds(cred, session['userid'], get_cred_id())
creds = cred.get_creds(get_cred_id())
# Get the project_id in case it has changed
utils.get_project_ids(creds)
except Exception as e:
flash("Error retrieving credentials: \n" + str(e), 'warning')

return render_template('service_creds.html', creds=creds,
vault=(settings.vault_url and settings.enable_external_vault))
if request.args.get('json', 0):
json_creds = json.dumps(creds)
to_delete = ['password', 'token', 'proxy', 'private_key', 'client_id', 'secret']
for elem in json_creds:
for key in to_delete:
if key in elem:
del elem[key]
return json_creds
else:
return render_template('service_creds.html', creds=creds,
vault=(settings.vault_url and settings.enable_external_vault))

@app.route('/write_creds', methods=['GET', 'POST'])
@authorized_with_valid_token
Expand Down Expand Up @@ -1174,8 +1171,6 @@ def write_creds():
if val_res != 1:
# Get project_id to save it to de DB
utils.get_project_ids([creds])
# delete cached credentials
utils.clear_cache_creds(session['userid'])
cred.write_creds(creds["id"], get_cred_id(), creds, cred_id in [None, ''])
if val_res == 0:
flash("Credentials successfully written!", 'success')
Expand All @@ -1192,8 +1187,6 @@ def delete_creds():

cred_id = request.args.get('cred_id', "")
try:
# delete cached credentials
utils.clear_cache_creds(session['userid'])
cred.delete_cred(cred_id, get_cred_id())
flash("Credentials successfully deleted!", 'success')
except Exception as ex:
Expand All @@ -1211,8 +1204,6 @@ def enable_creds():
val_res, val_msg = cred.validate_cred(get_cred_id(), cred_id)
if val_res == 2:
flash(val_msg, 'warning')
# delete cached credentials
utils.clear_cache_creds(session['userid'])
cred.enable_cred(cred_id, get_cred_id(), enable)
except Exception as ex:
flash("Error updating credentials %s!" % ex, 'error')
Expand Down
19 changes: 5 additions & 14 deletions app/templates/advanced_config.html
Original file line number Diff line number Diff line change
Expand Up @@ -247,23 +247,14 @@ <h5>Cloud Provider:</h5>

<div>
<label>Select Cloud Provider:</label>
{% if not creds %}
<label>No Cloud Provider defined. Add it in the Cloud Credentials page: <a class="btn btn-success text-white" href="{{ url_for('manage_creds') }}">Add</a></label>
{% else %}
<div id="noCloud" style="display: none;">
<label>No Cloud Provider defined. Add it in the Cloud Credentials page: <a class="btn btn-success text-white" href="{{ url_for('manage_creds') }}">Add</a></label>
</div>
<div id="cloudSelect">
<select class="js-example-basic-single js-states form-control" id="selectedCred" name="extra_opts.selectedCred" onchange="changedCred();">
<option name="selectedCred" value=""> - Select Cloud site - </option>
{% for cred in creds %}
{% if cred[type] not in ["InfrastructureManager"] %}
<option data-tenant-id="{{cred['tenant_id'] if 'tenant_id' in cred else '' }}" data-type="{{ cred['type'] }}" name="selectedCred" value={{ cred['id'] }}>
{{ cred['id'] }}
{%- if cred['type'] == 'fedcloud' and ((session['vos'] and cred['vo'] not in session['vos']) or 'project_id' not in cred) %}
(Warn)
{%- endif %}
</option>
{% endif %}
{% endfor %}
</select>
{% endif %}
</div>
</div>

<div id="cloudImages" style="display: none;">
Expand Down
10 changes: 5 additions & 5 deletions app/templates/config_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
{% for tab in tabs %}
{% set tab_id = tab.replace(" ", "_") %}
{% if loop.first %}
<li class="nav-item"><a class="nav-link {% if creds %}active{%endif%}" data-id="#{{tab_id}}" data-bs-toggle="tab" href=#{{tab_id}}>{{tab}}</a></li>
<li class="nav-item"><a class="nav-link active" data-id="#{{tab_id}}" data-bs-toggle="tab" href=#{{tab_id}}>{{tab}}</a></li>
{% else %}
<li class="nav-item"><a class="nav-link" data-id="#{{tab_id}}" data-bs-toggle="tab" href=#{{tab_id}}>{{tab}}</a></li>
{% endif %}
{% endfor %}
{% else %}
<li class="nav-item"><a class="nav-link" data-id="#InputValues" data-bs-toggle="tab" href=#InputValues>Input Values</a></li>
<li class="nav-item"><a class="nav-link active" data-id="#InputValues" data-bs-toggle="tab" href=#InputValues>Input Values</a></li>
{% endif %}
<li class="nav-item"><a class="nav-link {% if not creds %} active{%endif%}" data-id="#Advanced" data-bs-toggle="tab" href=#Advanced>Cloud Provider</a></li> <!-- always create advanced tab -->
<li class="nav-item"><a class="nav-link" id="tabAdvanced" data-id="#Advanced" data-bs-toggle="tab" href=#Advanced>Cloud Provider</a></li> <!-- always create advanced tab -->
</ul>
<!-- end tab creation section -->

Expand All @@ -24,7 +24,7 @@
{% for tab in tabs %}
{% set tab_id = tab.replace(" ", "_") %}
{% if loop.first %}
<div id="{{tab_id}}" class="tab-pane fade {% if creds %}show active{%endif%}">
<div id="{{tab_id}}" class="tab-pane fade show active">
{% else %}
<div id="{{tab_id}}" class="tab-pane fade">
{% endif %}
Expand All @@ -49,7 +49,7 @@

<!-- scheduling -->
<br>
<div id="Advanced" class="tab-pane fade {% if not creds %}show active{%endif%}">
<div id="Advanced" class="tab-pane fade">
{% include 'advanced_config.html' %}
</div>
</div>
Expand Down
28 changes: 28 additions & 0 deletions app/templates/createdep.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,33 @@ <h4 class="font-weight-bold text-primary">
}
});
});

// Load cloud sites
$(document).ready(function () {
$('#selectedCred').empty();
$('#selectedCred').append("<option value=''>Loading Cloud Sites ...</option>");
$.getJSON("{{url_for('manage_creds')}}?json=1", function( data ) {
if (data.length == 0) {
$('#selectedCred').empty();
$('#noCloud').show();
$('#cloudSelect').hide();
$('#tabAdvanced').tab('show');
} else {
$('#noCloud').hide();
$('#cloudSelect').show();
$('#selectedCred').empty();
$('#selectedCred').append("<option value=''> - Select Cloud site - </option>");
for (var i = 0; i < data.length; i++) {
var option = "<option name='selectedCred' value='" + data[i]["id"] + "'";
if ("tenant_id" in data[i]) {
option = option + " data-tenant-id='" + data[i]["tenant_id"] + "'";
}
option = option + " data-type='" + data[i]["type"] + "'";
option = option + ">" + data[i]["id"] + "</option>"
$('#selectedCred').append(option);
}
}
});
});
</script>
{% endblock %}
8 changes: 4 additions & 4 deletions app/templates/default_form.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<!-- start tabs creation section -->
<ul class="nav nav-tabs">
<li class="nav-item"><a class="nav-link {% if creds %}active{%endif%}" data-id="#InputValues" data-bs-toggle="tab" href="#InputValues">Input Values</a></li>
<li class="nav-item"><a class="nav-link {% if not creds %}active{%endif%}" data-id="#Advanced" data-bs-toggle="tab" href="#Advanced">Cloud Provider</a></li>
<li class="nav-item"><a class="nav-link active" data-id="#InputValues" data-bs-toggle="tab" href="#InputValues">Input Values</a></li>
<li class="nav-item"><a class="nav-link" id="tabAdvanced" data-id="#Advanced" data-bs-toggle="tab" href="#Advanced">Cloud Provider</a></li>
</ul>
<!-- end tab creation section -->
<div class="tab-content">
{% set inputs=template['inputs'] %}
  <!-- inputs -->
<div class="tab-pane fade {% if creds %}show active{%endif%}" id=InputValues>
<div class="tab-pane fade show active" id=InputValues>
{% if inputs|length > 0 %}
{% for key, value in inputs.items() %}
{% set input_value = input_values[key] | default(value.default, true) %}
Expand Down Expand Up @@ -44,7 +44,7 @@ <h2>No input value to submit.</h2>
<!-- end inputs -->

<!-- scheduling -->
<div class="tab-pane fade {% if not creds %}show active{%endif%}" id="Advanced">
<div class="tab-pane fade" id="Advanced">
{% include 'advanced_config.html' %}
</div>
</div>
Expand Down
15 changes: 6 additions & 9 deletions 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, utils
from app import create_app
from urllib.parse import urlparse
from mock import patch, MagicMock

Expand Down Expand Up @@ -431,20 +431,13 @@ 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",
"tenant_id": "tenid", "enabled": True}]
res = self.client.get('/configure?selected_tosca=simple-node-disk.yml&childs=users.yml')
self.assertEqual(200, res.status_code)
self.assertIn(b"Deploy a compute node getting the IP and SSH credentials to access via ssh", res.data)
self.assertIn(b'<option data-tenant-id="" data-type="fedcloud" name="selectedCred" '
b'value=credid>\n credid\n (Warn)\n'
b' </option>', res.data)
self.assertIn(b'<option data-tenant-id="tenid" data-type="OpenStack" '
b'name="selectedCred" value=credid1>\n credid1\n'
b' </option>', res.data)

res = self.client.get('/configure?selected_tosca=simple-node-disk.yml&inf_id=infid')
self.assertEqual(200, res.status_code)
Expand Down Expand Up @@ -570,13 +563,17 @@ 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)
self.assertIn(b'site_url', res.data)
self.assertIn(b'fedcloudRow.png', res.data)

res = self.client.get('/manage_creds?json=1')
self.assertEqual(200, res.status_code)
self.assertEqual(json.loads(res.data), [{"id": "credid", "type": "fedcloud", "host": "site_url",
"project_id": "project"}])

@patch("app.utils.avatar")
@patch("app.db_cred.DBCredentials.get_cred")
@patch("app.db_cred.DBCredentials.write_creds")
Expand Down
20 changes: 0 additions & 20 deletions app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
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 @@ -983,22 +982,3 @@ def merge_templates(template, new_template):
template["metadata"]["tabs"].update(tabs)

return template


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

res = []
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]

0 comments on commit b515ae7

Please sign in to comment.