Skip to content

Commit b459380

Browse files
authored
DSQL: migrate to automated request parsing (#10356)
1 parent 608bfa9 commit b459380

1 file changed

Lines changed: 49 additions & 52 deletions

File tree

‎moto/dsql/responses.py‎

Lines changed: 49 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
"""Handles incoming dsql requests, invokes methods, returns responses."""
22

3-
import json
43
from typing import Any
5-
from urllib.parse import unquote
64

7-
from moto.core.responses import ActionResult, BaseResponse
5+
from moto.core.responses import ActionResult, BaseResponse, EmptyResult
86

97
from .models import AuroraDSQLBackend, dsql_backends
108

@@ -14,20 +12,22 @@ class AuroraDSQLResponse(BaseResponse):
1412

1513
def __init__(self) -> None:
1614
super().__init__(service_name="dsql")
15+
self.automated_parameter_parsing = True
1716

1817
@property
1918
def dsql_backend(self) -> AuroraDSQLBackend:
2019
"""Return backend instance specific for this region."""
2120
return dsql_backends[self.current_account][self.region]
2221

2322
def create_cluster(self) -> ActionResult:
24-
params = json.loads(self.body)
25-
deletion_protection_enabled = params.get("deletionProtectionEnabled", True)
26-
tags = params.get("tags")
27-
client_token = params.get("clientToken")
28-
kms_encryption_key = params.get("kmsEncryptionKey")
29-
multi_region_properties = params.get("multiRegionProperties")
30-
policy = params.get("policy")
23+
deletion_protection_enabled = self._get_bool_param(
24+
"deletionProtectionEnabled", True
25+
)
26+
tags = self._get_param("tags")
27+
client_token = self._get_param("clientToken")
28+
kms_encryption_key = self._get_param("kmsEncryptionKey")
29+
multi_region_properties = self._get_param("multiRegionProperties")
30+
policy = self._get_param("policy")
3131
cluster = self.dsql_backend.create_cluster(
3232
deletion_protection_enabled=deletion_protection_enabled,
3333
tags=tags,
@@ -39,7 +39,7 @@ def create_cluster(self) -> ActionResult:
3939
return ActionResult(cluster)
4040

4141
def delete_cluster(self) -> ActionResult:
42-
identifier = self.path.split("/")[-1]
42+
identifier = self._get_param("identifier")
4343
cluster = self.dsql_backend.delete_cluster(identifier=identifier)
4444
result = {
4545
"identifier": cluster.identifier,
@@ -50,14 +50,14 @@ def delete_cluster(self) -> ActionResult:
5050
return ActionResult(result)
5151

5252
def get_cluster(self) -> ActionResult:
53-
identifier = self.path.split("/")[-1]
53+
identifier = self._get_param("identifier")
5454
cluster = self.dsql_backend.get_cluster(identifier=identifier)
5555
return ActionResult(cluster)
5656

5757
def list_clusters(self) -> ActionResult:
5858
clusters, next_token = self.dsql_backend.list_clusters(
59-
max_results=self._get_int_param("max-results"),
60-
next_token=self._get_param("next-token"),
59+
max_results=self._get_param("maxResults"),
60+
next_token=self._get_param("nextToken"),
6161
)
6262
result: dict[str, Any] = {
6363
"clusters": [cluster.to_summary() for cluster in clusters]
@@ -67,13 +67,14 @@ def list_clusters(self) -> ActionResult:
6767
return ActionResult(result)
6868

6969
def update_cluster(self) -> ActionResult:
70-
params = json.loads(self.body)
71-
identifier = self.path.split("/")[-1]
70+
identifier = self._get_param("identifier")
7271
cluster = self.dsql_backend.update_cluster(
7372
identifier=identifier,
74-
deletion_protection_enabled=params.get("deletionProtectionEnabled"),
75-
kms_encryption_key=params.get("kmsEncryptionKey"),
76-
multi_region_properties=params.get("multiRegionProperties"),
73+
deletion_protection_enabled=self._get_bool_param(
74+
"deletionProtectionEnabled"
75+
),
76+
kms_encryption_key=self._get_param("kmsEncryptionKey"),
77+
multi_region_properties=self._get_param("multiRegionProperties"),
7778
)
7879
return ActionResult(
7980
{
@@ -85,75 +86,70 @@ def update_cluster(self) -> ActionResult:
8586
)
8687

8788
def get_vpc_endpoint_service_name(self) -> ActionResult:
88-
identifier = self.path.split("/")[-2]
89+
identifier = self._get_param("identifier")
8990
result = self.dsql_backend.get_vpc_endpoint_service_name(identifier)
9091
return ActionResult(result)
9192

9293
def list_tags_for_resource(self) -> ActionResult:
93-
arn = unquote(self.path.split("/tags/", 1)[-1])
94-
identifier = arn.split("cluster/")[-1]
95-
tags = self.dsql_backend.list_tags_for_resource(identifier)
94+
resource_arn = self._get_param("resourceArn")
95+
tags = self.dsql_backend.list_tags_for_resource(resource_arn)
9696
return ActionResult({"tags": tags})
9797

9898
def tag_resource(self) -> ActionResult:
99-
params = json.loads(self.body)
100-
arn = unquote(self.path.split("/tags/", 1)[-1])
101-
identifier = arn.split("cluster/")[-1]
102-
self.dsql_backend.tag_resource(identifier, params["tags"])
103-
return ActionResult({})
99+
resource_arn = self._get_param("resourceArn")
100+
self.dsql_backend.tag_resource(resource_arn, self._get_param("tags"))
101+
return EmptyResult()
104102

105103
def untag_resource(self) -> ActionResult:
106-
arn = unquote(self.path.split("/tags/", 1)[-1])
107-
identifier = arn.split("cluster/")[-1]
108-
self.dsql_backend.untag_resource(
109-
identifier, self.querystring.get("tagKeys", [])
110-
)
111-
return ActionResult({})
104+
resource_arn = self._get_param("resourceArn")
105+
self.dsql_backend.untag_resource(resource_arn, self._get_param("tagKeys", []))
106+
return EmptyResult()
112107

113108
def put_cluster_policy(self) -> ActionResult:
114-
params = json.loads(self.body)
115-
identifier = self.path.split("/")[-2]
109+
identifier = self._get_param("identifier")
116110
version = self.dsql_backend.put_cluster_policy(
117-
identifier, params["policy"], params.get("expectedPolicyVersion")
111+
identifier,
112+
self._get_param("policy"),
113+
self._get_param("expectedPolicyVersion"),
118114
)
119115
return ActionResult({"policyVersion": version})
120116

121117
def get_cluster_policy(self) -> ActionResult:
122-
identifier = self.path.split("/")[-2]
118+
identifier = self._get_param("identifier")
123119
policy, version = self.dsql_backend.get_cluster_policy(identifier)
124120
return ActionResult({"policy": policy, "policyVersion": version})
125121

126122
def delete_cluster_policy(self) -> ActionResult:
127-
identifier = self.path.split("/")[-2]
123+
identifier = self._get_param("identifier")
128124
version = self.dsql_backend.delete_cluster_policy(
129-
identifier, self._get_param("expected-policy-version")
125+
identifier, self._get_param("expectedPolicyVersion")
130126
)
131127
return ActionResult({"policyVersion": version})
132128

133129
def create_stream(self) -> ActionResult:
134-
params = json.loads(self.body)
135-
cluster_identifier = self.path.split("/")[-1]
130+
cluster_identifier = self._get_param("clusterIdentifier")
136131
stream = self.dsql_backend.create_stream(
137132
cluster_identifier=cluster_identifier,
138-
target_definition=params["targetDefinition"],
139-
ordering=params["ordering"],
140-
format_=params["format"],
141-
tags=params.get("tags"),
142-
client_token=params.get("clientToken"),
133+
target_definition=self._get_param("targetDefinition"),
134+
ordering=self._get_param("ordering"),
135+
format_=self._get_param("format"),
136+
tags=self._get_param("tags"),
137+
client_token=self._get_param("clientToken"),
143138
)
144139
return ActionResult(stream)
145140

146141
def get_stream(self) -> ActionResult:
147-
cluster_identifier, stream_identifier = self.path.split("/")[-2:]
142+
cluster_identifier = self._get_param("clusterIdentifier")
143+
stream_identifier = self._get_param("streamIdentifier")
148144
stream = self.dsql_backend.get_stream(cluster_identifier, stream_identifier)
149145
return ActionResult(stream)
150146

151147
def list_streams(self) -> ActionResult:
152-
cluster_identifier = self.path.split("/")[-1]
148+
cluster_identifier = self._get_param("clusterIdentifier")
153149
streams, next_token = self.dsql_backend.list_streams(
154150
cluster_identifier=cluster_identifier,
155-
max_results=self._get_int_param("max-results"),
156-
next_token=self._get_param("next-token"),
151+
max_results=self._get_param("maxResults"),
152+
next_token=self._get_param("nextToken"),
157153
)
158154
result: dict[str, Any] = {
159155
"streams": [stream.to_summary() for stream in streams]
@@ -163,7 +159,8 @@ def list_streams(self) -> ActionResult:
163159
return ActionResult(result)
164160

165161
def delete_stream(self) -> ActionResult:
166-
cluster_identifier, stream_identifier = self.path.split("/")[-2:]
162+
cluster_identifier = self._get_param("clusterIdentifier")
163+
stream_identifier = self._get_param("streamIdentifier")
167164
stream = self.dsql_backend.delete_stream(cluster_identifier, stream_identifier)
168165
return ActionResult(
169166
{

0 commit comments

Comments
 (0)