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 493 494 495 496 497 498 499 500 501 502 503 504 505
|
"""OpenSearchIngestionBackend class with methods for supported APIs."""
from datetime import datetime
from typing import TYPE_CHECKING, Any, ClassVar, Optional
import yaml
from moto.core.base_backend import BackendDict, BaseBackend
from moto.core.common_models import BaseModel
from moto.moto_api._internal import mock_random as random
from moto.moto_api._internal.managed_state_model import ManagedState
from moto.utilities.paginator import paginate
from moto.utilities.tagging_service import TaggingService
from moto.utilities.utils import get_partition
from .exceptions import (
InvalidVPCOptionsException,
PipelineAlreadyExistsException,
PipelineInvalidStateException,
PipelineNotFoundException,
SecurityGroupNotFoundException,
SubnetNotFoundException,
)
if TYPE_CHECKING:
from moto.ec2.models import EC2Backend
class Pipeline(ManagedState, BaseModel):
CREATING_REASON = "The pipeline is being created. It is not able to ingest data."
ACTIVE_REASON = "The pipeline is ready to ingest data."
DELETING_REASON = "The pipeline is being deleted"
STOPPING_REASON = "The pipeline is being stopped"
STOPPED_REASON = "The pipeline is stopped"
STARTING_REASON = "The pipeline is starting. It is not able to ingest data"
UPDATING_REASON = "An update was triggered for the pipeline. It is still available to ingest data."
STATUS_REASON_MAP: ClassVar[dict[str, str]] = {
"CREATING": CREATING_REASON,
"ACTIVE": ACTIVE_REASON,
"STOPPING": STOPPING_REASON,
"STOPPED": STOPPED_REASON,
"STARTING": STARTING_REASON,
"UPDATING": UPDATING_REASON,
"DELETING": DELETING_REASON,
}
def __init__(
self,
pipeline_name: str,
account_id: str,
region: str,
min_units: int,
max_units: int,
pipeline_configuration_body: str,
log_publishing_options: Optional[dict[str, Any]],
vpc_options: Optional[dict[str, Any]],
buffer_options: Optional[dict[str, Any]],
encryption_at_rest_options: Optional[dict[str, Any]],
ingest_endpoint_urls: list[str],
serverless: bool,
vpc_endpoint_service: Optional[str],
vpc_endpoint: Optional[str],
vpc_id: Optional[str],
backend: "OpenSearchIngestionBackend",
):
ManagedState.__init__(
self,
model_name="osis::pipeline",
transitions=[
("CREATING", "ACTIVE"),
("UPDATING", "ACTIVE"),
("DELETING", "DELETED"),
("STOPPING", "STOPPED"),
("STARTING", "ACTIVE"),
],
)
self.pipeline_name = pipeline_name
self.account_id = account_id
self.region = region
self.min_units = min_units
self.max_units = max_units
self.pipeline_configuration_body_str = pipeline_configuration_body
self.pipeline_configuration_body = yaml.safe_load(pipeline_configuration_body)
self.log_publishing_options = log_publishing_options
self.vpc_options = vpc_options
self.buffer_options = buffer_options
self.encryption_at_rest_options = encryption_at_rest_options
self.ingest_endpoint_urls = ingest_endpoint_urls
self.serverless = serverless
self.vpc_endpoint_service = vpc_endpoint_service
self.vpc_endpoint = vpc_endpoint
self.vpc_id = vpc_id
self.backend = backend
self.status = "CREATING"
self.arn = self._get_arn(self.pipeline_name)
self.destinations = self._update_destinations()
if (
self.vpc_options is None
or self.vpc_options.get("VpcEndpointManagement", "SERVICE") == "SERVICE"
):
# Not returned in this case
self.vpc_endpoint_service = None
self.service_vpc_endpoints = self._get_service_vpc_endpoints()
self.created_at: datetime = datetime.now()
self.last_updated_at: datetime = datetime.now()
def _get_arn(self, name: str) -> str:
return f"arn:{get_partition(self.region)}:osis:{self.region}:{self.account_id}:pipeline/{name}"
def _get_service_vpc_endpoints(self) -> Optional[list[dict[str, str]]]:
# ServiceVpcEndpoint.VpcEndpointId not implemented
if self.serverless:
return [{"ServiceName": "OPENSEARCH_SERVERLESS"}]
else:
return None
def _update_destinations(self) -> list[dict[str, str]]:
destinations = []
for sub_pipeline in self.pipeline_configuration_body:
if sub_pipeline != "version":
for sink in self.pipeline_configuration_body[sub_pipeline]["sink"]:
for sink_type, sink_config in sink.items():
if sink_type == "opensearch":
if sink_config["aws"].get("serverless") is True:
service_name = "OpenSearch_Serverless"
else:
service_name = "OpenSearch"
endpoint = sink_config["hosts"][0]
elif sink_type == "s3":
service_name = "S3"
endpoint = sink_config["bucket"]
else:
continue
destinations.append(
{"ServiceName": service_name, "Endpoint": endpoint}
)
return destinations
@staticmethod
def is_serverless(pipeline_body: dict[str, Any]) -> bool: # type: ignore[misc]
serverless = False
for sub_pipeline in pipeline_body:
if sub_pipeline != "version":
for sink in pipeline_body[sub_pipeline]["sink"]:
for _, sink_config in sink.items():
serverless = (
sink_config.get("aws", {}).get("serverless", False)
or serverless
)
source_type = list(pipeline_body[sub_pipeline]["source"].keys())[0]
source_config = pipeline_body[sub_pipeline]["source"][source_type]
serverless = (
source_config.get("aws", {}).get("serverless", False) or serverless
)
return serverless
def delete(self) -> None:
self.status = "DELETING"
self.set_last_updated()
def get_created_at(self) -> str:
return self.created_at.astimezone().isoformat()
def get_last_updated_at(self) -> str:
return self.last_updated_at.astimezone().isoformat()
def set_last_updated(self) -> None:
self.last_updated_at = datetime.now()
def start(self) -> None:
self.status = "STARTING"
self.set_last_updated()
def stop(self) -> None:
self.status = "STOPPING"
self.set_last_updated()
def update(
self,
min_units: Optional[int],
max_units: Optional[int],
pipeline_configuration_body: Optional[str],
log_publishing_options: Optional[dict[str, Any]],
buffer_options: Optional[dict[str, Any]],
encryption_at_rest_options: Optional[dict[str, Any]],
) -> None:
if min_units is not None:
self.min_units = min_units
if max_units is not None:
self.max_units = max_units
if pipeline_configuration_body is not None:
self.pipeline_configuration_body_str = pipeline_configuration_body
self.pipeline_configuration_body = yaml.safe_load(
pipeline_configuration_body
)
if log_publishing_options is not None:
self.log_publishing_options = log_publishing_options
if buffer_options is not None:
self.buffer_options = buffer_options
if encryption_at_rest_options is not None:
self.encryption_at_rest_options = encryption_at_rest_options
self.destinations = self._update_destinations()
self.serverless = self.is_serverless(self.pipeline_configuration_body)
self.service_vpc_endpoints = self._get_service_vpc_endpoints()
self.status = "UPDATING"
self.set_last_updated()
def to_dict(self) -> dict[str, Any]:
return {
"PipelineName": self.pipeline_name,
"PipelineArn": self.arn,
"MinUnits": self.min_units,
"MaxUnits": self.max_units,
"Status": self.status,
"StatusReason": {
"Description": self.STATUS_REASON_MAP.get(self.status or "", ""),
},
"PipelineConfigurationBody": self.pipeline_configuration_body_str,
"CreatedAt": self.get_created_at(),
"LastUpdatedAt": self.get_last_updated_at(),
"IngestEndpointUrls": self.ingest_endpoint_urls,
"LogPublishingOptions": self.log_publishing_options,
"VpcEndpoints": None
if self.vpc_options is None
else [
{
"VpcEndpointId": self.vpc_endpoint,
"VpcId": self.vpc_id,
"VpcOptions": self.vpc_options,
}
],
"BufferOptions": self.buffer_options,
"EncryptionAtRestOptions": self.encryption_at_rest_options,
"VpcEndpointService": self.vpc_endpoint_service,
"ServiceVpcEndpoints": self.service_vpc_endpoints,
"Destinations": self.destinations,
"Tags": self.backend.list_tags_for_resource(self.arn)["Tags"],
}
def to_short_dict(self) -> dict[str, Any]:
return {
"Status": self.status,
"StatusReason": {
"Description": self.STATUS_REASON_MAP.get(self.status or "", ""),
},
"PipelineName": self.pipeline_name,
"PipelineArn": self.arn,
"MinUnits": self.min_units,
"MaxUnits": self.max_units,
"CreatedAt": self.get_created_at(),
"LastUpdatedAt": self.get_last_updated_at(),
"Destinations": self.destinations,
"Tags": self.backend.list_tags_for_resource(self.arn)["Tags"],
}
class OpenSearchIngestionBackend(BaseBackend):
"""Implementation of OpenSearchIngestion APIs."""
PAGINATION_MODEL = {
"list_pipelines": {
"input_token": "next_token",
"limit_key": "max_results",
"limit_default": 100,
"unique_attribute": "PipelineName",
},
}
PIPELINE_DELETE_VALID_STATES = [
"UPDATE_FAILED",
"ACTIVE",
"START_FAILED",
"STOPPED",
"CREATE_FAILED",
]
PIPELINE_STOP_VALID_STATES = ["UPDATE_FAILED", "ACTIVE"]
PIPELINE_START_VALID_STATES = ["START_FAILED", "STOPPED"]
PIPELINE_UPDATE_VALID_STATES = [
"UPDATE_FAILED",
"ACTIVE",
"START_FAILED",
"STOPPED",
]
def __init__(self, region_name: str, account_id: str):
super().__init__(region_name, account_id)
self._pipelines: dict[str, Pipeline] = {}
self.tagger = TaggingService()
@property
def ec2_backend(self) -> "EC2Backend": # type: ignore[misc]
from moto.ec2 import ec2_backends
return ec2_backends[self.account_id][self.region_name]
@property
def pipelines(self) -> dict[str, Pipeline]:
self._pipelines = {
name: pipeline
for name, pipeline in self._pipelines.items()
if pipeline.status != "DELETED"
}
return self._pipelines
def _get_ingest_endpoint_urls(
self, pipeline_name: str, endpoint_random_string: str
) -> list[str]:
return [
f"{pipeline_name}-{endpoint_random_string}.{self.region_name}.osis.amazonaws.com"
]
def _get_random_endpoint_string(self) -> str:
return random.get_random_string(length=26, lower_case=True)
def _get_vpc_endpoint(
self, vpc_id: str, vpc_options: dict[str, Any], service_name: str
) -> Optional[str]:
if vpc_options.get("VpcEndpointManagement", "SERVICE") == "SERVICE":
service_managed_endpoint = self.ec2_backend.create_vpc_endpoint(
vpc_id=vpc_id,
service_name=service_name,
endpoint_type="Interface",
security_group_ids=vpc_options.get("SecurityGroupIds"),
subnet_ids=vpc_options["SubnetIds"],
private_dns_enabled=False,
policy_document="OSIS Test Doc",
route_table_ids=[],
tags={"OSISManaged": "true"},
)
return service_managed_endpoint.id
else:
return None
def _get_vpc_endpoint_service(
self, pipeline_name: str, endpoint_random_string: str
) -> str:
return f"com.amazonaws.osis.{self.region_name}.{pipeline_name}-{endpoint_random_string}"
def _validate_and_get_vpc(self, vpc_options: dict[str, Any]) -> str:
from moto.ec2.exceptions import InvalidSubnetIdError
vpc_id = ""
for subnet_id in vpc_options["SubnetIds"]:
try:
subnet = self.ec2_backend.get_subnet(subnet_id)
except InvalidSubnetIdError:
# re-raising for more accurate error message
raise SubnetNotFoundException(subnet_id)
if vpc_id == "":
vpc_id = subnet.vpc_id
else:
if subnet.vpc_id != vpc_id:
raise InvalidVPCOptionsException(
"All specified subnets must belong to the same VPC."
)
for sg_id in vpc_options["SecurityGroupIds"]:
sg = self.ec2_backend.get_security_group_from_id(sg_id)
if sg is None:
raise SecurityGroupNotFoundException(sg_id)
return vpc_id
def create_pipeline(
self,
pipeline_name: str,
min_units: int,
max_units: int,
pipeline_configuration_body: str,
log_publishing_options: Optional[dict[str, Any]],
vpc_options: Optional[dict[str, Any]],
buffer_options: Optional[dict[str, bool]],
encryption_at_rest_options: Optional[dict[str, Any]],
tags: list[dict[str, str]],
) -> Pipeline:
if pipeline_name in self.pipelines:
raise PipelineAlreadyExistsException(pipeline_name)
serverless = Pipeline.is_serverless(yaml.safe_load(pipeline_configuration_body))
endpoint_random_string = self._get_random_endpoint_string()
endpoint_service = self._get_vpc_endpoint_service(
pipeline_name, endpoint_random_string
)
ingestion_endpoint_urls = self._get_ingest_endpoint_urls(
pipeline_name, endpoint_random_string
)
vpc_endpoint = None
vpc_id = None
if vpc_options is not None:
vpc_id = self._validate_and_get_vpc(vpc_options)
vpc_endpoint = self._get_vpc_endpoint(vpc_id, vpc_options, endpoint_service)
pipeline = Pipeline(
pipeline_name,
self.account_id,
self.region_name,
min_units,
max_units,
pipeline_configuration_body,
log_publishing_options,
vpc_options,
buffer_options,
encryption_at_rest_options,
ingestion_endpoint_urls,
serverless,
endpoint_service,
vpc_endpoint,
vpc_id,
backend=self,
)
self.pipelines[pipeline_name] = pipeline
self.tag_resource(pipeline.arn, tags)
return pipeline
def delete_pipeline(self, pipeline_name: str) -> None:
if pipeline_name not in self.pipelines:
raise PipelineNotFoundException(pipeline_name)
pipeline = self.pipelines[pipeline_name]
if pipeline.status not in self.PIPELINE_DELETE_VALID_STATES:
raise PipelineInvalidStateException(
"deletion", self.PIPELINE_DELETE_VALID_STATES, pipeline.status
)
pipeline.delete()
def start_pipeline(self, pipeline_name: str) -> Pipeline:
if pipeline_name not in self.pipelines:
raise PipelineNotFoundException(pipeline_name)
pipeline = self.pipelines[pipeline_name]
if pipeline.status not in self.PIPELINE_START_VALID_STATES:
raise PipelineInvalidStateException(
"starting", self.PIPELINE_START_VALID_STATES, pipeline.status
)
pipeline.start()
return pipeline
def stop_pipeline(self, pipeline_name: str) -> Pipeline:
if pipeline_name not in self.pipelines:
raise PipelineNotFoundException(pipeline_name)
pipeline = self.pipelines[pipeline_name]
if pipeline.status not in self.PIPELINE_STOP_VALID_STATES:
raise PipelineInvalidStateException(
"stopping", self.PIPELINE_STOP_VALID_STATES, pipeline.status
)
pipeline.stop()
return pipeline
def get_pipeline(self, pipeline_name: str) -> Pipeline:
if pipeline_name not in self.pipelines:
raise PipelineNotFoundException(pipeline_name)
pipeline = self.pipelines[pipeline_name]
pipeline.advance()
return pipeline
@paginate(pagination_model=PAGINATION_MODEL) # type: ignore
def list_pipelines(self) -> list[Pipeline]:
for pipeline in self.pipelines.values():
pipeline.advance()
return list(self.pipelines.values())
def list_tags_for_resource(self, arn: str) -> dict[str, list[dict[str, str]]]:
return self.tagger.list_tags_for_resource(arn)
def update_pipeline(
self,
pipeline_name: str,
min_units: Optional[int],
max_units: Optional[int],
pipeline_configuration_body: Optional[str],
log_publishing_options: Optional[dict[str, Any]],
buffer_options: Optional[dict[str, Any]],
encryption_at_rest_options: Optional[dict[str, Any]],
) -> Pipeline:
if pipeline_name not in self.pipelines:
raise PipelineNotFoundException(pipeline_name)
pipeline = self.pipelines[pipeline_name]
if pipeline.status not in self.PIPELINE_UPDATE_VALID_STATES:
raise PipelineInvalidStateException(
"updates", self.PIPELINE_UPDATE_VALID_STATES, pipeline.status
)
pipeline.update(
min_units,
max_units,
pipeline_configuration_body,
log_publishing_options,
buffer_options,
encryption_at_rest_options,
)
return pipeline
def tag_resource(self, arn: str, tags: list[dict[str, str]]) -> None:
self.tagger.tag_resource(arn, tags)
def untag_resource(self, arn: str, tag_keys: list[str]) -> None:
self.tagger.untag_resource_using_names(arn, tag_keys)
osis_backends = BackendDict(OpenSearchIngestionBackend, "osis")
|