Skip to content

Commit

Permalink
Created Lookup Modules 'site' and 'sites'.
Browse files Browse the repository at this point in the history
  • Loading branch information
lgetwan committed Sep 11, 2024
1 parent 5207610 commit 245a56f
Show file tree
Hide file tree
Showing 13 changed files with 636 additions and 0 deletions.
75 changes: 75 additions & 0 deletions .github/workflows/ans-int-test-lkp-site.yml
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.yml
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}}
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

0 comments on commit 245a56f

Please sign in to comment.