From 3198ed60bfe5f7ae5c693ad80f708b9291ecba9b Mon Sep 17 00:00:00 2001 From: d10s <79284025+D10S0VSkY-OSS@users.noreply.github.com> Date: Sat, 30 Dec 2023 01:27:05 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7refactor:=20aws=20update=20check=20?= =?UTF-8?q?if=20use=20by=20deployment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/aws/api/container/update.py | 3 + .../src/aws/infrastructure/repositories.py | 11 +++- sld-dashboard/app/home/routes.py | 4 +- .../app/home/templates/aws-edit.html | 59 +++++++++++-------- .../app/home/templates/aws-list.html | 3 + 5 files changed, 50 insertions(+), 30 deletions(-) diff --git a/sld-api-backend/src/aws/api/container/update.py b/sld-api-backend/src/aws/api/container/update.py index e379483..20fa482 100644 --- a/sld-api-backend/src/aws/api/container/update.py +++ b/sld-api-backend/src/aws/api/container/update.py @@ -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( @@ -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 diff --git a/sld-api-backend/src/aws/infrastructure/repositories.py b/sld-api-backend/src/aws/infrastructure/repositories.py index 5e2b3cf..39fd107 100644 --- a/sld-api-backend/src/aws/infrastructure/repositories.py +++ b/sld-api-backend/src/aws/infrastructure/repositories.py @@ -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: @@ -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 diff --git a/sld-dashboard/app/home/routes.py b/sld-dashboard/app/home/routes.py index b14b313..e51e492 100644 --- a/sld-dashboard/app/home/routes.py +++ b/sld-dashboard/app/home/routes.py @@ -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(" ",""), @@ -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: @@ -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")) diff --git a/sld-dashboard/app/home/templates/aws-edit.html b/sld-dashboard/app/home/templates/aws-edit.html index 0619059..78df6b1 100644 --- a/sld-dashboard/app/home/templates/aws-edit.html +++ b/sld-dashboard/app/home/templates/aws-edit.html @@ -42,7 +42,6 @@

Edit AWS Account

- {{data_json}}
@@ -74,19 +73,19 @@

Edit AWS Account

- {% for key, value in data_json.extra_variables.items() %} -
-
- -
-
- -
-
- -
-
- {% endfor %} +{% for key, value in data_json.extra_variables.items() %} +
+
+ +
+
+ +
+
+ +
+
+{% endfor %}
@@ -95,10 +94,12 @@

Edit AWS Account

- + - @@ -119,18 +120,17 @@

Edit AWS Account

+ + {% endblock javascripts %} \ No newline at end of file diff --git a/sld-dashboard/app/home/templates/aws-list.html b/sld-dashboard/app/home/templates/aws-list.html index 1c789ae..2161dc0 100644 --- a/sld-dashboard/app/home/templates/aws-list.html +++ b/sld-dashboard/app/home/templates/aws-list.html @@ -117,6 +117,9 @@

All aws accounts

Toggle Dropdown