Skip to content

Commit

Permalink
🔧refactor: aws update check if use by deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
D10S0VSkY-OSS committed Dec 30, 2023
1 parent 1244981 commit 3198ed6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 30 deletions.
3 changes: 3 additions & 0 deletions sld-api-backend/src/aws/api/container/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from src.shared.security import deps
from src.users.domain.entities import users as schemas_users
from src.users.infrastructure import repositories as crud_users
from src.shared.domain.exeptions.in_use import ResourceInUseError


async def update_aws_account(
Expand All @@ -31,5 +32,7 @@ async def update_aws_account(
action=f"Update AWS account {aws.squad} {aws.environment}",
)
return result
except ResourceInUseError as err:
raise HTTPException(status_code=409, detail=str(err))
except Exception as err:
raise err
11 changes: 9 additions & 2 deletions sld-api-backend/src/aws/infrastructure/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ async def create_aws_profile(db: Session, aws: schemas_aws.AwsAsumeProfile) -> s

async def update_aws_profile(db: Session, aws_account_id: int, updated_aws: schemas_aws.AwsAccountUpdate) -> schemas_aws.AwsAccountResponse:
db_aws = db.query(models.Aws_provider).filter(models.Aws_provider.id == aws_account_id).first()
db_deploy = (
db.query(Deploy)
.filter(Deploy.squad == db_aws.squad)
.filter(Deploy.environment == db_aws.environment)
.first())
if db_deploy and updated_aws.squad != db_aws.squad or updated_aws.environment != db_aws.environment:
raise ResourceInUseError(aws_account_id)

if db_aws:
if updated_aws.access_key_id:
Expand All @@ -65,11 +72,11 @@ async def update_aws_profile(db: Session, aws_account_id: int, updated_aws: sche
current_extra_variables = db_aws.extra_variables or {}
for key, value in current_extra_variables.items():
current_extra_variables[key] = decrypt(value)
current_extra_variables = {key: value for key, value in current_extra_variables.items() if key in updated_aws.extra_variables}

for key, value in updated_aws.extra_variables.items():
if "***" not in value:
if key and value and "***" not in value:
current_extra_variables[key] = value

encrypted_extra_variables = {key: encrypt(value) for key, value in current_extra_variables.items()}
db_aws.extra_variables = encrypted_extra_variables
db_aws.environment = updated_aws.environment
Expand Down
4 changes: 2 additions & 2 deletions sld-dashboard/app/home/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1561,7 +1561,6 @@ def edit_aws_account(account_id):
if request.method == "POST":
key_list = request.values.getlist("sld_key")
value_list = request.values.getlist("sld_value")
print(request.values)
aws_account_request: dict = {
"squad": form.squad.data.replace(" ",""),
"environment": form.environment.data.replace(" ",""),
Expand All @@ -1581,6 +1580,8 @@ def edit_aws_account(account_id):
flash(
f"Updated aws account for environment {form.environment.data} in {form.squad.data} "
)
return redirect(url_for("home_blueprint.route_template", template="aws-list"))

elif response.get("status_code") == 409:
flash(response["json"].get("detail"), "error")
else:
Expand Down Expand Up @@ -1613,7 +1614,6 @@ def list_aws_account():
return render_template(
"aws-list.html", name="Name", aws=content, external_api_dns=external_api_dns
)

except ValueError:
return redirect(url_for("base_blueprint.logout"))

Expand Down
59 changes: 33 additions & 26 deletions sld-dashboard/app/home/templates/aws-edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ <h1 class="h4">Edit AWS Account</h1>
<div class="row mb-4">
<div class="col-lg-4 col-sm-6">
<!-- Form -->
{{data_json}}
<div class="col-md-12 col-md-offset-1">
<form class="" method="post">
<div class="form-group">
Expand Down Expand Up @@ -74,19 +73,19 @@ <h1 class="h4">Edit AWS Account</h1>
<div class="col-lg-12">
<div id="inputFormRow">
<!-- Iterar sobre las variables existentes -->
{% for key, value in data_json.extra_variables.items() %}
<div class="input-group col-sm-8 mb-3">
<div class="col-sm-8 mb-2">
<input type="text" name="sld_key" class="form-control m-input" value="{{ key }}" placeholder="Enter variable name" autocomplete="off">
</div>
<div class="col-sm-8 mb-4">
<input type="text" name="sld_value" class="form-control m-input" value="{{ value }}" placeholder="Enter variable value" autocomplete="off">
</div>
<div class="input-group-append">
<button id="removeRow" type="button" class="btn btn-danger btn-sm">Remove</button>
</div>
</div>
{% endfor %}
{% for key, value in data_json.extra_variables.items() %}
<div class="input-group col-sm-8 mb-3 dynamic-row">
<div class="col-sm-8 mb-2">
<input type="text" name="sld_key" class="form-control m-input" value="{{ key }}" placeholder="Enter variable name" autocomplete="off" {% if data_json.extra_variables.get(key) %} readonly {% endif %}>
</div>
<div class="col-sm-8 mb-4">
<input type="text" name="sld_value" class="form-control m-input" value="{{ value }}" placeholder="Enter variable value" autocomplete="off">
</div>
<div class="input-group-append">
<button type="button" class="btn btn-danger btn-sm ml-2 removeRow">Remove</button>
</div>
</div>
{% endfor %}
</div>

<div id="newRow"></div>
Expand All @@ -95,10 +94,12 @@ <h1 class="h4">Edit AWS Account</h1>
</div>
<div class="border-top my-4"></div>
<!-- END Extra variables -->
<button type="submit" class="btn btn-primary" name="button">
Create new Account
<button type="submit" class="btn btn-primary w-30" name="button">
Update Account
</button>
<button type="button" class="btn btn-danger btn-lock w-30" id="cancelButton">
Exit
</button>

</form>
<!-- End Form -->

Expand All @@ -119,27 +120,33 @@ <h1 class="h4">Edit AWS Account</h1>
<script type="text/javascript">
// add row
$("#addRow").click(function () {
var html = '';
html += '<div class="row">';
html += '<div id="inputFormRow">';
html += '<div class="input-group col-sm-8 mb-3">';
html += ' <div class="col-sm-8 mb-2">';
var html = '<div class="row">';
html += '<div class="input-group col-sm-8 mb-3 dynamic-row">';
html += '<div class="col-sm-8 mb-2">';
html += '<input type="text" name="sld_key" class="form-control m-input" placeholder="Enter variable name" autocomplete="off">';
html += '</div>';
html += ' <div class="col-sm-8 mb-4">';
html += '<div class="col-sm-8 mb-4">';
html += '<input type="text" name="sld_value" class="form-control m-input" placeholder="Enter variable value" autocomplete="off">';
html += '</div>';
html += '<div class="input-group-append">';
html += '<button id="removeRow" type="button" class="btn btn-danger btn-sm">Remove</button>';
html += '<button type="button" class="btn btn-danger btn-sm removeRow">Remove</button>';
html += '</div>';
html += '</div>';
html += '</div>';

$('#newRow').append(html);
});

// remove row
$(document).on('click', '#removeRow', function () {
$(this).closest('#inputFormRow').remove();
$(document).on('click', '.removeRow', function () {
$(this).closest('.dynamic-row').remove();
});
</script>
<script type="text/javascript">
document.getElementById('cancelButton').addEventListener('click', function() {
// Redirect to /aws-list when the cancel button is clicked
window.location.href = '/aws-list';
});
</script>

{% endblock javascripts %}
3 changes: 3 additions & 0 deletions sld-dashboard/app/home/templates/aws-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ <h2 class="h4">All aws accounts</h2>
<span class="sr-only">Toggle Dropdown</span>
</button>
<div class="dropdown-menu">
<a class="dropdown-item"
href="{{ url_for('.edit_aws_account', account_id=aws_account.id)}}"><span
class="fas fa-edit mr-2"></span>Edit</a>
<a class="dropdown-item text-danger"
href="{{ url_for('.delete_aws_account', aws_account_id=aws_account.id)}}"><span
class="fas fa-trash-alt mr-2"></span>Remove</a>
Expand Down

0 comments on commit 3198ed6

Please sign in to comment.