|
1 |
| -from typing import Dict |
| 1 | +from typing import Dict, List |
2 | 2 |
|
3 | 3 | from chaoslib.exceptions import FailedActivity
|
4 | 4 | from chaoslib.types import Configuration, Secrets
|
5 | 5 |
|
6 | 6 | from chaosaws import aws_client
|
7 | 7 | from chaosaws.types import AWSResponse
|
8 | 8 |
|
9 |
| -__all__ = ["start_experiment", "stop_experiment"] |
| 9 | +__all__ = ["start_experiment", "stop_experiment", "stop_experiments_by_tags"] |
10 | 10 |
|
11 | 11 |
|
12 | 12 | def start_experiment(
|
@@ -107,3 +107,48 @@ def stop_experiment(
|
107 | 107 | return fis_client.stop_experiment(id=experiment_id)
|
108 | 108 | except Exception as ex:
|
109 | 109 | 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 |
0 commit comments