Debian Codegraph @

ansible_collections/community/hashi_vault/plugins/module_utils/_hashi_vault_module.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2021 Brian Scholer (@briantist)
3 # Simplified BSD License (see LICENSES/BSD-2-Clause.txt or https://opensource.org/licenses/BSD-2-Clause)
4 # SPDX-License-Identifier: BSD-2-Clause
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.module_utils.basic import AnsibleModule
16
17 from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_common import (
18 HashiVaultHelper,
19 HashiVaultHVACError,
20 HashiVaultOptionAdapter,
21 )
22 from ansible_collections.community.hashi_vault.plugins.module_utils._connection_options import HashiVaultConnectionOptions
23 from ansible_collections.community.hashi_vault.plugins.module_utils._authenticator import HashiVaultAuthenticator
24
25
26 class HashiVaultModule(AnsibleModule):
27 def __init__(self, *args, **kwargs):
28 if 'hashi_vault_custom_retry_callback' in kwargs:
29 callback = kwargs.pop('hashi_vault_custom_retry_callback')
30 else:
31 callback = self._generate_retry_callback
32
33 super(HashiVaultModule, self).__init__(*args, **kwargs)
34
35 try:
36 self.helper = HashiVaultHelper()
37 except HashiVaultHVACError as exc:
38 self.fail_json(
39 msg=exc.msg,
40 exception=exc.error
41 )
42
43 self.adapter = HashiVaultOptionAdapter.from_dict(self.params)
44 self.connection_options = HashiVaultConnectionOptions(option_adapter=self.adapter, retry_callback_generator=callback)
45 self.authenticator = HashiVaultAuthenticator(option_adapter=self.adapter, warning_callback=self.warn, deprecate_callback=self.deprecate)
46
47 @classmethod
48 def generate_argspec(cls, **kwargs):
49 spec = HashiVaultConnectionOptions.ARGSPEC.copy()
50 spec.update(HashiVaultAuthenticator.ARGSPEC.copy())
51 spec.update(**kwargs)
52
53 return spec
54
55 def _generate_retry_callback(self, retry_action):
56 '''returns a Retry callback function for modules'''
57 def _on_retry(retry_obj):
58 if retry_obj.total > 0:
59 if retry_action == 'warn':
60 self.warn('community.hashi_vault: %i %s remaining.' % (retry_obj.total, 'retry' if retry_obj.total == 1 else 'retries'))
61 else:
62 pass
63
64 return _on_retry