Skip to content

apigateway - refactor store#7750

Merged
bentsku merged 1 commit intomasterfrom
apigw-refactor-store
Mar 9, 2023
Merged

apigateway - refactor store#7750
bentsku merged 1 commit intomasterfrom
apigw-refactor-store

Conversation

@bentsku
Copy link
Contributor

@bentsku bentsku commented Feb 25, 2023

This PR refactors the store of API Gateway to make use of dictionary for an easier look up of RestAPI sub-entities.
I've added utilities to support the use of both old restored stores and new, fresh stores, but it makes it a bit verbose.

This refactor will also help in the recent parity work, to move more data into our stores and add more validations.

I'm not sure how much breakage we can do with persistence, but if we can break, I'll remove the utils and just use the new store attributes directly, which will be much cleaner. If we can't break it yet, we can keep those utils to support restored state, and remove them before v2. I will be working on API Gateway until then, so I can undertake that effort then.

Also cleaned up some methods that were not used anymore anywhere.

\cc @whummer @thrau about how this should tackled, should I break the store, or use these utils until then?

note: green lighted the breakage of persistence, so now this PR just uses the new store format 😄

@bentsku bentsku mentioned this pull request Feb 25, 2023
4 tasks
@bentsku bentsku self-assigned this Feb 25, 2023
@bentsku bentsku added the aws:apigateway Amazon API Gateway label Feb 25, 2023
@coveralls
Copy link

coveralls commented Feb 25, 2023

Coverage Status

Coverage: 85.177% (+0.06%) from 85.121% when pulling 80e8476 on apigw-refactor-store into b81ef49 on master.

@bentsku bentsku force-pushed the apigw-parity-work-2 branch from 835ca2a to 36b13dc Compare February 27, 2023 13:53
@bentsku bentsku force-pushed the apigw-refactor-store branch 2 times, most recently from d5f1d80 to b5d37e9 Compare February 27, 2023 14:24
@bentsku bentsku marked this pull request as ready for review February 27, 2023 17:15
@bentsku bentsku requested a review from calvernaz as a code owner February 27, 2023 17:15
@bentsku bentsku changed the title wip: apigateway - refactor store apigateway - refactor store Feb 27, 2023
@thrau
Copy link
Member

thrau commented Feb 27, 2023

@bentsku i would say it's OK to break persistence in master for now!

@bentsku
Copy link
Contributor Author

bentsku commented Feb 28, 2023

@bentsku i would say it's OK to break persistence in master for now!

Okay thanks! I'll refactor this PR to remove the utils to access the old version of the store, and will only use the new format instead to access resources. Thanks!

Base automatically changed from apigw-parity-work-2 to master February 28, 2023 14:28
@bentsku bentsku force-pushed the apigw-refactor-store branch from b5d37e9 to 2e49038 Compare February 28, 2023 18:27
@bentsku bentsku temporarily deployed to localstack-ext-tests February 28, 2023 18:27 — with GitHub Actions Inactive
@bentsku bentsku temporarily deployed to localstack-ext-tests February 28, 2023 18:29 — with GitHub Actions Inactive
@bentsku bentsku temporarily deployed to localstack-ext-tests February 28, 2023 18:52 — with GitHub Actions Inactive
@bentsku bentsku temporarily deployed to localstack-ext-tests February 28, 2023 19:09 — with GitHub Actions Inactive
@github-actions
Copy link

github-actions bot commented Feb 28, 2023

LocalStack integration with Pro

       3 files  +    1         3 suites  +1   1h 31m 22s ⏱️ - 2m 51s
1 782 tests +    4  1 401 ✔️ +    5  381 💤  -     1  0 ±0 
2 508 runs  +375  1 775 ✔️ +203  733 💤 +172  0 ±0 

Results for commit d1da5a4. ± Comparison against base commit 712c40b.

♻️ This comment has been updated with latest results.

@bentsku bentsku force-pushed the apigw-refactor-store branch from eb02f3e to deb89d3 Compare March 1, 2023 09:49
@bentsku bentsku temporarily deployed to localstack-ext-tests March 1, 2023 09:49 — with GitHub Actions Inactive
@bentsku bentsku temporarily deployed to localstack-ext-tests March 1, 2023 14:27 — with GitHub Actions Inactive
@bentsku bentsku requested a review from giograno March 1, 2023 15:32
Copy link
Contributor

@calvernaz calvernaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job, love to see code gone 👍

response = rest_api.to_dict()
remove_empty_attributes_from_rest_api(response)
store = get_apigateway_store(account_id=context.account_id, region=context.region)
store.rest_apis[request["restApiId"]].rest_api = response
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we wrap this into store.add_rest_api(api_id, response) same for delete ?

children = rest_api_resources.setdefault(parent_id, [])
children.append(response["id"])
# FIXME: delete conditional before v2
if rest_api := store.rest_apis.get(rest_api_id):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my initial comment as also related to abstract the store operations like this. Not sure if possible, but pretty much abstract the internals of the store into methods that can be refactored further without changing callers code in the future.

Copy link
Contributor Author

@bentsku bentsku Mar 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit difficult, as the error message will be different depending on the operation. It's hard to abstract that away.
I've dealt with a provider implementing a lot of logic into the store, and it made it really hard to maintain, because after a while you need to implement operation specific behaviour into the store logic.
But that is actually a leftover, and should be deleted, so thanks for pointing out 😄 (realised I hadn't push the cleanup of all these conditionals)

return {}


# -----------------------
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 🔥

"""
tags = tags or {}
result = fn(self, *args, tags=tags, **kwargs)
# TODO: lower the custom_id when getting it from the tags, as AWS is case insensitive
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AWS doesn't have this concept of custom_id, this is a feature localstack has to facilitate lookups. You probably mean tags in general are case-insensitive.

I do have a general sentiment, we should make these lookups case-insensitive but keep the original form. So logically would never fail (because of the casing) but keep visuals as the author intended.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://docs.aws.amazon.com/general/latest/gr/aws_tagging.html

check the tag keys example and the spreadsheets headers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the issue is, the ID will always be lowercase when part of the host. That's why I thought we should force the restApiId to be lower case, even though the user specified a cased tag. (tag could be cased, but not the ID once used to create the restApi).
Even though it's a LocalStack feature, a regular restApiId is always lowercase, and AWS will fail if you upper case an existing ID. Making it case insensitive changes that behaviour.

We could also simply raise an error if the user provided tag value is not lowercase for __custom_id__ or something.
My general idea is just that restApiId should be lowercase. There are no changes yet in behaviour anyway, as I keep it in a CaseInsentiveDict, but it makes the lookup more complicated for no real gain on our side.

@bentsku bentsku force-pushed the apigw-refactor-store branch from ddff601 to 80e8476 Compare March 2, 2023 13:22
@bentsku bentsku temporarily deployed to localstack-ext-tests March 2, 2023 13:22 — with GitHub Actions Inactive
@bentsku bentsku temporarily deployed to localstack-ext-tests March 2, 2023 16:49 — with GitHub Actions Inactive
@bentsku bentsku force-pushed the apigw-refactor-store branch from 80e8476 to d1da5a4 Compare March 4, 2023 13:19
@bentsku bentsku temporarily deployed to localstack-ext-tests March 4, 2023 13:19 — with GitHub Actions Inactive
@bentsku bentsku temporarily deployed to localstack-ext-tests March 6, 2023 07:42 — with GitHub Actions Inactive
@bentsku
Copy link
Contributor Author

bentsku commented Mar 6, 2023

Successful PR run against Pro: https://github.com/localstack/localstack/actions/runs/4341060270

@bentsku bentsku merged commit b302f29 into master Mar 9, 2023
@bentsku bentsku deleted the apigw-refactor-store branch March 9, 2023 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

aws:apigateway Amazon API Gateway

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants