Skip to content

Commit

Permalink
Fix bugs due to legacy code (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
maricaantonacci authored Jan 15, 2024
1 parent d960f09 commit 0d2e50a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 93 deletions.
8 changes: 1 addition & 7 deletions app/deployments/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
url_for,
)
from flask import current_app as app
from packaging import version
from werkzeug.exceptions import Forbidden
from werkzeug.utils import secure_filename

Expand Down Expand Up @@ -604,12 +603,7 @@ def remove_sla_from_template(template):
def add_sla_to_template(template, sla_id):
# Add or replace the placement policy

if version.parse(utils.getorchestratorversion(app.settings.orchestrator_url)) >= version.parse(
"2.2.0-SNAPSHOT"
):
tosca_sla_placement_type = "tosca.policies.indigo.SlaPlacement"
else:
tosca_sla_placement_type = "tosca.policies.Placement"
tosca_sla_placement_type = "tosca.policies.indigo.SlaPlacement"
template["topology_template"]["policies"] = [
{
"deploy_on_specific_site": {
Expand Down
86 changes: 44 additions & 42 deletions app/home/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,26 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import re
import json
import re
from datetime import datetime

from flask import (
Blueprint,
flash,
make_response,
redirect,
render_template,
request,
session,
url_for,
)
from flask import current_app as app
from markupsafe import Markup
from flask import current_app as app, Blueprint, render_template, request
from flask import redirect, url_for, session, make_response, flash

from app.extensions import redis_client, tosca
from app.iam import iam
from app.extensions import tosca, redis_client
from app.lib import utils, auth, dbhelpers, openstack
from app.lib import auth, dbhelpers, openstack, utils
from app.models.User import User

home_bp = Blueprint(
Expand Down Expand Up @@ -71,9 +81,7 @@ def submit_settings():
deploy_token = request.form.get("tosca_templates_token")

serialised_value = redis_client.get("last_configuration_info")
dashboard_configuration_info = (
json.loads(serialised_value) if serialised_value else {}
)
dashboard_configuration_info = json.loads(serialised_value) if serialised_value else {}

if repo_url:
app.logger.debug("Cloning TOSCA templates")
Expand All @@ -91,9 +99,7 @@ def submit_settings():
if repo_url:
dashboard_configuration_info["tosca_templates_url"] = repo_url
if tag_or_branch:
dashboard_configuration_info[
"tosca_templates_tag_or_branch"
] = tag_or_branch
dashboard_configuration_info["tosca_templates_tag_or_branch"] = tag_or_branch

repo_url = request.form.get("dashboard_configuration_url")
tag_or_branch = request.form.get("dashboard_configuration_tag_or_branch")
Expand All @@ -115,9 +121,7 @@ def submit_settings():
flash(message2, "success" if ret else "danger")
if ret:
if repo_url:
dashboard_configuration_info[
"dashboard_configuration_url"
] = repo_url
dashboard_configuration_info["dashboard_configuration_url"] = repo_url
if tag_or_branch:
dashboard_configuration_info[
"dashboard_configuration_tag_or_branch"
Expand All @@ -132,13 +136,13 @@ def submit_settings():
"danger",
)

app.logger.debug("Configuration reloaded")
reload_message = "Configuration reloaded"
flash(reload_message, "info")
app.logger.debug(reload_message)

now = datetime.now()
dashboard_configuration_info["updated_at"] = now.strftime("%d/%m/%Y %H:%M:%S")
redis_client.set(
"last_configuration_info", json.dumps(dashboard_configuration_info)
)
redis_client.set("last_configuration_info", json.dumps(dashboard_configuration_info))

if message1 or message2:
comment = request.form.get("message")
Expand Down Expand Up @@ -184,16 +188,14 @@ def set_template_access(tosca, user_groups, active_group):
regex = False if "groups_regex" not in visibility else True

if regex:
access_locked = not re.match(
visibility.get("groups_regex"), active_group
)
access_locked = not re.match(visibility.get("groups_regex"), active_group)
else:
allowed_groups = visibility.get("groups")
access_locked = True if active_group not in allowed_groups else False

if (
visibility.get("type") == "private" and not access_locked
) or visibility.get("type") == "protected":
if (visibility.get("type") == "private" and not access_locked) or visibility.get(
"type"
) == "protected":
v["metadata"]["access_locked"] = access_locked
info[k] = v
else:
Expand Down Expand Up @@ -224,15 +226,15 @@ def home():

@home_bp.route("/portfolio")
def portfolio():
""" GET STATUSES """
"""GET STATUSES"""
deps = dbhelpers.get_user_deployments(session["userid"])
statuses = {}
for dep in deps:
status = dep.status if dep.status else "UNKNOWN"
if status != 'DELETE_COMPLETE' and dep.remote == 1:
if status != "DELETE_COMPLETE" and dep.remote == 1:
statuses[status] = 1 if status not in statuses else statuses[status] + 1

if session.get('userid'):
if session.get("userid"):
# check database
# if user not found, insert
user = dbhelpers.get_user(session["userid"])
Expand All @@ -259,17 +261,19 @@ def portfolio():

services = dbhelpers.get_services(visibility="public")
services.extend(
dbhelpers.get_services(
visibility="private", groups=[session["active_usergroup"]]
)
dbhelpers.get_services(visibility="private", groups=[session["active_usergroup"]])
)
templates_info, enable_template_groups = check_template_access(
session["usergroups"], session["active_usergroup"]
)

return render_template(app.config.get('PORTFOLIO_TEMPLATE'), services=services, templates_info=templates_info,
enable_template_groups=enable_template_groups,
s_values=list(statuses.values()))
return render_template(
app.config.get("PORTFOLIO_TEMPLATE"),
services=services,
templates_info=templates_info,
enable_template_groups=enable_template_groups,
s_values=list(statuses.values()),
)

return redirect(url_for("home_bp.login"))

Expand Down Expand Up @@ -297,9 +301,7 @@ def callback():
status = payload["status"]
task = payload["task"]
uuid = payload["uuid"]
providername = (
payload["cloudProviderName"] if "cloudProviderName" in payload else ""
)
providername = payload["cloudProviderName"] if "cloudProviderName" in payload else ""
status_reason = payload["statusReason"] if "statusReason" in payload else ""
rf = 0

Expand Down Expand Up @@ -340,15 +342,15 @@ def callback():
"Deployment complete", mail_sender, [user_email], uuid, status
)
except Exception as error:
utils.logexception("sending email:".format(error))
utils.logexception("sending email: {}".format(error))

if status == "CREATE_FAILED":
try:
utils.create_and_send_email(
"Deployment failed", mail_sender, [user_email], uuid, status
)
except Exception as error:
utils.logexception("sending email:".format(error))
utils.logexception("sending email: {}".format(error))

if status == "UPDATE_COMPLETE":
try:
Expand All @@ -360,15 +362,15 @@ def callback():
status,
)
except Exception as error:
utils.logexception("sending email:".format(error))
utils.logexception("sending email: {}".format(error))

if status == "UPDATE_FAILED":
try:
utils.create_and_send_email(
"Deployment update failed", mail_sender, [user_email], uuid, status
)
except Exception as error:
utils.logexception("sending email:".format(error))
utils.logexception("sending email: {}".format(error))

resp = make_response("")
resp.status_code = 200
Expand Down Expand Up @@ -418,7 +420,7 @@ def sendaccessrequest():
)

except Exception as error:
utils.logexception("sending email:".format(error))
utils.logexception("sending email: {}".format(error))
flash(
"Sorry, an error occurred while sending your request. Please retry.",
"danger",
Expand Down Expand Up @@ -447,7 +449,7 @@ def contact():
)

except Exception as error:
utils.logexception("sending email:".format(error))
utils.logexception("sending email: {}".format(error))
return Markup(
"<div class='alert alert-danger' role='alert'>Oops, error sending message.</div>"
)
Expand Down
37 changes: 1 addition & 36 deletions app/home/templates/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
</fieldset>
<br>
<div class="dashboard-template-buttons-container">
<button type="submit" class="dashboard-button dashboard-button-primary dashboard-button-lg submitBtn" style="margin-left: auto;">Apply<i class="fas fa-save"></i></button>
<button type="submit" onclick="set_loading(true)" class="dashboard-button dashboard-button-primary dashboard-button-lg submitBtn" style="margin-left: auto;">Apply<i class="fas fa-save"></i></button>
</div>
</form>
</div>
Expand All @@ -166,22 +166,6 @@
</div>
</div>

<!-- Loading modal -->
<div class="modal" id="loadingModal" tabindex="-1" role="dialog">
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<div class="modal-body">
<div class="text-center">
<div class="spinner-border text-primary" role="status">
<span class="sr-only">Loading...</span>
</div>
</div>
<p class="text-center mt-2">Loading...</p>
</div>
</div>
</div>
</div>

<script>

$(document).ready(() => {
Expand All @@ -191,25 +175,6 @@
$("#dashboard_configuration_private").change(() => {
$("#privateRepoFields2").toggle(this.checked);
});

$("#settingsform").submit((event) => {
event.preventDefault();

// Show loading modal
set_loading(true)

var actionUrl = "{{ url_for('home_bp.submit_settings')}}";
// Perform asynchronous form submission
$.post(actionUrl, $(this).serialize(), function(response) {
// Hide loading modal
set_loading(false)

// Replace the entire content of the current page with the response
document.open();
document.write(response);
document.close();
});
});
});

$('#tableServices').dataTable( {
Expand Down
8 changes: 0 additions & 8 deletions app/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,6 @@ def logexception(err):
)
)


def getorchestratorversion(orchestrator_url):
url = orchestrator_url + "/info"
response = requests.get(url)

return response.json()["build"]["version"]


def getorchestratorconfiguration(orchestrator_url, access_token):
headers = {"Authorization": "bearer %s" % access_token}

Expand Down

0 comments on commit 0d2e50a

Please sign in to comment.