Skip to content

Commit

Permalink
🔧refactor: aws ui add extra variables for new accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
D10S0VSkY-OSS committed Dec 29, 2023
1 parent 722272f commit 4411991
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 57 deletions.
18 changes: 9 additions & 9 deletions sld-api-backend/src/aws/infrastructure/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ 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()
if db_aws:
if updated_aws.access_key_id:
Expand Down Expand Up @@ -72,13 +72,13 @@ async def update_aws_profile(db: Session, aws_account_id: int, updated_aws: sche


async def get_credentials_aws_profile(db: Session, environment: str, squad: str) -> schemas_aws.AwsAccountResponseRepo:
aws_provider_data = (
db_aws = (
db.query(models.Aws_provider)
.filter(models.Aws_provider.environment == environment)
.filter(models.Aws_provider.squad == squad)
.first()
)
return schemas_aws.AwsAccountResponseRepo.model_validate(obj=aws_provider_data)
return schemas_aws.AwsAccountResponseRepo.model_validate(obj=db_aws)


async def get_all_aws_profile(
Expand All @@ -95,10 +95,10 @@ async def get_all_aws_profile(
else:
query = query.filter(getattr(models.Aws_provider, field) == value)

results = query.order_by(desc(models.Aws_provider.id)).offset(skip).limit(limit).all()
db_aws = query.order_by(desc(models.Aws_provider.id)).offset(skip).limit(limit).all()

aws_profiles = []
for result in results:
for result in db_aws:
aws_profile = schemas_aws.AwsAccountResponse(
id=result.id,
squad=result.squad,
Expand All @@ -117,13 +117,13 @@ async def get_all_aws_profile(

async def delete_aws_profile_by_id(db: Session, aws_account_id: int) -> schemas_aws.AwsAccountResponse:
try:
aws_profile = db.query(models.Aws_provider).filter(
db_aws = db.query(models.Aws_provider).filter(
models.Aws_provider.id == aws_account_id
).first()
if aws_profile:
db.delete(aws_profile)
if db_aws:
db.delete(db_aws)
db.commit()
response_data = schemas_aws.AwsAccountResponse.model_validate(aws_profile)
response_data = schemas_aws.AwsAccountResponse.model_validate(db_aws)
return response_data
else:
raise f"AWS profile with id {aws_account_id} not found"
Expand Down
2 changes: 1 addition & 1 deletion sld-api-backend/src/deploy/api/container/plan/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async def update_plan_by_id(
status_code=403, detail=f"Not enough permissions in {squad}"
)
# Get credentials by providers supported
secreto = check_prefix(
secreto = await check_prefix(
db, stack_name=stack_name, environment=environment, squad=squad
)
# Get info from stack data
Expand Down
15 changes: 5 additions & 10 deletions sld-api-backend/test/config/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,29 +120,25 @@ class Settings(BaseSettings):
"access_key_id": os.getenv("AWS_ACCESS_KEY_ID"),
"secret_access_key": os.getenv("AWS_SECRET_ACCESS_KEY"),
"default_region": "eu-west-1",
"profile_name": "string",
"role_arn": "string",
"source_profile": "string",
"extra_variables": {"TF_VAR_aws_account_id": "1234567890", "TF_VAR_aws_secret": "1234"},
}
AWS_TEST_ACCOUNT_PRO: dict = {
"squad": "squad1",
"environment": "pro",
"access_key_id": os.getenv("AWS_ACCESS_KEY_ID"),
"secret_access_key": os.getenv("AWS_SECRET_ACCESS_KEY"),
"default_region": "eu-west-1",
"profile_name": "string",
"role_arn": "string",
"source_profile": "string",
"role_arn": "arn:aws:iam::1234567890:role/role_name",
"extra_variables": {"TF_VAR_aws_account_id": "1234567890", "TF_VAR_aws_secret": "1234"},
}
AWS_TEST_ACCOUNT_SQUAD2: dict = {
"squad": "squad2",
"environment": "develop",
"access_key_id": os.getenv("AWS_ACCESS_KEY_ID"),
"secret_access_key": os.getenv("AWS_SECRET_ACCESS_KEY"),
"default_region": "eu-west-1",
"profile_name": "string",
"role_arn": "string",
"source_profile": "string",
"role_arn": "arn:aws:iam::1234567890:role/role_name",
"extra_variables": {"TF_VAR_aws_account_id": "1234567890", "TF_VAR_aws_secret": "1234", "TF_VAR_db_password": "1234"},
}
AWS_TEST_ACCOUNT_SQUAD2_PRO: dict = {
"squad": "squad2",
Expand All @@ -151,7 +147,6 @@ class Settings(BaseSettings):
"secret_access_key": os.getenv("AWS_SECRET_ACCESS_KEY"),
"default_region": "eu-west-1",
"profile_name": "string",
"role_arn": "string",
"source_profile": "string",
}
DEPLOY_URI: str = "?tf_ver=1.0.7"
Expand Down
46 changes: 25 additions & 21 deletions sld-dashboard/app/home/forms.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
# -*- encoding: utf-8 -*-
from flask_wtf import FlaskForm
from wtforms import (BooleanField, PasswordField, StringField, TextAreaField, SelectField,
validators)
FormField, FieldList, validators)
from wtforms.fields import EmailField
from wtforms.validators import DataRequired


# login and registration
class DictField(StringField):
def process_formdata(self, valuelist):
if valuelist:
data = valuelist[0]
try:
# Try to parse the input as a dictionary
self.data = dict(eval(data))
except (SyntaxError, ValueError):
self.data = None
raise ValueError("Invalid dictionary format")


class ExtraVariableForm(FlaskForm):
key = StringField('Key')
value = StringField('Value')


class StackForm(FlaskForm):
Expand Down Expand Up @@ -193,6 +207,13 @@ class AwsForm(FlaskForm):
validators.DataRequired(message="Squad Name requerid."),
],
)
environment = StringField(
"Environment *",
[
validators.length(min=2, max=250, message="Environment out of reange."),
validators.DataRequired(message="Environment requerid."),
],
)
access_key_id = StringField(
"Access_key_id *",
[
Expand All @@ -216,31 +237,14 @@ class AwsForm(FlaskForm):
validators.DataRequired(message="default_region."),
],
)
profile_name = StringField(
"Profile_name",
[
validators.length(min=4, max=50, message="profile_name out of reange."),
],
)
role_arn = StringField(
"Role_arn",
[
validators.length(min=4, max=50, message="Role arn out of reange."),
],
)
source_profile = StringField(
"Source_profile",
[
validators.length(min=4, max=50, message="source_profile out of reange."),
],
)
environment = StringField(
"Environment *",
[
validators.length(min=2, max=250, message="Branch out of reange."),
validators.DataRequired(message="Environment requerid."),
],
)
extra_variables = FieldList(FormField(ExtraVariableForm), label='Extra Variables')



class GcpForm(FlaskForm):
Expand Down
6 changes: 4 additions & 2 deletions sld-dashboard/app/home/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1506,15 +1506,17 @@ def new_aws_account():
# Check if token no expired
check_unauthorized_token(token)
if request.method == "POST":
key_list = request.values.getlist("sld_key")
value_list = request.values.getlist("sld_value")
new_user: dict = {
"squad": form.squad.data.replace(" ",""),
"environment": form.environment.data.replace(" ",""),
"access_key_id": form.access_key_id.data.replace(" ",""),
"secret_access_key": form.secret_access_key.data.replace(" ",""),
"default_region": form.default_region.data.replace(" ",""),
"profile_name": form.profile_name.data.replace(" ",""),
"role_arn": form.role_arn.data.replace(" ",""),
"source_profile": form.source_profile.data.replace(" ",""),
"extra_variables": dict(list(zip(key_list, value_list)))

}
response = request_url(
verb="POST",
Expand Down
25 changes: 20 additions & 5 deletions sld-dashboard/app/home/templates/aws-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ <h2 class="h4">All aws accounts</h2>
<th>Squad</th>
<th>Environment</th>
<th>Default Region</th>
<th>Profile Name</th>
<th>Role Arn</th>
<th>Source Profile</th>
<th>Extra Variables</th>
<th></th>
</tr>
</thead>
Expand All @@ -87,9 +86,25 @@ <h2 class="h4">All aws accounts</h2>
<td><span class="font-weight-normal">{{ aws_account.squad }}</span></td>
<td><span class="font-weight-normal">{{ aws_account.environment }}</span></td>
<td><span class="font-weight-normal">{{ aws_account.default_region }}</span></td>
<td><span class="font-weight-normal">{{ aws_account.profile_name }}</span></td>
<td><span class="font-weight-normal">{{ aws_account.role_arn }}</span></td>
<td><span class="font-weight-normal">{{ aws_account.source_profile }}</span></td>
<td>
<span class="font-weight-normal">
{% if aws_account.role_arn %}
{{ aws_account.role_arn }}
{% else %}
-
{% endif %}
</span>
</td>
<td>
<span class="font-weight-normal" title="{% if aws_account.extra_variables %}{{ ', '.join(aws_account.extra_variables.keys()) }}{% endif %}">
{% if aws_account.extra_variables %}
{% set truncated_variables = aws_account.extra_variables.keys() | join(', ') | truncate(30, True, '...') %}
{{ truncated_variables }}
{% else %}
-
{% endif %}
</span>
</td>
<td>
<div class="btn-group">
{% if "yoda" in current_user.role %}
Expand Down
54 changes: 45 additions & 9 deletions sld-dashboard/app/home/templates/aws-new.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,27 @@ <h1 class="h4">Add New AWS Account</h1>
{{ render_field(form.default_region, class='form-control',value='eu-west-1') }}
</div>

<div class="form-group">
{{ render_field(form.profile_name, class='form-control',
placeholder='profile_name') }}
</div>
<div class="form-group">
{{ render_field(form.role_arn, class='form-control', placeholder='role_arn') }}
</div>
<div class="form-group">
{{ render_field(form.source_profile, class='form-control',
placeholder='source_profile') }}
</div>

<!-- Add custom variables -->
<div class="border-top my-4"></div>
<div class="row">
<div class="col-lg-12">
<div id="inputFormRow">
</div>

<div id="newRow"></div>
<button id="addRow"
type="button"
class="btn btn-info btn-sm"
title="Add extra secrets varibles like TF_VAR_secreto: mySeCret">Add
Extra Variable</button>
</div>
</div>
<div class="border-top my-4"></div>
<!-- End Add custom variables -->
<button type="submit" class="btn btn-primary" name="button">
Create new Account
</button>
Expand All @@ -99,4 +108,31 @@ <h1 class="h4">Add New AWS Account</h1>
{% endblock content %}

<!-- Specific Page JS goes HERE -->
{% block javascripts %}{% endblock javascripts %}
{% block javascripts %}
<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">';
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 += '<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 += '</div>';
html += '</div>';

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

// remove row
$(document).on('click', '#removeRow', function () {
$(this).closest('#inputFormRow').remove();
});
</script>
{% endblock javascripts %}

0 comments on commit 4411991

Please sign in to comment.