Skip to content

Commit 402ae60

Browse files
authored
Implement wait_for_connection on httpapi (#105)
Implement wait_for_connection on httpapi Reviewed-by: https://github.com/apps/ansible-zuul
1 parent 92732c0 commit 402ae60

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
minor_changes:
3+
- The httpapi connection plugin now works with `wait_for_connection`. This will periodically request
4+
the root page of the server described by the plugin's options until the request succeeds.
5+
This can only test that the server is reachable, the correctness or usability of the API is not guaranteed.

plugins/connection/httpapi.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ def __init__(self, play_context, new_stdin, *args, **kwargs):
193193
play_context, new_stdin, *args, **kwargs
194194
)
195195

196-
self._url = None
197196
self._auth = None
198197

199198
if self._network_os:
@@ -227,6 +226,13 @@ def __init__(self, play_context, new_stdin, *args, **kwargs):
227226
)
228227
self.queue_message("log", "network_os is set to %s" % self._network_os)
229228

229+
@property
230+
def _url(self):
231+
protocol = "https" if self.get_option("use_ssl") else "http"
232+
host = self.get_option("host")
233+
port = self.get_option("port") or (443 if protocol == "https" else 80)
234+
return "%s://%s:%s" % (protocol, host, port)
235+
230236
def update_play_context(self, pc_data):
231237
"""Updates the play context information for the connection"""
232238
pc_data = to_bytes(pc_data)
@@ -249,13 +255,6 @@ def update_play_context(self, pc_data):
249255

250256
def _connect(self):
251257
if not self.connected:
252-
protocol = "https" if self.get_option("use_ssl") else "http"
253-
host = self.get_option("host")
254-
port = self.get_option("port") or (
255-
443 if protocol == "https" else 80
256-
)
257-
self._url = "%s://%s:%s" % (protocol, host, port)
258-
259258
self.queue_message(
260259
"vvv",
261260
"ESTABLISH HTTP(S) CONNECTFOR USER: %s TO %s"
@@ -337,3 +336,13 @@ def send(self, path, data, **kwargs):
337336
response_buffer.seek(0)
338337

339338
return response, response_buffer
339+
340+
def transport_test(self, connect_timeout):
341+
"""This method enables wait_for_connection to work.
342+
343+
The sole purpose of this method is to raise an exception if the API's URL
344+
cannot be reached. As such, it does not do anything except attempt to
345+
request the root URL with no error handling.
346+
"""
347+
348+
open_url(self._url, timeout=connect_timeout)

0 commit comments

Comments
 (0)