generated from canonical/template-operator
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check response status in
OpenSearchDistribution.request()
(#210)
## Issue By default, `requests` does not raise an exception if an HTTP request errors (i.e. response status code 400-599) Currently, `request()` is expecting an exception to be raised if request errors For example, this usage of `request()` expects an `OpenSearchHttpError` if the status code is 400—but an exception will not be raised if the status code is 400 https://github.com/canonical/opensearch-operator/blob/c0225203320a412c3492ac827ac47b3b5b763787/lib/charms/opensearch/v0/opensearch_locking.py#L53-L68 Also, currently, requests with status code 400-599 are never retried, regardless of the value of `retries` argument ## Solution Calling `raise_for_status()` will raise a `requests.exceptions.HTTPError` exception if the response status code is 400-599 --------- Co-authored-by: Mehdi-Bendriss <[email protected]>
- Loading branch information
1 parent
4d27728
commit b4cf6a4
Showing
5 changed files
with
100 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Copyright 2023 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
|
||
"""File containing http related helpers.""" | ||
from typing import Any, Dict, List, Optional | ||
|
||
from charms.opensearch.v0.helper_networking import reachable_hosts | ||
from tenacity import RetryCallState | ||
|
||
# The unique Charmhub library identifier, never change it | ||
LIBID = "f8bb15ca9ffc4e3f8d113421e03abe06" | ||
|
||
# Increment this major API version when introducing breaking changes | ||
LIBAPI = 0 | ||
|
||
# Increment this PATCH version before using `charmcraft publish-lib` or reset | ||
# to 0 if you are raising the major API version | ||
LIBPATCH = 1 | ||
|
||
|
||
def error_http_retry_log( | ||
logger, retry_max: int, method: str, url: str, payload: Optional[Dict[str, Any]] | ||
): | ||
"""Return a custom log function to run before a new Tenacity retry.""" | ||
|
||
def log_error(retry_state: RetryCallState): | ||
logger.error( | ||
f"Request {method} to {url} with payload: {payload} failed." | ||
f"(Attempts left: {retry_max - retry_state.attempt_number})\n" | ||
f"\tError: {retry_state.outcome.exception()}" | ||
) | ||
|
||
return log_error | ||
|
||
|
||
def full_urls( | ||
primary_host: str, port: int, path: str, alt_hosts: List[str], check_hosts_reach: bool = False | ||
) -> List[str]: | ||
"""Returns a list of well formatted and potentially reachable hosts.""" | ||
target_hosts = [primary_host] | ||
if alt_hosts: | ||
target_hosts.extend([alt_host for alt_host in alt_hosts if alt_host != primary_host]) | ||
|
||
if not check_hosts_reach: | ||
return target_hosts | ||
|
||
return [ | ||
f"https://{host_candidate}:{port}/{path}" | ||
for host_candidate in reachable_hosts(target_hosts) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters