Skip to content

Commit

Permalink
Add 'base' connection plugin code (plugin_utils.connection) (#1340)
Browse files Browse the repository at this point in the history
Add 'base' connection plugin code

Depends-On: ansible/ansible-zuul-jobs#1748
SUMMARY
Adds a plugin_utils.connection.AWSConnectionBase to support future work on aws_ssm plugin.
ISSUE TYPE

Feature Pull Request

COMPONENT NAME
plugin_utils/connection.py
ADDITIONAL INFORMATION

Reviewed-by: Alina Buzachis <None>
Reviewed-by: Markus Bergholz <[email protected]>
  • Loading branch information
tremble authored Jan 19, 2023
1 parent fa3a786 commit 4a066e5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/refactor_connection_plugins.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- plugin_utils - Added ``AWSConnectionBase`` to support refactoring connection plugins (https://github.com/ansible-collections/amazon.aws/pull/1340).
19 changes: 19 additions & 0 deletions plugins/plugin_utils/connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# (c) 2023 Red Hat Inc.
#
# This file is part of Ansible
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from ansible.errors import AnsibleConnectionFailure
from ansible.plugins.connection import ConnectionBase

from ansible_collections.amazon.aws.plugins.plugin_utils.base import AWSPluginBase


class AWSConnectionBase(AWSPluginBase, ConnectionBase):

def _do_fail(self, message):
raise AnsibleConnectionFailure(message)

def __init__(self, *args, boto3_version=None, botocore_version=None, **kwargs):
super().__init__(*args, **kwargs)
self.require_aws_sdk(botocore_version=botocore_version, boto3_version=boto3_version)
Empty file.
41 changes: 41 additions & 0 deletions tests/unit/plugin_utils/connection/test_connection_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# (c) 2023 Red Hat Inc.
#
# This file is part of Ansible
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

import pytest
from unittest.mock import call
from unittest.mock import MagicMock
from unittest.mock import sentinel

from ansible.errors import AnsibleConnectionFailure

import ansible_collections.amazon.aws.plugins.plugin_utils.connection as utils_connection


def test_fail(monkeypatch):
monkeypatch.setattr(utils_connection.AWSConnectionBase, "__abstractmethods__", set())
monkeypatch.setattr(utils_connection.ConnectionBase, "__init__", MagicMock(name="__init__"))

connection_plugin = utils_connection.AWSConnectionBase() # pylint: disable=abstract-class-instantiated
with pytest.raises(AnsibleConnectionFailure, match=str(sentinel.ERROR_MSG)):
connection_plugin._do_fail(sentinel.ERROR_MSG)


def test_init(monkeypatch):
kwargs = {"example": sentinel.KWARG}
require_aws_sdk = MagicMock(name="require_aws_sdk")
require_aws_sdk.return_value = sentinel.RETURNED_SDK

monkeypatch.setattr(utils_connection.AWSConnectionBase, "__abstractmethods__", set())
monkeypatch.setattr(utils_connection.ConnectionBase, "__init__", MagicMock(name="__init__"))
monkeypatch.setattr(utils_connection.AWSConnectionBase, "require_aws_sdk", require_aws_sdk)

connection_plugin = utils_connection.AWSConnectionBase(sentinel.PARAM_TERMS, sentinel.PARAM_VARS, **kwargs) # pylint: disable=abstract-class-instantiated
assert require_aws_sdk.call_args == call(botocore_version=None, boto3_version=None)

connection_plugin = utils_connection.AWSConnectionBase( # pylint: disable=abstract-class-instantiated
sentinel.PARAM_ONE, sentinel.PARAM_TWO,
boto3_version=sentinel.PARAM_BOTO3, botocore_version=sentinel.PARAM_BOTOCORE,
**kwargs)
assert require_aws_sdk.call_args == call(botocore_version=sentinel.PARAM_BOTOCORE, boto3_version=sentinel.PARAM_BOTO3)

0 comments on commit 4a066e5

Please sign in to comment.