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

Created Lookup Modules 'site' and 'sites'. #658

Merged
merged 12 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions .github/workflows/ans-int-test-lkp-site.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# README:
# - When changing the module name, it needs to be changed in 'env:MODULE_NAME' and in 'on:pull_requests:path'!
#
# Resources:
# - Template for this file: https://github.com/ansible-collections/collection_template/blob/main/.github/workflows/ansible-test.yml
# - About Ansible integration tests: https://docs.ansible.com/ansible/latest/dev_guide/testing_integration.html

env:
NAMESPACE: checkmk
COLLECTION_NAME: general
MODULE_NAME: lookup_site

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

name: Ansible Integration Tests for Site Lookup Module
on:
workflow_dispatch:
pull_request:
branches:
- main
- devel
paths:
- 'plugins/lookup/site.py'
push:
paths:
- 'plugins/lookup/site.py'

jobs:

integration:
runs-on: ubuntu-22.04
name: Ⓐ${{ matrix.ansible }}+py${{ matrix.python }}
strategy:
fail-fast: false
matrix:
ansible:
- stable-2.15
- stable-2.16
- stable-2.17
- devel
python:
- '3.9'
- '3.10'
- '3.11'
- '3.12'
exclude:
# Exclude unsupported sets.
- ansible: stable-2.15
python: '3.12'

steps:
- name: Check out code
uses: actions/checkout@v4
with:
path: ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Install ansible-base (${{ matrix.ansible }})
run: pip install https://github.com/ansible/ansible/archive/${{ matrix.ansible }}.tar.gz --disable-pip-version-check

- name: Provide secrets file
run: echo "${{ secrets.CHECKMK_DOWNLOAD_PW }}" > ./tests/integration/files/.dl-secret
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}
env:
CHECKMK_DOWNLOAD_PW: ${{ secrets.CHECKMK_DOWNLOAD_PW }}

- name: Run integration test
run: ansible-test integration ${{env.MODULE_NAME}} -v --color --retry-on-error --continue-on-error --diff --python ${{ matrix.python }} --docker default
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}
75 changes: 75 additions & 0 deletions .github/workflows/ans-int-test-lkp-sites.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# README:
# - When changing the module name, it needs to be changed in 'env:MODULE_NAME' and in 'on:pull_requests:path'!
#
# Resources:
# - Template for this file: https://github.com/ansible-collections/collection_template/blob/main/.github/workflows/ansible-test.yml
# - About Ansible integration tests: https://docs.ansible.com/ansible/latest/dev_guide/testing_integration.html

env:
NAMESPACE: checkmk
COLLECTION_NAME: general
MODULE_NAME: lookup_sites

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

name: Ansible Integration Tests for Sites Lookup Module
on:
workflow_dispatch:
pull_request:
branches:
- main
- devel
paths:
- 'plugins/lookup/site.py'
push:
paths:
- 'plugins/lookup/site.py'

jobs:

integration:
runs-on: ubuntu-22.04
name: Ⓐ${{ matrix.ansible }}+py${{ matrix.python }}
strategy:
fail-fast: false
matrix:
ansible:
- stable-2.15
- stable-2.16
- stable-2.17
- devel
python:
- '3.9'
- '3.10'
- '3.11'
- '3.12'
exclude:
# Exclude unsupported sets.
- ansible: stable-2.15
python: '3.12'

steps:
- name: Check out code
uses: actions/checkout@v4
with:
path: ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Install ansible-base (${{ matrix.ansible }})
run: pip install https://github.com/ansible/ansible/archive/${{ matrix.ansible }}.tar.gz --disable-pip-version-check

- name: Provide secrets file
run: echo "${{ secrets.CHECKMK_DOWNLOAD_PW }}" > ./tests/integration/files/.dl-secret
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}
env:
CHECKMK_DOWNLOAD_PW: ${{ secrets.CHECKMK_DOWNLOAD_PW }}

- name: Run integration test
run: ansible-test integration ${{env.MODULE_NAME}} -v --color --retry-on-error --continue-on-error --diff --python ${{ matrix.python }} --docker default
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ Name | Description | Tests
[checkmk.general.rules](https://github.com/Checkmk/ansible-collection-checkmk.general/blob/main/plugins/lookup/rules.py)|Look up all rules.|[![Integration Tests for Rules Lookup Module](https://github.com/Checkmk/ansible-collection-checkmk.general/actions/workflows/ans-int-test-lkp-rules.yaml/badge.svg)](https://github.com/Checkmk/ansible-collection-checkmk.general/actions/workflows/ans-int-test-lkp-rules.yaml)
[checkmk.general.ruleset](https://github.com/Checkmk/ansible-collection-checkmk.general/blob/main/plugins/lookup/ruleset.py)|Look up ruleset attributes.|[![Integration Tests for Ruleset Lookup Module](https://github.com/Checkmk/ansible-collection-checkmk.general/actions/workflows/ans-int-test-lkp-rulesets.yaml/badge.svg)](https://github.com/Checkmk/ansible-collection-checkmk.general/actions/workflows/ans-int-test-lkp-rulesets.yaml)
[checkmk.general.rulesets](https://github.com/Checkmk/ansible-collection-checkmk.general/blob/main/plugins/lookup/rulesets.py)|Look up all rulesets.|[![Integration Tests for Rulesets Lookup Module](https://github.com/Checkmk/ansible-collection-checkmk.general/actions/workflows/ans-int-test-lkp-rulesets.yaml/badge.svg)](https://github.com/Checkmk/ansible-collection-checkmk.general/actions/workflows/ans-int-test-lkp-rulesets.yaml)
[checkmk.general.site](https://github.com/Checkmk/ansible-collection-checkmk.general/blob/main/plugins/lookup/site.py)|Look up site attributes.|[![Integration Tests for Site Lookup Module](https://github.com/Checkmk/ansible-collection-checkmk.general/actions/workflows/ans-int-test-lkp-site.yaml/badge.svg)](https://github.com/Checkmk/ansible-collection-checkmk.general/actions/workflows/ans-int-test-lkp-site.yaml)
[checkmk.general.sites](https://github.com/Checkmk/ansible-collection-checkmk.general/blob/main/plugins/lookup/sites.py)|Look up all sites.|[![Integration Tests for Sites Lookup Module](https://github.com/Checkmk/ansible-collection-checkmk.general/actions/workflows/ans-int-test-lkp-sites.yaml/badge.svg)](https://github.com/Checkmk/ansible-collection-checkmk.general/actions/workflows/ans-int-test-lkp-sites.yaml)
[checkmk.general.version](https://github.com/Checkmk/ansible-collection-checkmk.general/blob/main/plugins/lookup/version.py)|Look up version and edition information.|[![Integration Tests for Version Lookup Module](https://github.com/Checkmk/ansible-collection-checkmk.general/actions/workflows/ans-int-test-lkp-version.yaml/badge.svg)](https://github.com/Checkmk/ansible-collection-checkmk.general/actions/workflows/ans-int-test-lkp-version.yaml)

### Modules
Expand Down
170 changes: 170 additions & 0 deletions plugins/lookup/site.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
# Copyright: (c) 2023, Lars Getwan <[email protected]>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function

__metaclass__ = type

DOCUMENTATION = """
name: site
author: Lars Getwan (@lgetwan)
version_added: "5.3.0"

short_description: Show the configuration of a site

description:
- Returns the configuration of a distributed monitoring site

options:

_terms:
description: site ID
required: True

server_url:
description: URL of the Checkmk server.
required: True
vars:
- name: ansible_lookup_checkmk_server_url
env:
- name: ANSIBLE_LOOKUP_CHECKMK_SERVER_URL
ini:
- section: checkmk_lookup
key: server_url

site:
description: Site name for REST API access.
required: True
vars:
- name: ansible_lookup_checkmk_site
env:
- name: ANSIBLE_LOOKUP_CHECKMK_SITE
ini:
- section: checkmk_lookup
key: site

automation_user:
description: Automation user for the REST API access.
required: True
vars:
- name: ansible_lookup_checkmk_automation_user
env:
- name: ANSIBLE_LOOKUP_CHECKMK_AUTOMATION_USER
ini:
- section: checkmk_lookup
key: automation_user

automation_secret:
description: Automation secret for the REST API access.
required: True
vars:
- name: ansible_lookup_checkmk_automation_secret
env:
- name: ANSIBLE_LOOKUP_CHECKMK_AUTOMATION_SECRET
ini:
- section: checkmk_lookup
key: automation_secret

validate_certs:
description: Whether or not to validate TLS certificates.
type: boolean
required: False
default: True
vars:
- name: ansible_lookup_checkmk_validate_certs
env:
- name: ANSIBLE_LOOKUP_CHECKMK_VALIDATE_CERTS
ini:
- section: checkmk_lookup
key: validate_certs

notes:
- Like all lookups, this runs on the Ansible controller and is unaffected by other keywords such as 'become'.
If you need to use different permissions, you must change the command or run Ansible as another user.
- Alternatively, you can use a shell/command task that runs against localhost and registers the result.
- The directory of the play is used as the current working directory.
- It is B(NOT) possible to assign other variables to the variables mentioned in the C(vars) section!
This is a limitation of Ansible itself.
"""

EXAMPLES = """
- name: Get a site with a particular site id
ansible.builtin.debug:
msg: "site: {{ extensions }}"
vars:
extensions: "{{
lookup('checkmk.general.site',
'my_remote_site',
server_url=server_url,
site=site,
automation_user=automation_user,
automation_secret=automation_secret,
validate_certs=False
)
}}"

- name: "Use variables outside the module call."
ansible.builtin.debug:
msg: "site: {{ extensions }}"
vars:
ansible_lookup_checkmk_server_url: "http://myserver/"
ansible_lookup_checkmk_site: "mysite"
ansible_lookup_checkmk_automation_user: "myuser"
ansible_lookup_checkmk_automation_secret: "mysecret"
ansible_lookup_checkmk_validate_certs: false
attributes: "{{ lookup('checkmk.general.site', 'my_remote_site') }}"
"""

RETURN = """
_list:
description:
- The details of a particular site
type: list
elements: str
"""

import json

from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
from ansible_collections.checkmk.general.plugins.module_utils.lookup_api import (
CheckMKLookupAPI,
)


class LookupModule(LookupBase):
def run(self, terms, variables, **kwargs):
self.set_options(var_options=variables, direct=kwargs)
server_url = self.get_option("server_url")
site = self.get_option("site")
user = self.get_option("automation_user")
secret = self.get_option("automation_secret")
validate_certs = self.get_option("validate_certs")

site_url = server_url + "/" + site

api = CheckMKLookupAPI(
site_url=site_url,
user=user,
secret=secret,
validate_certs=validate_certs,
)

ret = []

for term in terms:
response = json.loads(api.get("/objects/site_connection/" + term))

if "code" in response:
raise AnsibleError(
"Received error for %s - %s: %s"
% (
response.get("url", ""),
response.get("code", ""),
response.get("msg", ""),
)
)

ret.append(response.get("extensions", {}))

return ret
Loading