Skip to content

Commit

Permalink
Merge pull request #418 from Checkmk/feature/handle-409-response
Browse files Browse the repository at this point in the history
Handle 409 response
  • Loading branch information
robin-checkmk authored Aug 21, 2023
2 parents 6bf3935 + 0b88146 commit d1c4d5a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/discovery.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- Discovery module - Added handling of 409 response.
15 changes: 14 additions & 1 deletion plugins/modules/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
403: (False, True, "Forbidden: Configuration via WATO is disabled."),
404: (False, True, "Not Found: Host could not be found."),
406: (False, True, "Not Acceptable."),
409: (False, False, "Conflict: A discovery background job is already running"),
415: (False, True, "Unsupported Media Type."),
500: (False, True, "General Server Error."),
}
Expand All @@ -153,7 +154,7 @@
400: (False, True, "Bad Request."),
403: (False, True, "Forbidden: Configuration via WATO is disabled."),
406: (False, True, "Not Acceptable."),
409: (False, True, "Conflict: A bulk discovery job is already active"),
409: (False, False, "Conflict: A bulk discovery job is already active"),
415: (False, True, "Unsupported Media Type."),
500: (False, True, "General Server Error."),
}
Expand Down Expand Up @@ -352,6 +353,18 @@ def run_module():

result = discovery.post()

# In case the API returns 409 (discovery running) we wait and try again.
# This can happen as example in versions where the endpoint doesn't respond with the correct redirect.
while (single_mode and result.http_code == 409) or (
len(module.params.get("hosts", [])) > 0 and result.http_code == 409
):
if single_mode:
time.sleep(1)
else:
time.sleep(10)

result = discovery.post()

# If single_mode and the API returns 302, check the service completion endpoint
# If not single_mode and the API returns 200, check the service completion endpoint
if (single_mode and result.http_code == 302) or (
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/targets/discovery/tasks/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

- name: "Run Single Discoveries."
block:
- name: "{{ outer_item.version }} - Refresh (Tabula Rasa in 2.1 and before, Rescan in 2.2 and newer)."
- name: "{{ outer_item.version }} - Rescan services."
discovery:
server_url: "{{ server_url }}"
site: "{{ outer_item.site }}"
Expand Down Expand Up @@ -145,7 +145,7 @@
run_once: true # noqa run-once[task]
loop: "{{ checkmk_hosts }}"

- name: "{{ outer_item.version }} - Bulk: Refresh (Tabula Rasa in 2.1 and before, Rescan in 2.2 and newer)."
- name: "{{ outer_item.version }} - Bulk: Rescan services."
discovery:
server_url: "{{ server_url }}"
site: "{{ outer_item.site }}"
Expand Down

0 comments on commit d1c4d5a

Please sign in to comment.