Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parameters): Add force_fetch option #341

Merged
merged 6 commits into from
Mar 15, 2021

Conversation

michaelbrewer
Copy link
Contributor

@michaelbrewer michaelbrewer commented Mar 14, 2021

Issue #, if available:

#131

Description of changes:

Changes:

  • Add new force_fetch to always fetch the latest value, even if there is a cached value

Example based on https://github.com/alexcasalboni/ssm-cache-python#complex-invalidation-based-on-signals

import os
from aws_lambda_powertools.utilities import parameters


class InvalidCredentials(Exception):
    """Raised when client get an invalid credentials error"""


class Client:
    password = None

    def configure(self, password):
        self.password = password

    def read_record(self):
        """Might raise an InvalidCredentials exception """


DB_PASS_PARAM = os.environ["DB_PASS_PARAM"]
db_client = Client()
db_client.configure(parameters.get_parameter(DB_PASS_PARAM))


def refresh_db_password():
    db_client.configure(parameters.get_parameter(DB_PASS_PARAM, force_fetch=True))


def read_record(is_retry=False):
    try:
        return db_client.read_record()
    except InvalidCredentials:
        if not is_retry:  # avoid infinite recursion
            refresh_db_password()  # force parameter refresh
            return read_record(is_retry=True)


def handler(event, context):
    return {"record": read_record()}

Checklist

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Changes:
- Add new force_update to always load even if there is a cached value
@codecov-io
Copy link

codecov-io commented Mar 14, 2021

Codecov Report

Merging #341 (02d3957) into develop (5daa45a) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff            @@
##           develop     #341   +/-   ##
========================================
  Coverage    99.88%   99.88%           
========================================
  Files           94       94           
  Lines         3521     3521           
  Branches       172      172           
========================================
  Hits          3517     3517           
  Misses           2        2           
  Partials         2        2           
Impacted Files Coverage Δ
aws_lambda_powertools/utilities/parameters/base.py 100.00% <100.00%> (ø)
aws_lambda_powertools/utilities/parameters/ssm.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5daa45a...02d3957. Read the comment docs.

@michaelbrewer
Copy link
Contributor Author

@heitorlessa I added a force_update option but i did notice that the sdk_options is confusing things for PyCharm

Screen Shot 2021-03-14 at 3 13 10 AM

@michaelbrewer
Copy link
Contributor Author

@heitorlessa I made it an explicit option throughout, the question is what should it be called?

force_update, refresh, force_fetch ?

@heitorlessa heitorlessa added this to the 1.12.0 milestone Mar 15, 2021
@heitorlessa
Copy link
Contributor

For completion, as @nmoutschen suggested, let's go with force_fetch as that doesn't make assumption whether we have something cache to be ignored.

@michaelbrewer michaelbrewer changed the title feat(parameters): Add force_update option feat(parameters): Add force_fetch option Mar 15, 2021
@michaelbrewer
Copy link
Contributor Author

For completion, as @nmoutschen suggested, let's go with force_fetch as that doesn't make assumption whether we have something cache to be ignored.

Ok @nmoutschen @heitorlessa i have made the update to force_fetch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants