Skip to content

Commit

Permalink
Core: Add RequestId-header to all responses (#7210)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblommers committed Jan 27, 2024
1 parent 168b869 commit 1f1e0ca
Show file tree
Hide file tree
Showing 23 changed files with 85 additions and 317 deletions.
31 changes: 6 additions & 25 deletions moto/autoscaling/responses.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from moto.core.common_types import TYPE_RESPONSE
from moto.core.responses import BaseResponse
from moto.core.utils import iso_8601_datetime_with_milliseconds
from moto.utilities.aws_headers import amz_crc32, amzn_request_id
from moto.utilities.aws_headers import amz_crc32

from .models import AutoScalingBackend, autoscaling_backends

Expand All @@ -13,6 +14,10 @@ def __init__(self) -> None:
def autoscaling_backend(self) -> AutoScalingBackend:
return autoscaling_backends[self.current_account][self.region]

@amz_crc32
def call_action(self) -> TYPE_RESPONSE:
return super().call_action()

def create_launch_configuration(self) -> str:
instance_monitoring_string = self._get_param("InstanceMonitoring.Enabled")
if instance_monitoring_string == "true":
Expand Down Expand Up @@ -139,17 +144,13 @@ def describe_scaling_activities(self) -> str:
template = self.response_template(DESCRIBE_SCALING_ACTIVITIES_TEMPLATE)
return template.render()

@amz_crc32
@amzn_request_id
def attach_instances(self) -> str:
group_name = self._get_param("AutoScalingGroupName")
instance_ids = self._get_multi_param("InstanceIds.member")
self.autoscaling_backend.attach_instances(group_name, instance_ids)
template = self.response_template(ATTACH_INSTANCES_TEMPLATE)
return template.render()

@amz_crc32
@amzn_request_id
def set_instance_health(self) -> str:
instance_id = self._get_param("InstanceId")
health_status = self._get_param("HealthStatus")
Expand All @@ -159,8 +160,6 @@ def set_instance_health(self) -> str:
template = self.response_template(SET_INSTANCE_HEALTH_TEMPLATE)
return template.render()

@amz_crc32
@amzn_request_id
def detach_instances(self) -> str:
group_name = self._get_param("AutoScalingGroupName")
instance_ids = self._get_multi_param("InstanceIds.member")
Expand All @@ -175,8 +174,6 @@ def detach_instances(self) -> str:
template = self.response_template(DETACH_INSTANCES_TEMPLATE)
return template.render(detached_instances=detached_instances)

@amz_crc32
@amzn_request_id
def attach_load_balancer_target_groups(self) -> str:
group_name = self._get_param("AutoScalingGroupName")
target_group_arns = self._get_multi_param("TargetGroupARNs.member")
Expand All @@ -187,8 +184,6 @@ def attach_load_balancer_target_groups(self) -> str:
template = self.response_template(ATTACH_LOAD_BALANCER_TARGET_GROUPS_TEMPLATE)
return template.render()

@amz_crc32
@amzn_request_id
def describe_load_balancer_target_groups(self) -> str:
group_name = self._get_param("AutoScalingGroupName")
target_group_arns = (
Expand All @@ -197,8 +192,6 @@ def describe_load_balancer_target_groups(self) -> str:
template = self.response_template(DESCRIBE_LOAD_BALANCER_TARGET_GROUPS)
return template.render(target_group_arns=target_group_arns)

@amz_crc32
@amzn_request_id
def detach_load_balancer_target_groups(self) -> str:
group_name = self._get_param("AutoScalingGroupName")
target_group_arns = self._get_multi_param("TargetGroupARNs.member")
Expand Down Expand Up @@ -347,34 +340,26 @@ def execute_policy(self) -> str:
template = self.response_template(EXECUTE_POLICY_TEMPLATE)
return template.render()

@amz_crc32
@amzn_request_id
def attach_load_balancers(self) -> str:
group_name = self._get_param("AutoScalingGroupName")
load_balancer_names = self._get_multi_param("LoadBalancerNames.member")
self.autoscaling_backend.attach_load_balancers(group_name, load_balancer_names)
template = self.response_template(ATTACH_LOAD_BALANCERS_TEMPLATE)
return template.render()

@amz_crc32
@amzn_request_id
def describe_load_balancers(self) -> str:
group_name = self._get_param("AutoScalingGroupName")
load_balancers = self.autoscaling_backend.describe_load_balancers(group_name)
template = self.response_template(DESCRIBE_LOAD_BALANCERS_TEMPLATE)
return template.render(load_balancers=load_balancers)

@amz_crc32
@amzn_request_id
def detach_load_balancers(self) -> str:
group_name = self._get_param("AutoScalingGroupName")
load_balancer_names = self._get_multi_param("LoadBalancerNames.member")
self.autoscaling_backend.detach_load_balancers(group_name, load_balancer_names)
template = self.response_template(DETACH_LOAD_BALANCERS_TEMPLATE)
return template.render()

@amz_crc32
@amzn_request_id
def enter_standby(self) -> str:
group_name = self._get_param("AutoScalingGroupName")
instance_ids = self._get_multi_param("InstanceIds.member")
Expand All @@ -399,8 +384,6 @@ def enter_standby(self) -> str:
timestamp=iso_8601_datetime_with_milliseconds(),
)

@amz_crc32
@amzn_request_id
def exit_standby(self) -> str:
group_name = self._get_param("AutoScalingGroupName")
instance_ids = self._get_multi_param("InstanceIds.member")
Expand Down Expand Up @@ -445,8 +428,6 @@ def set_instance_protection(self) -> str:
template = self.response_template(SET_INSTANCE_PROTECTION_TEMPLATE)
return template.render()

@amz_crc32
@amzn_request_id
def terminate_instance_in_auto_scaling_group(self) -> str:
instance_id = self._get_param("InstanceId")
should_decrement_string = self._get_param("ShouldDecrementDesiredCapacity")
Expand Down
8 changes: 3 additions & 5 deletions moto/awslambda/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from moto.core.responses import TYPE_RESPONSE, BaseResponse
from moto.core.utils import path_url
from moto.utilities.aws_headers import amz_crc32, amzn_request_id
from moto.utilities.aws_headers import amz_crc32

from .exceptions import (
FunctionAlreadyExists,
Expand Down Expand Up @@ -129,9 +129,8 @@ def versions(self, request: Any, full_url: str, headers: Any) -> TYPE_RESPONSE:
raise ValueError("Cannot handle request")

@amz_crc32
@amzn_request_id
def invoke( # type: ignore
self, request=None, full_url="", headers=None
def invoke(
self, request: Any, full_url: str, headers: Any
) -> Tuple[int, Dict[str, str], Union[str, bytes]]:
self.setup_class(request, full_url, headers)
if request.method == "POST":
Expand All @@ -140,7 +139,6 @@ def invoke( # type: ignore
raise ValueError("Cannot handle request")

@amz_crc32
@amzn_request_id
def invoke_async(
self, request: Any, full_url: str, headers: Any
) -> Tuple[int, Dict[str, str], Union[str, bytes]]:
Expand Down
39 changes: 0 additions & 39 deletions moto/batch/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from moto.core.models import default_user_config
from moto.core.responses import BaseResponse
from moto.utilities.aws_headers import amzn_request_id

from .models import BatchBackend, batch_backends

Expand All @@ -25,8 +24,6 @@ def _get_action(self) -> str:
# Return element after the /v1/*
return urlsplit(self.uri).path.lstrip("/").split("/")[1]

# CreateComputeEnvironment
@amzn_request_id
def createcomputeenvironment(self) -> str:
compute_env_name = self._get_param("computeEnvironmentName")
compute_resource = self._get_param("computeResources")
Expand All @@ -46,8 +43,6 @@ def createcomputeenvironment(self) -> str:

return json.dumps(result)

# DescribeComputeEnvironments
@amzn_request_id
def describecomputeenvironments(self) -> str:
compute_environments = self._get_param("computeEnvironments")

Expand All @@ -56,17 +51,13 @@ def describecomputeenvironments(self) -> str:
result = {"computeEnvironments": envs}
return json.dumps(result)

# DeleteComputeEnvironment
@amzn_request_id
def deletecomputeenvironment(self) -> str:
compute_environment = self._get_param("computeEnvironment")

self.batch_backend.delete_compute_environment(compute_environment)

return ""

# UpdateComputeEnvironment
@amzn_request_id
def updatecomputeenvironment(self) -> str:
compute_env_name = self._get_param("computeEnvironment")
compute_resource = self._get_param("computeResources")
Expand All @@ -84,8 +75,6 @@ def updatecomputeenvironment(self) -> str:

return json.dumps(result)

# CreateJobQueue
@amzn_request_id
def createjobqueue(self) -> str:
compute_env_order = self._get_param("computeEnvironmentOrder")
queue_name = self._get_param("jobQueueName")
Expand All @@ -107,8 +96,6 @@ def createjobqueue(self) -> str:

return json.dumps(result)

# DescribeJobQueues
@amzn_request_id
def describejobqueues(self) -> str:
job_queues = self._get_param("jobQueues")

Expand All @@ -117,8 +104,6 @@ def describejobqueues(self) -> str:
result = {"jobQueues": queues}
return json.dumps(result)

# UpdateJobQueue
@amzn_request_id
def updatejobqueue(self) -> str:
compute_env_order = self._get_param("computeEnvironmentOrder")
queue_name = self._get_param("jobQueue")
Expand All @@ -138,17 +123,13 @@ def updatejobqueue(self) -> str:

return json.dumps(result)

# DeleteJobQueue
@amzn_request_id
def deletejobqueue(self) -> str:
queue_name = self._get_param("jobQueue")

self.batch_backend.delete_job_queue(queue_name)

return ""

# RegisterJobDefinition
@amzn_request_id
def registerjobdefinition(self) -> str:
container_properties = self._get_param("containerProperties")
node_properties = self._get_param("nodeProperties")
Expand Down Expand Up @@ -181,17 +162,13 @@ def registerjobdefinition(self) -> str:

return json.dumps(result)

# DeregisterJobDefinition
@amzn_request_id
def deregisterjobdefinition(self) -> str:
queue_name = self._get_param("jobDefinition")

self.batch_backend.deregister_job_definition(queue_name)

return ""

# DescribeJobDefinitions
@amzn_request_id
def describejobdefinitions(self) -> str:
job_def_name = self._get_param("jobDefinitionName")
job_def_list = self._get_param("jobDefinitions")
Expand All @@ -204,8 +181,6 @@ def describejobdefinitions(self) -> str:
result = {"jobDefinitions": [job.describe() for job in job_defs]}
return json.dumps(result)

# SubmitJob
@amzn_request_id
def submitjob(self) -> str:
container_overrides = self._get_param("containerOverrides")
depends_on = self._get_param("dependsOn")
Expand All @@ -229,15 +204,11 @@ def submitjob(self) -> str:

return json.dumps(result)

# DescribeJobs
@amzn_request_id
def describejobs(self) -> str:
jobs = self._get_param("jobs")

return json.dumps({"jobs": self.batch_backend.describe_jobs(jobs)})

# ListJobs
@amzn_request_id
def listjobs(self) -> str:
job_queue = self._get_param("jobQueue")
job_status = self._get_param("jobStatus")
Expand All @@ -248,8 +219,6 @@ def listjobs(self) -> str:
result = {"jobSummaryList": [job.describe_short() for job in jobs]}
return json.dumps(result)

# TerminateJob
@amzn_request_id
def terminatejob(self) -> str:
job_id = self._get_param("jobId")
reason = self._get_param("reason")
Expand All @@ -258,16 +227,13 @@ def terminatejob(self) -> str:

return ""

# CancelJob
@amzn_request_id
def canceljob(self) -> str:
job_id = self._get_param("jobId")
reason = self._get_param("reason")
self.batch_backend.cancel_job(job_id, reason)

return ""

@amzn_request_id
def tags(self) -> str:
resource_arn = unquote(self.path).split("/v1/tags/")[-1]
tags = self._get_param("tags")
Expand All @@ -281,7 +247,6 @@ def tags(self) -> str:
self.batch_backend.untag_resource(resource_arn, tag_keys) # type: ignore[arg-type]
return ""

@amzn_request_id
def createschedulingpolicy(self) -> str:
body = json.loads(self.body)
name = body.get("name")
Expand All @@ -292,26 +257,22 @@ def createschedulingpolicy(self) -> str:
)
return json.dumps(policy.to_dict(create=True))

@amzn_request_id
def describeschedulingpolicies(self) -> str:
body = json.loads(self.body)
arns = body.get("arns") or []
policies = self.batch_backend.describe_scheduling_policies(arns)
return json.dumps({"schedulingPolicies": [pol.to_dict() for pol in policies]})

@amzn_request_id
def listschedulingpolicies(self) -> str:
arns = self.batch_backend.list_scheduling_policies()
return json.dumps({"schedulingPolicies": [{"arn": arn} for arn in arns]})

@amzn_request_id
def deleteschedulingpolicy(self) -> str:
body = json.loads(self.body)
arn = body["arn"]
self.batch_backend.delete_scheduling_policy(arn)
return ""

@amzn_request_id
def updateschedulingpolicy(self) -> str:
body = json.loads(self.body)
arn = body.get("arn")
Expand Down
3 changes: 0 additions & 3 deletions moto/cloudformation/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from moto.core.responses import BaseResponse
from moto.s3.exceptions import S3ClientError
from moto.utilities.aws_headers import amzn_request_id

from .exceptions import MissingParameterError, ValidationError
from .models import CloudFormationBackend, FakeStack, cloudformation_backends
Expand Down Expand Up @@ -153,7 +152,6 @@ def stack_name_exists(self, new_stack_name: str) -> bool:
return True
return False

@amzn_request_id
def create_change_set(self) -> str:
stack_name = self._get_param("StackName")
change_set_name = self._get_param("ChangeSetName")
Expand Down Expand Up @@ -220,7 +218,6 @@ def describe_change_set(self) -> str:
template = self.response_template(DESCRIBE_CHANGE_SET_RESPONSE_TEMPLATE)
return template.render(change_set=change_set)

@amzn_request_id
def execute_change_set(self) -> str:
stack_name = self._get_param("StackName")
change_set_name = self._get_param("ChangeSetName")
Expand Down
Loading

0 comments on commit 1f1e0ca

Please sign in to comment.