| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright (c) 2021 Brian Scholer (@briantist) |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
# FOR INTERNAL COLLECTION USE ONLY |
| 7 |
# The interfaces in this file are meant for use within the community.hashi_vault collection |
| 8 |
# and may not remain stable to outside uses. Changes may be made in ANY release, even a bugfix release. |
| 9 |
# See also: https://github.com/ansible/community/issues/539#issuecomment-780839686 |
| 10 |
# Please open an issue if you have questions about this. |
| 11 |
|
| 12 |
from __future__ import absolute_import, division, print_function |
| 13 |
__metaclass__ = type |
| 14 |
|
| 15 |
from ansible.plugins import AnsiblePlugin |
| 16 |
from ansible import constants as C |
| 17 |
from ansible.utils.display import Display |
| 18 |
|
| 19 |
from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_common import ( |
| 20 |
HashiVaultHelper, |
| 21 |
HashiVaultHVACError, |
| 22 |
HashiVaultOptionAdapter, |
| 23 |
) |
| 24 |
|
| 25 |
from ansible_collections.community.hashi_vault.plugins.module_utils._connection_options import HashiVaultConnectionOptions |
| 26 |
from ansible_collections.community.hashi_vault.plugins.module_utils._authenticator import HashiVaultAuthenticator |
| 27 |
|
| 28 |
|
| 29 |
display = Display() |
| 30 |
|
| 31 |
|
| 32 |
class HashiVaultPlugin(AnsiblePlugin): |
| 33 |
def __init__(self): |
| 34 |
super(HashiVaultPlugin, self).__init__() |
| 35 |
|
| 36 |
try: |
| 37 |
self.helper = HashiVaultHelper() |
| 38 |
except HashiVaultHVACError as exc: |
| 39 |
from ansible.errors import AnsibleError |
| 40 |
raise AnsibleError(exc.msg) |
| 41 |
|
| 42 |
self._options_adapter = HashiVaultOptionAdapter.from_ansible_plugin(self) |
| 43 |
self.connection_options = HashiVaultConnectionOptions(self._options_adapter, self._generate_retry_callback) |
| 44 |
self.authenticator = HashiVaultAuthenticator(self._options_adapter, display.warning, display.deprecated) |
| 45 |
|
| 46 |
def _generate_retry_callback(self, retry_action): |
| 47 |
'''returns a Retry callback function for plugins''' |
| 48 |
def _on_retry(retry_obj): |
| 49 |
if retry_obj.total > 0: |
| 50 |
if retry_action == 'warn': |
| 51 |
display.warning('community.hashi_vault: %i %s remaining.' % (retry_obj.total, 'retry' if retry_obj.total == 1 else 'retries')) |
| 52 |
else: |
| 53 |
pass |
| 54 |
|
| 55 |
return _on_retry |
| 56 |
|
| 57 |
def process_deprecations(self, collection_name='community.hashi_vault'): |
| 58 |
'''processes deprecations related to the collection''' |
| 59 |
|
| 60 |
# TODO: this is a workaround for deprecations not being shown in lookups |
| 61 |
# See: |
| 62 |
# - https://github.com/ansible/ansible/issues/73051 |
| 63 |
# - https://github.com/ansible/ansible/pull/73058 |
| 64 |
# - https://github.com/ansible/ansible/pull/73239 |
| 65 |
# - https://github.com/ansible/ansible/pull/73240 |
| 66 |
# |
| 67 |
# If a fix is backported to 2.9, this should be removed. |
| 68 |
# Otherwise, we'll have to test with fixes that are available and see how we |
| 69 |
# can determine whether to execute this conditionally. |
| 70 |
|
| 71 |
# nicked from cli/__init__.py |
| 72 |
# with slight customizations to help filter out relevant messages |
| 73 |
# (relying on the collection name since it's a valid attrib and we only have 1 plugin at this time) |
| 74 |
|
| 75 |
# warn about deprecated config options |
| 76 |
|
| 77 |
for deprecated in list(C.config.DEPRECATED): |
| 78 |
name = deprecated[0] |
| 79 |
why = deprecated[1]['why'] |
| 80 |
if deprecated[1].get('collection_name') != collection_name: |
| 81 |
continue |
| 82 |
|
| 83 |
if 'alternatives' in deprecated[1]: |
| 84 |
alt = ', use %s instead' % deprecated[1]['alternatives'] |
| 85 |
else: |
| 86 |
alt = '' |
| 87 |
ver = deprecated[1].get('version') |
| 88 |
date = deprecated[1].get('date') |
| 89 |
collection_name = deprecated[1].get('collection_name') |
| 90 |
display.deprecated("%s option, %s%s" % (name, why, alt), version=ver, date=date, collection_name=collection_name) |
| 91 |
|
| 92 |
# remove this item from the list so it won't get processed again by something else |
| 93 |
C.config.DEPRECATED.remove(deprecated) |