From 47077d83569a0af51853cb208449d44446339d44 Mon Sep 17 00:00:00 2001 From: rafcio19 Date: Sat, 30 Nov 2024 20:40:18 +0100 Subject: [PATCH] OpenSearch: richer output (#8348) --- moto/opensearch/models.py | 30 ++++++++++++++++++++---- tests/test_opensearch/test_opensearch.py | 18 ++++++++++++-- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/moto/opensearch/models.py b/moto/opensearch/models.py index 5032e0adc359..31c91821d7bf 100644 --- a/moto/opensearch/models.py +++ b/moto/opensearch/models.py @@ -1,7 +1,9 @@ +import datetime from typing import Any, Dict, List, Optional from moto.core.base_backend import BackendDict, BaseBackend from moto.core.common_models import BaseModel +from moto.core.utils import unix_time from moto.utilities.tagging_service import TaggingService from moto.utilities.utils import get_partition @@ -42,6 +44,25 @@ } +class EngineVersion(BaseModel): + def __init__(self, options: str, create_time: datetime.datetime) -> None: + self.options = options or "OpenSearch_2.5" + self.create_time = unix_time(create_time) + self.update_time = self.create_time + + def to_dict(self) -> Dict[str, Any]: + return { + "Options": self.options, + "Status": { + "CreationDate": self.create_time, + "PendingDeletion": False, + "State": "Active", + "UpdateDate": self.update_time, + "UpdateVersion": 28, + }, + } + + class OpenSearchDomain(BaseModel): def __init__( self, @@ -70,7 +91,7 @@ def __init__( self.arn = ( f"arn:{get_partition(region)}:es:{region}:{account_id}:domain/{domain_name}" ) - self.engine_version = engine_version or "OpenSearch 2.5" + self.engine_version = EngineVersion(engine_version, datetime.datetime.now()) self.cluster_config = cluster_config or {} self.ebs_options = ebs_options or {"EBSEnabled": False} self.access_policies = access_policies or "" @@ -120,7 +141,7 @@ def dct_options(self) -> Dict[str, Any]: return { "Endpoint": self.endpoint, "Endpoints": self.endpoints, - "EngineVersion": self.engine_version, + "EngineVersion": self.engine_version.to_dict(), "ClusterConfig": self.cluster_config, "EBSOptions": self.ebs_options, "AccessPolicies": self.access_policies, @@ -207,6 +228,7 @@ def update( self.software_update_options = ( software_update_options or self.software_update_options ) + self.engine_version.update_time = unix_time(datetime.datetime.now()) class OpenSearchServiceBackend(BaseBackend): @@ -335,7 +357,7 @@ def list_domain_names(self, engine_type: str) -> List[Dict[str, str]]: domains = [] for domain in self.domains.values(): if engine_type: - if engine_type in domain.engine_version: + if engine_type in domain.engine_version.options: domains.append( { "DomainName": domain.domain_name, @@ -348,7 +370,7 @@ def list_domain_names(self, engine_type: str) -> List[Dict[str, str]]: domains.append( { "DomainName": domain.domain_name, - "EngineType": domain.engine_version.split("_")[0], + "EngineType": domain.engine_version.options.split("_")[0], } ) return domains diff --git a/tests/test_opensearch/test_opensearch.py b/tests/test_opensearch/test_opensearch.py index 7c92475fba3a..1ce66fef1f44 100644 --- a/tests/test_opensearch/test_opensearch.py +++ b/tests/test_opensearch/test_opensearch.py @@ -1,8 +1,9 @@ import boto3 import pytest from botocore.exceptions import ClientError +from freezegun import freeze_time -from moto import mock_aws +from moto import mock_aws, settings # See our Development Tips on writing tests for hints on how to write good tests: # http://docs.getmoto.org/en/latest/docs/contributing/development_tips/tests.html @@ -47,7 +48,7 @@ def test_create_domain_with_some_options(): EngineVersion="OpenSearch_1.1", )["DomainStatus"] assert status["Created"] - assert status["EngineVersion"] == "OpenSearch_1.1" + assert status["EngineVersion"]["Options"] == "OpenSearch_1.1" assert status["DomainEndpointOptions"] == { "EnforceHTTPS": True, "TLSSecurityPolicy": "Policy-Min-TLS-1-0-2019-07", @@ -89,14 +90,27 @@ def test_describe_unknown_domain(): @mock_aws +@freeze_time("2015-01-01 12:00:00") def test_describe_domain(): + # Setup client = boto3.client("opensearch", region_name="eu-west-1") client.create_domain(DomainName="testdn") + # Execute status = client.describe_domain(DomainName="testdn")["DomainStatus"] + ev_status = status["EngineVersion"]["Status"] + + # Verify assert "DomainId" in status assert "DomainName" in status assert status["DomainName"] == "testdn" + assert status["EngineVersion"]["Options"] == "OpenSearch_2.5" + assert ev_status["State"] == "Active" + assert ev_status["PendingDeletion"] is False + + if not settings.TEST_SERVER_MODE: + assert ev_status["CreationDate"] == 1420113600.0 + assert ev_status["UpdateDate"] == 1420113600.0 @mock_aws