1# -*- coding: utf-8 -*-
2# Copyright: (c) 2018, Ansible Project
3# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)
4
5from __future__ import absolute_import, division, print_function
6__metaclass__ = type
7
8import traceback
9
10from ansible.module_utils.basic import env_fallback, missing_required_lib
11
12HAS_HEROKU = False
13HEROKU_IMP_ERR = None
14try:
15    import heroku3
16    HAS_HEROKU = True
17except ImportError:
18    HEROKU_IMP_ERR = traceback.format_exc()
19
20
21class HerokuHelper():
22    def __init__(self, module):
23        self.module = module
24        self.check_lib()
25        self.api_key = module.params["api_key"]
26
27    def check_lib(self):
28        if not HAS_HEROKU:
29            self.module.fail_json(msg=missing_required_lib('heroku3'), exception=HEROKU_IMP_ERR)
30
31    @staticmethod
32    def heroku_argument_spec():
33        return dict(
34            api_key=dict(fallback=(env_fallback, ['HEROKU_API_KEY', 'TF_VAR_HEROKU_API_KEY']), type='str', no_log=True))
35
36    def get_heroku_client(self):
37        client = heroku3.from_key(self.api_key)
38
39        if not client.is_authenticated:
40            self.module.fail_json(msg='Heroku authentication failure, please check your API Key')
41
42        return client
43