Skip to content

Commit cb68a6b

Browse files
committed
add action to stop experiments by tags
Signed-off-by: Sylvain Hellegouarch <[email protected]>
1 parent a6a5929 commit cb68a6b

File tree

4 files changed

+74
-5
lines changed

4 files changed

+74
-5
lines changed

CHANGELOG.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22

33
## [Unreleased][]
44

5-
[Unreleased]: https://github.com/chaostoolkit-incubator/chaostoolkit-aws/compare/0.26.0...HEAD
5+
[Unreleased]: https://github.com/chaostoolkit-incubator/chaostoolkit-aws/compare/0.27.0...HEAD
6+
7+
## [0.27.0][] - 2023-11-29
8+
9+
[0.27.0]: https://github.com/chaostoolkit-incubator/chaostoolkit-aws/compare/0.26.0...0.27.0
10+
11+
### Added
12+
13+
- Action to stop FIS experiments by tags
614

715
## [0.26.0][] - 2023-11-20
816

chaosaws/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from chaoslib.types import Configuration, DiscoveredActivities, Discovery, Secrets
1717
from logzero import logger
1818

19-
__version__ = "0.26.0"
19+
__version__ = "0.27.0"
2020
__all__ = ["__version__", "discover", "aws_client", "signed_api_call"]
2121

2222

chaosaws/fis/actions.py

+47-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from typing import Dict
1+
from typing import Dict, List
22

33
from chaoslib.exceptions import FailedActivity
44
from chaoslib.types import Configuration, Secrets
55

66
from chaosaws import aws_client
77
from chaosaws.types import AWSResponse
88

9-
__all__ = ["start_experiment", "stop_experiment"]
9+
__all__ = ["start_experiment", "stop_experiment", "stop_experiments_by_tags"]
1010

1111

1212
def start_experiment(
@@ -107,3 +107,48 @@ def stop_experiment(
107107
return fis_client.stop_experiment(id=experiment_id)
108108
except Exception as ex:
109109
raise FailedActivity(f"Stop Experiment failed, reason was: {ex}")
110+
111+
112+
def stop_experiments_by_tags(
113+
tags: Dict[str, str],
114+
configuration: Configuration = None,
115+
secrets: Secrets = None,
116+
) -> List[AWSResponse]:
117+
"""
118+
Stops the experiments matching the given tags.
119+
120+
Useful in rollbacks when experiment id isn't known.
121+
122+
:param tags: Dict[str, str] representing tags to lookup experiments
123+
:param configuration: Configuration object representing the CTK Configuration
124+
:param secrets: Secret object representing the CTK Secrets
125+
:returns: AWSResponse representing the response from FIS upon stopping the
126+
experiment
127+
128+
Examples
129+
--------
130+
>>> stop_experiments_by_tags(tags={"mytarget": "123"})
131+
[{'ResponseMetadata': {'RequestId': 'e5e9f9a9-f4d0-4d72-8704-1f26cc8b6ad6',
132+
'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Fri, 13 Aug 2021 09:12:17 GMT',
133+
...'experiment': {'id': 'EXPTUCK2dxepXgkR38',
134+
'experimentTemplateId': 'EXT6oWVA1WrLNy4XS', ... }]
135+
"""
136+
fis_client = aws_client(
137+
resource_name="fis", configuration=configuration, secrets=secrets
138+
)
139+
140+
try:
141+
experiments = fis_client.list_experiments(maxResults=100)
142+
except Exception as ex:
143+
raise FailedActivity(f"Listing Experiments failed, reason was: {ex}")
144+
145+
stopped = []
146+
for x in experiments["experiments"]:
147+
try:
148+
if x["tags"] == tags:
149+
result = fis_client.stop_experiment(id=x["id"])
150+
stopped.append(result)
151+
except Exception as ex:
152+
raise FailedActivity(f"Stop Experiment failed, reason was: {ex}")
153+
154+
return stopped

tests/fis/test_fis_actions.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
import pytest
44
from chaoslib.exceptions import FailedActivity
55

6-
from chaosaws.fis.actions import start_experiment, stop_experiment
6+
from chaosaws.fis.actions import (
7+
start_experiment,
8+
stop_experiment,
9+
stop_experiments_by_tags,
10+
)
711

812

913
def test_that_fis_action_modules___all___attribute_exposed_correctly():
@@ -127,3 +131,15 @@ def test_that_stop_experiment_returns_client_response(aws_client):
127131

128132
actual_resp = stop_experiment(experiment_id="an-id")
129133
assert actual_resp == resp
134+
135+
136+
@patch("chaosaws.fis.actions.aws_client", autospec=True)
137+
def test_that_stop_experiment_by_tags(aws_client):
138+
client = MagicMock()
139+
aws_client.return_value = client
140+
141+
resp = {"experiments": [{"id": "an-id", "tags": {"test-tag": "a-value"}}]}
142+
client.list_experiments.return_value = resp
143+
144+
stop_experiments_by_tags(tags={"test-tag": "a-value"})
145+
client.stop_experiment.assert_called_once_with(id="an-id")

0 commit comments

Comments
 (0)