From 443327f66534cece43418c5f14214126c0ffb067 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Tue, 27 Feb 2018 19:24:29 +0530 Subject: [PATCH] New module: digital_ocean_region_facts (#35911) Signed-off-by: Abhijeet Kasurde --- .../digital_ocean_region_facts.py | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 lib/ansible/modules/cloud/digital_ocean/digital_ocean_region_facts.py diff --git a/lib/ansible/modules/cloud/digital_ocean/digital_ocean_region_facts.py b/lib/ansible/modules/cloud/digital_ocean/digital_ocean_region_facts.py new file mode 100644 index 00000000000..e58279ec955 --- /dev/null +++ b/lib/ansible/modules/cloud/digital_ocean/digital_ocean_region_facts.py @@ -0,0 +1,117 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# Copyright: (c) 2018, Ansible Project +# Copyright: (c) 2018, Abhijeet Kasurde +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + + +ANSIBLE_METADATA = { + 'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community' +} + + +DOCUMENTATION = ''' +--- +module: digital_ocean_region_facts +short_description: Gather facts about DigitalOcean regions +description: + - This module can be used to gather facts about regions. +author: "Abhijeet Kasurde (@akasurde)" +version_added: "2.6" +extends_documentation_fragment: digital_ocean.documentation +requirements: + - "python >= 2.6" +''' + + +EXAMPLES = ''' +- name: Gather facts about all regions + digital_ocean_region_facts: + oauth_token: "{{ oauth_token }}" + +- name: Get Name of region where slug is known + digital_ocean_region_facts: + oauth_token: "{{ oauth_token }}" + register: resp_out +- debug: var=resp_out +- set_fact: + region_slug: "{{ item.name }}" + with_items: "{{ resp_out.data|json_query(name) }}" + vars: + name: "[?slug==`nyc1`]" +- debug: var=region_slug +''' + + +RETURN = ''' +data: + description: DigitalOcean regions facts + returned: success + type: list + sample: [ + { + "available": true, + "features": [ + "private_networking", + "backups", + "ipv6", + "metadata", + "install_agent", + "storage" + ], + "name": "New York 1", + "sizes": [ + "512mb", + "s-1vcpu-1gb", + "1gb", + "s-3vcpu-1gb", + "s-1vcpu-2gb", + "s-2vcpu-2gb", + "2gb", + "s-1vcpu-3gb", + "s-2vcpu-4gb", + "4gb", + "c-2", + "m-1vcpu-8gb", + "8gb", + "s-4vcpu-8gb", + "s-6vcpu-16gb", + "16gb" + ], + "slug": "nyc1" + }, + ] +''' + +from traceback import format_exc +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.digital_ocean import DigitalOceanHelper +from ansible.module_utils._text import to_native + + +def core(module): + rest = DigitalOceanHelper(module) + + base_url = 'regions?' + regions = rest.get_paginated_data(base_url=base_url, data_key_name='regions') + + module.exit_json(changed=False, data=regions) + + +def main(): + argument_spec = DigitalOceanHelper.digital_ocean_argument_spec() + module = AnsibleModule(argument_spec=argument_spec) + + try: + core(module) + except Exception as e: + module.fail_json(msg=to_native(e), exception=format_exc()) + + +if __name__ == '__main__': + main()