moto/moto/dax/responses.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

116 lines
4.6 KiB
Python
Raw Normal View History

2021-12-27 19:15:37 -01:00
import json
import re
from moto.core.responses import BaseResponse
from .exceptions import InvalidParameterValueException
2022-12-18 15:08:32 -01:00
from .models import dax_backends, DAXBackend
2021-12-27 19:15:37 -01:00
class DAXResponse(BaseResponse):
2022-12-18 15:08:32 -01:00
def __init__(self) -> None:
2022-08-13 09:49:43 +00:00
super().__init__(service_name="dax")
2021-12-27 19:15:37 -01:00
@property
2022-12-18 15:08:32 -01:00
def dax_backend(self) -> DAXBackend:
2022-08-13 09:49:43 +00:00
return dax_backends[self.current_account][self.region]
2021-12-27 19:15:37 -01:00
2022-12-18 15:08:32 -01:00
def create_cluster(self) -> str:
2021-12-27 19:15:37 -01:00
params = json.loads(self.body)
cluster_name = params.get("ClusterName")
node_type = params.get("NodeType")
description = params.get("Description")
replication_factor = params.get("ReplicationFactor")
iam_role_arn = params.get("IamRoleArn")
tags = params.get("Tags", [])
sse_specification = params.get("SSESpecification", {})
2022-05-19 11:08:02 +00:00
encryption_type = params.get("ClusterEndpointEncryptionType", "NONE")
2021-12-27 19:15:37 -01:00
self._validate_arn(iam_role_arn)
self._validate_name(cluster_name)
cluster = self.dax_backend.create_cluster(
cluster_name=cluster_name,
node_type=node_type,
description=description,
replication_factor=replication_factor,
iam_role_arn=iam_role_arn,
tags=tags,
sse_specification=sse_specification,
2022-05-19 11:08:02 +00:00
encryption_type=encryption_type,
2021-12-27 19:15:37 -01:00
)
return json.dumps(dict(Cluster=cluster.to_json()))
2022-12-18 15:08:32 -01:00
def delete_cluster(self) -> str:
2021-12-27 19:15:37 -01:00
cluster_name = json.loads(self.body).get("ClusterName")
cluster = self.dax_backend.delete_cluster(cluster_name)
return json.dumps(dict(Cluster=cluster.to_json()))
2022-12-18 15:08:32 -01:00
def describe_clusters(self) -> str:
2021-12-27 19:15:37 -01:00
params = json.loads(self.body)
cluster_names = params.get("ClusterNames", [])
max_results = params.get("MaxResults")
next_token = params.get("NextToken")
for name in cluster_names:
self._validate_name(name)
clusters, next_token = self.dax_backend.describe_clusters(
cluster_names=cluster_names, max_results=max_results, next_token=next_token
)
return json.dumps(
{"Clusters": [c.to_json() for c in clusters], "NextToken": next_token}
)
2022-12-18 15:08:32 -01:00
def _validate_arn(self, arn: str) -> None:
2021-12-27 19:15:37 -01:00
if not arn.startswith("arn:"):
raise InvalidParameterValueException(f"ARNs must start with 'arn:': {arn}")
sections = arn.split(":")
if len(sections) < 3:
raise InvalidParameterValueException(
f"Second colon partition not found: {arn}"
)
if len(sections) < 4:
raise InvalidParameterValueException(f"Third colon vendor not found: {arn}")
if len(sections) < 5:
raise InvalidParameterValueException(
f"Fourth colon (region/namespace delimiter) not found: {arn}"
)
if len(sections) < 6:
raise InvalidParameterValueException(
f"Fifth colon (namespace/relative-id delimiter) not found: {arn}"
)
2022-12-18 15:08:32 -01:00
def _validate_name(self, name: str) -> None:
2021-12-27 19:15:37 -01:00
msg = "Cluster ID specified is not a valid identifier. Identifiers must begin with a letter; must contain only ASCII letters, digits, and hyphens; and must not end with a hyphen or contain two consecutive hyphens."
if not re.match("^[a-z][a-z0-9-]+[a-z0-9]$", name):
raise InvalidParameterValueException(msg)
if "--" in name:
raise InvalidParameterValueException(msg)
2022-12-18 15:08:32 -01:00
def list_tags(self) -> str:
2021-12-27 19:15:37 -01:00
params = json.loads(self.body)
resource_name = params.get("ResourceName")
tags = self.dax_backend.list_tags(resource_name=resource_name)
return json.dumps(tags)
2022-12-18 15:08:32 -01:00
def increase_replication_factor(self) -> str:
2021-12-27 19:15:37 -01:00
params = json.loads(self.body)
cluster_name = params.get("ClusterName")
new_replication_factor = params.get("NewReplicationFactor")
cluster = self.dax_backend.increase_replication_factor(
cluster_name=cluster_name, new_replication_factor=new_replication_factor
2021-12-27 19:15:37 -01:00
)
return json.dumps({"Cluster": cluster.to_json()})
2022-12-18 15:08:32 -01:00
def decrease_replication_factor(self) -> str:
2021-12-27 19:15:37 -01:00
params = json.loads(self.body)
cluster_name = params.get("ClusterName")
new_replication_factor = params.get("NewReplicationFactor")
node_ids_to_remove = params.get("NodeIdsToRemove")
cluster = self.dax_backend.decrease_replication_factor(
cluster_name=cluster_name,
new_replication_factor=new_replication_factor,
node_ids_to_remove=node_ids_to_remove,
)
return json.dumps({"Cluster": cluster.to_json()})