-
Notifications
You must be signed in to change notification settings - Fork 78
Filter regions based on account availabilities in get_regions function #625
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
base: dev
Are you sure you want to change the base?
Changes from all commits
ed9638e
d00bef5
be19673
4034689
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import ipaddress | ||
| import logging | ||
| import os | ||
| import random | ||
| import time | ||
|
|
@@ -26,6 +27,7 @@ | |
| PlacementGroupType, | ||
| PostgreSQLDatabase, | ||
| ) | ||
| from linode_api4.errors import ApiError | ||
| from linode_api4.linode_client import LinodeClient, MonitorClient | ||
| from linode_api4.objects import Region | ||
|
|
||
|
|
@@ -36,6 +38,15 @@ | |
| RUN_LONG_TESTS = "RUN_LONG_TESTS" | ||
| SKIP_E2E_FIREWALL = "SKIP_E2E_FIREWALL" | ||
|
|
||
| ALL_ACCOUNT_AVAILABILITIES = { | ||
| "Linodes", | ||
| "NodeBalancers", | ||
| "Block Storage", | ||
| "Kubernetes", | ||
| } | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def get_token(): | ||
| return os.environ.get(ENV_TOKEN_NAME, None) | ||
|
|
@@ -58,9 +69,37 @@ def get_regions( | |
|
|
||
| regions = client.regions() | ||
|
|
||
| account_regional_availabilities = {} | ||
| try: | ||
| account_availabilities = client.account.availabilities() | ||
| for availability in account_availabilities: | ||
| account_regional_availabilities[availability.region] = ( | ||
| availability.available | ||
| ) | ||
| except ApiError: | ||
| logger.warning( | ||
| "Failed to retrieve account availabilities for regions. " | ||
| "Assuming required capabilities are available in all regions for this account. " | ||
| "Tests may fail if the account lacks access to necessary capabilities in the selected region." | ||
| ) | ||
|
|
||
| if capabilities is not None: | ||
| regions = [ | ||
| v for v in regions if set(capabilities).issubset(v.capabilities) | ||
| v | ||
| for v in regions | ||
| if set(capabilities).issubset(v.capabilities) | ||
| and set(capabilities) | ||
| .intersection(ALL_ACCOUNT_AVAILABILITIES) | ||
| .issubset( | ||
| account_regional_availabilities.get( | ||
| v.id, | ||
| ( | ||
| [] | ||
| if account_regional_availabilities | ||
| else ALL_ACCOUNT_AVAILABILITIES | ||
| ), | ||
| ) | ||
| ) | ||
zliang-akamai marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+91
to
+102
|
||
| ] | ||
|
|
||
| if site_type is not None: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The set intersection
set(capabilities).intersection(ALL_ACCOUNT_AVAILABILITIES)is computed for every region in the list comprehension. Consider calculating this once before the list comprehension and storing it in a variable to avoid redundant computation.