Skip to content

Commit 58546ca

Browse files
committed
Corrections to failing tests
Signed-off-by: Andrew Block <andy.block@gmail.com>
1 parent fee9fc1 commit 58546ca

File tree

10 files changed

+98
-77
lines changed

10 files changed

+98
-77
lines changed

plugins/doc_fragments/jbossnetwork_search_options.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

plugins/module_utils/args_common.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright [2023] [Red Hat, Inc.]
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
from __future__ import absolute_import, division, print_function
216

317
__metaclass__ = type
@@ -12,12 +26,12 @@
1226
client_secret=dict(no_log=True, required=False),
1327
api_url=dict(required=False, default=JBOSS_NETWORK_API_URL),
1428
sso_url=dict(required=False, default=REDHAT_SSO_URL),
15-
validate_certs=dict(required=False,default=True, type=bool)
29+
validate_certs=dict(required=False, default=True, type=bool)
1630
)
1731

1832
JBOSS_NETWORK_SEARCH_ARGS_SPEC = dict(
1933
product_id=dict(required=False),
2034
product_category=dict(required=False),
2135
product_type=dict(required=False),
2236
product_version=dict(required=False)
23-
)
37+
)

plugins/module_utils/constants.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright [2023] [Red Hat, Inc.]
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
from __future__ import absolute_import, division, print_function
216

317
__metaclass__ = type
@@ -7,10 +21,9 @@
721
REDHAT_PRODUCT_DOWNLOAD_CLIENT_ID_ENV_VAR = "REDHAT_PRODUCT_DOWNLOAD_CLIENT_ID"
822
REDHAT_PRODUCT_DOWNLOAD_CLIENT_SECRET_ENV_VAR = "REDHAT_PRODUCT_DOWNLOAD_CLIENT_SECRET"
923

10-
1124
API_SERVICE_PATH = "/v1/middleware"
1225
LIST_PRODUCT_CATEGORIES_ENDPOINT = "/list/categories"
13-
SEARCH_ENDPOINT = "/search"
26+
SEARCH_ENDPOINT = "/search"
1427

1528
QUERY_PAGE_SIZE = 100
1629

@@ -24,4 +37,4 @@
2437
SEARCH_PARAM_NAME = "name"
2538
SEARCH_PARAM_VERSION = "version"
2639
SEARCH_PARAM_CATEGORY = "category"
27-
SEARCH_PARAM_TYPE = "type"
40+
SEARCH_PARAM_TYPE = "type"

plugins/module_utils/jbossnetwork.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright [2023] [Red Hat, Inc.]
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
from __future__ import absolute_import, division, print_function
216

317
__metaclass__ = type
@@ -30,7 +44,6 @@ def get_authenticated_session(module, sso_url, validate_certs, client_id, client
3044
"grant_type": "client_credentials",
3145
}
3246

33-
3447
# Obtain Access Token
3548
token_request = requests.post(
3649
f"{sso_url}/auth/realms/redhat-external/protocol/openid-connect/token",
@@ -52,8 +65,8 @@ def get_authenticated_session(module, sso_url, validate_certs, client_id, client
5265

5366
return session
5467

55-
def generate_search_params(product_category, product_id, product_type, product_version):
5668

69+
def generate_search_params(product_category, product_id, product_type, product_version):
5770
search_params = {
5871
SEARCH_PARAM_CATEGORY: product_category,
5972
SEARCH_PARAM_TYPE: product_type,
@@ -65,11 +78,15 @@ def generate_search_params(product_category, product_id, product_type, product_v
6578

6679
return search_params
6780

68-
def perform_search(session, url, validate_certs, params={}):
81+
82+
def perform_search(session, url, validate_certs, params=None):
6983

7084
nextCursor = None
7185
results = []
7286

87+
if params is None:
88+
params = {}
89+
7390
while True:
7491

7592
pagination_params = {}
@@ -91,7 +108,7 @@ def perform_search(session, url, validate_certs, params={}):
91108

92109
results.extend(
93110
query_result_json[RESULTS_FIELD])
94-
111+
95112
if NEXT_CURSOR_FIELD not in query_result_json or query_result_json[NEXT_CURSOR_FIELD] is None:
96113
break
97114

plugins/modules/product_download.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
extends_documentation_fragment:
2929
- files
3030
- middleware_automation.common.jbossnetwork_connection_options
31-
- middleware_automation.common.jbossnetwork_search_options
3231
options:
3332
dest:
3433
description:
@@ -136,23 +135,25 @@
136135
LIST_PRODUCT_CATEGORIES_ENDPOINT
137136
)
138137

139-
140138
try:
141139
import requests
142140
HAS_REQUESTS = True
143141
except ImportError:
144142
HAS_REQUESTS = False
145143
REQUESTS_IMP_ERR = traceback.format_exc()
144+
else:
145+
REQUESTS_IMP_ERR = None
146146

147147

148148
def argspec():
149149
argument_spec = copy.deepcopy(JBOSS_NETWORK_COMMON_ARGS_SPEC)
150150
argument_spec.update(copy.deepcopy(JBOSS_NETWORK_SEARCH_ARGS_SPEC))
151151
argument_spec["dest"] = dict(required=True)
152-
argument_spec["force"] = dict(required=False,default=False, type=bool)
152+
argument_spec["force"] = dict(required=False, default=False, type=bool)
153153

154154
return argument_spec
155155

156+
156157
def main():
157158
module = AnsibleModule(
158159
argument_spec=argspec(),
@@ -161,7 +162,7 @@ def main():
161162

162163
if not HAS_REQUESTS:
163164
module.fail_json(msg=missing_required_lib("requests"), exception=REQUESTS_IMP_ERR)
164-
165+
165166
client_id = module.params.get('client_id')
166167
client_secret = module.params.get('client_secret')
167168
api_url = module.params.get('api_url')
@@ -178,28 +179,29 @@ def main():
178179
client_id = os.environ.get(REDHAT_PRODUCT_DOWNLOAD_CLIENT_ID_ENV_VAR)
179180

180181
if not client_id:
181-
module.fail_json(msg=str(f"Client ID not specified and unable to determine Client ID from '{REDHAT_PRODUCT_DOWNLOAD_CLIENT_ID_ENV_VAR}' environment variable."))
182+
module.fail_json(msg=str("Client ID not specified and unable to determine Client ID "
183+
f"from '{REDHAT_PRODUCT_DOWNLOAD_CLIENT_ID_ENV_VAR}' environment variable."))
182184

183185
if not client_secret:
184186
client_secret = os.environ.get(REDHAT_PRODUCT_DOWNLOAD_CLIENT_SECRET_ENV_VAR)
185187

186188
if not client_secret:
187-
module.fail_json(msg=str(f"Client Secret not specified and unable to determine Client Secret from '{REDHAT_PRODUCT_DOWNLOAD_CLIENT_SECRET_ENV_VAR}' environment variable."))
189+
module.fail_json(msg=str("Client Secret not specified and unable to determine Client Secret "
190+
f"from '{REDHAT_PRODUCT_DOWNLOAD_CLIENT_SECRET_ENV_VAR}' environment variable."))
188191

189192
if not dest:
190193
module.fail_json(msg=str("Destination path not provided"))
191-
194+
192195
session = get_authenticated_session(module, sso_url, validate_certs, client_id, client_secret)
193196

194197
api_base_url = f"{api_url}{API_SERVICE_PATH}"
195198

196-
197199
if product_category is not None:
198200
# List Product Categories
199201
product_categories = []
200202

201203
try:
202-
product_categories = perform_search(session, f"{api_base_url}{LIST_PRODUCT_CATEGORIES_ENDPOINT}",validate_certs)
204+
product_categories = perform_search(session, f"{api_base_url}{LIST_PRODUCT_CATEGORIES_ENDPOINT}", validate_certs)
203205
except Exception as err:
204206
module.fail_json(msg="Error Listing Available Product Categories: %s" % (to_native(err)))
205207

@@ -212,7 +214,7 @@ def main():
212214
search_params = generate_search_params(product_category, product_id, product_type, product_version)
213215

214216
try:
215-
search_results = perform_search(session, f"{api_base_url}{SEARCH_ENDPOINT}", validate_certs,search_params)
217+
search_results = perform_search(session, f"{api_base_url}{SEARCH_ENDPOINT}", validate_certs, search_params)
216218
except Exception as err:
217219
module.fail_json(msg="Error Searching for Products: %s" % (to_native(err)))
218220

@@ -225,10 +227,10 @@ def main():
225227
]
226228

227229
for productIdx, product in enumerate(search_results):
228-
msg.append(f"{productIdx+1} - ({search_results[productIdx]['id']}) {search_results[productIdx]['title']}.")
229-
230+
msg.append(f"{productIdx+1} - ({product['id']}) {product['title']}.")
231+
230232
module.fail_json(msg=" ".join(msg))
231-
233+
232234
file_name = search_results[0]['file_path'].rsplit('/')[-1]
233235

234236
dest_is_dir = os.path.isdir(dest)
@@ -241,11 +243,10 @@ def main():
241243
dest=dest
242244
)
243245

244-
245246
if os.path.exists(dest) and not force:
246247
file_args = module.load_file_common_arguments(module.params, path=dest)
247248
result['changed'] = module.set_fs_attributes_if_different(file_args, False)
248-
249+
249250
if result['changed']:
250251
module.exit_json(msg="file already exists but file attributes changed", **result)
251252
module.exit_json(msg="file already exists", **result)
@@ -265,12 +266,13 @@ def main():
265266
module.fail_json(msg="Destination %s is not writable" % (os.path.dirname(dest)), **result)
266267

267268
try:
268-
with session.get(search_results[0]["download_path"], verify=validate_certs, stream=True, allow_redirects=True, headers={"User-Agent": "product_download"}) as r:
269+
with session.get(search_results[0]["download_path"], verify=validate_certs,
270+
stream=True, allow_redirects=True, headers={"User-Agent": "product_download"}) as r:
269271
r.raise_for_status()
270272
with open(dest, 'wb') as f:
271273
for chunk in r.iter_content(chunk_size=8192):
272274
f.write(chunk)
273-
275+
274276
result['changed'] = True
275277
except Exception as err:
276278
module.fail_json(msg="Error Downloading %s: %s" % (search_results[0]['title'], to_native(err)))
@@ -282,7 +284,7 @@ def main():
282284
result['md5sum'] = module.md5(dest)
283285
except ValueError:
284286
result['md5sum'] = None
285-
287+
286288
module.exit_json(msg="", **result)
287289

288290

0 commit comments

Comments
 (0)