xoxys.general/plugins/modules/corenetworks_token.py

113 lines
3.0 KiB
Python

# -*- coding: utf-8 -*-
"""Module to control corenetworks DNS API."""
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
DOCUMENTATION = """
---
module: corenetworks_dns
short_description: Interface with the DNS API of core-networks.de
description:
- "Manages DNS zones and records via the core networks API, see the docs: U(https://beta.api.core-networks.de/doc/)."
options:
api_user:
description:
- Account API username. If omitted, the environment variables C(CN_API_USER) and C(CN_API_PASSWORD) will be looked for.
type: str
api_password:
description:
- Account API password.
type: str
state:
description:
- whether the record should exist or not
choices: [ "present" ]
default: present
type: str
requirements:
- "corenetworks >= 0.1.3"
author: "Robert Kaussow (@xoxys)"
""" # noqa
EXAMPLES = """
- name: Obtain an API token using env variables
corenetworks_token:
delegate_to: localhost
register: my_token
- name: Obtain an API token using username and password attribute
corenetworks_token:
api_user: testuser
api_password: secure
delegate_to: localhost
register: my_token
- debug:
msg: "{{ my_token }}"
- name: Use the token
corenetworks_dns:
api_token: "{{ my_token.session.token }}"
zone: my.com
type: CNAME
value: example.com
state: present
delegate_to: localhost
"""
RETURN = r"""# """
import traceback
CORENETWORKS_IMP_ERR = None
try:
from corenetworks import CoreNetworks
from corenetworks.exceptions import CoreNetworksException
HAS_CORENETWORKS = True
except ImportError:
CORENETWORKS_IMP_ERR = traceback.format_exc()
HAS_CORENETWORKS = False
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
def main():
module = AnsibleModule(
argument_spec=dict(
api_user=dict(type="str"),
api_password=dict(type="str", no_log=True),
state=dict(type="str", choices=["present"], default="present"),
),
supports_check_mode=True,
)
if not HAS_CORENETWORKS:
module.fail_json(msg=missing_required_lib("corenetworks"), exception=CORENETWORKS_IMP_ERR)
api_user = module.params.get("api_user")
api_password = module.params.get("api_password")
# perform actions
try:
# request throtteling to workaround the current rate limit
changed = False
client = CoreNetworks(user=api_user, password=api_password, auto_commit=True)
session = {"token": client._auth.token}
if hasattr(client._auth, "expires"):
session["expires"] = client._auth.expires.strftime("%Y-%m-%d, %H:%M:%S")
module.exit_json(changed=changed, session=session)
except CoreNetworksException as e:
module.fail_json(msg="Failure in core networks API communication: {}".format(str(e)))
module.fail_json(msg="Unknown what you wanted me to do")
if __name__ == "__main__":
main()