-
Notifications
You must be signed in to change notification settings - Fork 10
initial migration of pyrfc module to sap_libs #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
d9ee239
initial migration
rainerleber 74220dd
add suboptions
rainerleber d5432e9
add documentation
rainerleber 7202727
add symlink
rainerleber 90ed1a3
fix sanity
rainerleber c2c6703
add test
rainerleber 953fc67
fix sanity
rainerleber 2dbb876
add more tests
rainerleber c2d5356
comment
rainerleber c500d2c
fix intendation
rainerleber 112d871
add mock module
rainerleber 3e5d2bb
change test
rainerleber 0f7d95e
fix
rainerleber 70a712f
change to false
rainerleber 415b03b
change test
rainerleber 9596d08
test with keyerror
rainerleber 8637838
change
rainerleber 179ed3b
change code style and test
rainerleber 0b68f56
revert for compatibility
rainerleber dc344f1
quick test
rainerleber 88b23e1
add working tests
rainerleber d4c4340
ignore license
rainerleber 43c4733
change
rainerleber 4b2a248
change
rainerleber d857df4
delete symlink
rainerleber 66a380d
refactoring as discussed
rainerleber 29e7463
change
rainerleber 9067510
change ignore
rainerleber f2af54f
change location
rainerleber 9e1f908
change
rainerleber 003aa3f
change
rainerleber d609309
change license
rainerleber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,6 @@ jobs: | |
| - stable-2.11 | ||
| - stable-2.12 | ||
| - stable-2.13 | ||
| - devel | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| # Copyright: (c) 2022, Sean Freeman , | ||
| # Rainer Leber <[email protected]> <[email protected]> | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import (absolute_import, division, print_function) | ||
| __metaclass__ = type | ||
|
|
||
| from ansible.module_utils.basic import missing_required_lib | ||
|
|
||
| import traceback | ||
|
|
||
| PYRFC_LIBRARY_IMPORT_ERROR = None | ||
| try: | ||
| import pyrfc | ||
| except ImportError: | ||
| PYRFC_LIBRARY_IMPORT_ERROR = traceback.format_exc() | ||
| HAS_PYRFC_LIBRARY = False | ||
| else: | ||
| HAS_PYRFC_LIBRARY = True | ||
|
|
||
|
|
||
| def get_connection(module, conn_params): | ||
| if not HAS_PYRFC_LIBRARY: | ||
| module.fail_json(msg=missing_required_lib( | ||
| "python-gitlab"), exception=PYRFC_LIBRARY_IMPORT_ERROR) | ||
|
|
||
| module.warn('Connecting ... %s' % conn_params['ashost']) | ||
| if "saprouter" in conn_params: | ||
| module.warn("...via SAPRouter to SAP System") | ||
| elif "gwhost" in conn_params: | ||
| module.warn("...via Gateway to SAP System") | ||
| else: | ||
| module.warn("...direct to SAP System") | ||
|
|
||
| conn = pyrfc.Connection(**conn_params) | ||
|
|
||
| module.warn("Verifying connection is open/alive: %s" % conn.alive) | ||
| return conn | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| #!/usr/bin/python | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| # Copyright: (c) 2022, Sean Freeman , | ||
| # Rainer Leber <[email protected]> <[email protected]> | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| from __future__ import absolute_import, division, print_function | ||
| __metaclass__ = type | ||
|
|
||
| DOCUMENTATION = r''' | ||
| --- | ||
| module: sap_pyrfc | ||
|
|
||
| short_description: This module executes rfc functions. | ||
|
|
||
| version_added: "1.2.0" | ||
|
|
||
| description: | ||
| - This module will executes rfc calls on a sap system. | ||
| - It is a generic approach to call rfc functions on a SAP System. | ||
| - This module should be used where no module or role is provided. | ||
|
|
||
| options: | ||
| function: | ||
| description: The SAP RFC function to call. | ||
| required: true | ||
| type: str | ||
| parameters: | ||
| description: The parameters which are needed by the function. | ||
| required: true | ||
| type: dict | ||
| connection: | ||
| description: The required connection details. | ||
| required: true | ||
| type: dict | ||
| suboptions: | ||
| ashost: | ||
| description: The required host for the SAP system. Can be either an FQDN or IP Address. | ||
| type: str | ||
| required: true | ||
| sysid: | ||
| description: The systemid of the SAP system. | ||
| type: str | ||
| required: false | ||
| sysnr: | ||
| description: | ||
| - The system number of the SAP system. | ||
| - You must quote the value to ensure retaining the leading zeros. | ||
| type: str | ||
| required: true | ||
| client: | ||
| description: | ||
| - The client number to connect to. | ||
| - You must quote the value to ensure retaining the leading zeros. | ||
| type: str | ||
| required: true | ||
| user: | ||
| description: The required username for the SAP system. | ||
| type: str | ||
| required: true | ||
| passwd: | ||
| description: The required password for the SAP system. | ||
| type: str | ||
| required: true | ||
| lang: | ||
| description: The used language to execute. | ||
| type: str | ||
| required: false | ||
|
|
||
| requirements: | ||
| - pyrfc >= 2.4.0 | ||
|
|
||
| author: | ||
| - Sean Freeman (@seanfreeman) | ||
| - Rainer Leber (@rainerleber) | ||
| ''' | ||
|
|
||
| EXAMPLES = ''' | ||
| - name: test the pyrfc module | ||
| community.sap_libs.sap_pyrfc: | ||
| function: STFC_CONNECTION | ||
| parameters: | ||
| REQUTEXT: "Hello SAP!" | ||
| connection: | ||
| ashost: s4hana.poc.cloud | ||
| sysid: TDT | ||
| sysnr: "01" | ||
| client: "400" | ||
| user: DDIC | ||
| passwd: Password1 | ||
| lang: EN | ||
| ''' | ||
|
|
||
| RETURN = r''' | ||
| result: | ||
| description: The execution description. | ||
| type: dict | ||
| returned: always | ||
| sample: {"ECHOTEXT": "Hello SAP!", | ||
| "RESPTEXT": "SAP R/3 Rel. 756 Sysid: TST Date: 20220710 Time: 140717 Logon_Data: 000/DDIC/E"} | ||
| ''' | ||
|
|
||
| import traceback | ||
| from ansible.module_utils.basic import AnsibleModule, missing_required_lib | ||
| from ..module_utils.pyrfc_handler import get_connection | ||
|
|
||
| try: | ||
| from pyrfc import ABAPApplicationError, ABAPRuntimeError, CommunicationError, Connection, LogonError | ||
| except ImportError: | ||
| HAS_PYRFC_LIBRARY = False | ||
| PYRFC_LIBRARY_IMPORT_ERROR = traceback.format_exc() | ||
| else: | ||
| HAS_PYRFC_LIBRARY = True | ||
|
|
||
|
|
||
| def main(): | ||
| msg = None | ||
| params_spec = dict( | ||
| ashost=dict(type='str', required=True), | ||
| sysid=dict(type='str', required=False), | ||
| sysnr=dict(type='str', required=True), | ||
| client=dict(type='str', required=True), | ||
| user=dict(type='str', required=True), | ||
| passwd=dict(type='str', required=True, no_log=True), | ||
| lang=dict(type='str', required=False), | ||
| ) | ||
|
|
||
| argument_spec = dict(function=dict(required=True, type='str'), | ||
| parameters=dict(required=True, type='dict'), | ||
| connection=dict( | ||
| required=True, type='dict', options=params_spec), | ||
| ) | ||
|
|
||
| module = AnsibleModule( | ||
| argument_spec=argument_spec, | ||
| supports_check_mode=True, | ||
| ) | ||
|
|
||
| function = module.params.get('function') | ||
| func_params = module.params.get('parameters') | ||
| conn_params = module.params.get('connection') | ||
|
|
||
| if not HAS_PYRFC_LIBRARY: | ||
| module.fail_json( | ||
| msg=missing_required_lib('pyrfc'), | ||
| exception=PYRFC_LIBRARY_IMPORT_ERROR) | ||
|
|
||
| # Check mode | ||
| if module.check_mode: | ||
| msg = "function: %s; params: %s; login: %s" % ( | ||
| function, func_params, conn_params) | ||
| module.exit_json(msg=msg, changed=True) | ||
|
|
||
| try: | ||
| conn = get_connection(module, conn_params) | ||
| result = conn.call(function, **func_params) | ||
| except CommunicationError as err: | ||
| msg = "Could not connect to server" | ||
| error_msg = err.message | ||
| except LogonError as err: | ||
| msg = "Could not log in" | ||
| error_msg = err.message | ||
| except (ABAPApplicationError, ABAPRuntimeError) as err: | ||
| msg = "ABAP error occurred" | ||
| error_msg = err.message | ||
| except Exception as err: | ||
| msg = "Something went wrong." | ||
| error_msg = err | ||
| else: | ||
| module.exit_json(changed=True, result=result) | ||
|
|
||
| if msg: | ||
| module.fail_json(msg=msg, exception=error_msg) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| plugins/modules/sap_pyrfc.py validate-modules:missing-gplv3-license # Licensed under Apache 2.0 | ||
rkpobe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| plugins/modules/sap_pyrfc.py validate-modules:missing-gplv3-license # Licensed under Apache 2.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| plugins/modules/sap_pyrfc.py validate-modules:missing-gplv3-license # Licensed under Apache 2.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| plugins/modules/sap_pyrfc.py validate-modules:missing-gplv3-license # Licensed under Apache 2.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| plugins/modules/sap_pyrfc.py validate-modules:missing-gplv3-license # Licensed under Apache 2.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import (absolute_import, division, print_function) | ||
|
|
||
| __metaclass__ = type | ||
|
|
||
| import sys | ||
| from ansible_collections.community.sap_libs.tests.unit.compat.mock import patch, MagicMock | ||
| from ansible_collections.community.sap_libs.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args | ||
|
|
||
| sys.modules['pyrfc'] = MagicMock() | ||
| sys.modules['pyrfc.Connection'] = MagicMock() | ||
| from ansible_collections.community.sap_libs.plugins.modules import sap_pyrfc | ||
|
|
||
|
|
||
| class TestSAPRfcModule(ModuleTestCase): | ||
|
|
||
| def setUp(self): | ||
| super(TestSAPRfcModule, self).setUp() | ||
| self.module = sap_pyrfc | ||
|
|
||
| def tearDown(self): | ||
| super(TestSAPRfcModule, self).tearDown() | ||
|
|
||
| def test_without_required_parameters(self): | ||
| """Failure must occurs when all parameters are missing""" | ||
| with self.assertRaises(AnsibleFailJson): | ||
| set_module_args({}) | ||
| self.module.main() | ||
|
|
||
| def test_error_module_not_found(self): | ||
| """tests fail module error""" | ||
|
|
||
| set_module_args({ | ||
| "function": "STFC_CONNECTION", | ||
| "parameters": {"REQUTEXT": "Hello SAP!"}, | ||
| "connection": {"ashost": "s4hana.poc.cloud", | ||
| "sysnr": "01", | ||
| "client": "400", | ||
| "user": "DDIC", | ||
| "passwd": "Password1", | ||
| "lang": "EN"} | ||
| }) | ||
|
|
||
| with self.assertRaises(AnsibleFailJson) as result: | ||
| self.module.HAS_PYRFC_LIBRARY = False | ||
| self.module.PYRFC_LIBRARY_IMPORT_ERROR = 'Module not found' | ||
rainerleber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.module.main() | ||
| self.assertEqual( | ||
| result.exception.args[0]['exception'], 'Module not found') | ||
|
|
||
| def test_success_communication(self): | ||
| """tests success""" | ||
| set_module_args({ | ||
| "function": "STFC_CONNECTION", | ||
| "parameters": {"REQUTEXT": "Hello SAP!"}, | ||
| "connection": {"ashost": "s4hana.poc.cloud", | ||
| "sysnr": "01", | ||
| "client": "400", | ||
| "user": "DDIC", | ||
| "passwd": "Password1", | ||
| "lang": "EN"} | ||
| }) | ||
| with patch.object(self.module, 'get_connection') as patch_call: | ||
| patch_call.call.return_value = 'Patched' | ||
| with self.assertRaises(AnsibleExitJson) as result: | ||
| self.module.main() | ||
| self.assertEqual(result.exception.args[0]['changed'], True) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.