From 57bc607307c76f277b3b39da3282be9a2007fe12 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Fri, 5 Jan 2024 23:40:46 +0000 Subject: [PATCH] Techdebt: Simplify MQ routing (#7190) --- moto/mq/exceptions.py | 37 +++-------- moto/mq/responses.py | 142 ++++++++++++------------------------------ moto/mq/urls.py | 30 +++------ 3 files changed, 56 insertions(+), 153 deletions(-) diff --git a/moto/mq/exceptions.py b/moto/mq/exceptions.py index eba0efe41..b2c596a62 100644 --- a/moto/mq/exceptions.py +++ b/moto/mq/exceptions.py @@ -1,5 +1,4 @@ import json -from typing import Any from moto.core.exceptions import JsonRESTError @@ -11,58 +10,38 @@ class MQError(JsonRESTError): class UnknownBroker(MQError): def __init__(self, broker_id: str): super().__init__("NotFoundException", "Can't find requested broker") - self.broker_id = broker_id - - def get_body( - self, *args: Any, **kwargs: Any - ) -> str: # pylint: disable=unused-argument body = { "errorAttribute": "broker-id", - "message": f"Can't find requested broker [{self.broker_id}]. Make sure your broker exists.", + "message": f"Can't find requested broker [{broker_id}]. Make sure your broker exists.", } - return json.dumps(body) + self.description = json.dumps(body) class UnknownConfiguration(MQError): def __init__(self, config_id: str): super().__init__("NotFoundException", "Can't find requested configuration") - self.config_id = config_id - - def get_body( - self, *args: Any, **kwargs: Any - ) -> str: # pylint: disable=unused-argument body = { "errorAttribute": "configuration_id", - "message": f"Can't find requested configuration [{self.config_id}]. Make sure your configuration exists.", + "message": f"Can't find requested configuration [{config_id}]. Make sure your configuration exists.", } - return json.dumps(body) + self.description = json.dumps(body) class UnknownUser(MQError): def __init__(self, username: str): super().__init__("NotFoundException", "Can't find requested user") - self.username = username - - def get_body( - self, *args: Any, **kwargs: Any - ) -> str: # pylint: disable=unused-argument body = { "errorAttribute": "username", - "message": f"Can't find requested user [{self.username}]. Make sure your user exists.", + "message": f"Can't find requested user [{username}]. Make sure your user exists.", } - return json.dumps(body) + self.description = json.dumps(body) class UnknownEngineType(MQError): def __init__(self, engine_type: str): super().__init__("BadRequestException", "") - self.engine_type = engine_type - - def get_body( - self, *args: Any, **kwargs: Any - ) -> str: # pylint: disable=unused-argument body = { "errorAttribute": "engineType", - "message": f"Broker engine type [{self.engine_type}] is invalid. Valid values are: [ACTIVEMQ]", + "message": f"Broker engine type [{engine_type}] is invalid. Valid values are: [ACTIVEMQ]", } - return json.dumps(body) + self.description = json.dumps(body) diff --git a/moto/mq/responses.py b/moto/mq/responses.py index ce3d9c1a3..2365076fe 100644 --- a/moto/mq/responses.py +++ b/moto/mq/responses.py @@ -1,9 +1,7 @@ """Handles incoming mq requests, invokes methods, returns responses.""" import json -from typing import Any from urllib.parse import unquote -from moto.core.common_types import TYPE_RESPONSE from moto.core.responses import BaseResponse from .models import MQBackend, mq_backends @@ -20,67 +18,7 @@ class MQResponse(BaseResponse): """Return backend instance specific for this region.""" return mq_backends[self.current_account][self.region] - def broker(self, request: Any, full_url: str, headers: Any) -> TYPE_RESPONSE: # type: ignore[return] - self.setup_class(request, full_url, headers) - if request.method == "GET": - return self.describe_broker() - if request.method == "DELETE": - return self.delete_broker() - if request.method == "PUT": - return self.update_broker() - - def brokers(self, request: Any, full_url: str, headers: Any) -> TYPE_RESPONSE: # type: ignore[return] - self.setup_class(request, full_url, headers) - if request.method == "POST": - return self.create_broker() - if request.method == "GET": - return self.list_brokers() - - def configuration(self, request: Any, full_url: str, headers: Any) -> TYPE_RESPONSE: # type: ignore[return] - self.setup_class(request, full_url, headers) - if request.method == "GET": - return self.describe_configuration() - if request.method == "PUT": - return self.update_configuration() - - def configurations(self, request: Any, full_url: str, headers: Any) -> TYPE_RESPONSE: # type: ignore[return] - self.setup_class(request, full_url, headers) - if request.method == "POST": - return self.create_configuration() - if request.method == "GET": - return self.list_configurations() - - def configuration_revision(self, request: Any, full_url: str, headers: Any) -> TYPE_RESPONSE: # type: ignore[return] - self.setup_class(request, full_url, headers) - if request.method == "GET": - return self.get_configuration_revision() - - def tags(self, request: Any, full_url: str, headers: Any) -> TYPE_RESPONSE: # type: ignore[return] - self.setup_class(request, full_url, headers) - if request.method == "POST": - return self.create_tags() - if request.method == "DELETE": - return self.delete_tags() - if request.method == "GET": - return self.list_tags() - - def user(self, request: Any, full_url: str, headers: Any) -> TYPE_RESPONSE: # type: ignore[return] - self.setup_class(request, full_url, headers) - if request.method == "POST": - return self.create_user() - if request.method == "GET": - return self.describe_user() - if request.method == "PUT": - return self.update_user() - if request.method == "DELETE": - return self.delete_user() - - def users(self, request: Any, full_url: str, headers: Any) -> TYPE_RESPONSE: # type: ignore[return] - self.setup_class(request, full_url, headers) - if request.method == "GET": - return self.list_users() - - def create_broker(self) -> TYPE_RESPONSE: + def create_broker(self) -> str: params = json.loads(self.body) authentication_strategy = params.get("authenticationStrategy") auto_minor_version_upgrade = params.get("autoMinorVersionUpgrade") @@ -122,9 +60,9 @@ class MQResponse(BaseResponse): ) # Lowercase members - boto3 will convert it into UpperCase resp = {"brokerArn": broker_arn, "brokerId": broker_id} - return 200, {}, json.dumps(resp) + return json.dumps(resp) - def update_broker(self) -> TYPE_RESPONSE: + def update_broker(self) -> str: params = json.loads(self.body) broker_id = self.path.split("/")[-1] authentication_strategy = params.get("authenticationStrategy") @@ -150,32 +88,32 @@ class MQResponse(BaseResponse): ) return self.describe_broker() - def delete_broker(self) -> TYPE_RESPONSE: + def delete_broker(self) -> str: broker_id = self.path.split("/")[-1] self.mq_backend.delete_broker(broker_id=broker_id) - return 200, {}, json.dumps(dict(brokerId=broker_id)) + return json.dumps(dict(brokerId=broker_id)) - def describe_broker(self) -> TYPE_RESPONSE: + def describe_broker(self) -> str: broker_id = self.path.split("/")[-1] broker = self.mq_backend.describe_broker(broker_id=broker_id) resp = broker.to_json() resp["tags"] = self.mq_backend.list_tags(broker.arn) - return 200, {}, json.dumps(resp) + return json.dumps(resp) - def list_brokers(self) -> TYPE_RESPONSE: + def list_brokers(self) -> str: brokers = self.mq_backend.list_brokers() - return 200, {}, json.dumps(dict(brokerSummaries=[b.summary() for b in brokers])) + return json.dumps(dict(brokerSummaries=[b.summary() for b in brokers])) - def create_user(self) -> TYPE_RESPONSE: + def create_user(self) -> str: params = json.loads(self.body) broker_id = self.path.split("/")[-3] username = self.path.split("/")[-1] console_access = params.get("consoleAccess", False) groups = params.get("groups", []) self.mq_backend.create_user(broker_id, username, console_access, groups) - return 200, {}, "{}" + return "{}" - def update_user(self) -> TYPE_RESPONSE: + def update_user(self) -> str: params = json.loads(self.body) broker_id = self.path.split("/")[-3] username = self.path.split("/")[-1] @@ -187,30 +125,30 @@ class MQResponse(BaseResponse): groups=groups, username=username, ) - return 200, {}, "{}" + return "{}" - def describe_user(self) -> TYPE_RESPONSE: + def describe_user(self) -> str: broker_id = self.path.split("/")[-3] username = self.path.split("/")[-1] user = self.mq_backend.describe_user(broker_id, username) - return 200, {}, json.dumps(user.to_json()) + return json.dumps(user.to_json()) - def delete_user(self) -> TYPE_RESPONSE: + def delete_user(self) -> str: broker_id = self.path.split("/")[-3] username = self.path.split("/")[-1] self.mq_backend.delete_user(broker_id, username) - return 200, {}, "{}" + return "{}" - def list_users(self) -> TYPE_RESPONSE: + def list_users(self) -> str: broker_id = self.path.split("/")[-2] users = self.mq_backend.list_users(broker_id=broker_id) resp = { "brokerId": broker_id, "users": [{"username": u.username} for u in users], } - return 200, {}, json.dumps(resp) + return json.dumps(resp) - def create_configuration(self) -> TYPE_RESPONSE: + def create_configuration(self) -> str: params = json.loads(self.body) name = params.get("name") engine_type = params.get("engineType") @@ -220,56 +158,54 @@ class MQResponse(BaseResponse): config = self.mq_backend.create_configuration( name, engine_type, engine_version, tags ) - return 200, {}, json.dumps(config.to_json()) + return json.dumps(config.to_json()) - def describe_configuration(self) -> TYPE_RESPONSE: + def describe_configuration(self) -> str: config_id = self.path.split("/")[-1] config = self.mq_backend.describe_configuration(config_id) resp = config.to_json() resp["tags"] = self.mq_backend.list_tags(config.arn) - return 200, {}, json.dumps(resp) + return json.dumps(resp) - def list_configurations(self) -> TYPE_RESPONSE: + def list_configurations(self) -> str: configs = self.mq_backend.list_configurations() resp = {"configurations": [c.to_json() for c in configs]} - return 200, {}, json.dumps(resp) + return json.dumps(resp) - def update_configuration(self) -> TYPE_RESPONSE: + def update_configuration(self) -> str: config_id = self.path.split("/")[-1] params = json.loads(self.body) data = params.get("data") description = params.get("description") config = self.mq_backend.update_configuration(config_id, data, description) - return 200, {}, json.dumps(config.to_json()) + return json.dumps(config.to_json()) - def get_configuration_revision(self) -> TYPE_RESPONSE: + def describe_configuration_revision(self) -> str: revision_id = self.path.split("/")[-1] config_id = self.path.split("/")[-3] revision = self.mq_backend.describe_configuration_revision( config_id, revision_id ) - return 200, {}, json.dumps(revision.to_json()) + return json.dumps(revision.to_json()) - def create_tags(self) -> TYPE_RESPONSE: + def create_tags(self) -> str: resource_arn = unquote(self.path.split("/")[-1]) tags = json.loads(self.body).get("tags", {}) self.mq_backend.create_tags(resource_arn, tags) - return 200, {}, "{}" + return "{}" - def delete_tags(self) -> TYPE_RESPONSE: + def delete_tags(self) -> str: resource_arn = unquote(self.path.split("/")[-1]) tag_keys = self._get_param("tagKeys") self.mq_backend.delete_tags(resource_arn, tag_keys) - return 200, {}, "{}" + return "{}" - def list_tags(self) -> TYPE_RESPONSE: + def list_tags(self) -> str: resource_arn = unquote(self.path.split("/")[-1]) tags = self.mq_backend.list_tags(resource_arn) - return 200, {}, json.dumps({"tags": tags}) + return json.dumps({"tags": tags}) - def reboot(self, request: Any, full_url: str, headers: Any) -> TYPE_RESPONSE: # type: ignore[return] - self.setup_class(request, full_url, headers) - if request.method == "POST": - broker_id = self.path.split("/")[-2] - self.mq_backend.reboot_broker(broker_id=broker_id) - return 200, {}, "{}" + def reboot_broker(self) -> str: + broker_id = self.path.split("/")[-2] + self.mq_backend.reboot_broker(broker_id=broker_id) + return "{}" diff --git a/moto/mq/urls.py b/moto/mq/urls.py index df059e42f..7fde551b7 100644 --- a/moto/mq/urls.py +++ b/moto/mq/urls.py @@ -7,25 +7,13 @@ url_bases = [ url_paths = { - "{0}/v1/brokers/(?P[^/]+)$": MQResponse.method_dispatch( - MQResponse.broker - ), - "{0}/v1/brokers/(?P[^/]+)/reboot$": MQResponse.method_dispatch( - MQResponse.reboot - ), - "{0}/v1/brokers/(?P[^/]+)/users$": MQResponse.method_dispatch( - MQResponse.users - ), - "{0}/v1/brokers/(?P[^/]+)/users/(?P[^/]+)$": MQResponse.method_dispatch( - MQResponse.user - ), - "{0}/v1/brokers$": MQResponse.method_dispatch(MQResponse.brokers), - "{0}/v1/configurations$": MQResponse.method_dispatch(MQResponse.configurations), - "{0}/v1/configurations/(?P[^/]+)$": MQResponse.method_dispatch( - MQResponse.configuration - ), - "{0}/v1/configurations/(?P[^/]+)/revisions/(?P[^/]+)$": MQResponse.method_dispatch( - MQResponse.configuration_revision - ), - "{0}/v1/tags/(?P[^/]+)$": MQResponse.method_dispatch(MQResponse.tags), + "{0}/v1/brokers/(?P[^/]+)$": MQResponse.dispatch, + "{0}/v1/brokers/(?P[^/]+)/reboot$": MQResponse.dispatch, + "{0}/v1/brokers/(?P[^/]+)/users$": MQResponse.dispatch, + "{0}/v1/brokers/(?P[^/]+)/users/(?P[^/]+)$": MQResponse.dispatch, + "{0}/v1/brokers$": MQResponse.dispatch, + "{0}/v1/configurations$": MQResponse.dispatch, + "{0}/v1/configurations/(?P[^/]+)$": MQResponse.dispatch, + "{0}/v1/configurations/(?P[^/]+)/revisions/(?P[^/]+)$": MQResponse.dispatch, + "{0}/v1/tags/(?P[^/]+)$": MQResponse.dispatch, }