Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Elasticsearch - Expose the tagging functionality #8515

Merged
merged 6 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions moto/es/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
"{0}/2015-01-01/es/domain$": OpenSearchServiceResponse.domains,
"{0}/2015-01-01/es/domain/(?P<domainname>[^/]+)": OpenSearchServiceResponse.domain,
"{0}/2015-01-01/es/domain-info$": OpenSearchServiceResponse.list_domains,
"{0}/2015-01-01/tags/?$": OpenSearchServiceResponse.tags,
"{0}/2015-01-01/tags-removal/?": OpenSearchServiceResponse.tag_removal,
"{0}/2021-01-01/domain$": OpenSearchServiceResponse.dispatch,
"{0}/2021-01-01/opensearch/compatibleVersions": OpenSearchServiceResponse.dispatch,
"{0}/2021-01-01/opensearch/domain": OpenSearchServiceResponse.dispatch,
"{0}/2021-01-01/opensearch/domain/(?P<domainname>[^/]+)": OpenSearchServiceResponse.dispatch,
"{0}/2021-01-01/opensearch/domain/(?P<domainname>[^/]+)/config": OpenSearchServiceResponse.dispatch,
"{0}/2021-01-01/opensearch/domain-info$": OpenSearchServiceResponse.dispatch,
"{0}/2021-01-01/tags/?": OpenSearchServiceResponse.dispatch,
"{0}/2021-01-01/tags-removal/": OpenSearchServiceResponse.dispatch,
"{0}/2021-01-01/tags/?$": OpenSearchServiceResponse.dispatch,
"{0}/2021-01-01/tags-removal/?": OpenSearchServiceResponse.dispatch,
}
16 changes: 16 additions & 0 deletions moto/opensearch/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ def domain(cls, request: Any, full_url: str, headers: Any) -> TYPE_RESPONSE: #
if request.method == "GET":
return 200, {}, response.describe_domain()

@classmethod
def tags(cls, request: Any, full_url: str, headers: Any) -> TYPE_RESPONSE: # type: ignore
response = cls()
response.setup_class(request, full_url, headers)
if request.method == "GET":
return 200, {}, response.list_tags()
if request.method == "POST":
return 200, {}, response.add_tags()

@classmethod
def tag_removal(cls, request: Any, full_url: str, headers: Any) -> TYPE_RESPONSE: # type: ignore
response = cls()
response.setup_class(request, full_url, headers)
if request.method == "POST":
return 200, {}, response.remove_tags()

def create_domain(self) -> str:
domain_name = self._get_param("DomainName")
if not re.match(r"^[a-z][a-z0-9\-]+$", domain_name):
Expand Down
78 changes: 78 additions & 0 deletions tests/test_es/test_domain_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import boto3

from moto import mock_aws


@mock_aws
def test_create_without_tags():
client = boto3.client("es", region_name="us-east-1")
arn = client.create_elasticsearch_domain(DomainName="testdn")["DomainStatus"]["ARN"]

assert client.list_tags(ARN=arn)["TagList"] == []


@mock_aws
def test_create_with_tags():
client = boto3.client("es", region_name="us-east-1")
domain = client.create_elasticsearch_domain(
DomainName="testdn", TagList=[{"Key": "k1", "Value": "v1"}]
)
arn = domain["DomainStatus"]["ARN"]

assert client.list_tags(ARN=arn)["TagList"] == [{"Key": "k1", "Value": "v1"}]


@mock_aws
def test_add_tags():
client = boto3.client("es", region_name="us-east-1")
domain = client.create_elasticsearch_domain(DomainName="testdn")

arn = domain["DomainStatus"]["ARN"]

client.add_tags(
ARN=arn, TagList=[{"Key": "k1", "Value": "v1"}, {"Key": "k2", "Value": "v2"}]
)

assert client.list_tags(ARN=arn)["TagList"] == [
{"Key": "k1", "Value": "v1"},
{"Key": "k2", "Value": "v2"},
]


@mock_aws
def test_update_tag():
client = boto3.client("es", region_name="us-east-1")
domain = client.create_elasticsearch_domain(
DomainName="testdn", TagList=[{"Key": "k1", "Value": "v1"}]
)

arn = domain["DomainStatus"]["ARN"]
assert client.list_tags(ARN=arn)["TagList"] == [{"Key": "k1", "Value": "v1"}]

# add the same key again with a different value
client.add_tags(ARN=arn, TagList=[{"Key": "k1", "Value": "v2"}])

assert client.list_tags(ARN=arn)["TagList"] == [
{"Key": "k1", "Value": "v2"},
]


@mock_aws
def test_remove_tags():
client = boto3.client("es", region_name="us-east-1")
domain = client.create_elasticsearch_domain(
DomainName="testdn",
TagList=[{"Key": "k1", "Value": "v1"}, {"Key": "k2", "Value": "v2"}],
)
arn = domain["DomainStatus"]["ARN"]

assert client.list_tags(ARN=arn)["TagList"] == [
{"Key": "k1", "Value": "v1"},
{"Key": "k2", "Value": "v2"},
]

client.remove_tags(ARN=arn, TagKeys=["k1"])
assert client.list_tags(ARN=arn)["TagList"] == [{"Key": "k2", "Value": "v2"}]

client.remove_tags(ARN=arn, TagKeys=["k2"])
assert client.list_tags(ARN=arn)["TagList"] == []
Loading