Add the Pagination Utility to the Elasticache describe_cache_clusters model + response (#7500)

This commit is contained in:
Andrew Tanner 2024-03-21 16:19:44 -04:00 committed by GitHub
parent 90bdd72555
commit 3edc0c751c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 36 additions and 23 deletions

View File

@ -1,9 +1,10 @@
from re import compile as re_compile
from typing import Any, Dict, List, Optional, Tuple
from typing import Any, Dict, List, Optional
from moto.core.base_backend import BackendDict, BaseBackend
from moto.core.common_models import BaseModel
from moto.core.utils import utcnow
from moto.utilities.paginator import paginate
from ..moto_api._internal import mock_random
from .exceptions import (
@ -13,6 +14,7 @@ from .exceptions import (
UserAlreadyExists,
UserNotFound,
)
from .utils import PAGINATION_MODEL
class User(BaseModel):
@ -312,25 +314,24 @@ class ElastiCacheBackend(BaseBackend):
self.cache_clusters[cache_cluster_id] = cache_cluster
return cache_cluster
@paginate(PAGINATION_MODEL)
def describe_cache_clusters(
self,
cache_cluster_id: str,
max_records: int,
marker: str,
) -> Tuple[str, List[CacheCluster]]:
if marker is None:
marker = str(mock_random.uuid4())
) -> List[CacheCluster]:
if max_records is None:
max_records = 100
if cache_cluster_id:
if cache_cluster_id in self.cache_clusters:
cache_cluster = self.cache_clusters[cache_cluster_id]
return marker, [cache_cluster]
return list([cache_cluster])
else:
raise CacheClusterNotFound(cache_cluster_id)
cache_clusters = list(self.cache_clusters.values())[:max_records]
return marker, cache_clusters
return cache_clusters
def delete_cache_cluster(self, cache_cluster_id: str) -> CacheCluster:
if cache_cluster_id:

View File

@ -128,7 +128,8 @@ class ElastiCacheResponse(BaseResponse):
cache_cluster_id = self._get_param("CacheClusterId")
max_records = self._get_int_param("MaxRecords")
marker = self._get_param("Marker")
marker, cache_clusters = self.elasticache_backend.describe_cache_clusters(
cache_clusters, marker = self.elasticache_backend.describe_cache_clusters(
cache_cluster_id=cache_cluster_id,
marker=marker,
max_records=max_records,
@ -347,7 +348,7 @@ DESCRIBE_CACHE_CLUSTERS_TEMPLATE = """<DescribeCacheClustersResponse xmlns="http
<RequestId>1549581b-12b7-11e3-895e-1334aEXAMPLE</RequestId>
</ResponseMetadata>
<DescribeCacheClustersResult>
<Marker>{{ marker }}</Marker>
{% if marker %}<Marker>{{ marker }}</Marker>{% endif %}
<CacheClusters>
{% for cache_cluster in cache_clusters %}
<member>

View File

@ -0,0 +1,8 @@
PAGINATION_MODEL = {
"describe_cache_clusters": {
"input_token": "marker",
"limit_key": "max_records",
"limit_default": 100,
"unique_attribute": "cache_cluster_id",
},
}

View File

@ -291,28 +291,31 @@ def test_create_duplicate_cache_cluster():
def test_describe_all_cache_clusters():
client = boto3.client("elasticache", region_name="us-east-2")
test_memcached_cache_cluster_exist = False
num_clusters = 5
match_number = 0
cache_cluster_id = "test-cache-cluster"
cache_cluster_engine = "memcached"
cache_cluster_num_cache_nodes = 5
client.create_cache_cluster(
CacheClusterId=cache_cluster_id,
Engine=cache_cluster_engine,
NumCacheNodes=cache_cluster_num_cache_nodes,
)
for i in range(num_clusters):
client.create_cache_cluster(
CacheClusterId=f"test-cache-cluster-{i}",
Engine="memcached",
NumCacheNodes=5,
)
resp = client.describe_cache_clusters()
assert resp["ResponseMetadata"]["HTTPStatusCode"] == 200
cache_clusters = resp["CacheClusters"]
# account for the default cluster created moto/elasticache/models.py line 148
del cache_clusters[0]
for cache_cluster in cache_clusters:
if cache_cluster["CacheClusterId"] == cache_cluster_id:
test_memcached_cache_cluster_exist = True
for i, cache_cluster in enumerate(cache_clusters):
if cache_cluster["CacheClusterId"] == f"test-cache-cluster-{i}":
match_number = match_number + 1
assert resp["ResponseMetadata"]["HTTPStatusCode"] == 200
assert test_memcached_cache_cluster_exist
if match_number == (num_clusters):
assert True
else:
assert False
@mock_aws