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

Cache quota calls #22

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions aws_quota/check/quota_check.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
import cachetools
from cachetools.keys import hashkey
from aws_quota.utils import get_account_id, get_paginated_results
import enum
import typing

import boto3
from botocore.config import Config

# create custom hash key that ignores boto client
def get_service_quota_cache_key(sq_client, service_code, quota_code):
return hashkey(service_code, quota_code)

# create custom hash key that ignores boto client
def get_default_service_quota_cache_key(sq_client, service_code, quota_code):
return hashkey("default", service_code, quota_code)

@cachetools.cached(cache=cachetools.TTLCache(1000, 3600), key=get_service_quota_cache_key)
def get_service_quota(sq_client: boto3.client, service_code, quota_code):
return sq_client.get_service_quota(ServiceCode=service_code, QuotaCode=quota_code)['Quota']

@cachetools.cached(cache=cachetools.TTLCache(1000, 3600), key=get_default_service_quota_cache_key)
def get_default_service_quota(sq_client: boto3.client, service_code, quota_code):
return sq_client.get_aws_default_service_quota(ServiceCode=service_code, QuotaCode=quota_code)['Quota']

class QuotaScope(enum.Enum):
ACCOUNT = 0
Expand All @@ -19,7 +36,7 @@ class QuotaCheck:
quota_code: str = None
warning_threshold: float = None
error_threshold: float = None
# retries are needed to handle rate limitting
# retries are needed to handle rate limiting
# https://docs.aws.amazon.com/sdkref/latest/guide/feature-retry-behavior.html
retry_attempts: int = 15

Expand Down Expand Up @@ -64,9 +81,9 @@ def label_values(self):
@property
def maximum(self) -> int:
try:
return int(self.sq_client.get_service_quota(ServiceCode=self.service_code, QuotaCode=self.quota_code)['Quota']['Value'])
return int(get_service_quota(self.sq_client, self.service_code, self.quota_code)['Value'])
except self.sq_client.exceptions.NoSuchResourceException:
return int(self.sq_client.get_aws_default_service_quota(ServiceCode=self.service_code, QuotaCode=self.quota_code)['Quota']['Value'])
return int(get_default_service_quota(self.sq_client, self.service_code, self.quota_code)['Value'])

@property
def current(self) -> int:
Expand Down
Loading