From a0374d820908fd0e1471762c714ad51159f0a440 Mon Sep 17 00:00:00 2001 From: Luke Murphy Date: Sat, 28 Mar 2020 17:55:36 +0100 Subject: [PATCH] Try to unit test but give up for now --- library/__init__.py | 1 + library/gandi_dns.py | 67 ++++++++++++++++++++++++++++++++++++++++-- setup.cfg | 8 ++++- test/conftest.py | 9 ++++++ test/test_gandi_dns.py | 11 +++++-- 5 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 library/__init__.py create mode 100644 test/conftest.py diff --git a/library/__init__.py b/library/__init__.py new file mode 100644 index 0000000..901e511 --- /dev/null +++ b/library/__init__.py @@ -0,0 +1 @@ +VERSION = "0.0.1" diff --git a/library/gandi_dns.py b/library/gandi_dns.py index 8837f9b..e24fb40 100644 --- a/library/gandi_dns.py +++ b/library/gandi_dns.py @@ -1,11 +1,43 @@ #!/usr/bin/python3 -from ansible.module_utils.basic import AnsibleModule +import traceback +from subprocess import check_output + +from ansible.module_utils.basic import AnsibleModule, missing_required_lib DOCUMENTATION = """ --- module: gandi_dns -short_description: Manage Gandi DNS entries +short_description: Manage Gandi DNS entries. +requirements: + - python >= 3.8 + - gandi.cli >= 1.5 +author: + - Luke Murphy (@decentral1se) +options: + apirest_key: + description: + - The Gandi REST API key. It may also be specified by the + C(API_REST_KEY) environment variable. See the + U(https://github.com/Gandi/gandi.cli/blob/master/gandicli.man.rst#environment) + type: str + required: true + domain: + description: The domain name you're working with. + type: str + required: true + ipv4: + description: The IP v4 address that the domain refers to. + type: str + required: true + state: + description: + - The desired instance state. + type: str + choices: + - present + - absent + required: true """ EXAMPLES = """ @@ -14,12 +46,41 @@ EXAMPLES = """ apirest_key: "{{ apirest_key }}" domain: foobar.autonomic.zone ipv4: 192.168.1.2 + state: present """ +RETURN = """ +TODO +""" + +GANDI_CLI_IMP_ERR = None +try: + from gandi import cli # noqa + + HAS_GANDI_DEPENDENCY = True +except ImportError: + GANDI_IMP_ERR = traceback.format_exc() + HAS_GANDI_DEPENDENCY = False + + +def retrieve_domain(domain): + """Retrieve information about an existing domain.""" + pass + def main(): + """Module entrypoint.""" module = AnsibleModule(argument_spec={}) - module.exit_json(changed=False, meta={"TO": "DO"}) + + if not HAS_GANDI_DEPENDENCY: + msg = missing_required_lib('gandi.cli') + module.fail_json(msg=msg, exception=GANDI_IMP_ERR) + + if module.params['state'] == 'present': + domain = retrieve_domain(module.params['domain']) + + if module.params['state'] == 'absent': + pass if __name__ == "__main__": diff --git a/setup.cfg b/setup.cfg index 749b5d7..005710a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -11,6 +11,12 @@ skip = .venv, .tox include_trailing_comma = True [metadata] -name = autonomic.gandi +name = library author = decentral1se version = 0.0.1 + +[options] +packages = find: +install_requires = + ansible >= 2.9.6, <= 2.10 + gandi.cli >= 1.5, <= 2.0 diff --git a/test/conftest.py b/test/conftest.py new file mode 100644 index 0000000..4a953ee --- /dev/null +++ b/test/conftest.py @@ -0,0 +1,9 @@ +import json + +from ansible.module_utils import basic +from ansible.module_utils._text import to_bytes + + +def set_module_args(args): + args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) + basic._ANSIBLE_ARGS = to_bytes(args) diff --git a/test/test_gandi_dns.py b/test/test_gandi_dns.py index d20310c..3b3ca68 100644 --- a/test/test_gandi_dns.py +++ b/test/test_gandi_dns.py @@ -1,2 +1,9 @@ -def test_gandi_dns(): - assert 666 == 666 +import pytest + +gandi_cli = pytest.importorskip('gandi.cli') + + +def test_TODO(): + # TODO(decentral1se): discover how to unit test the module so far it is a + # bit of a weird python packaging circus and there are lots of errors... + assert 1 == 1