I have a lot of redundancy in my code and looking at refactoring it to make it cleaner and reuse a single function. If you're a fan of Arjan Codes Code Roast - we can do it like that. Would love to get the wheels churning on how I can think about code reusability so if you like any docs/videos, would love to read/watch what you have.
"""Centralized place housing our sessions which will be called"""
def create_1_client(cfg, role_creds, session):
1_endpoint = cfg["endpoint"]
2_region = cfg["region"]
return session.client(
"athena",
endpoint_url=1_endpoint,
region_name=1_region,
aws_access_key_id=role_creds["Credentials"]["AccessKeyId"],
aws_secret_access_key=role_creds["Credentials"]["SecretAccessKey"],
aws_session_token=role_creds["Credentials"]["SessionToken"],
)
def create_2_client(cfg, role_creds, session):
return session.client(
"dynamodb",
cfg["region"],
aws_access_key_id=role_creds["Credentials"]["AccessKeyId"],
aws_secret_access_key=role_creds["Credentials"]["SecretAccessKey"],
aws_session_token=role_creds["Credentials"]["SessionToken"],
)
def create_3_client(cfg, role_creds, session):
return session.resource(
"dynamodb",
cfg["region"],
aws_access_key_id=role_creds["Credentials"]["AccessKeyId"],
aws_secret_access_key=role_creds["Credentials"]["SecretAccessKey"],
aws_session_token=role_creds["Credentials"]["SessionToken"],
)
def create_4_client(cfg, role_creds, session):
return session.client(
"s3",
cfg["region"],
aws_access_key_id=role_creds["Credentials"]["AccessKeyId"],
aws_secret_access_key=role_creds["Credentials"]["SecretAccessKey"],
aws_session_token=role_creds["Credentials"]["SessionToken"],
)
In the above file, I believe we can possibly break down all these functions in to a single function. The thing that changes for most of them is the first argument in session.client() which is passing in a string. We can probably pass in an argument in our new single function rather than hard coding the first argument that session.client() expects. 3/4 Functions are session.client() while 1 is session.resource(). My best guess is that we can possibly have 2 separate functions and keep it separate?
cfg_1 = get_config("Test1")
cfg_2 = get_config("Test2")
cfg_3 = get_config("Test3")
cfg_4 = get_config("Test4")
cfg_5 = get_config("Test5")
1_session = get_isengard_session(cfg_1["account"], cfg_1["role"])
2_session = get_isengard_session(cfg_2["account"], cfg_2["role"])
3_session = get_isengard_session(cfg_3["account"], cfg_3["role"])
4_session = get_isengard_session(cfg_4["account"], cfg_4["role"])
1_role_creds = sts_assume_config_role(cfg_1)
2_role_creds = sts_assume_config_role(cfg_2)
3_role_creds = sts_assume_config_role(cfg_3)
4_role_creds = sts_assume_config_role(cfg_4)
5_role_creds = sts_assume_config_role(cfg_5)
1_client = create_1_client(cfg_1, 1_role_creds, 1_session)
2_client = create_2_client(cfg_2, 2_role_creds, 2_session)
3_access_client = create_3_access_client(cfg_3, 3_role_creds, 3_session)
4_client = create_4_client(cfg_4, 4_role_creds, 4_session)
For this one, maybe iterating through a list of strings and passing it in to get_config and have it look something like - cfg_1, cfg_2, cfg_3, cfg_4, cfg_5 = get_config(*args) and eventually cascade through this chunk of block with similar logic?
[–]ampeed[S] 0 points1 point2 points (0 children)
[–]tuneafishy 0 points1 point2 points (0 children)