Debian Codegraph @

ansible_collections/community/sap_libs/plugins/module_utils/pyrfc_handler.py

raw
1 #!/usr/bin/env python
2
3 # Copyright (c) 2022-2026 The Project Contributors.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 # For a detailed list of copyright holders and contribution history,
18 # please refer to the CONTRIBUTORS.md file in the project root.
19
20 from __future__ import (absolute_import, division, print_function)
21 __metaclass__ = type
22
23 from ansible.module_utils.basic import missing_required_lib
24
25 import traceback
26
27 PYRFC_LIBRARY_IMPORT_ERROR = None
28 try:
29 import pyrfc
30 except ImportError:
31 PYRFC_LIBRARY_IMPORT_ERROR = traceback.format_exc()
32 HAS_PYRFC_LIBRARY = False
33 else:
34 HAS_PYRFC_LIBRARY = True
35
36
37 def get_connection(module, conn_params):
38 if not HAS_PYRFC_LIBRARY:
39 module.fail_json(msg=missing_required_lib(
40 "pyrfc"), exception=PYRFC_LIBRARY_IMPORT_ERROR)
41
42 module.warn('Connecting ... %s' % conn_params['ashost'])
43 if "saprouter" in conn_params:
44 module.warn("...via SAPRouter to SAP System")
45 elif "gwhost" in conn_params:
46 module.warn("...via Gateway to SAP System")
47 else:
48 module.warn("...direct to SAP System")
49
50 conn = pyrfc.Connection(**conn_params)
51
52 module.warn("Verifying connection is open/alive: %s" % conn.alive)
53 return conn