Skip to content

Commit 72e64f8

Browse files
committed
Fix linting for BI modules.
1 parent 8dffdb0 commit 72e64f8

File tree

5 files changed

+116
-154
lines changed

5 files changed

+116
-154
lines changed

plugins/lookup/bi_aggregation.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,19 @@
4343
- section: checkmk_lookup
4444
key: site
4545
46-
auth_type:
46+
automation_auth_type:
4747
description: Authentication type to use with the Checkmk API.
4848
type: str
4949
required: False
5050
choices: ["bearer", "basic", "cookie"]
5151
default: "bearer"
5252
vars:
53-
- name: ansible_lookup_checkmk_auth_type
53+
- name: ansible_lookup_checkmk_automation_auth_type
5454
env:
55-
- name: ANSIBLE_LOOKUP_CHECKMK_AUTH_TYPE
55+
- name: ANSIBLE_LOOKUP_CHECKMK_automation_auth_type
5656
ini:
5757
- section: checkmk_lookup
58-
key: auth_type
58+
key: automation_auth_type
5959
6060
automation_user:
6161
description: Automation user for the REST API access.
@@ -79,16 +79,16 @@
7979
- section: checkmk_lookup
8080
key: automation_secret
8181
82-
auth_cookie:
82+
automation_auth_cookie:
8383
description: Authentication cookie for the REST API access.
8484
required: False
8585
vars:
86-
- name: ansible_lookup_checkmk_auth_cookie
86+
- name: ansible_lookup_checkmk_automation_auth_cookie
8787
env:
88-
- name: ANSIBLE_LOOKUP_CHECKMK_AUTH_COOKIE
88+
- name: ANSIBLE_LOOKUP_CHECKMK_automation_auth_cookie
8989
ini:
9090
- section: checkmk_lookup
91-
key: auth_cookie
91+
key: automation_auth_cookie
9292
9393
validate_certs:
9494
description: Whether or not to validate TLS certificates.
@@ -137,10 +137,10 @@ class ExtendedCheckmkAPI(CheckMKLookupAPI):
137137
def __init__(
138138
self,
139139
site_url,
140-
auth_type="bearer",
140+
automation_auth_type="bearer",
141141
automation_user=None,
142142
automation_secret=None,
143-
auth_cookie=None,
143+
automation_auth_cookie=None,
144144
validate_certs=True,
145145
):
146146
headers = {
@@ -150,15 +150,15 @@ def __init__(
150150
cookies = {}
151151

152152
# Bearer Authentication: "Bearer USERNAME PASSWORD"
153-
if auth_type == "bearer":
153+
if automation_auth_type == "bearer":
154154
if not automation_user or not automation_secret:
155155
raise ValueError(
156156
"`automation_user` and `automation_secret` are required for bearer authentication."
157157
)
158158
headers["Authorization"] = f"Bearer {automation_user} {automation_secret}"
159159

160160
# Basic Authentication
161-
elif auth_type == "basic":
161+
elif automation_auth_type == "basic":
162162
if not automation_user or not automation_secret:
163163
raise ValueError(
164164
"`automation_user` and `automation_secret` are required for basic authentication."
@@ -168,13 +168,17 @@ def __init__(
168168
headers["Authorization"] = f"Basic {auth_b64}"
169169

170170
# Cookie Authentication
171-
elif auth_type == "cookie":
172-
if not auth_cookie:
173-
raise ValueError("`auth_cookie` is required for cookie authentication.")
174-
cookies["auth_cmk"] = auth_cookie
171+
elif automation_auth_type == "cookie":
172+
if not automation_auth_cookie:
173+
raise ValueError(
174+
"`automation_auth_cookie` is required for cookie authentication."
175+
)
176+
cookies["auth_cmk"] = automation_auth_cookie
175177

176178
else:
177-
raise ValueError(f"Unsupported `auth_type`: {auth_type}")
179+
raise ValueError(
180+
f"Unsupported `automation_auth_type`: {automation_auth_type}"
181+
)
178182

179183
super().__init__(
180184
site_url=site_url, user="", secret="", validate_certs=validate_certs
@@ -189,21 +193,21 @@ def run(self, terms, variables=None, **kwargs):
189193
aggregation_id = terms[0]
190194
server_url = self.get_option("server_url")
191195
site = self.get_option("site")
192-
auth_type = self.get_option("auth_type")
196+
automation_auth_type = self.get_option("automation_auth_type")
193197
automation_user = self.get_option("automation_user")
194198
automation_secret = self.get_option("automation_secret")
195-
auth_cookie = self.get_option("auth_cookie")
199+
automation_auth_cookie = self.get_option("automation_auth_cookie")
196200
validate_certs = self.get_option("validate_certs")
197201

198202
site_url = f"{server_url.rstrip('/')}/{site}"
199203

200204
try:
201205
api = ExtendedCheckmkAPI(
202206
site_url=site_url,
203-
auth_type=auth_type,
207+
automation_auth_type=automation_auth_type,
204208
automation_user=automation_user,
205209
automation_secret=automation_secret,
206-
auth_cookie=auth_cookie,
210+
automation_auth_cookie=automation_auth_cookie,
207211
validate_certs=validate_certs,
208212
)
209213
except ValueError as e:

plugins/lookup/bi_rule.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
- section: checkmk_lookup
4444
key: site
4545
46-
auth_type:
46+
automation_auth_type:
4747
description: The authentication type to use ('bearer', 'basic', 'cookie').
4848
required: False
4949
default: 'bearer'
@@ -56,7 +56,7 @@
5656
description: The automation secret or password for authentication.
5757
required: True
5858
59-
auth_cookie:
59+
automation_auth_cookie:
6060
description: The authentication cookie value if using cookie-based authentication.
6161
required: False
6262
@@ -128,10 +128,10 @@ class ExtendedCheckmkAPI(CheckMKLookupAPI):
128128
def __init__(
129129
self,
130130
site_url,
131-
auth_type="bearer",
131+
automation_auth_type="bearer",
132132
automation_user=None,
133133
automation_secret=None,
134-
auth_cookie=None,
134+
automation_auth_cookie=None,
135135
validate_certs=True,
136136
):
137137
headers = {
@@ -141,15 +141,15 @@ def __init__(
141141
cookies = {}
142142

143143
# Bearer Authentication: "Bearer USERNAME PASSWORD"
144-
if auth_type == "bearer":
144+
if automation_auth_type == "bearer":
145145
if not automation_user or not automation_secret:
146146
raise ValueError(
147147
"`automation_user` and `automation_secret` are required for bearer authentication."
148148
)
149149
headers["Authorization"] = f"Bearer {automation_user} {automation_secret}"
150150

151151
# Basic Authentication
152-
elif auth_type == "basic":
152+
elif automation_auth_type == "basic":
153153
if not automation_user or not automation_secret:
154154
raise ValueError(
155155
"`automation_user` and `automation_secret` are required for basic authentication."
@@ -159,13 +159,17 @@ def __init__(
159159
headers["Authorization"] = f"Basic {auth_b64}"
160160

161161
# Cookie Authentication
162-
elif auth_type == "cookie":
163-
if not auth_cookie:
164-
raise ValueError("`auth_cookie` is required for cookie authentication.")
165-
cookies["auth_cmk"] = auth_cookie
162+
elif automation_auth_type == "cookie":
163+
if not automation_auth_cookie:
164+
raise ValueError(
165+
"`automation_auth_cookie` is required for cookie authentication."
166+
)
167+
cookies["auth_cmk"] = automation_auth_cookie
166168

167169
else:
168-
raise ValueError(f"Unsupported `auth_type`: {auth_type}")
170+
raise ValueError(
171+
f"Unsupported `automation_auth_type`: {automation_auth_type}"
172+
)
169173

170174
super().__init__(
171175
site_url=site_url, user="", secret="", validate_certs=validate_certs
@@ -182,28 +186,28 @@ def run(self, terms, variables=None, **kwargs):
182186
try:
183187
server_url = self.get_option("server_url")
184188
site = self.get_option("site")
185-
auth_type = self.get_option("auth_type") or "bearer"
189+
automation_auth_type = self.get_option("automation_auth_type") or "bearer"
186190
automation_user = self.get_option("automation_user")
187191
automation_secret = self.get_option("automation_secret")
188-
auth_cookie = self.get_option("auth_cookie")
192+
automation_auth_cookie = self.get_option("automation_auth_cookie")
189193
validate_certs = self.get_option("validate_certs")
190194
except KeyError as e:
191195
raise AnsibleError(f"Missing required configuration option: {str(e)}")
192196

193197
site_url = f"{server_url.rstrip('/')}/{site}"
194198

195199
# Optional: Debugging prints
196-
# print(f"auth_type: {auth_type}")
200+
# print(f"automation_auth_type: {automation_auth_type}")
197201
# print(f"server_url: {server_url}")
198202
# print(f"rule_id: {rule_id}")
199203

200204
try:
201205
api = ExtendedCheckmkAPI(
202206
site_url=site_url,
203-
auth_type=auth_type,
207+
automation_auth_type=automation_auth_type,
204208
automation_user=automation_user,
205209
automation_secret=automation_secret,
206-
auth_cookie=auth_cookie,
210+
automation_auth_cookie=automation_auth_cookie,
207211
validate_certs=validate_certs,
208212
)
209213
except ValueError as e:

plugins/modules/bi_aggregation.py

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
description:
7070
- Node generation definition.
7171
type: dict
72-
required: true
72+
required: false
7373
suboptions:
7474
search:
7575
description:
@@ -107,7 +107,7 @@
107107
checkmk.general.bi_aggregation:
108108
server_url: "http://myserver/"
109109
site: "mysite"
110-
auth_type: "bearer"
110+
automation_auth_type: "bearer"
111111
automation_user: "myuser"
112112
automation_secret: "mysecret"
113113
aggregation:
@@ -142,7 +142,7 @@
142142
checkmk.general.bi_aggregation:
143143
server_url: "http://myserver/"
144144
site: "mysite"
145-
auth_type: "bearer"
145+
automation_auth_type: "bearer"
146146
automation_user: "myuser"
147147
automation_secret: "mysecret"
148148
aggregation:
@@ -174,6 +174,9 @@
174174
from ansible.module_utils.basic import AnsibleModule
175175
from ansible_collections.checkmk.general.plugins.module_utils.api import CheckmkAPI
176176
from ansible_collections.checkmk.general.plugins.module_utils.differ import ConfigDiffer
177+
from ansible_collections.checkmk.general.plugins.module_utils.utils import (
178+
base_argument_spec,
179+
)
177180

178181

179182
class ExtendedCheckmkAPI(CheckmkAPI):
@@ -185,35 +188,37 @@ class ExtendedCheckmkAPI(CheckmkAPI):
185188
def __init__(self, module):
186189
"""Initialize ExtendedCheckmkAPI with authentication handling."""
187190
super().__init__(module)
188-
auth_type = self.params.get("auth_type", "bearer")
191+
automation_auth_type = self.params.get("automation_auth_type", "bearer")
189192
automation_user = self.params.get("automation_user")
190193
automation_secret = self.params.get("automation_secret")
191-
auth_cookie = self.params.get("auth_cookie")
194+
automation_auth_cookie = self.params.get("automation_auth_cookie")
192195

193-
if auth_type == "bearer":
196+
if automation_auth_type == "bearer":
194197
if not automation_user or not automation_secret:
195198
self.module.fail_json(
196199
msg="`automation_user` and `automation_secret` are required for bearer authentication."
197200
)
198201
self.headers["Authorization"] = (
199202
f"Bearer {automation_user} {automation_secret}"
200203
)
201-
elif auth_type == "basic":
204+
elif automation_auth_type == "basic":
202205
if not automation_user or not automation_secret:
203206
self.module.fail_json(
204207
msg="`automation_user` and `automation_secret` are required for basic authentication."
205208
)
206209
auth_str = f"{automation_user}:{automation_secret}"
207210
auth_b64 = base64.b64encode(auth_str.encode("utf-8")).decode("utf-8")
208211
self.headers["Authorization"] = f"Basic {auth_b64}"
209-
elif auth_type == "cookie":
210-
if not auth_cookie:
212+
elif automation_auth_type == "cookie":
213+
if not automation_auth_cookie:
211214
self.module.fail_json(
212-
msg="`auth_cookie` is required for cookie authentication."
215+
msg="`automation_auth_cookie` is required for cookie authentication."
213216
)
214-
self.cookies["auth_cmk"] = auth_cookie
217+
self.cookies["auth_cmk"] = automation_auth_cookie
215218
else:
216-
self.module.fail_json(msg=f"Unsupported `auth_type`: {auth_type}")
219+
self.module.fail_json(
220+
msg=f"Unsupported `automation_auth_type`: {automation_auth_type}"
221+
)
217222

218223

219224
class BIAggregationHTTPCodes:
@@ -415,18 +420,8 @@ def run_module():
415420
Returns:
416421
None: The result is returned to Ansible via module.exit_json().
417422
"""
418-
module_args = dict(
419-
server_url=dict(type="str", required=True),
420-
site=dict(type="str", required=True),
421-
auth_type=dict(
422-
type="str",
423-
choices=["bearer", "basic", "cookie"],
424-
default="bearer",
425-
required=False,
426-
),
427-
automation_user=dict(type="str", required=False),
428-
automation_secret=dict(type="str", required=False, no_log=True),
429-
auth_cookie=dict(type="str", required=False, no_log=True),
423+
argument_spec = base_argument_spec()
424+
argument_spec.update(
430425
aggregation=dict(
431426
type="dict",
432427
required=True,
@@ -466,17 +461,16 @@ def run_module():
466461
),
467462
),
468463
state=dict(type="str", default="present", choices=["present", "absent"]),
469-
validate_certs=dict(type="bool", default=True, required=False),
470464
)
471465

472466
required_if = [
473-
("auth_type", "bearer", ["automation_user", "automation_secret"]),
474-
("auth_type", "basic", ["automation_user", "automation_secret"]),
475-
("auth_type", "cookie", ["auth_cookie"]),
467+
("automation_auth_type", "bearer", ["automation_user", "automation_secret"]),
468+
("automation_auth_type", "basic", ["automation_user", "automation_secret"]),
469+
("automation_auth_type", "cookie", ["automation_auth_cookie"]),
476470
]
477471

478472
module = AnsibleModule(
479-
argument_spec=module_args,
473+
argument_spec=argument_spec,
480474
supports_check_mode=True,
481475
required_if=required_if,
482476
)

0 commit comments

Comments
 (0)