Debian Codegraph @

ansible_collections/community/general/plugins/module_utils/_heroku.py

raw
1 # Copyright (c) 2018, Ansible Project
2 # Simplified BSD License (see LICENSES/BSD-2-Clause.txt or https://opensource.org/licenses/BSD-2-Clause)
3 # SPDX-License-Identifier: BSD-2-Clause
4
5 # Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time.
6 # Do not use this from other collections or standalone plugins/modules!
7
8 from __future__ import annotations
9
10 import traceback
11 import typing as t
12
13 from ansible.module_utils.basic import env_fallback, missing_required_lib
14
15 if t.TYPE_CHECKING:
16 from ansible.module_utils.basic import AnsibleModule
17
18 HAS_HEROKU = False
19 HEROKU_IMP_ERR = None
20 try:
21 import heroku3
22
23 HAS_HEROKU = True
24 except ImportError:
25 HEROKU_IMP_ERR = traceback.format_exc()
26
27
28 class HerokuHelper:
29 def __init__(self, module: AnsibleModule) -> None:
30 self.module = module
31 self.check_lib()
32 self.api_key = module.params["api_key"]
33
34 def check_lib(self) -> None:
35 if not HAS_HEROKU:
36 self.module.fail_json(msg=missing_required_lib("heroku3"), exception=HEROKU_IMP_ERR)
37
38 @staticmethod
39 def heroku_argument_spec() -> dict[str, t.Any]:
40 return dict(
41 api_key=dict(fallback=(env_fallback, ["HEROKU_API_KEY", "TF_VAR_HEROKU_API_KEY"]), type="str", no_log=True)
42 )
43
44 def get_heroku_client(self):
45 client = heroku3.from_key(self.api_key)
46
47 if not client.is_authenticated:
48 self.module.fail_json(msg="Heroku authentication failure, please check your API Key")
49
50 return client