Skip to content

Commit 5c0bf90

Browse files
Convert f-strings to str.format() (#19)
* Convert f-strings to str.format() * Fix sanity ansible-format-automatic-specification * skip tests on PRs * typo * load_file_common_args has path in params * python 2.7 urllib open has no context manager
1 parent 6d2d90a commit 5c0bf90

File tree

5 files changed

+67
-64
lines changed

5 files changed

+67
-64
lines changed

molecule/prod_eap/converge.yml

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,45 @@
66
client_id: "{{ lookup('env', 'PROD_JBOSSNETWORK_API_CLIENTID') }}"
77
client_secret: "{{ lookup('env', 'PROD_JBOSSNETWORK_API_SECRET') }}"
88
tasks:
9-
- name: "Ensures required secret are provided."
10-
ansible.builtin.assert:
11-
that:
12-
- client_id is defined and client_id | length > 0
13-
- client_secret is defined and client_secret | length > 0
14-
fail_msg: "Missing required env vars PROD_JBOSSNETWORK_API_CLIENTID and/or PROD_JBOSSNETWORK_API_SECRET"
9+
- name: Execute scenario if staging credentials are provided
10+
when:
11+
- client_id is defined and client_password is defined
12+
block:
13+
- name: "Ensures required secret are provided."
14+
ansible.builtin.assert:
15+
that:
16+
- client_id is defined and client_id | length > 0
17+
- client_secret is defined and client_secret | length > 0
18+
fail_msg: "Missing required env vars PROD_JBOSSNETWORK_API_CLIENTID and/or PROD_JBOSSNETWORK_API_SECRET"
1519

16-
- name: Search EAP Product
17-
middleware_automation.common.product_search:
18-
client_id: "{{ client_id }}"
19-
client_secret: "{{ client_secret }}"
20-
product_type: DISTRIBUTION
21-
product_version: '7.4'
22-
product_category: appplatform
23-
register: rhn_products
24-
no_log: false
20+
- name: Search EAP Product
21+
middleware_automation.common.product_search:
22+
client_id: "{{ client_id }}"
23+
client_secret: "{{ client_secret }}"
24+
product_type: DISTRIBUTION
25+
product_version: '7.4'
26+
product_category: appplatform
27+
register: rhn_products
28+
no_log: false
2529

26-
- name: Search install zipfile
27-
ansible.builtin.set_fact:
28-
rhn_filtered_products: "{{ rhn_products.results | selectattr('file_path', 'match', '[^/]*/jboss-eap-7.4.0.zip$') }}"
30+
- name: Search install zipfile
31+
ansible.builtin.set_fact:
32+
rhn_filtered_products: "{{ rhn_products.results | selectattr('file_path', 'match', '[^/]*/jboss-eap-7.4.0.zip$') }}"
2933

30-
- name: Retrieve product download using JBossNetwork API
31-
middleware_automation.common.product_search:
32-
client_id: "{{ client_id }}"
33-
client_secret: "{{ client_secret }}"
34-
product_type: BUGFIX
35-
product_version: '7.4'
36-
product_category: appplatform
37-
register: rhn_products
38-
no_log: true
34+
- name: Retrieve product download using JBossNetwork API
35+
middleware_automation.common.product_search:
36+
client_id: "{{ client_id }}"
37+
client_secret: "{{ client_secret }}"
38+
product_type: BUGFIX
39+
product_version: '7.4'
40+
product_category: appplatform
41+
register: rhn_products
42+
no_log: true
3943

40-
- name: Determine patch versions list
41-
ansible.builtin.set_fact:
42-
filtered_versions: "{{ rhn_products.results | map(attribute='file_path') | select('match', '^[^/]*/jboss-eap-.*[0-9]*[.][0-9]*[.][0-9]*.*$') | map('regex_replace','[^/]*/jboss-eap-([0-9]*[.][0-9]*[.][0-9]*)-.*','\\1' ) | list | unique }}"
44+
- name: Determine patch versions list
45+
ansible.builtin.set_fact:
46+
filtered_versions: "{{ rhn_products.results | map(attribute='file_path') | select('match', '^[^/]*/jboss-eap-.*[0-9]*[.][0-9]*[.][0-9]*.*$') | map('regex_replace','[^/]*/jboss-eap-([0-9]*[.][0-9]*[.][0-9]*)-.*','\\1' ) | list | unique }}"
4347

44-
- name: Determine latest version
45-
ansible.builtin.debug:
46-
msg: "JBoss EAP 7.4 most recent CP is version {{ filtered_versions | middleware_automation.common.version_sort | last }}"
48+
- name: Determine latest version
49+
ansible.builtin.debug:
50+
msg: "JBoss EAP 7.4 most recent CP is version {{ filtered_versions | middleware_automation.common.version_sort | last }}"

molecule/stage_eap/converge.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
---
22
- name: Converge
3-
hosts: all
4-
gather_facts: yes
3+
hosts: localhost
4+
gather_facts: no
55
vars:
6-
client_id: "{{ lookup('env', 'STAGE_JBOSSNETWORK_API_CLIENTID') }}"
7-
client_secret: "{{ lookup('env', 'STAGE_JBOSSNETWORK_API_SECRET') }}"
6+
client_id: "{{ lookup('env', 'STAGE_JBOSSNETWORK_API_CLIENTID') }}"
7+
client_secret: "{{ lookup('env', 'STAGE_JBOSSNETWORK_API_SECRET') }}"
88
tasks:
99
- name: Execute scenario if staging credentials are provided
1010
when:
1111
- client_id is defined and client_password is defined
1212
block:
13-
1413
- name: Search EAP Product
1514
middleware_automation.common.product_search:
1615
api_url: "https://jbossnetwork.stage.api.redhat.com"

plugins/module_utils/jbossnetwork.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def get_authenticated_session(module, sso_url, validate_certs, client_id, client
4949
session = Request(validate_certs=validate_certs, headers={})
5050

5151
try:
52-
token_request = session.post(f"{sso_url}/auth/realms/redhat-external/protocol/openid-connect/token", data=urlencode(token_request_data))
52+
token_request = session.post("{0}/auth/realms/redhat-external/protocol/openid-connect/token".format(sso_url), data=urlencode(token_request_data))
5353

5454
except Exception as err:
5555
module.fail_json(msg="Error Retrieving SSO Access Token: %s" % (to_native(err)))
@@ -58,7 +58,7 @@ def get_authenticated_session(module, sso_url, validate_certs, client_id, client
5858

5959
# Setup Session
6060
session.headers = {
61-
"Authorization": f"Bearer {access_token}",
61+
"Authorization": "Bearer {0}".format(access_token),
6262
"Content-Type": "application/json",
6363
}
6464

plugins/modules/product_download.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -186,41 +186,41 @@ def main():
186186

187187
if not client_id:
188188
module.fail_json(msg=str("Client ID not specified and unable to determine Client ID "
189-
f"from '{REDHAT_PRODUCT_DOWNLOAD_CLIENT_ID_ENV_VAR}' environment variable."))
189+
"from '{0}' environment variable.".format(REDHAT_PRODUCT_DOWNLOAD_CLIENT_ID_ENV_VAR)))
190190

191191
if not client_secret:
192192
client_secret = os.environ.get(REDHAT_PRODUCT_DOWNLOAD_CLIENT_SECRET_ENV_VAR)
193193

194194
if not client_secret:
195195
module.fail_json(msg=str("Client Secret not specified and unable to determine Client Secret "
196-
f"from '{REDHAT_PRODUCT_DOWNLOAD_CLIENT_SECRET_ENV_VAR}' environment variable."))
196+
"from '{0}' environment variable.".format(REDHAT_PRODUCT_DOWNLOAD_CLIENT_SECRET_ENV_VAR)))
197197

198198
if not dest:
199199
module.fail_json(msg=str("Destination path not provided"))
200200

201201
session = get_authenticated_session(module, sso_url, validate_certs, client_id, client_secret)
202202

203-
api_base_url = f"{api_url}{API_SERVICE_PATH}"
203+
api_base_url = "{0}{1}".format(api_url, API_SERVICE_PATH)
204204

205205
if product_category is not None:
206206
# List Product Categories
207207
product_categories = []
208208

209209
try:
210-
product_categories = perform_search(session, f"{api_base_url}{LIST_PRODUCT_CATEGORIES_ENDPOINT}", validate_certs)
210+
product_categories = perform_search(session, "{0}{1}".format(api_base_url, LIST_PRODUCT_CATEGORIES_ENDPOINT), validate_certs)
211211
except Exception as err:
212212
module.fail_json(msg="Error Listing Available Product Categories: %s" % (to_native(err)))
213213

214214
if product_category not in product_categories:
215-
module.fail_json(msg=f"'{product_category}' is not a valid Product Category")
215+
module.fail_json(msg="'{0}' is not a valid Product Category".format(product_category))
216216

217217
# Search for Products
218218
search_results = []
219219

220220
search_params = generate_search_params(product_category, product_id, product_type, product_version)
221221

222222
try:
223-
search_results = perform_search(session, f"{api_base_url}{SEARCH_ENDPOINT}", validate_certs, search_params)
223+
search_results = perform_search(session, "{0}{1}".format(api_base_url, SEARCH_ENDPOINT), validate_certs, search_params)
224224
except Exception as err:
225225
module.fail_json(msg="Error Searching for Products: %s" % (to_native(err)))
226226

@@ -229,11 +229,11 @@ def main():
229229
# Print error with results if more than 1 exists
230230
if products_found != 1:
231231
msg = [
232-
(f"Error: Unable to locate a single product to download. '{products_found}' products found.")
232+
("Error: Unable to locate a single product to download. '{0}' products found.".format(products_found))
233233
]
234234

235235
for productIdx, product in enumerate(search_results):
236-
msg.append(f"{productIdx+1} - ({product['id']}) {product['title']}.")
236+
msg.append("{0} - ({1}) {2}.".format(productIdx + 1, product['id'], product['title']))
237237

238238
module.fail_json(msg=" ".join(msg))
239239

@@ -250,7 +250,7 @@ def main():
250250
)
251251

252252
if os.path.exists(dest) and not force:
253-
file_args = module.load_file_common_arguments(module.params, path=dest)
253+
file_args = module.load_file_common_arguments(module.params)
254254
result['changed'] = module.set_fs_attributes_if_different(file_args, False)
255255

256256
if result['changed']:
@@ -272,20 +272,20 @@ def main():
272272
module.fail_json(msg="Destination %s is not writable" % (os.path.dirname(dest)), **result)
273273

274274
try:
275-
with session.get(search_results[0]["download_path"], follow_redirects=True, headers={"User-Agent": "product_download"}) as r:
276-
chunk_size = 8192
277-
with open(dest, 'wb') as f:
278-
while True:
279-
chunk = r.read(chunk_size)
280-
if not chunk:
281-
break
282-
f.write(chunk)
275+
r = session.get(search_results[0]["download_path"], follow_redirects=True, headers={"User-Agent": "product_download"})
276+
chunk_size = 8192
277+
with open(dest, 'wb') as f:
278+
while True:
279+
chunk = r.read(chunk_size)
280+
if not chunk:
281+
break
282+
f.write(chunk)
283283

284284
result['changed'] = True
285285
except Exception as err:
286286
module.fail_json(msg="Error Downloading %s: %s" % (search_results[0]['title'], to_native(err)))
287287

288-
file_args = module.load_file_common_arguments(module.params, path=dest)
288+
file_args = module.load_file_common_arguments(module.params)
289289
result['changed'] = module.set_fs_attributes_if_different(file_args, result['changed'])
290290

291291
try:

plugins/modules/product_search.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,38 +167,38 @@ def main():
167167

168168
if not client_id:
169169
module.fail_json(msg=str("Client ID not specified and unable to determine Client ID "
170-
f"from '{REDHAT_PRODUCT_DOWNLOAD_CLIENT_ID_ENV_VAR}' environment variable."))
170+
"from '{0}' environment variable.".format(REDHAT_PRODUCT_DOWNLOAD_CLIENT_ID_ENV_VAR)))
171171

172172
if not client_secret:
173173
client_secret = os.environ.get(REDHAT_PRODUCT_DOWNLOAD_CLIENT_SECRET_ENV_VAR)
174174

175175
if not client_secret:
176176
module.fail_json(msg=str("Client Secret not specified and unable to determine Client Secret "
177-
f"from '{REDHAT_PRODUCT_DOWNLOAD_CLIENT_SECRET_ENV_VAR}' environment variable."))
177+
"from '{0}' environment variable.".format(REDHAT_PRODUCT_DOWNLOAD_CLIENT_SECRET_ENV_VAR)))
178178

179179
session = get_authenticated_session(module, sso_url, validate_certs, client_id, client_secret)
180180

181-
api_base_url = f"{api_url}{API_SERVICE_PATH}"
181+
api_base_url = "{0}{1}".format(api_url, API_SERVICE_PATH)
182182

183183
if product_category is not None:
184184
# List Product Categories
185185
product_categories = []
186186

187187
try:
188-
product_categories = perform_search(session, f"{api_base_url}{LIST_PRODUCT_CATEGORIES_ENDPOINT}", validate_certs)
188+
product_categories = perform_search(session, "{0}{1}".format(api_base_url, LIST_PRODUCT_CATEGORIES_ENDPOINT), validate_certs)
189189
except Exception as err:
190190
module.fail_json(msg="Error Listing Available Product Categories: %s" % (to_native(err)))
191191

192192
if product_category not in product_categories:
193-
module.fail_json(msg=f"'{product_category}' is not a valid Product Category")
193+
module.fail_json(msg="'{0}' is not a valid Product Category".format(product_category))
194194

195195
# Search for Products
196196
search_results = []
197197

198198
search_params = generate_search_params(product_category, product_id, product_type, product_version)
199199

200200
try:
201-
search_results = perform_search(session, f"{api_base_url}{SEARCH_ENDPOINT}", validate_certs, search_params)
201+
search_results = perform_search(session, "{0}{1}".format(api_base_url, SEARCH_ENDPOINT), validate_certs, search_params)
202202
except Exception as err:
203203
module.fail_json(msg="Error Searching for Products: %s" % (to_native(err)))
204204

0 commit comments

Comments
 (0)