Skip to content

Commit

Permalink
OpenSearch: richer output (#8348)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafcio19 authored Nov 30, 2024
1 parent 59fd5a1 commit 47077d8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
30 changes: 26 additions & 4 deletions moto/opensearch/models.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 ""
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
18 changes: 16 additions & 2 deletions tests/test_opensearch/test_opensearch.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 47077d8

Please sign in to comment.