Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable self-hosted healthchecks.io instances #37

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,22 @@ N/A
signal: start
```

### Using a Private Instance of Healthchecks.io

The `api_base_url` variable allows you to specify the custom API URL of your healthchecks.io instance, which can be used to connect to your private instance.
mamercad marked this conversation as resolved.
Show resolved Hide resolved

Example Ansible configuration using the `api_base_url` variable:

```yaml
- name: Create a check named "test"
community.healthchecksio.checks:
state: present
api_base_url: {{ api_base_url }}
api_key: "{{ api_key }}"
name: test
unique: ["name"]
```

### Installing the Collection from Ansible Galaxy

Before using this collection, you need to install it with the Ansible Galaxy command-line tool:
Expand Down
2 changes: 2 additions & 0 deletions changelogs/fragments/32-selfhosted.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- Support for using a private instance of Healthchecks.io (https://github.com/ansible-collections/community.healthchecksio/pull/37).
mamercad marked this conversation as resolved.
Show resolved Hide resolved
26 changes: 22 additions & 4 deletions plugins/module_utils/healthchecksio.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from ansible.module_utils.urls import fetch_url
from ansible.module_utils._text import to_text
from ansible.module_utils.basic import env_fallback
from urllib.parse import urljoin


class Response(object):
Expand Down Expand Up @@ -42,7 +43,14 @@ def status_code(self):
class HealthchecksioHelper:
def __init__(self, module):
self.module = module
self.baseurl = "https://healthchecks.io/api/v1"
api_base_url = module.params.get("api_base_url")
if api_base_url == self.healthchecksio_argument_spec().get("api_base_url").get(
"default"
):
self.ping_api_base_url = "https://hc-ping.com"
else:
self.ping_api_base_url = urljoin(api_base_url, "/ping")
self.baseurl = urljoin(api_base_url, "/api/v1")
self.timeout = module.params.get("timeout", 30)
self.api_token = module.params.get("api_token")
self.headers = {"X-Api-Key": self.api_token}
Expand Down Expand Up @@ -90,7 +98,7 @@ def delete(self, path, data=None):
def head(self, path, data=None):
resp, info = fetch_url(
self.module,
"https://hc-ping.com/{0}".format(path),
"{0}/{1}".format(self.ping_api_base_url, path),
data=data,
headers=self.headers,
method="HEAD",
Expand All @@ -117,6 +125,16 @@ def healthchecksio_argument_spec():
required=True,
no_log=True,
),
api_base_url=dict(
type="str",
fallback=(
env_fallback,
["HEALTHCHECKSIO_API_BASE_URL", "HC_API_BASE_URL"],
),
required=False,
no_log=True,
default="https://healthchecks.io",
mamercad marked this conversation as resolved.
Show resolved Hide resolved
),
)


Expand Down Expand Up @@ -274,7 +292,7 @@ def __init__(self, module):
def get_uuid(self, json_data):
ping_url = json_data.get("ping_url", None)
if ping_url is not None:
uuid = ping_url.split("/")[3]
uuid = ping_url.split("/")[-1]
if len(uuid) > 0:
return uuid
else:
Expand Down Expand Up @@ -328,7 +346,7 @@ def create(self):
channels = request_params["channels"]

# If all request parameters (except unique and api_key) match, exit without changes
skip_idempotency_params = ["unique", "api_key", "channels"]
skip_idempotency_params = ["unique", "api_key", "api_base_url", "channels"]
if (
len(c) == 1
and all(
Expand Down