1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
|
"""EMRServerlessBackend class with methods for supported APIs."""
import inspect
import re
from collections.abc import Iterator
from datetime import datetime
from typing import Any, Optional, Union
from moto.core.base_backend import BackendDict, BaseBackend
from moto.core.common_models import BaseModel
from moto.core.utils import iso_8601_datetime_without_milliseconds
from moto.emrcontainers.utils import paginated_list
from moto.utilities.utils import get_partition
from .exceptions import (
AccessDeniedException,
ResourceNotFoundException,
ValidationException,
)
from .utils import (
default_auto_start_configuration,
default_auto_stop_configuration,
random_appplication_id,
random_job_id,
)
APPLICATION_ARN_TEMPLATE = "arn:{partition}:emr-serverless:{region}:{account_id}:/applications/{application_id}"
JOB_RUN_ARN_TEMPLATE = "arn:{partition}:emr-serverless:{region}:{account_id}:/applications/{application_id}/jobruns/{job_run_id}"
# Defaults used for creating an EMR Serverless application
APPLICATION_STATUS = "STARTED"
JOB_STATUS = "SUCCESS"
class FakeApplication(BaseModel):
def __init__(
self,
name: str,
release_label: str,
application_type: str,
client_token: str,
account_id: str,
region_name: str,
initial_capacity: str,
maximum_capacity: str,
tags: dict[str, str],
auto_start_configuration: str,
auto_stop_configuration: str,
network_configuration: str,
):
# Provided parameters
self.name = name
self.release_label = release_label
self.application_type = application_type.capitalize()
self.client_token = client_token
self.initial_capacity = initial_capacity
self.maximum_capacity = maximum_capacity
self.auto_start_configuration = (
auto_start_configuration or default_auto_start_configuration()
)
self.auto_stop_configuration = (
auto_stop_configuration or default_auto_stop_configuration()
)
self.network_configuration = network_configuration
self.tags: dict[str, str] = tags or {}
# Service-generated-parameters
self.id = random_appplication_id()
self.arn = APPLICATION_ARN_TEMPLATE.format(
partition="aws",
region=region_name,
account_id=account_id,
application_id=self.id,
)
self.state = APPLICATION_STATUS
self.state_details = ""
self.created_at = iso_8601_datetime_without_milliseconds(
datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
)
self.updated_at = self.created_at
def __iter__(self) -> Iterator[tuple[str, Any]]:
yield "applicationId", self.id
yield "name", self.name
yield "arn", self.arn
yield (
"autoStartConfig",
self.auto_start_configuration,
)
yield (
"autoStopConfig",
self.auto_stop_configuration,
)
def to_dict(self) -> dict[str, Any]:
"""
Dictionary representation of an EMR Serverless Application.
When used in `list-applications`, capacity, auto-start/stop configs, and tags are not returned. https://docs.aws.amazon.com/emr-serverless/latest/APIReference/API_ListApplications.html
When used in `get-application`, more details are returned. https://docs.aws.amazon.com/emr-serverless/latest/APIReference/API_GetApplication.html#API_GetApplication_ResponseSyntax
"""
caller_methods = inspect.stack()[1].function
caller_methods_type = caller_methods.split("_")[0]
if caller_methods_type in ["get", "update"]:
response = {
"applicationId": self.id,
"name": self.name,
"arn": self.arn,
"releaseLabel": self.release_label,
"type": self.application_type,
"state": self.state,
"stateDetails": self.state_details,
"createdAt": self.created_at,
"updatedAt": self.updated_at,
"autoStartConfiguration": self.auto_start_configuration,
"autoStopConfiguration": self.auto_stop_configuration,
"tags": self.tags,
}
else:
response = {
"id": self.id,
"name": self.name,
"arn": self.arn,
"releaseLabel": self.release_label,
"type": self.application_type,
"state": self.state,
"stateDetails": self.state_details,
"createdAt": self.created_at,
"updatedAt": self.updated_at,
}
if self.network_configuration:
response.update({"networkConfiguration": self.network_configuration})
if self.initial_capacity:
response.update({"initialCapacity": self.initial_capacity})
if self.maximum_capacity:
response.update({"maximumCapacity": self.maximum_capacity})
return response
class FakeJobRun(BaseModel):
def __init__(
self,
application_id: str,
client_token: str,
execution_role_arn: str,
account_id: str,
region_name: str,
release_label: str,
application_type: str,
job_driver: Optional[dict[str, dict[str, Union[str, list[str]]]]],
configuration_overrides: Optional[dict[str, Union[list[Any], dict[str, Any]]]],
tags: Optional[dict[str, str]],
network_configuration: Optional[dict[str, list[str]]],
execution_timeout_minutes: Optional[int],
name: Optional[str],
):
self.name = name
self.application_id = application_id
self.client_token = client_token
self.execution_role_arn = execution_role_arn
self.job_driver = job_driver
self.configuration_overrides = configuration_overrides
self.network_configuration = network_configuration
self.execution_timeout_minutes = execution_timeout_minutes or 720
# Service-generated-parameters
self.id = random_job_id()
self.arn = JOB_RUN_ARN_TEMPLATE.format(
partition="aws",
account_id=account_id,
application_id=self.application_id,
region=region_name,
job_run_id=self.id,
)
self.release_label = release_label
self.application_type = application_type
self.state = JOB_STATUS
self.state_details: Optional[str] = None
self.created_by: Optional[str] = None
self.created_at: str = iso_8601_datetime_without_milliseconds(
datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
)
self.updated_at: str = self.created_at
self.total_execution_duration_seconds: int = 0
self.billed_resource_utilization: dict[str, float] = {
"vCPUHour": 0.0,
"memoryGBHour": 0.0,
"storageGBHour": 0.0,
}
self.tags = tags
def to_dict(self, caller_methods_type: str) -> dict[str, Any]:
# The response structure is different for get/update and list
if caller_methods_type in ["get", "update"]:
response = {
"applicationId": self.application_id,
"jobRunId": self.id,
"name": self.name,
"arn": self.arn,
"createdBy": self.created_by,
"createdAt": self.created_at,
"updatedAt": self.updated_at,
"executionRole": self.execution_role_arn,
"state": self.state,
"stateDetails": self.state_details,
"releaseLabel": self.release_label,
"configurationOverrides": self.configuration_overrides,
"jobDriver": self.job_driver,
"tags": self.tags,
"networkConfiguration": self.network_configuration,
"totalExecutionDurationSeconds": self.total_execution_duration_seconds,
"executionTimeoutMinutes": self.execution_timeout_minutes,
"billedResourceUtilization": self.billed_resource_utilization,
}
else:
response = {
"applicationId": self.application_id,
"id": self.id,
"name": self.name,
"arn": self.arn,
"createdBy": self.created_by,
"createdAt": self.created_at,
"updatedAt": self.updated_at,
"executionRole": self.execution_role_arn,
"state": self.state,
"stateDetails": self.state_details,
"releaseLabel": self.release_label,
"type": self.application_type,
}
return response
class EMRServerlessBackend(BaseBackend):
"""Implementation of EMRServerless APIs."""
def __init__(self, region_name: str, account_id: str):
super().__init__(region_name, account_id)
self.region_name = region_name
self.partition = get_partition(region_name)
self.applications: dict[str, FakeApplication] = {}
self.job_runs: dict[
str, list[FakeJobRun]
] = {} # {application_id: [job_run1, job_run2]}
def create_application(
self,
name: str,
release_label: str,
application_type: str,
client_token: str,
initial_capacity: str,
maximum_capacity: str,
tags: dict[str, str],
auto_start_configuration: str,
auto_stop_configuration: str,
network_configuration: str,
) -> FakeApplication:
if application_type not in ["HIVE", "SPARK"]:
raise ValidationException(f"Unsupported engine {application_type}")
if not re.match(r"emr-[0-9]{1}\.[0-9]{1,2}\.0(" "|-[0-9]{8})", release_label):
raise ValidationException(
f"Type '{application_type}' is not supported for release label '{release_label}' or release label does not exist"
)
application = FakeApplication(
name=name,
release_label=release_label,
application_type=application_type,
account_id=self.account_id,
region_name=self.region_name,
client_token=client_token,
initial_capacity=initial_capacity,
maximum_capacity=maximum_capacity,
tags=tags,
auto_start_configuration=auto_start_configuration,
auto_stop_configuration=auto_stop_configuration,
network_configuration=network_configuration,
)
self.applications[application.id] = application
return application
def delete_application(self, application_id: str) -> None:
if application_id not in self.applications.keys():
raise ResourceNotFoundException(application_id)
if self.applications[application_id].state not in ["CREATED", "STOPPED"]:
raise ValidationException(
f"Application {application_id} must be in one of the following statuses [CREATED, STOPPED]. "
f"Current status: {self.applications[application_id].state}"
)
self.applications[application_id].state = "TERMINATED"
def get_application(self, application_id: str) -> dict[str, Any]:
if application_id not in self.applications.keys():
raise ResourceNotFoundException(application_id)
return self.applications[application_id].to_dict()
def list_applications(
self, next_token: Optional[str], max_results: int, states: Optional[list[str]]
) -> tuple[list[dict[str, Any]], Optional[str]]:
applications = [
application.to_dict() for application in self.applications.values()
]
if states:
applications = [
application
for application in applications
if application["state"] in states
]
sort_key = "name"
return paginated_list(applications, sort_key, max_results, next_token)
def start_application(self, application_id: str) -> None:
if application_id not in self.applications.keys():
raise ResourceNotFoundException(application_id)
self.applications[application_id].state = "STARTED"
def stop_application(self, application_id: str) -> None:
if application_id not in self.applications.keys():
raise ResourceNotFoundException(application_id)
self.applications[application_id].state = "STOPPED"
def update_application(
self,
application_id: str,
initial_capacity: Optional[str],
maximum_capacity: Optional[str],
auto_start_configuration: Optional[str],
auto_stop_configuration: Optional[str],
network_configuration: Optional[str],
) -> dict[str, Any]:
if application_id not in self.applications.keys():
raise ResourceNotFoundException(application_id)
if self.applications[application_id].state not in ["CREATED", "STOPPED"]:
raise ValidationException(
f"Application {application_id} must be in one of the following statuses [CREATED, STOPPED]. "
f"Current status: {self.applications[application_id].state}"
)
if initial_capacity:
self.applications[application_id].initial_capacity = initial_capacity
if maximum_capacity:
self.applications[application_id].maximum_capacity = maximum_capacity
if auto_start_configuration:
self.applications[
application_id
].auto_start_configuration = auto_start_configuration
if auto_stop_configuration:
self.applications[
application_id
].auto_stop_configuration = auto_stop_configuration
if network_configuration:
self.applications[
application_id
].network_configuration = network_configuration
self.applications[
application_id
].updated_at = iso_8601_datetime_without_milliseconds(
datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
)
return self.applications[application_id].to_dict()
def start_job_run(
self,
application_id: str,
client_token: str,
execution_role_arn: str,
job_driver: Optional[dict[str, dict[str, Union[str, list[str]]]]],
configuration_overrides: Optional[dict[str, Union[list[Any], dict[str, Any]]]],
tags: Optional[dict[str, str]],
execution_timeout_minutes: Optional[int],
name: Optional[str],
) -> FakeJobRun:
role_account_id = execution_role_arn.split(":")[4]
if role_account_id != self.account_id:
raise AccessDeniedException("Cross-account pass role is not allowed.")
if execution_timeout_minutes and execution_timeout_minutes < 5:
raise ValidationException("RunTimeout must be at least 5 minutes.")
application_resp = self.get_application(application_id)
job_run = FakeJobRun(
application_id=application_id,
client_token=client_token,
execution_role_arn=execution_role_arn,
account_id=self.account_id,
region_name=self.region_name,
release_label=application_resp["releaseLabel"],
application_type=application_resp["type"],
job_driver=job_driver,
configuration_overrides=configuration_overrides,
tags=tags,
network_configuration=application_resp.get("networkConfiguration"),
execution_timeout_minutes=execution_timeout_minutes,
name=name,
)
if application_resp["state"] == "TERMINATED":
raise ValidationException(
f"Application {application_id} is terminated. Cannot start job run."
)
if application_id not in self.job_runs:
self.job_runs[application_id] = []
self.job_runs[application_id].append(job_run)
return job_run
def get_job_run(self, application_id: str, job_run_id: str) -> FakeJobRun:
if application_id not in self.job_runs.keys():
raise ResourceNotFoundException(application_id, "Application")
job_run_ids = [job_run.id for job_run in self.job_runs[application_id]]
if job_run_id not in job_run_ids:
raise ResourceNotFoundException(job_run_id, "JobRun")
filtered_job_runs = [
job_run
for job_run in self.job_runs[application_id]
if job_run.id == job_run_id
]
assert len(filtered_job_runs) == 1
job_run: FakeJobRun = filtered_job_runs[0]
return job_run
def cancel_job_run(self, application_id: str, job_run_id: str) -> tuple[str, str]:
# implement here
if application_id not in self.job_runs.keys():
raise ResourceNotFoundException(application_id, "Application")
job_run_ids = [job_run.id for job_run in self.job_runs[application_id]]
if job_run_id not in job_run_ids:
raise ResourceNotFoundException(job_run_id, "JobRun")
self.job_runs[application_id][job_run_ids.index(job_run_id)].state = "CANCELLED"
return application_id, job_run_id
def list_job_runs(
self,
application_id: str,
max_results: int,
next_token: Optional[str],
created_at_after: Optional[str],
created_at_before: Optional[str],
states: Optional[list[str]],
) -> tuple[list[dict[str, Any]], Optional[str]]:
if application_id not in self.job_runs.keys():
raise ResourceNotFoundException(application_id, "Application")
job_runs = self.job_runs[application_id]
if states:
job_runs = [job_run for job_run in job_runs if job_run.state in states]
if created_at_after:
job_runs = [
job_run
for job_run in job_runs
if datetime.strptime(job_run.created_at, "%Y-%m-%dT%H:%M:%SZ")
> datetime.strptime(created_at_after, "%Y-%m-%dT%H:%M:%SZ")
]
if created_at_before:
job_runs = [
job_run
for job_run in job_runs
if datetime.strptime(job_run.created_at, "%Y-%m-%dT%H:%M:%SZ")
< datetime.strptime(created_at_before, "%Y-%m-%dT%H:%M:%SZ")
]
job_run_dicts = [job_run.to_dict("list") for job_run in job_runs]
sort_key = "createdAt"
return paginated_list(job_run_dicts, sort_key, max_results, next_token)
emrserverless_backends = BackendDict(EMRServerlessBackend, "emr-serverless")
|