2828extends_documentation_fragment:
2929 - files
3030 - middleware_automation.common.jbossnetwork_connection_options
31- - middleware_automation.common.jbossnetwork_search_options
3231options:
3332 dest:
3433 description:
136135 LIST_PRODUCT_CATEGORIES_ENDPOINT
137136)
138137
139-
140138try :
141139 import requests
142140 HAS_REQUESTS = True
143141except ImportError :
144142 HAS_REQUESTS = False
145143 REQUESTS_IMP_ERR = traceback .format_exc ()
144+ else :
145+ REQUESTS_IMP_ERR = None
146146
147147
148148def 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+
156157def 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